-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathStartup.cs
More file actions
287 lines (248 loc) · 11.9 KB
/
Startup.cs
File metadata and controls
287 lines (248 loc) · 11.9 KB
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
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using OpenIddict.Server;
using OpenIddict.Server.AspNetCore;
using OpenIddict.Server.DataProtection;
using OpenIddict.Validation;
using OpenIddict.Validation.AspNetCore;
using OpenIddict.Validation.DataProtection;
using OrchardCore.BackgroundTasks;
using OrchardCore.Data.Migration;
using OrchardCore.Deployment;
using OrchardCore.DisplayManagement.Extensions;
using OrchardCore.Environment.Shell.Builders;
using OrchardCore.Modules;
using OrchardCore.Navigation;
using OrchardCore.OpenId.Configuration;
using OrchardCore.OpenId.Deployment;
using OrchardCore.OpenId.Drivers;
using OrchardCore.OpenId.Handlers;
using OrchardCore.OpenId.Migrations;
using OrchardCore.OpenId.Recipes;
using OrchardCore.OpenId.Services;
using OrchardCore.OpenId.Services.Handlers;
using OrchardCore.OpenId.Settings;
using OrchardCore.OpenId.Tasks;
using OrchardCore.Recipes;
using OrchardCore.Security;
using OrchardCore.Security.Permissions;
namespace OrchardCore.OpenId;
public sealed class Startup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
// Register the OpenIddict core services and the Orchard migrations, managers and default YesSql stores.
// The default YesSql stores can be replaced by another database by referencing the corresponding
// OpenIddict package (e.g OpenIddict.EntityFrameworkCore) and registering it in the options.
services.AddOpenIddict()
.AddCore(options =>
{
options.AddOrchardMigrations()
.UseOrchardManagers()
.UseYesSql();
});
services.AddPermissionProvider<Permissions>();
}
}
[Feature(OpenIdConstants.Features.Client)]
public sealed class ClientStartup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
services.AddNavigationProvider<ClientAdminMenu>();
services.TryAddSingleton<IOpenIdClientService, OpenIdClientService>();
// Note: the following services are registered using TryAddEnumerable to prevent duplicate registrations.
services.AddSiteDisplayDriver<OpenIdClientSettingsDisplayDriver>();
services.AddRecipeExecutionStep<OpenIdClientSettingsStep>();
// Register the options initializers required by the OpenID Connect client handler.
services.TryAddEnumerable(new[]
{
// Orchard-specific initializers:
ServiceDescriptor.Singleton<IConfigureOptions<AuthenticationOptions>, OpenIdClientConfiguration>(),
ServiceDescriptor.Singleton<IConfigureOptions<OpenIdConnectOptions>, OpenIdClientConfiguration>(),
// Built-in initializers:
ServiceDescriptor.Singleton<IPostConfigureOptions<OpenIdConnectOptions>, OpenIdConnectPostConfigureOptions>(),
});
}
}
[Feature(OpenIdConstants.Features.Server)]
public sealed class ServerStartup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
services.AddNavigationProvider<ServerAdminMenu>();
services.AddOpenIddict()
.AddServer(options =>
{
options.UseAspNetCore();
options.UseDataProtection();
options.AddEventHandler(PersistStoresHandler.Descriptor);
});
services.TryAddSingleton<IOpenIdServerService, OpenIdServerService>();
services.AddDataMigration<DefaultScopesMigration>();
services.AddDataMigration<PushedAuthorizationRequestsMigration>();
services.AddDisplayDriver<OpenIdServerSettings, OpenIdServerSettingsDisplayDriver>();
// Note: the following services are registered using TryAddEnumerable to prevent duplicate registrations.
services.TryAddEnumerable(new[]
{
ServiceDescriptor.Scoped<IRoleRemovedEventHandler, OpenIdApplicationRoleRemovedEventHandler>(),
ServiceDescriptor.Singleton<IBackgroundTask, OpenIdBackgroundTask>(),
});
services.AddRecipeExecutionStep<OpenIdServerSettingsStep>()
.AddRecipeExecutionStep<OpenIdApplicationStep>()
.AddRecipeExecutionStep<OpenIdScopeStep>();
// Note: the OpenIddict ASP.NET host adds an authentication options initializer that takes care of
// registering the server ASP.NET Core handler. Yet, it MUST NOT be registered at this stage
// as it is lazily registered by OpenIdServerConfiguration only after checking the OpenID server
// settings are valid and can be safely used in this tenant without causing runtime exceptions.
// To prevent that, the initializer is manually removed from the services collection of the tenant.
services.RemoveAll<IConfigureOptions<AuthenticationOptions>, OpenIddictServerAspNetCoreConfiguration>();
services.TryAddEnumerable(new[]
{
ServiceDescriptor.Singleton<IConfigureOptions<AuthenticationOptions>, OpenIdServerConfiguration>(),
ServiceDescriptor.Singleton<IConfigureOptions<OpenIddictServerOptions>, OpenIdServerConfiguration>(),
ServiceDescriptor.Singleton<IConfigureOptions<OpenIddictServerAspNetCoreOptions>, OpenIdServerConfiguration>(),
ServiceDescriptor.Singleton<IConfigureOptions<OpenIddictServerDataProtectionOptions>, OpenIdServerConfiguration>(),
});
}
public override async ValueTask ConfigureAsync(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider)
{
var settings = await GetServerSettingsAsync();
if (settings == null)
{
return;
}
if (settings.AuthorizationEndpointPath.HasValue)
{
routes.MapAreaControllerRoute(
name: "Access.Authorize",
areaName: typeof(Startup).Namespace,
pattern: settings.AuthorizationEndpointPath.Value,
defaults: new { controller = "Access", action = "Authorize" }
);
}
if (settings.LogoutEndpointPath.HasValue)
{
routes.MapAreaControllerRoute(
name: "Access.Logout",
areaName: typeof(Startup).Namespace,
pattern: settings.LogoutEndpointPath.Value,
defaults: new { controller = "Access", action = "Logout" }
);
}
if (settings.TokenEndpointPath.HasValue)
{
routes.MapAreaControllerRoute(
name: "Access.Token",
areaName: typeof(Startup).Namespace,
pattern: settings.TokenEndpointPath.Value,
defaults: new { controller = "Access", action = "Token" }
);
}
if (settings.UserinfoEndpointPath.HasValue)
{
routes.MapAreaControllerRoute(
name: "UserInfo.Me",
areaName: typeof(Startup).Namespace,
pattern: settings.UserinfoEndpointPath.Value,
defaults: new { controller = "UserInfo", action = "Me" }
);
}
async Task<OpenIdServerSettings> GetServerSettingsAsync()
{
// Note: the OpenID server service is registered as a singleton service and thus can be
// safely used with the non-scoped/root service provider available at this stage.
var service = serviceProvider.GetRequiredService<IOpenIdServerService>();
var configuration = await service.GetSettingsAsync();
if ((await service.ValidateSettingsAsync(configuration)).Any(result => result != ValidationResult.Success))
{
return null;
}
return configuration;
}
}
}
[RequireFeatures("OrchardCore.Deployment", OpenIdConstants.Features.Server)]
public sealed class ServerDeploymentStartup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
services.AddDeployment<OpenIdServerDeploymentSource, OpenIdServerDeploymentStep, OpenIdServerDeploymentStepDriver>();
}
}
[Feature(OpenIdConstants.Features.Validation)]
public sealed class ValidationStartup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
services.AddNavigationProvider<ValidationAdminMenu>();
services.AddOpenIddict()
.AddValidation(options =>
{
options.UseAspNetCore();
options.UseDataProtection();
options.UseSystemNetHttp();
});
services.TryAddSingleton<IOpenIdValidationService, OpenIdValidationService>();
// Note: the following services are registered using TryAddEnumerable to prevent duplicate registrations.
services.AddDisplayDriver<OpenIdValidationSettings, OpenIdValidationSettingsDisplayDriver>();
services.AddRecipeExecutionStep<OpenIdValidationSettingsStep>();
// Note: the OpenIddict ASP.NET host adds an authentication options initializer that takes care of
// registering the validation handler. Yet, it MUST NOT be registered at this stage as it is
// lazily registered by OpenIdValidationConfiguration only after checking the OpenID validation
// settings are valid and can be safely used in this tenant without causing runtime exceptions.
// To prevent that, the initializer is manually removed from the services collection of the tenant.
services.RemoveAll<IConfigureOptions<AuthenticationOptions>, OpenIddictValidationAspNetCoreConfiguration>();
services.TryAddEnumerable(new[]
{
ServiceDescriptor.Singleton<IConfigureOptions<AuthenticationOptions>, OpenIdValidationConfiguration>(),
ServiceDescriptor.Singleton<IConfigureOptions<ApiAuthorizationOptions>, OpenIdValidationConfiguration>(),
ServiceDescriptor.Singleton<IConfigureOptions<OpenIddictValidationOptions>, OpenIdValidationConfiguration>(),
ServiceDescriptor.Singleton<IConfigureOptions<OpenIddictValidationDataProtectionOptions>, OpenIdValidationConfiguration>(),
});
}
}
[RequireFeatures("OrchardCore.Deployment", OpenIdConstants.Features.Validation)]
public sealed class ValidationDeploymentStartup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
services.AddDeployment<OpenIdValidationDeploymentSource, OpenIdValidationDeploymentStep, OpenIdValidationDeploymentStepDriver>();
}
}
[Feature(OpenIdConstants.Features.Management)]
public sealed class ManagementStartup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
services.AddNavigationProvider<ManagementAdminMenu>();
}
}
internal static class OpenIdServiceCollectionExtensions
{
public static IServiceCollection RemoveAll(this IServiceCollection services, Type serviceType, Type implementationType)
{
Debug.Assert(services != null, "The services collection shouldn't be null.");
Debug.Assert(serviceType != null, "The service type shouldn't be null.");
Debug.Assert(implementationType != null, "The implementation type shouldn't be null.");
for (var index = services.Count - 1; index >= 0; index--)
{
var descriptor = services[index];
if (descriptor.ServiceType == serviceType && descriptor.GetImplementationType() == implementationType)
{
services.RemoveAt(index);
}
}
return services;
}
public static IServiceCollection RemoveAll<TService, TImplementation>(this IServiceCollection services)
where TImplementation : TService
=> services.RemoveAll(typeof(TService), typeof(TImplementation));
}