Skip to content

Commit 0e0085a

Browse files
authored
Merge pull request #304 from SciSharp/keycloak-auth
add keycloak auth
2 parents d3f2b5a + 496528c commit 0e0085a

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,22 @@
1919

2020
<ItemGroup>
2121

22+
2223
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
2324
</ItemGroup>
2425

2526
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
2627
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="8.0.2" />
2728
<PackageReference Include="AspNet.Security.OAuth.GitHub" Version="8.0.0" />
29+
<PackageReference Include="AspNet.Security.OAuth.Keycloak" Version="8.0.0" />
2830
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.2" />
2931
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="8.0.2" />
3032
</ItemGroup>
3133

3234
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
3335
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="6.0.27" />
3436
<PackageReference Include="AspNet.Security.OAuth.GitHub" Version="6.0.15" />
37+
<PackageReference Include="AspNet.Security.OAuth.Keycloak" Version="6.0.15" />
3538
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.25" />
3639
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="6.0.26" />
3740
</ItemGroup>

src/Infrastructure/BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,21 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv
101101
});
102102
}
103103

104+
// Keycloak Identiy OAuth
105+
if (!string.IsNullOrWhiteSpace(config["OAuth:Keycloak:ClientId"]) && !string.IsNullOrWhiteSpace(config["OAuth:Keycloak:ClientSecret"]))
106+
{
107+
builder = builder.AddKeycloak(options =>
108+
{
109+
options.BaseAddress = new Uri(config["OAuth:Keycloak:BaseAddress"]);
110+
options.Realm = config["OAuth:Keycloak:Realm"];
111+
options.ClientId = config["OAuth:Keycloak:ClientId"];
112+
options.ClientSecret = config["OAuth:Keycloak:ClientSecret"];
113+
options.AccessType = AspNet.Security.OAuth.Keycloak.KeycloakAuthenticationAccessType.Confidential;
114+
int version = Convert.ToInt32(config["OAuth:Keycloak:Version"]??"22") ;
115+
options.Version = new Version(version,0);
116+
});
117+
}
118+
104119
// Add services to the container.
105120
services.AddControllers()
106121
.AddJsonOptions(options =>

src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ public async Task<ActionResult<Token>> GetToken([FromHeader(Name = "Authorizatio
3636

3737
[AllowAnonymous]
3838
[HttpGet("/sso/{provider}")]
39-
public async Task<IActionResult> Authorize([FromRoute] string provider)
39+
public async Task<IActionResult> Authorize([FromRoute] string provider,string redirectUrl)
4040
{
41-
return Challenge(new AuthenticationProperties { RedirectUri = $"page/user/me" }, provider);
41+
return Challenge(new AuthenticationProperties { RedirectUri = redirectUrl }, provider);
4242
}
4343

4444
[AllowAnonymous]

src/WebStarter/appsettings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
"Google": {
2222
"ClientId": "",
2323
"ClientSecret": ""
24+
},
25+
"Keycloak": {
26+
"BaseAddress": "",
27+
"Realm": "",
28+
"ClientId": "",
29+
"ClientSecret": "",
30+
"Version": 22
2431
}
2532
},
2633

0 commit comments

Comments
 (0)