-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientAuthenticationHandler.cs
More file actions
41 lines (34 loc) · 1.46 KB
/
Copy pathClientAuthenticationHandler.cs
File metadata and controls
41 lines (34 loc) · 1.46 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
41
namespace Vinder.Federation.Application.Handlers.Identity;
public sealed class ClientAuthenticationHandler(
ITenantCollection tenantCollection,
ISecurityTokenService tokenService
) : IMessageHandler<ClientAuthenticationCredentials, Result<ClientAuthenticationResult>>
{
public async Task<Result<ClientAuthenticationResult>> HandleAsync(
ClientAuthenticationCredentials parameters, CancellationToken cancellation = default)
{
var filters = TenantFilters.WithSpecifications()
.WithClientId(parameters.ClientId)
.Build();
var tenants = await tenantCollection.GetTenantsAsync(filters, cancellation: cancellation);
var tenant = tenants.FirstOrDefault();
if (tenant is null)
{
return Result<ClientAuthenticationResult>.Failure(AuthenticationErrors.ClientNotFound);
}
if (parameters.ClientSecret != tenant.SecretHash)
{
return Result<ClientAuthenticationResult>.Failure(AuthenticationErrors.InvalidClientCredentials);
}
var tokenResult = await tokenService.GenerateAccessTokenAsync(tenant, cancellation);
if (tokenResult.IsFailure)
{
return Result<ClientAuthenticationResult>.Failure(tokenResult.Error);
}
var response = new ClientAuthenticationResult
{
AccessToken = tokenResult.Data!.Value
};
return Result<ClientAuthenticationResult>.Success(response);
}
}