/
Startup.cs
213 lines (187 loc) · 10.1 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System.Threading.Tasks;
using ffdc_sample_dotnet3.Core.Configuration;
using ffdc_sample_dotnet3.Core.HttpClients;
using ffdc_sample_dotnet3.Core.PrivateKeyTokenGenerator;
using IdentityModel;
using IdentityModel.AspNetCore.AccessTokenManagement;
using IdentityModel.Client;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
namespace ffdc_sample_dotnet3
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddHttpContextAccessor();
var finastraConfiguration = new FinastraConfiguration();
var finastraConfigSection = Configuration.GetSection("Finastra");
finastraConfigSection.Bind(finastraConfiguration);
#region Configuration of Configuration -)
services.Configure<FinastraConfiguration>(finastraConfigSection);
services.AddSingleton<IValidateOptions<FinastraConfiguration>, ConfigurationValidator>();
services.TryAddSingleton(provider => provider.GetRequiredService<IOptions<FinastraConfiguration>>().Value);
#endregion
// service providing private_key_jwt
services.AddPrivateKeyJwtGenerator(o =>
{
o.TokenEndpoint = finastraConfiguration.Oauth2Configuration.Issuer; // Finastra is validating against Issuer
o.TokenLifeTimeInMinutes =
finastraConfiguration.Oauth2Configuration.PrivateKeyJwtExpirationTimeInMinutes;
}
);
#region B2B token management service configuration
//for B2B private_key_jwt auth method - register custom client assertion configuration
if (finastraConfiguration.Oauth2Configuration.ClientAuthenticationMethod ==
ClientAuthenticationMethod.private_key_jwt)
{
services.AddTransient<ITokenClientConfigurationService, ClientAssertionTokenClientConfigurationService>();
}// client_credentials will use DefaultTokenClientConfigurationService registered on next line
services.AddAccessTokenManagement(o =>
{
// Here we can register either one client token service or many, depending on configuration
// and business needs.
// This sample registers one client named "finastra_login_service" per auth type.
//in case of client_secret method - need to provide tokenEndpoint, clientId, secret.
if (finastraConfiguration.Oauth2Configuration.ClientAuthenticationMethod ==
ClientAuthenticationMethod.client_secret)
{
o.Client.Clients.Add("finastra_login_service", new ClientCredentialsTokenRequest
{
Address = finastraConfiguration.Oauth2Configuration.TokenEndpoint,
ClientId = finastraConfiguration.Oauth2Configuration.B2B.ClientId,
ClientSecret = finastraConfiguration.Oauth2Configuration.B2B.ClientSecret
});
}
else
{
// this token client configured to use private_jwt method.
// in this case we need only tokenEndpoint - credentials set via private_key_jwt.
// ClientAssertion added in ClientAssertionTokenClientConfigurationService.GetClientCredentialsRequestAsync
// see class ClientAssertionTokenClientConfigurationService
o.Client.Clients.Add("finastra_login_service", new ClientCredentialsTokenRequest
{
Address = finastraConfiguration.Oauth2Configuration.TokenEndpoint
});
}
});
// B2B flow config. Typed client registration and link to TokenManagementService.
services.AddHttpClient<IFfdcHttpClient, FfdcHttpClient>()
.AddClientAccessTokenHandler("finastra_login_service");
////alternative way - use custom httpMessageHandler for outbound requests.
////This sample implementation can use service discovery, instead of explicit token, issuer addresses.
//services.AddPrivateKeyJwtHttpClient(o =>
//{
// o.AuthorityEndpoint = finastraConfiguration.Oauth2Configuration.AuthorityEndpoint;
//});
#endregion
#region B2C OIDC config start
services.AddAuthentication(o =>
{
o.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
o.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(o =>
{
o.ClientId = finastraConfiguration.Oauth2Configuration.B2C.ClientId;
o.ClientSecret = finastraConfiguration.Oauth2Configuration.B2C.ClientSecret;
o.Authority = finastraConfiguration.Oauth2Configuration.AuthorityEndpoint;
o.CallbackPath = "/signin-oidc"; // this path or your custom one should be registered in FFDC application configuration. See FFDC application registration doc.
o.SaveTokens = true;
o.ResponseType = OpenIdConnectResponseType.Code;
// Indicates a Query Response see: http://openid.net/specs/openid-connect-core-1_0.html#ImplicitAuthResponse.
// other variant - form POST.
o.ResponseMode = OpenIdConnectResponseMode.Query;
o.Events.OnAuthorizationCodeReceived = context =>
{
if (finastraConfiguration.Oauth2Configuration.ClientAuthenticationMethod ==
ClientAuthenticationMethod.client_secret)
{
return Task.CompletedTask;
//for client secret no need to set clientAssertion.
}
var tokenGenerator = context.HttpContext.RequestServices.GetService<IPrivateKeyJwtGenerator>();
context.TokenEndpointRequest.ClientAssertionType = OidcConstants.ClientAssertionTypes.JwtBearer;
context.TokenEndpointRequest.ClientAssertion = tokenGenerator.CreateClientAuthJwt(new PrivateKeyJwtOptions
{
ClientId = finastraConfiguration.Oauth2Configuration.B2C.ClientId,
JwkKeyId = finastraConfiguration.Oauth2Configuration.B2B.JwkKeyId,
PrivateKeyPath = finastraConfiguration.Oauth2Configuration.B2B.PrivateKey
//other params if they are different in config
});
return Task.CompletedTask;
};
});
//B2C http client registration and attach userAccessTokenHandler
//OAUTH configuration is taken from OpenIdConnect Config above
services.AddHttpClient<IAccountsHttpClient, AccountsHttpClient>()
.AddUserAccessTokenHandler();
#endregion
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
public class ClientAssertionTokenClientConfigurationService : DefaultTokenClientConfigurationService
{
private readonly IPrivateKeyJwtGenerator _privateKeyJwtGenerator;
private readonly FinastraConfiguration _finastraConfiguration;
public ClientAssertionTokenClientConfigurationService(IOptions<AccessTokenManagementOptions> accessTokenManagementOptions,
IOptionsMonitor<OpenIdConnectOptions> oidcOptions,
IAuthenticationSchemeProvider schemeProvider,
IPrivateKeyJwtGenerator privateKeyJwtGenerator,
FinastraConfiguration finastraConfiguration
) : base(accessTokenManagementOptions, oidcOptions, schemeProvider)
{
_privateKeyJwtGenerator = privateKeyJwtGenerator;
_finastraConfiguration = finastraConfiguration;
}
// need this override to recalculate ClientAssertion.Value in B2B flow.
public override async Task<ClientCredentialsTokenRequest> GetClientCredentialsRequestAsync(string clientName)
{
var r = await base.GetClientCredentialsRequestAsync(clientName);
r.ClientAssertion.Type = OidcConstants.ClientAssertionTypes.JwtBearer;
r.ClientAssertion.Value = _privateKeyJwtGenerator.CreateClientAuthJwt(new PrivateKeyJwtOptions
{
JwkKeyId = _finastraConfiguration.Oauth2Configuration.B2B.JwkKeyId,
ClientId = _finastraConfiguration.Oauth2Configuration.B2B.ClientId,
PrivateKeyPath = _finastraConfiguration.Oauth2Configuration.B2B.PrivateKey
});
return r;
}
}
}