-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientCredentialsGrantHandler.cs
More file actions
40 lines (32 loc) · 1.45 KB
/
Copy pathClientCredentialsGrantHandler.cs
File metadata and controls
40 lines (32 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
namespace HttpsRichardy.Federation.Application.Handlers.Authorization;
public sealed class ClientCredentialsGrantHandler(IClientCollection clientCollection, ISecurityTokenService tokenService) :
IAuthorizationFlowHandler
{
public Grant Grant => Grant.ClientCredentials;
public async Task<Result<ClientAuthenticationResult>> HandleAsync(ClientAuthenticationCredentials parameters, CancellationToken cancellation = default)
{
var filters = ClientFilters.WithSpecifications()
.WithClientId(parameters.ClientId)
.Build();
var clients = await clientCollection.GetClientsAsync(filters, cancellation: cancellation);
var client = clients.FirstOrDefault();
if (client is null)
{
return Result<ClientAuthenticationResult>.Failure(AuthenticationErrors.ClientNotFound);
}
if (parameters.ClientSecret != client.Secret)
{
return Result<ClientAuthenticationResult>.Failure(AuthenticationErrors.InvalidClientCredentials);
}
var tokenResult = await tokenService.GenerateAccessTokenAsync(client, cancellation);
if (tokenResult.IsFailure)
{
return Result<ClientAuthenticationResult>.Failure(tokenResult.Error);
}
var response = new ClientAuthenticationResult
{
AccessToken = tokenResult.Data!.Value
};
return Result<ClientAuthenticationResult>.Success(response);
}
}