Merge pull request #304 from SciSharp/keycloak-auth

add keycloak auth
This commit is contained in:
C. Oceania 2024-02-17 08:21:27 -06:00 committed by GitHub
commit 0e0085a3d8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 27 additions and 2 deletions

View file

@ -19,12 +19,14 @@
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="8.0.2" />
<PackageReference Include="AspNet.Security.OAuth.GitHub" Version="8.0.0" />
<PackageReference Include="AspNet.Security.OAuth.Keycloak" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="8.0.2" />
</ItemGroup>
@ -32,6 +34,7 @@
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="6.0.27" />
<PackageReference Include="AspNet.Security.OAuth.GitHub" Version="6.0.15" />
<PackageReference Include="AspNet.Security.OAuth.Keycloak" Version="6.0.15" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.25" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="6.0.26" />
</ItemGroup>

View file

@ -101,6 +101,21 @@ public static class BotSharpOpenApiExtensions
});
}
// Keycloak Identiy OAuth
if (!string.IsNullOrWhiteSpace(config["OAuth:Keycloak:ClientId"]) && !string.IsNullOrWhiteSpace(config["OAuth:Keycloak:ClientSecret"]))
{
builder = builder.AddKeycloak(options =>
{
options.BaseAddress = new Uri(config["OAuth:Keycloak:BaseAddress"]);
options.Realm = config["OAuth:Keycloak:Realm"];
options.ClientId = config["OAuth:Keycloak:ClientId"];
options.ClientSecret = config["OAuth:Keycloak:ClientSecret"];
options.AccessType = AspNet.Security.OAuth.Keycloak.KeycloakAuthenticationAccessType.Confidential;
int version = Convert.ToInt32(config["OAuth:Keycloak:Version"]??"22") ;
options.Version = new Version(version,0);
});
}
// Add services to the container.
services.AddControllers()
.AddJsonOptions(options =>

View file

@ -36,9 +36,9 @@ public class UserController : ControllerBase
[AllowAnonymous]
[HttpGet("/sso/{provider}")]
public async Task<IActionResult> Authorize([FromRoute] string provider)
public async Task<IActionResult> Authorize([FromRoute] string provider,string redirectUrl)
{
return Challenge(new AuthenticationProperties { RedirectUri = $"page/user/me" }, provider);
return Challenge(new AuthenticationProperties { RedirectUri = redirectUrl }, provider);
}
[AllowAnonymous]

View file

@ -21,6 +21,13 @@
"Google": {
"ClientId": "",
"ClientSecret": ""
},
"Keycloak": {
"BaseAddress": "",
"Realm": "",
"ClientId": "",
"ClientSecret": "",
"Version": 22
}
},