Skip to content

Commit 34e7b72

Browse files
Support Keycloak v18+
Support changes to the resource paths in Keycloak 18.0+. Resolves #695.
1 parent 5d8cabb commit 34e7b72

File tree

5 files changed

+70
-8
lines changed

5 files changed

+70
-8
lines changed

docs/keycloak.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ services.AddAuthentication(options => /* Auth configuration */)
1212
options.ClientSecret = "my-client-secret";
1313
options.Domain = "mydomain.local";
1414
options.Realm = "myrealm";
15+
options.Version = new Version(19, 0);
1516
});
1617
```
1718

@@ -25,6 +26,7 @@ services.AddAuthentication(options => /* Auth configuration */)
2526
options.ClientId = "my-client-id";
2627
options.Domain = "mydomain.local";
2728
options.Realm = "myrealm";
29+
options.Version = new Version(19, 0);
2830
});
2931
```
3032

@@ -38,6 +40,7 @@ services.AddAuthentication(options => /* Auth configuration */)
3840
options.ClientId = "my-client-id";
3941
options.ClientSecret = "my-client-secret";
4042
options.Realm = "myrealm";
43+
options.Version = new Version(19, 0);
4144
});
4245
```
4346

@@ -56,3 +59,4 @@ Only one of either `BaseAddress` or `Domain` is required to be set. If both are
5659
| Property Name | Property Type | Description | Default Value |
5760
| :------------ | :--------------------------------- | :--------------------------------------- | :---------------------------------------------- |
5861
| `AccessType` | `KeycloakAuthenticationAccessType` | The Keycloak client's access token type. | `KeycloakAuthenticationAccessType.Confidential` |
62+
| `Version` | `Version?` | The Keycloak server version. | `null` |

src/AspNet.Security.OAuth.Keycloak/KeycloakAuthenticationOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public KeycloakAuthenticationOptions()
5454
/// </summary>
5555
public string? Realm { get; set; }
5656

57+
/// <summary>
58+
/// Gets or sets the version of Keycloak being used.
59+
/// </summary>
60+
public Version? Version { get; set; }
61+
5762
/// <inheritdoc />
5863
public override void Validate()
5964
{

src/AspNet.Security.OAuth.Keycloak/KeycloakPostConfigureOptions.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace AspNet.Security.OAuth.Keycloak;
1212
/// </summary>
1313
public class KeycloakPostConfigureOptions : IPostConfigureOptions<KeycloakAuthenticationOptions>
1414
{
15+
private static readonly Version NoAuthPrefixVersion = new(18, 0);
16+
1517
public void PostConfigure([NotNull] string name, [NotNull] KeycloakAuthenticationOptions options)
1618
{
1719
if ((!string.IsNullOrWhiteSpace(options.Domain) || options.BaseAddress is not null) &&
@@ -36,7 +38,15 @@ private static string CreateUrl(KeycloakAuthenticationOptions options, string re
3638
builder.Scheme = Uri.UriSchemeHttps;
3739
}
3840

39-
builder.Path = new PathString("/auth/realms")
41+
var pathBase = new PathString("/");
42+
43+
if (options.Version is null || options.Version < NoAuthPrefixVersion)
44+
{
45+
pathBase = pathBase.Add("/auth");
46+
}
47+
48+
builder.Path = pathBase
49+
.Add("/realms")
4050
.Add("/" + options.Realm!.Trim('/'))
4151
.Add(resource);
4252

test/AspNet.Security.OAuth.Providers.Tests/Keycloak/KeycloakTests.cs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,39 @@ static void ConfigureServices(IServiceCollection services)
5252
}
5353

5454
[Theory]
55-
[InlineData(ClaimTypes.NameIdentifier, "995c1500-0dca-495e-ba72-2499d370d181")]
56-
[InlineData(ClaimTypes.Email, "[email protected]")]
57-
[InlineData(ClaimTypes.GivenName, "John")]
58-
[InlineData(ClaimTypes.Role, "admin")]
59-
[InlineData(ClaimTypes.Name, "John Smith")]
60-
public async Task Can_Sign_In_Using_Keycloak_Domain(string claimType, string claimValue)
55+
[InlineData(null, ClaimTypes.NameIdentifier, "995c1500-0dca-495e-ba72-2499d370d181")]
56+
[InlineData(null, ClaimTypes.Email, "[email protected]")]
57+
[InlineData(null, ClaimTypes.GivenName, "John")]
58+
[InlineData(null, ClaimTypes.Role, "admin")]
59+
[InlineData(null, ClaimTypes.Name, "John Smith")]
60+
[InlineData("17.0", ClaimTypes.NameIdentifier, "995c1500-0dca-495e-ba72-2499d370d181")]
61+
[InlineData("17.0", ClaimTypes.Email, "[email protected]")]
62+
[InlineData("17.0", ClaimTypes.GivenName, "John")]
63+
[InlineData("17.0", ClaimTypes.Role, "admin")]
64+
[InlineData("17.0", ClaimTypes.Name, "John Smith")]
65+
[InlineData("18.0", ClaimTypes.NameIdentifier, "995c1500-0dca-495e-ba72-2499d370d181")]
66+
[InlineData("18.0", ClaimTypes.Email, "[email protected]")]
67+
[InlineData("18.0", ClaimTypes.GivenName, "John")]
68+
[InlineData("18.0", ClaimTypes.Role, "admin")]
69+
[InlineData("18.0", ClaimTypes.Name, "John Smith")]
70+
[InlineData("19.0", ClaimTypes.NameIdentifier, "995c1500-0dca-495e-ba72-2499d370d181")]
71+
[InlineData("19.0", ClaimTypes.Email, "[email protected]")]
72+
[InlineData("19.0", ClaimTypes.GivenName, "John")]
73+
[InlineData("19.0", ClaimTypes.Role, "admin")]
74+
[InlineData("19.0", ClaimTypes.Name, "John Smith")]
75+
public async Task Can_Sign_In_Using_Keycloak_Domain(string? version, string claimType, string claimValue)
6176
{
6277
// Arrange
63-
static void ConfigureServices(IServiceCollection services)
78+
void ConfigureServices(IServiceCollection services)
6479
{
6580
services.PostConfigureAll<KeycloakAuthenticationOptions>((options) =>
6681
{
6782
options.Domain = "keycloak.local";
83+
84+
if (version is not null)
85+
{
86+
options.Version = Version.Parse(version);
87+
}
6888
});
6989
}
7090

test/AspNet.Security.OAuth.Providers.Tests/Keycloak/bundle.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,29 @@
2424
"email": "[email protected]"
2525
}
2626
},
27+
{
28+
"uri": "https://keycloak.local/realms/myrealm/protocol/openid-connect/token",
29+
"method": "POST",
30+
"contentFormat": "json",
31+
"contentJson": {
32+
"access_token": "79d687a0ea4910c6662b2e38116528fdcd65f0d1",
33+
"expires_in": 3600,
34+
"token_type": "Bearer",
35+
"scope": "openid",
36+
"refresh_token": "c1de730eef1b2072b48799000ec7cde4ea6d2af0"
37+
}
38+
},
39+
{
40+
"uri": "https://keycloak.local/realms/myrealm/protocol/openid-connect/userinfo",
41+
"contentFormat": "json",
42+
"contentJson": {
43+
"sub": "995c1500-0dca-495e-ba72-2499d370d181",
44+
"roles": "admin",
45+
"name": "John Smith",
46+
"given_name": "John",
47+
"email": "[email protected]"
48+
}
49+
},
2750
{
2851
"uri": "http://localhost:8080/auth/realms/myrealm/protocol/openid-connect/token",
2952
"method": "POST",

0 commit comments

Comments
 (0)