| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using Elsa.Identity.Contracts; |
| | | 3 | | using Elsa.Identity.Models; |
| | | 4 | | using FastEndpoints; |
| | | 5 | | using JetBrains.Annotations; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Identity.Endpoints.RefreshToken; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Generates a new token for the current user. |
| | | 11 | | /// </summary> |
| | | 12 | | [PublicAPI] |
| | | 13 | | internal class RefreshToken : EndpointWithoutRequest<LoginResponse> |
| | | 14 | | { |
| | | 15 | | private readonly IUserProvider _userProvider; |
| | | 16 | | private readonly IAccessTokenIssuer _tokenIssuer; |
| | | 17 | | |
| | | 18 | | /// <inheritdoc /> |
| | 1 | 19 | | public RefreshToken(IUserProvider userProvider, IAccessTokenIssuer tokenIssuer) |
| | | 20 | | { |
| | 1 | 21 | | _userProvider = userProvider; |
| | 1 | 22 | | _tokenIssuer = tokenIssuer; |
| | 1 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public override void Configure() |
| | | 27 | | { |
| | 1 | 28 | | Post("/identity/refresh-token"); |
| | 1 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | | 32 | | public override async Task<LoginResponse> ExecuteAsync(CancellationToken cancellationToken) |
| | | 33 | | { |
| | 0 | 34 | | var user = await _userProvider.FindByNameAsync(User.Identity!.Name!, cancellationToken); |
| | | 35 | | |
| | 0 | 36 | | if (user == null) |
| | 0 | 37 | | return new LoginResponse(false, null, null); |
| | | 38 | | |
| | 0 | 39 | | var tokens = await _tokenIssuer.IssueTokensAsync(user, cancellationToken); |
| | | 40 | | |
| | 0 | 41 | | return new LoginResponse(true, tokens.AccessToken, tokens.RefreshToken); |
| | 0 | 42 | | } |
| | | 43 | | } |