Skip to content

Commit 799b762

Browse files
feat: the builder now receives an collection, making the selection of the handler based on the grant type more flexible and decoupled.
1 parent a777562 commit 799b762

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
namespace HttpsRichardy.Federation.Application.Handlers.Identity;
22

3-
public sealed class ClientAuthenticationHandler(IRealmCollection realmCollection, IUserCollection userCollection, ITokenCollection tokenCollection, ISecurityTokenService tokenService) :
3+
public sealed class ClientAuthenticationHandler(IEnumerable<IAuthorizationFlowHandler> handlers) :
44
IDispatchHandler<ClientAuthenticationCredentials, Result<ClientAuthenticationResult>>
55
{
66
public async Task<Result<ClientAuthenticationResult>> HandleAsync(
77
ClientAuthenticationCredentials parameters, CancellationToken cancellation = default)
88
{
9-
IAuthorizationFlowHandler handler = parameters.GrantType switch
9+
var grant = parameters.GrantType switch
1010
{
11-
SupportedGrantType.AuthorizationCode => new AuthorizationCodeGrantHandler(realmCollection, userCollection, tokenService, tokenCollection),
12-
SupportedGrantType.ClientCredentials => new ClientCredentialsGrantHandler(realmCollection, tokenService),
11+
SupportedGrantType.ClientCredentials => Grant.ClientCredentials,
12+
SupportedGrantType.AuthorizationCode => Grant.AuthorizationCode,
13+
14+
_ => Grant.Unspecified
1315
};
1416

17+
var handler = handlers.FirstOrDefault(handler => handler.Grant == grant);
18+
if (handler is null)
19+
{
20+
return Result<ClientAuthenticationResult>.Failure(AuthorizationErrors.UnsupportedGrant);
21+
}
22+
1523
return await handler.HandleAsync(parameters, cancellation);
1624
}
1725
}

0 commit comments

Comments
 (0)