< Summary

Information
Class: Elsa.Identity.Options.IdentityTokenOptions
Assembly: Elsa.Identity
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Options/IdentityTokenOptions.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 18
Coverable lines: 18
Total lines: 72
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_SigningKey()100%210%
get_Issuer()100%210%
get_Audience()100%210%
get_AccessTokenLifetime()100%210%
get_RefreshTokenLifetime()100%210%
get_TenantIdClaimsType()100%210%
CreateSecurityKey()100%210%
ConfigureJwtBearerOptions(...)100%210%
ValidateLifetime(...)0%2040%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Options/IdentityTokenOptions.cs

#LineLine coverage
 1using System.IdentityModel.Tokens.Jwt;
 2using System.Text;
 3using Elsa.Identity.Constants;
 4using Microsoft.AspNetCore.Authentication.JwtBearer;
 5using Microsoft.IdentityModel.Tokens;
 6
 7namespace Elsa.Identity.Options;
 8
 9/// <summary>
 10/// Represents options about token validation and generation.
 11/// </summary>
 12public class IdentityTokenOptions
 13{
 14    /// <summary>
 15    /// The key to use when signing tokens
 16    /// </summary>
 017    public string SigningKey { get; set; } = null!;
 18
 19    /// <summary>
 20    /// The issuer to use when creating and validating tokens
 21    /// </summary>
 022    public string Issuer { get; set; } = "http://elsa.api";
 23
 24    /// <summary>
 25    /// The audience to use when creating and validating tokens
 26    /// </summary>
 027    public string Audience { get; set; } = "http://elsa.api";
 28
 29    /// <summary>
 30    /// The lifetime of access tokens
 31    /// </summary>
 032    public TimeSpan AccessTokenLifetime { get; set; } = TimeSpan.FromHours(1);
 33
 34    /// <summary>
 35    /// The lifetime of refresh tokens
 36    /// </summary>
 037    public TimeSpan RefreshTokenLifetime { get; set; } = TimeSpan.FromHours(2);
 38
 39    /// <summary>
 40    /// Gets or sets the claim type that hold the tenant ID in the user's claims.
 41    /// If not set, <see cref="CustomClaimTypes.TenantId" /> will be used
 42    /// </summary>
 043    public string TenantIdClaimsType { get; set; } = CustomClaimTypes.TenantId;
 44
 45    /// <summary>
 46    /// Creates a new <see cref="SecurityKey"/> from the <see cref="SigningKey"/>.
 47    /// </summary>
 48    /// <returns></returns>
 049    public SecurityKey CreateSecurityKey() => new SymmetricSecurityKey(Encoding.ASCII.GetBytes(SigningKey));
 50
 51    /// <summary>
 52    /// Configures the <see cref="JwtBearerOptions"/> with the values from this instance.
 53    /// </summary>
 54    /// <param name="options">The options to configure.</param>
 55    public void ConfigureJwtBearerOptions(JwtBearerOptions options)
 56    {
 057        options.TokenValidationParameters = new TokenValidationParameters
 058        {
 059            IssuerSigningKey = CreateSecurityKey(),
 060            ValidAudience = Audience,
 061            ValidIssuer = Issuer,
 062            ValidateLifetime = true,
 063            LifetimeValidator = ValidateLifetime,
 064            NameClaimType = JwtRegisteredClaimNames.Name
 065        };
 066    }
 67
 68    private static bool ValidateLifetime(DateTime? notBefore, DateTime? expires, SecurityToken securityToken, TokenValid
 69    {
 070        return expires != null && expires > DateTime.UtcNow;
 71    }
 72}