| | | 1 | | using System.Security.Claims; |
| | | 2 | | using Elsa.Common; |
| | | 3 | | using Elsa.Identity.Constants; |
| | | 4 | | using Elsa.Identity.Contracts; |
| | | 5 | | using Elsa.Identity.Models; |
| | | 6 | | using Elsa.Identity.Options; |
| | | 7 | | using Microsoft.Extensions.Options; |
| | | 8 | | using Microsoft.IdentityModel.JsonWebTokens; |
| | | 9 | | using Microsoft.IdentityModel.Tokens; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Identity.Services; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Creates signed Elsa JWTs from trusted issuance data. |
| | | 15 | | /// </summary> |
| | 5 | 16 | | public sealed class DefaultElsaTokenService(ISystemClock systemClock, IOptions<IdentityTokenOptions> identityTokenOption |
| | | 17 | | { |
| | | 18 | | /// <inheritdoc /> |
| | | 19 | | public ValueTask<IssuedAccessToken> IssueAccessTokenAsync(TokenIssuanceContext context, CancellationToken cancellati |
| | | 20 | | { |
| | 2 | 21 | | return IssueTokenAsync(context, TokenUse.Access, identityTokenOptions.Value.AccessTokenLifetime, cancellationTok |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public ValueTask<IssuedAccessToken> IssueRefreshTokenAsync(TokenIssuanceContext context, CancellationToken cancellat |
| | | 26 | | { |
| | 1 | 27 | | return IssueTokenAsync(context, TokenUse.Refresh, identityTokenOptions.Value.RefreshTokenLifetime, cancellationT |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | private ValueTask<IssuedAccessToken> IssueTokenAsync( |
| | | 31 | | TokenIssuanceContext context, |
| | | 32 | | string tokenUse, |
| | | 33 | | TimeSpan lifetime, |
| | | 34 | | CancellationToken cancellationToken) |
| | | 35 | | { |
| | 3 | 36 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 3 | 37 | | var tokenOptions = identityTokenOptions.Value; |
| | | 38 | | |
| | 3 | 39 | | if (string.IsNullOrWhiteSpace(tokenOptions.SigningKey)) |
| | 0 | 40 | | throw new InvalidOperationException("No signing key configured"); |
| | 3 | 41 | | if (string.IsNullOrWhiteSpace(tokenOptions.Issuer)) |
| | 0 | 42 | | throw new InvalidOperationException("No issuer configured"); |
| | 3 | 43 | | if (string.IsNullOrWhiteSpace(tokenOptions.Audience)) |
| | 0 | 44 | | throw new InvalidOperationException("No audience configured"); |
| | | 45 | | |
| | 3 | 46 | | var claims = new List<Claim> |
| | 3 | 47 | | { |
| | 3 | 48 | | new(JwtRegisteredClaimNames.Sub, context.User.Id), |
| | 3 | 49 | | new(JwtRegisteredClaimNames.Name, context.User.Name) |
| | 3 | 50 | | }; |
| | 3 | 51 | | claims.AddRange(context.AdditionalClaims); |
| | | 52 | | |
| | 3 | 53 | | if (!string.IsNullOrWhiteSpace(context.User.TenantId)) |
| | 1 | 54 | | claims.Add(new Claim(tokenOptions.TenantIdClaimsType, context.User.TenantId)); |
| | | 55 | | |
| | 3 | 56 | | if (!string.IsNullOrWhiteSpace(context.ExternalAuthenticationSessionId)) |
| | 1 | 57 | | claims.Add(new Claim(CustomClaimTypes.ExternalAuthenticationSessionId, context.ExternalAuthenticationSession |
| | | 58 | | |
| | 3 | 59 | | var expiresAt = systemClock.UtcNow.Add(lifetime); |
| | 4 | 60 | | claims.AddRange(context.Roles.Select(x => new Claim(ClaimTypes.Role, x))); |
| | 4 | 61 | | claims.AddRange(context.Permissions.Select(x => new Claim("permissions", x))); |
| | 3 | 62 | | claims.Add(new Claim(TokenUse.ClaimType, tokenUse)); |
| | | 63 | | |
| | 3 | 64 | | var descriptor = new SecurityTokenDescriptor |
| | 3 | 65 | | { |
| | 3 | 66 | | Subject = new ClaimsIdentity(claims), |
| | 3 | 67 | | Expires = expiresAt.UtcDateTime, |
| | 3 | 68 | | Issuer = tokenOptions.Issuer, |
| | 3 | 69 | | Audience = tokenOptions.Audience, |
| | 3 | 70 | | SigningCredentials = new SigningCredentials(tokenOptions.CreateSecurityKey(), SecurityAlgorithms.HmacSha256S |
| | 3 | 71 | | }; |
| | 3 | 72 | | var token = new JsonWebTokenHandler().CreateToken(descriptor); |
| | | 73 | | |
| | 3 | 74 | | return ValueTask.FromResult(new IssuedAccessToken(token, expiresAt)); |
| | | 75 | | } |
| | | 76 | | } |