|
| 1 | +using System.Text.Json; |
| 2 | + |
1 | 3 | var builder = DistributedApplication.CreateBuilder(args); |
2 | 4 |
|
3 | 5 | var api = builder.AddProject<Projects.MockOidcApp_Api>("api") |
4 | 6 | .WithExternalHttpEndpoints(); |
5 | 7 |
|
6 | | -builder.AddNpmApp("vite", "../MockOidcApp.Vite", "dev") |
| 8 | +var vite = builder.AddNpmApp("vite", "../MockOidcApp.Vite", "dev") |
7 | 9 | .WithReference(api) |
8 | 10 | .WithEnvironment("BROWSER", "none") |
9 | 11 | .WithHttpEndpoint(env: "VITE_PORT", port: 5100) |
10 | 12 | .WithExternalHttpEndpoints() |
11 | 13 | .PublishAsDockerFile(); |
12 | 14 |
|
| 15 | +if (builder.ExecutionContext.IsPublishMode == false) |
| 16 | +{ |
| 17 | + var clientId = Guid.NewGuid().ToString(); |
| 18 | + var tenantId = Guid.NewGuid().ToString(); |
| 19 | + var certPassword = Guid.NewGuid().ToString(); |
| 20 | + var certExportExe = builder.AddExecutable("cert-export-exe", "dotnet", ".", "dev-certs", "https", "-ep", "./dev-certificates/aspnetapp.pfx", "-p", certPassword, "--trust", "--verbose"); |
| 21 | + |
| 22 | + var mockEntra = builder.AddContainer("mock-entra", "ghcr.io/soluto/oidc-server-mock") |
| 23 | + .WaitForCompletion(certExportExe) |
| 24 | + .WithEnvironment("ASPNETCORE_Kestrel__Certificates__Default__Password", certPassword) |
| 25 | + .WithEnvironment("ASPNETCORE_Kestrel__Certificates__Default__Path", "/https/aspnetapp.pfx") |
| 26 | + .WithBindMount("./dev-certificates", "/https") |
| 27 | + .WithEnvironment("ASPNETCORE_URLS", "https://+:443") |
| 28 | + .WithHttpsEndpoint(targetPort: 443); |
| 29 | + |
| 30 | + mockEntra |
| 31 | + .WithEnvironment("ASPNET_SERVICES_OPTIONS_INLINE", System.Text.Json.JsonSerializer.Serialize(new { BasePath = $"/{tenantId}/v2.0" })) |
| 32 | + .WithEnvironment( |
| 33 | + "CLIENTS_CONFIGURATION_INLINE", |
| 34 | + () => System.Text.Json.JsonSerializer.Serialize(new[] |
| 35 | + { |
| 36 | + new |
| 37 | + { |
| 38 | + ClientId = clientId, |
| 39 | + ClientName = "Sample App", |
| 40 | + AllowedGrantTypes = new [] { "authorization_code" }, |
| 41 | + RedirectUris = new [] { vite.GetEndpoint("http").Url }, |
| 42 | + PostLogoutRedirectUris = new [] { vite.GetEndpoint("http").Url }, |
| 43 | + AllowedScopes = new [] { "openid", "profile", $"api://{clientId}/access_as_user" }, |
| 44 | + AllowOfflineAccess = true, |
| 45 | + RequireClientSecret = false, |
| 46 | + AlwaysSendClientClaims = true, |
| 47 | + Claims = new [] { |
| 48 | + new { Type = "aud", Value = $"api://{clientId}" }, |
| 49 | + new { Type = "ver", Value = $"1.0" }, |
| 50 | + }, |
| 51 | + ClientClaimsPrefix = string.Empty, |
| 52 | + } |
| 53 | + })) |
| 54 | + .WithEnvironment("USERS_CONFIGURATION_INLINE", () => System.Text.Json.JsonSerializer.Serialize(new[] |
| 55 | + { |
| 56 | + new |
| 57 | + { |
| 58 | + SubjectId = "1", |
| 59 | + Username = "admin@test.com", |
| 60 | + Password = "Password123", |
| 61 | + Claims = new [] |
| 62 | + { |
| 63 | + new { Type = "name", Value = "Frank Gardner" }, |
| 64 | + new { Type = "scp", Value = "access_as_user"}, |
| 65 | + } |
| 66 | + } |
| 67 | + })) |
| 68 | + .WithEnvironment( |
| 69 | + "SERVER_OPTIONS_INLINE", |
| 70 | + () => JsonSerializer.Serialize(new |
| 71 | + { |
| 72 | + Cors = new |
| 73 | + { |
| 74 | + CorsPaths = new[] |
| 75 | + { |
| 76 | + $"/{tenantId}/v2.0/.well-known/openid-configuration", |
| 77 | + $"/{tenantId}/v2.0/connect/token" |
| 78 | + } |
| 79 | + }, |
| 80 | + })) |
| 81 | + .WithEnvironment( |
| 82 | + "API_SCOPES_INLINE", |
| 83 | + () => JsonSerializer.Serialize(new[] |
| 84 | + { |
| 85 | + new |
| 86 | + { |
| 87 | + Name = $"api://{clientId}/access_as_user", |
| 88 | + UserClaims = new[] |
| 89 | + { |
| 90 | + "scp" |
| 91 | + } |
| 92 | + } |
| 93 | + })); |
| 94 | + |
| 95 | + api |
| 96 | + .WithEnvironment("AzureAd__Instance", mockEntra.GetEndpoint("https")) |
| 97 | + .WithEnvironment("AzureAd__ClientId", clientId) |
| 98 | + .WithEnvironment("AzureAd__TenantId", tenantId); |
| 99 | +} |
| 100 | + |
13 | 101 | builder.Build().Run(); |
0 commit comments