| | | 1 | | using System.Security.Claims; |
| | | 2 | | using Elsa.Common; |
| | | 3 | | using Elsa.Extensions; |
| | | 4 | | using Elsa.Identity.Contracts; |
| | | 5 | | using Elsa.Identity.Entities; |
| | | 6 | | using Elsa.Identity.Models; |
| | | 7 | | using Elsa.Identity.Options; |
| | | 8 | | using FastEndpoints.Security; |
| | | 9 | | using Microsoft.Extensions.Options; |
| | | 10 | | using Microsoft.IdentityModel.JsonWebTokens; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Identity.Services; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Default implementation of <see cref="IAccessTokenIssuer"/>. |
| | | 16 | | /// </summary> |
| | 1 | 17 | | public class DefaultAccessTokenIssuer(IRoleProvider roleProvider, ISystemClock systemClock, IOptions<IdentityTokenOption |
| | | 18 | | { |
| | | 19 | | /// <inheritdoc /> |
| | | 20 | | public async ValueTask<IssuedTokens> IssueTokensAsync(User user, CancellationToken cancellationToken = default) |
| | | 21 | | { |
| | 0 | 22 | | var roles = (await roleProvider.FindByIdsAsync(user.Roles, cancellationToken)).ToList(); |
| | 0 | 23 | | var permissions = roles.SelectMany(x => x.Permissions).ToList(); |
| | 0 | 24 | | var roleNames = roles.Select(x => x.Name).ToList(); |
| | 0 | 25 | | var tokenOptions = identityTokenOptions.Value; |
| | 0 | 26 | | var signingKey = tokenOptions.SigningKey; |
| | 0 | 27 | | var issuer = tokenOptions.Issuer; |
| | 0 | 28 | | var audience = tokenOptions.Audience; |
| | 0 | 29 | | var accessTokenLifetime = tokenOptions.AccessTokenLifetime; |
| | 0 | 30 | | var refreshTokenLifetime = tokenOptions.RefreshTokenLifetime; |
| | | 31 | | |
| | 0 | 32 | | if (string.IsNullOrWhiteSpace(signingKey)) throw new Exception("No signing key configured"); |
| | 0 | 33 | | if (string.IsNullOrWhiteSpace(issuer)) throw new Exception("No issuer configured"); |
| | 0 | 34 | | if (string.IsNullOrWhiteSpace(audience)) throw new Exception("No audience configured"); |
| | | 35 | | |
| | 0 | 36 | | var nameClaim = new Claim(JwtRegisteredClaimNames.Name, user.Name); |
| | 0 | 37 | | var claims = new List<Claim> |
| | 0 | 38 | | { |
| | 0 | 39 | | nameClaim |
| | 0 | 40 | | }; |
| | | 41 | | |
| | 0 | 42 | | if (!string.IsNullOrWhiteSpace(user.TenantId)) |
| | | 43 | | { |
| | 0 | 44 | | var tenantIdClaim = new Claim(tokenOptions.TenantIdClaimsType, user.TenantId); |
| | 0 | 45 | | claims.Add(tenantIdClaim); |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | var now = systemClock.UtcNow; |
| | 0 | 49 | | var accessTokenExpiresAt = now.Add(accessTokenLifetime); |
| | 0 | 50 | | var refreshTokenExpiresAt = now.Add(refreshTokenLifetime); |
| | 0 | 51 | | var accessToken = JwtBearer.CreateToken(options => ConfigureTokenOptions(options, accessTokenExpiresAt.UtcDateTi |
| | 0 | 52 | | var refreshToken = JwtBearer.CreateToken(options => ConfigureTokenOptions(options, refreshTokenExpiresAt.UtcDate |
| | | 53 | | |
| | 0 | 54 | | return new IssuedTokens(accessToken, refreshToken); |
| | | 55 | | |
| | | 56 | | void ConfigureTokenOptions(JwtCreationOptions options, DateTime expireAt) |
| | | 57 | | { |
| | 0 | 58 | | options.SigningKey = signingKey; |
| | 0 | 59 | | options.ExpireAt = expireAt; |
| | 0 | 60 | | options.Issuer = issuer; |
| | 0 | 61 | | options.Audience = audience; |
| | 0 | 62 | | options.User.Claims.AddRange(claims); |
| | 0 | 63 | | options.User.Permissions.AddRange(permissions); |
| | 0 | 64 | | options.User.Roles.AddRange(roleNames); |
| | 0 | 65 | | } |
| | 0 | 66 | | } |
| | | 67 | | } |