@@ -7,9 +7,11 @@ namespace Common;
77using Azure . Security . KeyVault . Secrets ;
88using Microsoft . Extensions . DependencyInjection ;
99using Microsoft . Extensions . Hosting ;
10+ using Microsoft . Extensions . Logging ;
1011
1112public static class JwtTokenExtension
1213{
14+
1315 /// <summary>
1416 /// gets the private key from the local dir or from keyvault if the function is running in azure
1517 /// </summary>
@@ -18,40 +20,53 @@ public static class JwtTokenExtension
1820 /// <returns></returns>
1921 public static IHostBuilder AddJwtTokenSigning ( this IHostBuilder hostBuilder , bool useFakeBearerTokenService = false )
2022 {
21- JwtPrivateKey jwtPrivateKey ;
22- // Azure
23- hostBuilder . AddConfiguration < JwtTokenServiceConfig > ( out JwtTokenServiceConfig config ) ;
24- if ( ! string . IsNullOrEmpty ( config . KeyVaultConnectionString ) )
25- {
26- var certClient = new CertificateClient ( vaultUri : new Uri ( config . KeyVaultConnectionString ) , credential : new DefaultAzureCredential ( ) ) ;
27- var privateKey = certClient . DownloadCertificate ( config . KeyNamePrivateKey ) ;
28-
29- jwtPrivateKey = new JwtPrivateKey ( CertificateToString ( privateKey ) ) ;
30- }
31- // Local
32- else
33- {
34- jwtPrivateKey = new JwtPrivateKey ( GetPrivateKey ( config . LocalPrivateKeyFileName ) ) ;
35- }
23+ var loggerFactory = LoggerFactory . Create ( builder => builder . AddConsole ( ) ) ;
24+ var logger = loggerFactory . CreateLogger ( "program.cs" ) ;
3625
37- var host = hostBuilder . ConfigureServices ( _ =>
26+ JwtPrivateKey jwtPrivateKey ;
27+ try
3828 {
39- _ . AddMemoryCache ( ) ;
40- _ . AddSingleton ( jwtPrivateKey ) ;
41- _ . AddSingleton < IAuthorizationClientCredentials , AuthorizationClientCredentials > ( ) ;
42- _ . AddSingleton < IJwtTokenService , JwtTokenService > ( ) ;
43- _ . AddSingleton < ISigningCredentialsProvider , SigningCredentialsProvider > ( ) ;
44- if ( ! useFakeBearerTokenService )
29+ // Azure
30+ hostBuilder . AddConfiguration < JwtTokenServiceConfig > ( out JwtTokenServiceConfig config ) ;
31+ if ( ! string . IsNullOrEmpty ( config . KeyVaultConnectionString ) )
4532 {
46- _ . AddSingleton < IBearerTokenService , BearerTokenService > ( ) ;
33+ var certClient = new CertificateClient ( vaultUri : new Uri ( config . KeyVaultConnectionString ) , credential : new DefaultAzureCredential ( ) ) ;
34+ var privateKey = certClient . DownloadCertificate ( config . KeyNamePrivateKey ) ;
35+
36+ logger . LogInformation ( "got certificate from key vault" ) ;
37+ jwtPrivateKey = new JwtPrivateKey ( CertificateToString ( privateKey . Value ) ) ;
4738 }
39+ // Local
4840 else
4941 {
50- _ . AddSingleton < IBearerTokenService , BearerTokenServiceMock > ( ) ;
42+ jwtPrivateKey = new JwtPrivateKey ( GetPrivateKey ( config . LocalPrivateKeyFileName ) ) ;
5143 }
52- } ) ;
5344
54- return host ;
45+ var host = hostBuilder . ConfigureServices ( _ =>
46+ {
47+ _ . AddMemoryCache ( ) ;
48+ _ . AddSingleton ( jwtPrivateKey ) ;
49+ _ . AddSingleton < IAuthorizationClientCredentials , AuthorizationClientCredentials > ( ) ;
50+ _ . AddSingleton < IJwtTokenService , JwtTokenService > ( ) ;
51+ _ . AddSingleton < ISigningCredentialsProvider , SigningCredentialsProvider > ( ) ;
52+ if ( ! useFakeBearerTokenService )
53+ {
54+ _ . AddSingleton < IBearerTokenService , BearerTokenService > ( ) ;
55+ }
56+ else
57+ {
58+ _ . AddSingleton < IBearerTokenService , BearerTokenServiceMock > ( ) ;
59+ }
60+ } ) ;
61+
62+ return host ;
63+ }
64+ catch ( Exception ex )
65+ {
66+ logger . LogError ( ex , ex . Message ) ;
67+ throw ;
68+ }
69+
5570 }
5671
5772 private static string CertificateToString ( X509Certificate2 certificate )
0 commit comments