Skip to content

Commit 1a9a333

Browse files
ZonnexElin Fokineelinohlsson
authored
feat(core): #468 pass serviceprovider to UseClientCertificate (#502)
* feat(core): #468 pass serviceprovider to UseClientCertificate * Added example to documentation about how to use a custom certificate service. --------- Co-authored-by: Elin Fokine <ElinO@activesolution.se> Co-authored-by: Elin Fokine <elin.ohlsson@outlook.com>
1 parent 86e679a commit 1a9a333

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

docs/articles/bankid.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,18 @@ services.AddBankId(bankId =>
528528
});
529529
```
530530

531+
### Using client certificate from custom certificate service
532+
533+
```csharp
534+
services.AddBankId(bankId =>
535+
{
536+
bankId
537+
.UseProductionEnvironment()
538+
.UseClientCertificate(sp => sp.GetRequiredService<MyCustomCertificateService>().GetCertificate())
539+
...
540+
});
541+
```
542+
531543
### Adding schemas
532544

533545
* *Same device*: Launches the BankID app on the same device, no need to enter any personal identity number.

src/ActiveLogin.Authentication.BankId.Core/IBankIdBuilderExtensions.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,29 @@ public static IBankIdBuilder UseClientCertificate(this IBankIdBuilder builder, F
4949
void ConfigureHttpClientHandler(IServiceProvider sp, SocketsHttpHandler httpClientHandler)
5050
{
5151
var clientCertificate = configureClientCertificate();
52-
httpClientHandler.SslOptions.ClientCertificates = new X509Certificate2Collection { clientCertificate };
52+
httpClientHandler.SslOptions.ClientCertificates ??= new X509Certificate2Collection();
53+
httpClientHandler.SslOptions.ClientCertificates.Add(clientCertificate);
54+
}
55+
}
56+
57+
/// <summary>
58+
/// Add client certificate for authenticating against the BankID API to the list of available certificates for the http client handler to choose from.
59+
/// </summary>
60+
/// <param name="builder"></param>
61+
/// <param name="configureClientCertificate">The certificate to add.</param>
62+
/// <returns></returns>
63+
public static IBankIdBuilder UseClientCertificate(this IBankIdBuilder builder, Func<IServiceProvider, X509Certificate2> configureClientCertificate)
64+
{
65+
builder.ConfigureAppApiHttpClientHandler(ConfigureHttpClientHandler);
66+
builder.ConfigureVerifyApiHttpClientHandler(ConfigureHttpClientHandler);
67+
68+
return builder;
69+
70+
void ConfigureHttpClientHandler(IServiceProvider sp, SocketsHttpHandler httpClientHandler)
71+
{
72+
var clientCertificate = configureClientCertificate(sp);
73+
httpClientHandler.SslOptions.ClientCertificates ??= new X509Certificate2Collection();
74+
httpClientHandler.SslOptions.ClientCertificates.Add(clientCertificate);
5375
}
5476
}
5577

@@ -186,7 +208,12 @@ internal static IBankIdBuilder UseEnvironment(this IBankIdBuilder builder, Uri a
186208
/// <param name="useBankIdClientCertificate">Use the BankID client certificate (for test) from the BankID documentation.</param>
187209
/// <param name="clientCertificateFormat">If using the BankID client certificate (for test). Select the preferred format p12, pem or pfx.</param>
188210
/// <returns></returns>
189-
public static IBankIdBuilder UseTestEnvironment(this IBankIdBuilder builder, bool useBankIdRootCertificate = true, bool useBankIdClientCertificate = true, TestCertificateFormat clientCertificateFormat = TestCertificateFormat.PFX)
211+
public static IBankIdBuilder UseTestEnvironment(
212+
this IBankIdBuilder builder,
213+
bool useBankIdRootCertificate = true,
214+
bool useBankIdClientCertificate = true,
215+
TestCertificateFormat clientCertificateFormat = TestCertificateFormat.PFX
216+
)
190217
{
191218
builder.UseEnvironment(BankIdUrls.AppApiTestBaseUrl, BankIdUrls.VerifyApiTestBaseUrl, BankIdEnvironments.Test);
192219
builder.Services.AddTransient<IBankIdCertificatePolicyResolver, BankIdCertificatePolicyResolverForTest>();

0 commit comments

Comments
 (0)