From 391cb1b6e37b94961f60bd8f6398bbe2a3ef3efe Mon Sep 17 00:00:00 2001 From: waldekmastykarz Date: Wed, 25 Sep 2024 12:37:21 +0200 Subject: [PATCH] Refactors JWT creation --- dev-proxy/Jwt/Jwt.cs | 31 ------------------------------ dev-proxy/Jwt/JwtCreatorOptions.cs | 2 +- dev-proxy/Jwt/JwtOptions.cs | 2 +- dev-proxy/Jwt/JwtTokenGenerator.cs | 12 +----------- 4 files changed, 3 insertions(+), 44 deletions(-) delete mode 100644 dev-proxy/Jwt/Jwt.cs diff --git a/dev-proxy/Jwt/Jwt.cs b/dev-proxy/Jwt/Jwt.cs deleted file mode 100644 index 0e92869d..00000000 --- a/dev-proxy/Jwt/Jwt.cs +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -using System.IdentityModel.Tokens.Jwt; - -namespace Microsoft.DevProxy.Jwt; - -internal record Jwt(string Id, string Scheme, string Name, string Audience, DateTimeOffset NotBefore, DateTimeOffset Expires, DateTimeOffset Issued, string Token) -{ - public IEnumerable Scopes { get; set; } = []; - - public IEnumerable Roles { get; set; } = []; - - public IDictionary CustomClaims { get; set; } = new Dictionary(); - - public static Jwt Create( - string scheme, - JwtSecurityToken token, - string encodedToken, - IEnumerable scopes, - IEnumerable roles, - IDictionary customClaims) - { - return new Jwt(token.Id, scheme, token.Subject, string.Join(", ", token.Audiences), token.ValidFrom, token.ValidTo, token.IssuedAt, encodedToken) - { - Scopes = scopes, - Roles = roles, - CustomClaims = customClaims - }; - } -} \ No newline at end of file diff --git a/dev-proxy/Jwt/JwtCreatorOptions.cs b/dev-proxy/Jwt/JwtCreatorOptions.cs index a42070e8..4a7dcec4 100644 --- a/dev-proxy/Jwt/JwtCreatorOptions.cs +++ b/dev-proxy/Jwt/JwtCreatorOptions.cs @@ -27,7 +27,7 @@ public static JwtCreatorOptions Create(JwtOptions options) Scopes = options.Scopes ?? [], Claims = options.Claims ?? [], NotBefore = DateTime.UtcNow, - ExpiresOn = DateTime.UtcNow.AddMinutes(options.ValidFor == 0 ? 60 : options.ValidFor) + ExpiresOn = DateTime.UtcNow.AddMinutes(options.ValidFor ?? 60) }; } } \ No newline at end of file diff --git a/dev-proxy/Jwt/JwtOptions.cs b/dev-proxy/Jwt/JwtOptions.cs index d670e3ae..b84933f4 100644 --- a/dev-proxy/Jwt/JwtOptions.cs +++ b/dev-proxy/Jwt/JwtOptions.cs @@ -11,5 +11,5 @@ public class JwtOptions public IEnumerable? Roles { get; set; } public IEnumerable? Scopes { get; set; } public Dictionary? Claims { get; set; } - public double ValidFor { get; set; } + public double? ValidFor { get; set; } } diff --git a/dev-proxy/Jwt/JwtTokenGenerator.cs b/dev-proxy/Jwt/JwtTokenGenerator.cs index 115029f7..1222ed77 100644 --- a/dev-proxy/Jwt/JwtTokenGenerator.cs +++ b/dev-proxy/Jwt/JwtTokenGenerator.cs @@ -18,16 +18,6 @@ internal static string CreateToken(JwtOptions jwtOptions) ); var jwtToken = jwtIssuer.CreateSecurityToken(options); - - var jwt = Jwt.Create( - options.Scheme, - jwtToken, - new JwtSecurityTokenHandler().WriteToken(jwtToken), - options.Scopes, - options.Roles, - options.Claims - ); - - return jwt.Token; + return new JwtSecurityTokenHandler().WriteToken(jwtToken); } } \ No newline at end of file