|
| 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 clientSecret = Guid.NewGuid().ToString(); |
| 20 | + var certPassword = Guid.NewGuid().ToString(); |
| 21 | + var certExportExe = builder.AddExecutable("cert-export-exe", "dotnet", ".", "dev-certs", "https", "-ep", "./dev-certificates/aspnetapp.pfx", "-p", certPassword, "--trust"); |
| 22 | + |
| 23 | + var mockEntra = builder.AddContainer("mock-entra", "ghcr.io/soluto/oidc-server-mock") |
| 24 | + .WaitForCompletion(certExportExe) |
| 25 | + .WithEnvironment("ASPNETCORE_Kestrel__Certificates__Default__Password", certPassword) |
| 26 | + .WithEnvironment("ASPNETCORE_Kestrel__Certificates__Default__Path", "/https/aspnetapp.pfx") |
| 27 | + .WithBindMount("./dev-certificates", "/https") |
| 28 | + .WithEnvironment("ASPNETCORE_URLS", "https://+:443") |
| 29 | + .WithHttpsEndpoint(targetPort: 443); |
| 30 | + |
| 31 | + mockEntra |
| 32 | + .WithEnvironment("CLIENTS_CONFIGURATION_INLINE", () => System.Text.Json.JsonSerializer.Serialize(new[] |
| 33 | + { |
| 34 | + new |
| 35 | + { |
| 36 | + ClientId = clientId, |
| 37 | + // ClientSecrets = new[] { clientSecret }, |
| 38 | + // Description = "Client for implicit flow", |
| 39 | + AllowedGrantTypes = new [] { "authorization_code" }, |
| 40 | + RedirectUris = new [] { vite.GetEndpoint("http").Url }, |
| 41 | + // PostLogoutRedirectUris = new [] { frontend.GetEndpoint("https").Url + "/signout-callback-oidc" }, |
| 42 | + AllowedScopes = new [] { "openid", "profile", $"api://{clientId}/access_as_user" }, |
| 43 | + AllowOfflineAccess = true, |
| 44 | + RequireClientSecret = false, |
| 45 | + AlwaysSendClientClaims = true, |
| 46 | + Claims = new [] { |
| 47 | + new { Type = "aud", Value = $"api://{clientId}" }, |
| 48 | + new { Type = "ver", Value = $"1.0" }, |
| 49 | + new { Type = "tid", Value = tenantId }, |
| 50 | + }, |
| 51 | + ClientClaimsPrefix = string.Empty, |
| 52 | + AlwaysIncludeUserClaimsInIdToken = true, |
| 53 | + } |
| 54 | + })) |
| 55 | + .WithEnvironment("USERS_CONFIGURATION_INLINE", () => System.Text.Json.JsonSerializer.Serialize(new[] |
| 56 | + { |
| 57 | + new |
| 58 | + { |
| 59 | + SubjectId = "1", |
| 60 | + Username = "admin@test.com", |
| 61 | + Password = "Password123", |
| 62 | + Claims = new [] |
| 63 | + { |
| 64 | + new { Type = "name", Value = "Frank Gardner" }, |
| 65 | + // new { Type = "tid", Value = Guid.NewGuid().ToString() }, |
| 66 | + // new { Type = "aud", Value = $"api://{clientId}" }, |
| 67 | + new { Type = "scp", Value = "access_as_user"}, |
| 68 | + } |
| 69 | + } |
| 70 | + })) |
| 71 | + .WithEnvironment( |
| 72 | + "SERVER_OPTIONS_INLINE", |
| 73 | + () => JsonSerializer.Serialize(new |
| 74 | + { |
| 75 | + // Discovery = new { ShowGrantTypes = false }, |
| 76 | + Cors = new |
| 77 | + { |
| 78 | + CorsPaths = new[] |
| 79 | + { |
| 80 | + // $"/{tenantId}/.well-known/openid-configuration", |
| 81 | + // $"/{tenantId}/connect/token", |
| 82 | + $"/{tenantId}/v2.0/.well-known/openid-configuration", |
| 83 | + $"/{tenantId}/v2.0/connect/token" |
| 84 | + } |
| 85 | + }, |
| 86 | + // EmitStaticAudienceClaim = true, |
| 87 | + // Endpoints = new |
| 88 | + // { |
| 89 | + // EnablePushedAuthorizationEndpoint = false, |
| 90 | + // EnableCheckSessionEndpoint = false, |
| 91 | + // EnableTokenRevocationEndpoint = false, |
| 92 | + // EnableIntrospectionEndpoint = false, |
| 93 | + // EnableBackchannelAuthenticationEndpoint = false |
| 94 | + // }, |
| 95 | + // UserInteraction = new { PromptValuesSupported = Array.Empty<object>() } |
| 96 | + // IssuerUri = $"{mockEntra.GetEndpoint("https").Url}/", |
| 97 | + EmitScopesAsSpaceDelimitedStringInJwt = true, |
| 98 | + })) |
| 99 | + .WithEnvironment("OVERRIDE_STANDARD_IDENTITY_RESOURCES", "true") |
| 100 | + .WithEnvironment( |
| 101 | + "IDENTITY_RESOURCES_INLINE", |
| 102 | + () => JsonSerializer.Serialize(new[] |
| 103 | + { |
| 104 | + new { Name = "openid", ClaimTypes = new[] { "sub" } }, |
| 105 | + new { Name = "profile", ClaimTypes = new[] { "name" } } |
| 106 | + })) |
| 107 | + .WithEnvironment( |
| 108 | + "API_SCOPES_INLINE", |
| 109 | + () => JsonSerializer.Serialize(new[] |
| 110 | + { |
| 111 | + new |
| 112 | + { |
| 113 | + Name = $"api://{clientId}/access_as_user", |
| 114 | + UserClaims = new[] |
| 115 | + { |
| 116 | + "tid", |
| 117 | + "name", |
| 118 | + // "oid", |
| 119 | + // "email", |
| 120 | + "scp" |
| 121 | + } |
| 122 | + } |
| 123 | + })) |
| 124 | + .WithEnvironment("ASPNET_SERVICES_OPTIONS_INLINE", System.Text.Json.JsonSerializer.Serialize(new { BasePath = $"/{tenantId}/v2.0" })) |
| 125 | + // .WithEnvironment("ASPNET_SERVICES_OPTIONS_INLINE", System.Text.Json.JsonSerializer.Serialize(new { BasePath = $"/{tenantId}" })) |
| 126 | + .WithEnvironment("SERVER_CORS_ALLOWED_ORIGINS_INLINE", () => JsonSerializer.Serialize(new [] { vite.GetEndpoint("http").Url })); |
| 127 | + |
| 128 | + api |
| 129 | + .WithEnvironment("AzureAd__Instance", mockEntra.GetEndpoint("https")) |
| 130 | + .WithEnvironment("AzureAd__ClientId", clientId) |
| 131 | + .WithEnvironment("AzureAd__TenantId", tenantId) |
| 132 | + // .WithEnvironment("AzureAd__SaveTokens", "true") |
| 133 | + ; |
| 134 | + |
| 135 | + vite.WaitFor(mockEntra); |
| 136 | +} |
| 137 | + |
13 | 138 | builder.Build().Run(); |
0 commit comments