-
Notifications
You must be signed in to change notification settings - Fork 854
Add Keycloak component #4289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Keycloak component #4289
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
9e8b32b
Add Keycloak component
julioct 033c190
Use ReferenceExpression and default password generation
julioct 743cf73
Use tag 24.0
julioct 24cf23d
Add Keycloak component
julioct 0d34dbc
Remove Keycloak icon
julioct c2be071
Switch to service discovery and populate full endpoint on host.
julioct 0a03849
Add resource name argument and use service discovery for Keycloak bas…
julioct 9ff4a64
Disable package validation
julioct 742c4a7
Don't set a base address for the HttpClient
julioct 258df28
Remove blank lines in README
julioct 5717d72
Moved realm from AppHost to component
julioct 9f865fd
Extend AuthenticationBuilder instead of IHostApplicationBuilder
julioct 2c3b8f2
Improve docs
julioct ed2f91e
Add tests
julioct 75b3b49
Use data volume in Playground
julioct 79ca77d
Address feedback
julioct 87ae126
Added WithRealmImport test
julioct df0e2ca
Address feedback
julioct 89e4861
Address feedback
julioct 4d48146
Use a fixed port
julioct 57e8c0a
Rename component to Aspire.Keycloak.Authentication
julioct c114b37
Added missing tests
julioct 94b7f8f
Add SuppressFinalPackageVersion for the initial version of the Keyclo…
eerhardt 9f1b503
Minor PR feedback
eerhardt d8b5a0f
Apply minor feedback
julioct File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
playground/keycloak/Keycloak.ApiService/Keycloak.ApiService.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\..\src\Components\Aspire.Keycloak.Authentication\Aspire.Keycloak.Authentication.csproj" /> | ||
| <ProjectReference Include="..\..\Playground.ServiceDefaults\Playground.ServiceDefaults.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| // Add service defaults & Aspire components. | ||
| builder.AddServiceDefaults(); | ||
|
|
||
| // Add services to the container. | ||
| builder.Services.AddProblemDetails(); | ||
|
|
||
| builder.Services.AddAuthentication() | ||
| .AddKeycloakJwtBearer("keycloak", realm: "WeatherShop", options => | ||
| { | ||
| options.RequireHttpsMetadata = false; | ||
| options.Audience = "weather.api"; | ||
| }); | ||
|
|
||
| builder.Services.AddAuthorizationBuilder(); | ||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| // Configure the HTTP request pipeline. | ||
| app.UseExceptionHandler(); | ||
|
|
||
| var summaries = new[] | ||
| { | ||
| "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | ||
| }; | ||
|
|
||
| app.MapGet("/weatherforecast", () => | ||
| { | ||
| var forecast = Enumerable.Range(1, 5).Select(index => | ||
| new WeatherForecast | ||
| ( | ||
| DateOnly.FromDateTime(DateTime.Now.AddDays(index)), | ||
| Random.Shared.Next(-20, 55), | ||
| summaries[Random.Shared.Next(summaries.Length)] | ||
| )) | ||
| .ToArray(); | ||
| return forecast; | ||
| }) | ||
| .RequireAuthorization(); | ||
|
|
||
| app.MapDefaultEndpoints(); | ||
|
|
||
| app.Run(); | ||
|
|
||
| sealed record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) | ||
| { | ||
| public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | ||
| } |
25 changes: 25 additions & 0 deletions
25
playground/keycloak/Keycloak.ApiService/Properties/launchSettings.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { | ||
| "$schema": "https://json.schemastore.org/launchsettings.json", | ||
| "profiles": { | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "launchUrl": "weatherforecast", | ||
| "applicationUrl": "http://localhost:5502", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "https": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "launchUrl": "weatherforecast", | ||
| "applicationUrl": "https://localhost:7492;http://localhost:5502", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| } | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
playground/keycloak/Keycloak.ApiService/appsettings.Development.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <Project> | ||
|
|
||
| <Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" /> | ||
|
|
||
| <!-- NOTE: This line is only required because we are using P2P references, not NuGet. It will not exist in real apps. --> | ||
| <Import Project="../../../src/Aspire.Hosting.AppHost/build/Aspire.Hosting.AppHost.props" /> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <Project> | ||
|
|
||
| <Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.targets', '$(MSBuildThisFileDirectory)../'))" /> | ||
|
|
||
| <!-- NOTE: These lines are only required because we are using P2P references, not NuGet. They will not exist in real apps. --> | ||
| <Import Project="..\..\..\src\Aspire.Hosting.AppHost\build\Aspire.Hosting.AppHost.targets" /> | ||
| <Import Project="..\..\..\src\Aspire.Hosting.Sdk\SDK\Sdk.targets" /> | ||
|
|
||
| </Project> |
19 changes: 19 additions & 0 deletions
19
playground/keycloak/Keycloak.AppHost/Keycloak.AppHost.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <IsAspireHost>true</IsAspireHost> | ||
| <UserSecretsId>230093c1-93fe-469d-bcdb-9c8a3c1f699c</UserSecretsId> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\..\src\Aspire.Hosting.AppHost\Aspire.Hosting.AppHost.csproj" IsAspireProjectResource="False" /> | ||
| <ProjectReference Include="..\..\..\src\Aspire.Hosting.Keycloak\Aspire.Hosting.Keycloak.csproj" IsAspireProjectResource="False" /> | ||
| <ProjectReference Include="..\Keycloak.ApiService\Keycloak.ApiService.csproj" /> | ||
| <ProjectReference Include="..\Keycloak.Web\Keycloak.Web.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| var builder = DistributedApplication.CreateBuilder(args); | ||
|
|
||
| var keycloak = builder.AddKeycloak("keycloak", 8080) | ||
| .WithDataVolume() | ||
| .WithRealmImport("../realms"); | ||
julioct marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| var apiService = builder.AddProject<Projects.Keycloak_ApiService>("apiservice") | ||
| .WithReference(keycloak); | ||
|
|
||
| builder.AddProject<Projects.Keycloak_Web>("webfrontend") | ||
| .WithExternalHttpEndpoints() | ||
| .WithReference(keycloak) | ||
| .WithReference(apiService); | ||
|
|
||
| builder.Build().Run(); | ||
julioct marked this conversation as resolved.
Show resolved
Hide resolved
|
||
29 changes: 29 additions & 0 deletions
29
playground/keycloak/Keycloak.AppHost/Properties/launchSettings.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| { | ||
| "$schema": "https://json.schemastore.org/launchsettings.json", | ||
| "profiles": { | ||
| "https": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "https://localhost:17241;http://localhost:15079", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development", | ||
| "DOTNET_ENVIRONMENT": "Development", | ||
| "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21125", | ||
| "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22112" | ||
| } | ||
| }, | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "http://localhost:15079", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development", | ||
| "DOTNET_ENVIRONMENT": "Development", | ||
| "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19191", | ||
| "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20230" | ||
| } | ||
| } | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
playground/keycloak/Keycloak.AppHost/appsettings.Development.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning", | ||
| "Aspire.Hosting.Dcp": "Warning" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using System.Net.Http.Headers; | ||
| using Microsoft.AspNetCore.Authentication; | ||
|
|
||
| namespace Keycloak.Web; | ||
|
|
||
| public class AuthorizationHandler(IHttpContextAccessor httpContextAccessor) : DelegatingHandler | ||
| { | ||
| protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
| { | ||
| var httpContext = httpContextAccessor.HttpContext ?? | ||
| throw new InvalidOperationException("No HttpContext available from the IHttpContextAccessor!"); | ||
|
|
||
| var accessToken = await httpContext.GetTokenAsync("access_token"); | ||
|
|
||
| if (!string.IsNullOrEmpty(accessToken)) | ||
| { | ||
| request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); | ||
| } | ||
|
|
||
| return await base.SendAsync(request, cancellationToken); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
|
|
||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <base href="/" /> | ||
| <link rel="stylesheet" href="bootstrap/bootstrap.min.css" /> | ||
| <link rel="stylesheet" href="app.css" /> | ||
| <link rel="stylesheet" href="Keycloak.Web.styles.css" /> | ||
| <link rel="icon" type="image/png" href="favicon.png" /> | ||
| <HeadOutlet /> | ||
| </head> | ||
|
|
||
| <body> | ||
| <Routes /> | ||
| <script src="_framework/blazor.web.js"></script> | ||
| </body> | ||
|
|
||
| </html> |
21 changes: 21 additions & 0 deletions
21
playground/keycloak/Keycloak.Web/Components/Layout/LoginDisplay.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| @using Microsoft.AspNetCore.Components.Authorization | ||
| @using System.Security.Claims; | ||
| @using System.Security.Principal; | ||
| @using System.Security.Cryptography | ||
| @using System.Text | ||
| @using System.IdentityModel.Tokens.Jwt | ||
|
|
||
| <AuthorizeView> | ||
| <Authorized> | ||
| <div> | ||
| <span>Welcome @context.User.Identity?.Name!</span> | ||
| <form method="post" action="authentication/logout" style="display:inline;"> | ||
| <AntiforgeryToken /> | ||
| <button type="submit" class="btn btn-link">Logout</button> | ||
| </form> | ||
| </div> | ||
| </Authorized> | ||
| <NotAuthorized> | ||
| <a href="authentication/login" class="btn btn-primary">Login</a> | ||
| </NotAuthorized> | ||
| </AuthorizeView> |
23 changes: 23 additions & 0 deletions
23
playground/keycloak/Keycloak.Web/Components/Layout/MainLayout.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| @inherits LayoutComponentBase | ||
|
|
||
| <div class="page"> | ||
| <div class="sidebar"> | ||
| <NavMenu /> | ||
| </div> | ||
|
|
||
| <main> | ||
| <div class="top-row px-4"> | ||
| <LoginDisplay /> | ||
| </div> | ||
|
|
||
| <article class="content px-4"> | ||
| @Body | ||
| </article> | ||
| </main> | ||
| </div> | ||
|
|
||
| <div id="blazor-error-ui"> | ||
| An unhandled error has occurred. | ||
| <a href="" class="reload">Reload</a> | ||
| <a class="dismiss">🗙</a> | ||
| </div> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.