This repository has been archived by the owner on Sep 18, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 764
/
IdentityServerServiceFactory.cs
352 lines (306 loc) · 13.1 KB
/
IdentityServerServiceFactory.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
* Copyright 2014, 2015 Dominick Baier, Brock Allen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using IdentityServer3.Core.Logging;
using IdentityServer3.Core.Services;
using IdentityServer3.Core.Services.Default;
using IdentityServer3.Core.Validation;
using System;
using System.Collections.Generic;
namespace IdentityServer3.Core.Configuration
{
/// <summary>
/// Use this class to replace built-in services, or add additional dependencies to the container
/// </summary>
public class IdentityServerServiceFactory
{
static readonly ILog Logger = LogProvider.GetCurrentClassLogger();
static readonly Registration<IExternalClaimsFilter> DefaultClaimsFilter;
/// <summary>
/// Initializes the <see cref="IdentityServerServiceFactory"/> class.
/// </summary>
static IdentityServerServiceFactory()
{
DefaultClaimsFilter = new Registration<IExternalClaimsFilter>(resolver =>
{
var aggregateFilter = new AggregateExternalClaimsFilter(
new NormalizingClaimsFilter(),
new FacebookClaimsFilter(),
new TwitterClaimsFilter()
);
return aggregateFilter;
});
}
// keep list of any additional dependencies the
// hosting application might need. these will be
// added to the DI container
readonly List<Registration> _registrations = new List<Registration>();
/// <summary>
/// Initializes a new instance of the <see cref="IdentityServerServiceFactory"/> class.
/// </summary>
public IdentityServerServiceFactory()
{
this.ExternalClaimsFilter = DefaultClaimsFilter;
CustomGrantValidators = new List<Registration<ICustomGrantValidator>>();
// register default secret parsers
SecretParsers = new List<Registration<ISecretParser>>
{
new Registration<ISecretParser, BasicAuthenticationSecretParser>(),
new Registration<ISecretParser, PostBodySecretParser>(),
new Registration<ISecretParser, X509CertificateSecretParser>()
};
// register default secret validators
SecretValidators = new List<Registration<ISecretValidator>>
{
new Registration<ISecretValidator, HashedSharedSecretValidator>(),
new Registration<ISecretValidator, X509CertificateThumbprintSecretValidator>()
};
}
/// <summary>
/// Gets the a list of additional dependencies.
/// </summary>
/// <value>
/// The dependencies.
/// </value>
public IEnumerable<Registration> Registrations
{
get { return _registrations; }
}
/// <summary>
/// Adds a registration to the dependency list
/// </summary>
/// <typeparam name="T">Type of the dependency</typeparam>
/// <param name="registration">The registration.</param>
public void Register<T>(Registration<T> registration)
where T : class
{
_registrations.Add(registration);
}
///////////////////////
// mandatory (external)
///////////////////////
/// <summary>
/// Gets or sets the user service. The user service implements user authentication against the local user store as well as association of external users. There are standard implementations for in-memory, MembershipReboot and ASP.NET Identity
/// </summary>
/// <value>
/// The user service.
/// </value>
public Registration<IUserService> UserService { get; set; }
/// <summary>
/// Gets or sets the scope store. The scope store implements retrieval of scopes configuration data.
/// </summary>
/// <value>
/// The scope store.
/// </value>
public Registration<IScopeStore> ScopeStore { get; set; }
/// <summary>
/// Gets or sets the client store. The client store implements retrieval of client configuration data.
/// </summary>
/// <value>
/// The client store.
/// </value>
public Registration<IClientStore> ClientStore { get; set; }
///////////////////////
// mandatory (for authorization code, reference & refresh tokens and consent)
// but with default in memory implementation
///////////////////////
/// <summary>
/// Gets or sets the authorization code store - implements storage and retrieval of authorization codes.
/// </summary>
/// <value>
/// The authorization code store.
/// </value>
public Registration<IAuthorizationCodeStore> AuthorizationCodeStore { get; set; }
/// <summary>
/// Gets or sets the token handle store - Implements storage and retrieval of handles for reference tokens.
/// </summary>
/// <value>
/// The token handle store.
/// </value>
public Registration<ITokenHandleStore> TokenHandleStore { get; set; }
/// <summary>
/// Gets or sets the consent store - Implements storage and retrieval of consent decisions.
/// </summary>
/// <value>
/// The consent store.
/// </value>
public Registration<IConsentStore> ConsentStore { get; set; }
/// <summary>
/// Gets or sets the refresh token store - Implements storage and retrieval of refresh tokens.
/// </summary>
/// <value>
/// The refresh token store.
/// </value>
public Registration<IRefreshTokenStore> RefreshTokenStore { get; set; }
/// <summary>
/// Gets or sets the view service - Implements retrieval of UI assets. Defaults to using the embedded assets.
/// </summary>
/// <value>
/// The view service.
/// </value>
public Registration<IViewService> ViewService { get; set; }
///////////////////////
// optional (with default implementations)
///////////////////////
/// <summary>
/// Gets or sets the consent service - Implements logic of consent decisions
/// </summary>
/// <value>
/// The consent service.
/// </value>
public Registration<IConsentService> ConsentService { get; set; }
/// <summary>
/// Gets or sets the client permissions service - Implements retrieval and revocation of consents, reference and refresh tokens.
/// </summary>
/// <value>
/// The client permissions service.
/// </value>
public Registration<IClientPermissionsService> ClientPermissionsService { get; set; }
/// <summary>
/// Gets or sets the custom grant validator - Implements validation of custom grant types.
/// </summary>
/// <value>
/// The custom grant validator.
/// </value>
public List<Registration<ICustomGrantValidator>> CustomGrantValidators { get; set; }
/// <summary>
/// Gets or sets the custom request validator - Implements custom additional validation of authorize and token requests.
/// </summary>
/// <value>
/// The custom request validator.
/// </value>
public Registration<ICustomRequestValidator> CustomRequestValidator { get; set; }
/// <summary>
/// Gets or sets the claims provider - Implements retrieval of claims for identity and access tokens.
/// </summary>
/// <value>
/// The claims provider.
/// </value>
public Registration<IClaimsProvider> ClaimsProvider { get; set; }
/// <summary>
/// Gets or sets the token service - Implements creation of security tokens definitions.
/// </summary>
/// <value>
/// The token service.
/// </value>
public Registration<ITokenService> TokenService { get; set; }
/// <summary>
/// Gets or sets the refresh token service - Implements creation and updates of refresh tokens.
/// </summary>
/// <value>
/// The refresh token service.
/// </value>
public Registration<IRefreshTokenService> RefreshTokenService { get; set; }
/// <summary>
/// Gets or sets the token signing service - Implements creation and signing of security tokens.
/// </summary>
/// <value>
/// The token signing service.
/// </value>
public Registration<ITokenSigningService> TokenSigningService { get; set; }
/// <summary>
/// Gets or sets the external claims filter - Implements filtering and transformation of claims for external identity providers.
/// </summary>
/// <value>
/// The external claims filter.
/// </value>
public Registration<IExternalClaimsFilter> ExternalClaimsFilter { get; set; }
/// <summary>
/// Gets or sets the event service.
/// </summary>
/// <value>
/// The event service.
/// </value>
public Registration<IEventService> EventService { get; set; }
/// <summary>
/// Gets or sets the custom token validator - Implements custom additional validation of tokens for the token validation endpoints.
/// </summary>
/// <value>
/// The custom token validator.
/// </value>
public Registration<ICustomTokenValidator> CustomTokenValidator { get; set; }
/// <summary>
/// Gets or sets the redirect URI validator.
/// </summary>
/// <value>
/// The redirect URI validator.
/// </value>
public Registration<IRedirectUriValidator> RedirectUriValidator { get; set; }
/// <summary>
/// Gets or sets the localization service.
/// </summary>
/// <value>
/// The localization service.
/// </value>
public Registration<ILocalizationService> LocalizationService { get; set; }
/// <summary>
/// Gets or sets the secret parsers.
/// </summary>
/// <value>
/// The secret parsers.
/// </value>
public IEnumerable<Registration<ISecretParser>> SecretParsers { get; set; }
/// <summary>
/// Gets or sets the secret validators.
/// </summary>
/// <value>
/// The secret validators.
/// </value>
public IEnumerable<Registration<ISecretValidator>> SecretValidators { get; set; }
/// <summary>
/// Gets or sets the CORS policy service.
/// </summary>
/// <value>
/// The CORS policy service.
/// </value>
public Registration<ICorsPolicyService> CorsPolicyService { get; set; }
/// Gets or sets the custom token response generator
/// </summary>
/// <value>
/// The custom token response generator
/// </value>
public Registration<ICustomTokenResponseGenerator> CustomTokenResponseGenerator { get; set; }
/// <summary>
/// Gets or sets the authentication session validator.
/// </summary>
/// <value>
/// The authentication session validator.
/// </value>
public Registration<IAuthenticationSessionValidator> AuthenticationSessionValidator { get; set; }
/// <summary>
/// Gets or sets the signing key service.
/// </summary>
/// <value>
/// The signing key service.
/// </value>
public Registration<ISigningKeyService> SigningKeyService { get; set; }
internal void Validate()
{
if (UserService == null) LogAndStop("UserService not configured");
if (ScopeStore == null) LogAndStop("ScopeStore not configured.");
if (ClientStore == null) LogAndStop("ClientStore not configured.");
if (AuthorizationCodeStore == null) Logger.Warn("AuthorizationCodeStore not configured - falling back to InMemory");
if (TokenHandleStore == null) Logger.Warn("TokenHandleStore not configured - falling back to InMemory");
if (ConsentStore == null) Logger.Warn("ConsentStore not configured - falling back to InMemory");
if (RefreshTokenStore == null) Logger.Warn("RefreshTokenStore not configured - falling back to InMemory");
if (RedirectUriValidator != null) Logger.Warn("Using custom redirect URI validator - you are running with scissors.");
}
private void LogAndStop(string message)
{
Logger.Error(message);
throw new InvalidOperationException(message);
}
}
}