-
Notifications
You must be signed in to change notification settings - Fork 392
Expand file tree
/
Copy pathProgram.cs
More file actions
298 lines (250 loc) · 13.1 KB
/
Program.cs
File metadata and controls
298 lines (250 loc) · 13.1 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
288
289
290
291
292
293
294
295
296
297
298
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.
using AppHosts.All;
using Microsoft.Extensions.Configuration;
var builder = DistributedApplication.CreateBuilder(args);
// Create a collection of the project resources to be used by the orchestrator
// This will allow us to refer back to the projects when setting up references.
var projectRegistry = new Dictionary<string, IResourceBuilder<ProjectResource>>();
// Configure options
var appConfig = builder.Configuration
.GetSection("AspireProjectConfiguration")
.Get<AppHostConfiguration>();
if (appConfig is null)
{
throw new InvalidOperationException("AspireProjectConfiguration not found in appsettings.json");
}
ConfigureIdentityServerHosts();
ConfigureApis();
ConfigureClients();
builder.Build().Run();
void ConfigureIdentityServerHosts()
{
// These hosts don't require additional infrastructure
if (HostIsEnabled(nameof(Projects.Host_Main8)))
{
var hostMain = builder
.AddProject<Projects.Host_Main8>("is-host")
.WithHttpHealthCheck(path: "/.well-known/openid-configuration");
projectRegistry.Add("is-host", hostMain);
}
if (HostIsEnabled(nameof(Projects.Host_Main9)))
{
var hostMain = builder
.AddProject<Projects.Host_Main9>("is-host")
.WithHttpHealthCheck(path: "/.well-known/openid-configuration");
projectRegistry.Add("is-host", hostMain);
}
if (HostIsEnabled(nameof(Projects.Host_Main10)))
{
var hostMain = builder
.AddProject<Projects.Host_Main10>("is-host")
.WithHttpHealthCheck(path: "/.well-known/openid-configuration");
projectRegistry.Add("is-host", hostMain);
}
// These hosts require a database
var dbHosts = new List<string>
{
nameof(Projects.Host_AspNetIdentity8),
nameof(Projects.Host_AspNetIdentity9),
nameof(Projects.Host_AspNetIdentity10),
nameof(Projects.Host_EntityFramework8),
nameof(Projects.Host_EntityFramework9),
nameof(Projects.Host_EntityFramework10)
};
if (dbHosts.Any(HostIsEnabled))
{
// Adds SQL Server to the builder (requires Docker).
// Feel free to use your preferred docker management service.
var sqlServer = builder
.AddSqlServer(name: "SqlServer", port: 62949)
.WithLifetime(ContainerLifetime.Persistent);
var identityServerDb = sqlServer
.AddDatabase(name: "IdentityServerDb", databaseName: "IdentityServer");
if (HostIsEnabled(nameof(Projects.Host_AspNetIdentity8)))
{
var hostAspNetIdentity = builder.AddProject<Projects.Host_AspNetIdentity8>(name: "is-host")
.WithHttpHealthCheck(path: "/.well-known/openid-configuration")
.WithReference(identityServerDb, connectionName: "DefaultConnection");
if (appConfig.RunDatabaseMigrations)
{
var aspnetMigration = builder.AddProject<Projects.AspNetIdentityDb>(name: "aspnetidentitydb-migrations")
.WithReference(identityServerDb, connectionName: "DefaultConnection")
.WaitFor(identityServerDb);
hostAspNetIdentity.WaitForCompletion(aspnetMigration);
}
projectRegistry.Add("is-host", hostAspNetIdentity);
}
if (HostIsEnabled(nameof(Projects.Host_AspNetIdentity9)))
{
var hostAspNetIdentity = builder.AddProject<Projects.Host_AspNetIdentity9>(name: "is-host")
.WithHttpHealthCheck(path: "/.well-known/openid-configuration")
.WithReference(identityServerDb, connectionName: "DefaultConnection");
if (appConfig.RunDatabaseMigrations)
{
var aspnetMigration = builder.AddProject<Projects.AspNetIdentityDb>(name: "aspnetidentitydb-migrations")
.WithReference(identityServerDb, connectionName: "DefaultConnection")
.WaitFor(identityServerDb);
hostAspNetIdentity.WaitForCompletion(aspnetMigration);
}
projectRegistry.Add("is-host", hostAspNetIdentity);
}
if (HostIsEnabled(nameof(Projects.Host_AspNetIdentity10)))
{
var hostAspNetIdentity = builder.AddProject<Projects.Host_AspNetIdentity10>(name: "is-host")
.WithHttpHealthCheck(path: "/.well-known/openid-configuration")
.WithReference(identityServerDb, connectionName: "DefaultConnection");
if (appConfig.RunDatabaseMigrations)
{
var aspnetMigration = builder.AddProject<Projects.AspNetIdentityDb>(name: "aspnetidentitydb-migrations")
.WithReference(identityServerDb, connectionName: "DefaultConnection")
.WaitFor(identityServerDb);
hostAspNetIdentity.WaitForCompletion(aspnetMigration);
}
projectRegistry.Add("is-host", hostAspNetIdentity);
}
if (HostIsEnabled(nameof(Projects.Host_EntityFramework8)))
{
var hostEntityFramework = builder.AddProject<Projects.Host_EntityFramework8>(name: "is-host")
.WithHttpHealthCheck(path: "/.well-known/openid-configuration")
.WithReference(identityServerDb, connectionName: "DefaultConnection");
if (appConfig.RunDatabaseMigrations)
{
var idSrvMigration = builder.AddProject<Projects.IdentityServerDb>(name: "identityserverdb-migrations")
.WithReference(identityServerDb, connectionName: "DefaultConnection")
.WaitFor(identityServerDb);
hostEntityFramework.WaitForCompletion(idSrvMigration);
}
projectRegistry.Add("is-host", hostEntityFramework);
}
if (HostIsEnabled(nameof(Projects.Host_EntityFramework9)))
{
var hostEntityFramework = builder.AddProject<Projects.Host_EntityFramework9>(name: "is-host")
.WithHttpHealthCheck(path: "/.well-known/openid-configuration")
.WithReference(identityServerDb, connectionName: "DefaultConnection");
if (appConfig.RunDatabaseMigrations)
{
var idSrvMigration = builder.AddProject<Projects.IdentityServerDb>(name: "identityserverdb-migrations")
.WithReference(identityServerDb, connectionName: "DefaultConnection")
.WaitFor(identityServerDb);
hostEntityFramework.WaitForCompletion(idSrvMigration);
}
projectRegistry.Add("is-host", hostEntityFramework);
}
if (HostIsEnabled(nameof(Projects.Host_EntityFramework10)))
{
var hostEntityFramework = builder.AddProject<Projects.Host_EntityFramework10>(name: "is-host")
.WithHttpHealthCheck(path: "/.well-known/openid-configuration")
.WithReference(identityServerDb, connectionName: "DefaultConnection");
if (appConfig.RunDatabaseMigrations)
{
var idSrvMigration = builder.AddProject<Projects.IdentityServerDb>(name: "identityserverdb-migrations")
.WithReference(identityServerDb, connectionName: "DefaultConnection")
.WaitFor(identityServerDb);
hostEntityFramework.WaitForCompletion(idSrvMigration);
}
projectRegistry.Add("is-host", hostEntityFramework);
}
}
bool HostIsEnabled(string name) =>
appConfig.IdentityHost?.Equals(name, StringComparison.OrdinalIgnoreCase) ?? false;
}
void ConfigureApis()
{
RegisterApiIfEnabled<Projects.SimpleApi>("simple-api");
RegisterApiIfEnabled<Projects.ResourceBasedApi>("resource-based-api");
RegisterApiIfEnabled<Projects.DPoPApi>("dpop-api");
RegisterApiIfEnabled<Projects.MtlsApi>("mtls-api");
}
void ConfigureClients()
{
ConfigureWebClients();
ConfigureConsoleClients();
}
void ConfigureWebClients()
{
RegisterClientIfEnabled<Projects.MvcCode>("mvc-code");
RegisterClientIfEnabled<Projects.MvcDPoP>("mvc-dpop");
RegisterClientIfEnabled<Projects.JsOidc>("js-oidc");
RegisterClientIfEnabled<Projects.MvcAutomaticTokenManagement>("mvc-automatic-token-management");
RegisterClientIfEnabled<Projects.MvcHybridBackChannel>("mvc-hybrid-backchannel");
RegisterClientIfEnabled<Projects.MvcJarJwt>("mvc-jar-jwt");
RegisterClientIfEnabled<Projects.MvcJarUriJwt>("mvc-jar-uri-jwt");
RegisterClientIfEnabled<Projects.Web>("web");
RegisterTemplateIfEnabled<Projects.IdentityServerTemplate>("template-is", 7001);
RegisterTemplateIfEnabled<Projects.IdentityServerEmpty>("template-is-empty", 7002);
RegisterTemplateIfEnabled<Projects.IdentityServerInMem>("template-is-inmem", 7003);
RegisterTemplateIfEnabled<Projects.IdentityServerAspNetIdentity>("template-is-aspid", 7004);
RegisterTemplateIfEnabled<Projects.IdentityServerEntityFramework>("template-is-ef", 7005);
}
void ConfigureConsoleClients()
{
RegisterClientIfEnabled<Projects.ConsoleCibaClient>("console-ciba-client", explicitStart: true);
RegisterClientIfEnabled<Projects.ConsoleDeviceFlow>("console-device-flow", explicitStart: true);
RegisterClientIfEnabled<Projects.ConsoleClientCredentialsFlow>("console-client-credentials-flow", explicitStart: true);
RegisterClientIfEnabled<Projects.ConsoleClientCredentialsFlowCallingIdentityServerApi>("console-client-credentials-flow-callingidentityserverapi", explicitStart: true);
RegisterClientIfEnabled<Projects.ConsoleClientCredentialsFlowPostBody>("console-client-credentials-flow-postbody", explicitStart: true);
RegisterClientIfEnabled<Projects.ConsoleClientCredentialsFlowDPoP>("console-client-credentials-flow-dpop", explicitStart: true);
RegisterClientIfEnabled<Projects.ConsoleDcrClient>("console-dcr-client", explicitStart: true);
RegisterClientIfEnabled<Projects.ConsoleEphemeralMtlsClient>("console-ephemeral-mtls-client", explicitStart: true);
RegisterClientIfEnabled<Projects.ConsoleExtensionGrant>("console-extension-grant", explicitStart: true);
RegisterClientIfEnabled<Projects.ConsoleIntrospectionClient>("console-introspection-client", explicitStart: true);
RegisterClientIfEnabled<Projects.ConsoleMTLSClient>("console-mtls-client", explicitStart: true);
RegisterClientIfEnabled<Projects.ConsoleParameterizedScopeClient>("console-parameterized-scope-client", explicitStart: true);
RegisterClientIfEnabled<Projects.ConsolePrivateKeyJwtClient>("console-private-key-jwt-client", explicitStart: true);
RegisterClientIfEnabled<Projects.ConsoleResourceOwnerFlow>("console-resource-owner-flow", explicitStart: true);
RegisterClientIfEnabled<Projects.ConsoleResourceOwnerFlowPublic>("console-resource-owner-flow-public", explicitStart: true);
RegisterClientIfEnabled<Projects.ConsoleResourceOwnerFlowReference>("console-resource-owner-flow-reference", explicitStart: true);
RegisterClientIfEnabled<Projects.ConsoleResourceOwnerFlowRefreshToken>("console-resource-owner-flow-refresh-token", explicitStart: true);
RegisterClientIfEnabled<Projects.ConsoleResourceOwnerFlowUserInfo>("console-resource-owner-flow-userinfo", explicitStart: true);
RegisterClientIfEnabled<Projects.WindowsConsoleSystemBrowser>("console-system-browser", explicitStart: true);
RegisterClientIfEnabled<Projects.ConsoleScopesResources>("console-scopes-resources", explicitStart: true);
RegisterClientIfEnabled<Projects.ConsoleCode>("console-code", explicitStart: true);
RegisterClientIfEnabled<Projects.ConsoleResourceIndicators>("console-resource-indicators", explicitStart: true);
}
bool ClientIsEnabled(string name)
{
if (appConfig.UseClients == null)
{
return false;
}
return appConfig.UseClients.TryGetValue(name, out var enabled) && enabled;
}
void RegisterApiIfEnabled<T>(string name) where T : IProjectMetadata, new()
{
if (ApiIsEnabled(typeof(T).Name))
{
var api = builder.AddProject<T>(name)
.WithEnvironment(
name: "is-host",
endpointReference: projectRegistry["is-host"].GetEndpoint(name: "https")
);
projectRegistry.Add(name, api);
}
}
bool ApiIsEnabled(string name)
{
if (appConfig.UseApis == null)
{
return false;
}
return appConfig.UseApis.TryGetValue(name, out var enabled) && enabled;
}
IResourceBuilder<ProjectResource>? RegisterTemplateIfEnabled<T>(string name, int port) where T : IProjectMetadata, new() =>
RegisterClientIfEnabled<T>(name, useLaunchProfile: false)?.WithHttpsEndpoint(port: port);
IResourceBuilder<ProjectResource>? RegisterClientIfEnabled<T>(string name, bool explicitStart = false, bool useLaunchProfile = true) where T : IProjectMetadata, new()
{
if (ClientIsEnabled(typeof(T).Name))
{
var resourceBuilder = useLaunchProfile ?
builder.AddProject<T>(name) :
builder.AddProject<T>(name, launchProfileName: null);
resourceBuilder.AddIdentityAndApiReferences(projectRegistry);
if (explicitStart)
{
resourceBuilder.WithExplicitStart();
}
return resourceBuilder;
}
return null;
}