Skip to content

Commit 9eea475

Browse files
Farid AhamatFaridAhamat
authored andcommitted
Fix Keycloak exception for Secret Key if using public access type
Co-authored-by: FaridAhamat <[email protected]>
1 parent 324262f commit 9eea475

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
2+
// See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
3+
// for more information concerning the license and the contributors participating to this project.
4+
5+
namespace AspNet.Security.OAuth.Keycloak
6+
{
7+
public enum KeycloakAuthenticationAccessType
8+
{
9+
Confidential,
10+
Public,
11+
BearerOnly,
12+
}
13+
}

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public KeycloakAuthenticationOptions()
3737
ClaimActions.MapJsonKey(ClaimTypes.Role, "roles");
3838
}
3939

40+
/// <summary>
41+
/// Gets or sets the value for Keycloak client's access type.
42+
/// </summary>
43+
public KeycloakAuthenticationAccessType AccessType { get; set; }
44+
4045
/// <summary>
4146
/// Gets or sets the base address of the Keycloak server.
4247
/// </summary>
@@ -51,5 +56,42 @@ public KeycloakAuthenticationOptions()
5156
/// Gets or sets the Keycloak realm to use for authentication.
5257
/// </summary>
5358
public string? Realm { get; set; }
59+
60+
/// <inheritdoc />
61+
public override void Validate()
62+
{
63+
try
64+
{
65+
// HACK
66+
// We want all of the base validation except for ClientSecret,
67+
// so rather than re-implement it all, catch the exception thrown
68+
// for that being null and only throw if we aren't using public access type.
69+
// This does mean that three checks have to be re-implemented
70+
// because the won't be validated if the ClientSecret validation fails.
71+
base.Validate();
72+
}
73+
catch (ArgumentException ex) when (ex.ParamName == nameof(ClientSecret))
74+
{
75+
if (AccessType != KeycloakAuthenticationAccessType.Public)
76+
{
77+
throw;
78+
}
79+
}
80+
81+
if (string.IsNullOrEmpty(AuthorizationEndpoint))
82+
{
83+
throw new ArgumentException($"The '{nameof(AuthorizationEndpoint)}' option must be provided.", nameof(AuthorizationEndpoint));
84+
}
85+
86+
if (string.IsNullOrEmpty(TokenEndpoint))
87+
{
88+
throw new ArgumentException($"The '{nameof(TokenEndpoint)}' option must be provided.", nameof(TokenEndpoint));
89+
}
90+
91+
if (!CallbackPath.HasValue)
92+
{
93+
throw new ArgumentException($"The '{nameof(CallbackPath)}' option must be provided.", nameof(CallbackPath));
94+
}
95+
}
5496
}
5597
}

0 commit comments

Comments
 (0)