| | | 1 | | using System.IdentityModel.Tokens.Jwt; |
| | | 2 | | using System.Text; |
| | | 3 | | using Elsa.Identity.Constants; |
| | | 4 | | using Microsoft.AspNetCore.Authentication.JwtBearer; |
| | | 5 | | using Microsoft.IdentityModel.Tokens; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Identity.Options; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Represents options about token validation and generation. |
| | | 11 | | /// </summary> |
| | | 12 | | public class IdentityTokenOptions |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// The key to use when signing tokens |
| | | 16 | | /// </summary> |
| | 0 | 17 | | public string SigningKey { get; set; } = null!; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// The issuer to use when creating and validating tokens |
| | | 21 | | /// </summary> |
| | 0 | 22 | | public string Issuer { get; set; } = "http://elsa.api"; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// The audience to use when creating and validating tokens |
| | | 26 | | /// </summary> |
| | 0 | 27 | | public string Audience { get; set; } = "http://elsa.api"; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// The lifetime of access tokens |
| | | 31 | | /// </summary> |
| | 0 | 32 | | public TimeSpan AccessTokenLifetime { get; set; } = TimeSpan.FromHours(1); |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// The lifetime of refresh tokens |
| | | 36 | | /// </summary> |
| | 0 | 37 | | 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> |
| | 0 | 43 | | 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> |
| | 0 | 49 | | 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 | | { |
| | 0 | 57 | | options.TokenValidationParameters = new TokenValidationParameters |
| | 0 | 58 | | { |
| | 0 | 59 | | IssuerSigningKey = CreateSecurityKey(), |
| | 0 | 60 | | ValidAudience = Audience, |
| | 0 | 61 | | ValidIssuer = Issuer, |
| | 0 | 62 | | ValidateLifetime = true, |
| | 0 | 63 | | LifetimeValidator = ValidateLifetime, |
| | 0 | 64 | | NameClaimType = JwtRegisteredClaimNames.Name |
| | 0 | 65 | | }; |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | private static bool ValidateLifetime(DateTime? notBefore, DateTime? expires, SecurityToken securityToken, TokenValid |
| | | 69 | | { |
| | 0 | 70 | | return expires != null && expires > DateTime.UtcNow; |
| | | 71 | | } |
| | | 72 | | } |