< Summary

Information
Class: Elsa.Identity.Services.DefaultAccessTokenIssuer
Assembly: Elsa.Identity
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Services/DefaultAccessTokenIssuer.cs
Line coverage
2%
Covered lines: 1
Uncovered lines: 35
Coverable lines: 36
Total lines: 67
Line coverage: 2.7%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
IssueTokensAsync()0%7280%
ConfigureTokenOptions()100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Services/DefaultAccessTokenIssuer.cs

#LineLine coverage
 1using System.Security.Claims;
 2using Elsa.Common;
 3using Elsa.Extensions;
 4using Elsa.Identity.Contracts;
 5using Elsa.Identity.Entities;
 6using Elsa.Identity.Models;
 7using Elsa.Identity.Options;
 8using FastEndpoints.Security;
 9using Microsoft.Extensions.Options;
 10using Microsoft.IdentityModel.JsonWebTokens;
 11
 12namespace Elsa.Identity.Services;
 13
 14/// <summary>
 15/// Default implementation of <see cref="IAccessTokenIssuer"/>.
 16/// </summary>
 117public class DefaultAccessTokenIssuer(IRoleProvider roleProvider, ISystemClock systemClock, IOptions<IdentityTokenOption
 18{
 19    /// <inheritdoc />
 20    public async ValueTask<IssuedTokens> IssueTokensAsync(User user, CancellationToken cancellationToken = default)
 21    {
 022        var roles = (await roleProvider.FindByIdsAsync(user.Roles, cancellationToken)).ToList();
 023        var permissions = roles.SelectMany(x => x.Permissions).ToList();
 024        var roleNames = roles.Select(x => x.Name).ToList();
 025        var tokenOptions = identityTokenOptions.Value;
 026        var signingKey = tokenOptions.SigningKey;
 027        var issuer = tokenOptions.Issuer;
 028        var audience = tokenOptions.Audience;
 029        var accessTokenLifetime = tokenOptions.AccessTokenLifetime;
 030        var refreshTokenLifetime = tokenOptions.RefreshTokenLifetime;
 31
 032        if (string.IsNullOrWhiteSpace(signingKey)) throw new Exception("No signing key configured");
 033        if (string.IsNullOrWhiteSpace(issuer)) throw new Exception("No issuer configured");
 034        if (string.IsNullOrWhiteSpace(audience)) throw new Exception("No audience configured");
 35
 036        var nameClaim = new Claim(JwtRegisteredClaimNames.Name, user.Name);
 037        var claims = new List<Claim>
 038        {
 039            nameClaim
 040        };
 41
 042        if (!string.IsNullOrWhiteSpace(user.TenantId))
 43        {
 044            var tenantIdClaim = new Claim(tokenOptions.TenantIdClaimsType, user.TenantId);
 045            claims.Add(tenantIdClaim);
 46        }
 47
 048        var now = systemClock.UtcNow;
 049        var accessTokenExpiresAt = now.Add(accessTokenLifetime);
 050        var refreshTokenExpiresAt = now.Add(refreshTokenLifetime);
 051        var accessToken = JwtBearer.CreateToken(options => ConfigureTokenOptions(options, accessTokenExpiresAt.UtcDateTi
 052        var refreshToken = JwtBearer.CreateToken(options => ConfigureTokenOptions(options, refreshTokenExpiresAt.UtcDate
 53
 054        return new IssuedTokens(accessToken, refreshToken);
 55
 56        void ConfigureTokenOptions(JwtCreationOptions options, DateTime expireAt)
 57        {
 058            options.SigningKey = signingKey;
 059            options.ExpireAt = expireAt;
 060            options.Issuer = issuer;
 061            options.Audience = audience;
 062            options.User.Claims.AddRange(claims);
 063            options.User.Permissions.AddRange(permissions);
 064            options.User.Roles.AddRange(roleNames);
 065        }
 066    }
 67}