diff --git a/src/dotnet-decode-jwt/ClaimsDisplayer.cs b/src/dotnet-decode-jwt/ClaimsDisplayer.cs index 87a2b67..cdcc1b4 100644 --- a/src/dotnet-decode-jwt/ClaimsDisplayer.cs +++ b/src/dotnet-decode-jwt/ClaimsDisplayer.cs @@ -1,73 +1,71 @@ -using System; -using Newtonsoft.Json; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; -namespace DotNet.Decode.Jwt +namespace DotNet.Decode.Jwt; + +public class ClaimsDisplayer { - public class ClaimsDisplayer - { - private readonly IConsole _console; - private readonly TimeZoneInfo _localTimeZone; + private readonly IConsole _console; + private readonly TimeZoneInfo _localTimeZone; - private const string ExpirationTimeKeyName = "exp"; - private const string NotBeforeKeyName = "nbf"; - private const string IssuedAtKeyName = "iat"; + private const string ExpirationTimeKeyName = "exp"; + private const string NotBeforeKeyName = "nbf"; + private const string IssuedAtKeyName = "iat"; - private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); - public ClaimsDisplayer(IConsole console, TimeZoneInfo localTimeZone) - { - _console = console; - _localTimeZone = localTimeZone; - } + public ClaimsDisplayer(IConsole console, TimeZoneInfo localTimeZone) + { + _console = console; + _localTimeZone = localTimeZone; + } - public void DisplayClaims(JObject claims) + public void DisplayClaims(JObject claims) + { + try { - try + if (claims.Count == 0) { - if (claims.Count == 0) - { - _console.ForegroundColor = ConsoleColor.DarkGray; - _console.WriteLine("There was no claims in the JWT."); - } - else - { - _console.WriteLine(string.Empty); - _console.ForegroundColor = ConsoleColor.Yellow; - _console.WriteLine($"Expiration Time ({ExpirationTimeKeyName}): {FormatDateTime(claims, ExpirationTimeKeyName)}"); - _console.WriteLine($"Not Before ({NotBeforeKeyName}): {FormatDateTime(claims, NotBeforeKeyName)}"); - _console.WriteLine($"Issued At ({IssuedAtKeyName}): {FormatDateTime(claims, IssuedAtKeyName)}"); - _console.ForegroundColor = ConsoleColor.Green; - _console.WriteLine(string.Empty); - _console.WriteLine("Claims are:"); - _console.WriteLine(string.Empty); - _console.ResetColor(); - - _console.WriteLine(JsonConvert.SerializeObject(claims, Formatting.Indented)); - } + _console.ForegroundColor = ConsoleColor.DarkGray; + _console.WriteLine("There was no claims in the JWT."); } - finally + else { + _console.WriteLine(string.Empty); + _console.ForegroundColor = ConsoleColor.Yellow; + _console.WriteLine($"Expiration Time ({ExpirationTimeKeyName}): {FormatDateTime(claims, ExpirationTimeKeyName)}"); + _console.WriteLine($"Not Before ({NotBeforeKeyName}): {FormatDateTime(claims, NotBeforeKeyName)}"); + _console.WriteLine($"Issued At ({IssuedAtKeyName}): {FormatDateTime(claims, IssuedAtKeyName)}"); + _console.ForegroundColor = ConsoleColor.Green; + _console.WriteLine(string.Empty); + _console.WriteLine("Claims are:"); + _console.WriteLine(string.Empty); _console.ResetColor(); + + _console.WriteLine(JsonConvert.SerializeObject(claims, Formatting.Indented)); } } - - private string FormatDateTime(JObject claims, string key) + finally { - if (!claims.TryGetValue(key, out var token)) return "N/A"; + _console.ResetColor(); + } + } - var timestamp = token.Value(); + private string FormatDateTime(JObject claims, string key) + { + if (!claims.TryGetValue(key, out var token)) return "N/A"; - var utcTime = Epoch.AddSeconds(timestamp); + var timestamp = token.Value(); - var localTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, _localTimeZone); + var utcTime = Epoch.AddSeconds(timestamp); - return $"{FormatDateTime(utcTime)} UTC / {FormatDateTime(localTime)} {_localTimeZone.DisplayName}"; - } + var localTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, _localTimeZone); - private static string FormatDateTime(DateTime date) - { - return $"{date.ToLongDateString()} {date:HH:mm:ss}"; - } + return $"{FormatDateTime(utcTime)} UTC / {FormatDateTime(localTime)} {_localTimeZone.DisplayName}"; + } + + private static string FormatDateTime(DateTime date) + { + return $"{date.ToLongDateString()} {date:HH:mm:ss}"; } } diff --git a/src/dotnet-decode-jwt/JwtClaimsDecoder.cs b/src/dotnet-decode-jwt/JwtClaimsDecoder.cs index 3e3b6fe..f1663e3 100644 --- a/src/dotnet-decode-jwt/JwtClaimsDecoder.cs +++ b/src/dotnet-decode-jwt/JwtClaimsDecoder.cs @@ -1,48 +1,46 @@ -using System; -using System.Text; +using System.Text; using Newtonsoft.Json; using Newtonsoft.Json.Linq; -namespace DotNet.Decode.Jwt +namespace DotNet.Decode.Jwt; + +public static class JwtClaimsDecoder { - public static class JwtClaimsDecoder + public static JObject GetClaims(string jwt) { - public static JObject GetClaims(string jwt) + var base64UrlClaimsSet = GetBase64UrlClaimsSet(jwt); + var claimsSet = DecodeBase64Url(base64UrlClaimsSet); + + try { - var base64UrlClaimsSet = GetBase64UrlClaimsSet(jwt); - var claimsSet = DecodeBase64Url(base64UrlClaimsSet); - - try - { - return JObject.Parse(claimsSet); - } - catch (JsonReaderException e) - { - throw new FormatException(e.Message, e); - } + return JObject.Parse(claimsSet); } - - private static string GetBase64UrlClaimsSet(string jwt) + catch (JsonReaderException e) { - var firstDotIndex = jwt.IndexOf('.'); - var lastDotIndex = jwt.LastIndexOf('.'); + throw new FormatException(e.Message, e); + } + } - if (firstDotIndex == -1 || lastDotIndex <= firstDotIndex) - { - throw new FormatException("The JWT should contain two periods."); - } + private static string GetBase64UrlClaimsSet(string jwt) + { + var firstDotIndex = jwt.IndexOf('.'); + var lastDotIndex = jwt.LastIndexOf('.'); - return jwt.Substring(firstDotIndex + 1, lastDotIndex - firstDotIndex - 1); + if (firstDotIndex == -1 || lastDotIndex <= firstDotIndex) + { + throw new FormatException("The JWT should contain two periods."); } - private static string DecodeBase64Url(string base64Url) - { - var base64 = base64Url - .Replace('-', '+') - .Replace('_', '/') - .PadRight(base64Url.Length + (4 - base64Url.Length % 4) % 4, '='); + return jwt.Substring(firstDotIndex + 1, lastDotIndex - firstDotIndex - 1); + } - return Encoding.UTF8.GetString(Convert.FromBase64String(base64)); - } + private static string DecodeBase64Url(string base64Url) + { + var base64 = base64Url + .Replace('-', '+') + .Replace('_', '/') + .PadRight(base64Url.Length + (4 - base64Url.Length % 4) % 4, '='); + + return Encoding.UTF8.GetString(Convert.FromBase64String(base64)); } } diff --git a/src/dotnet-decode-jwt/Program.cs b/src/dotnet-decode-jwt/Program.cs index dc2884c..bb16b33 100644 --- a/src/dotnet-decode-jwt/Program.cs +++ b/src/dotnet-decode-jwt/Program.cs @@ -1,35 +1,32 @@ -using System; +namespace DotNet.Decode.Jwt; -namespace DotNet.Decode.Jwt +class Program { - class Program + static void Main(string[] args) { - static void Main(string[] args) + if (args.Length != 1) { - if (args.Length != 1) - { - Console.WriteLine("A single argument should be provided:"); - Console.WriteLine("dotnet decode-jwt eyJhbGciOiJub25lIn0.ewogICAgImlzcyI6ICJiZXN0LWlzc3VlciIsCiAgICAic3ViIjogIm5pY2Utc3ViamVjdCIsCiAgICAiYXVkIjogWyJhdWRpZW5jZS1vbmUiLCAiYXVkaWVuY2UtdHdvIl0sCiAgICAiZXhwIjogMTUyODY5MTM1MCwKICAgICJuYmYiOiAxNTI4NjkwNzUwLAogICAgImlhdCI6IDE1Mjg2OTA3NTAsCiAgICAianRpIjogImMzMTk3ZGNiLWUxMTMtNDc3OC04OTc5LWI5NTZmNjg0MDA3ZiIsCiAgICAiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvZW1haWxhZGRyZXNzIjogImhpQG1lLmNvbSIsCiAgICAic29tZS1udW1iZXIiOiAxMi41NiwKICAgICJuZXN0ZWQtY2xhaW0iOiB7CiAgICAgICAgImhpIjogIkknbSIsCiAgICAgICAgImEiOiAibmVzdGVkIGNsYWltIgogICAgfQp9Cg==."); - return; - } + Console.WriteLine("A single argument should be provided:"); + Console.WriteLine("dotnet decode-jwt eyJhbGciOiJub25lIn0.ewogICAgImlzcyI6ICJiZXN0LWlzc3VlciIsCiAgICAic3ViIjogIm5pY2Utc3ViamVjdCIsCiAgICAiYXVkIjogWyJhdWRpZW5jZS1vbmUiLCAiYXVkaWVuY2UtdHdvIl0sCiAgICAiZXhwIjogMTUyODY5MTM1MCwKICAgICJuYmYiOiAxNTI4NjkwNzUwLAogICAgImlhdCI6IDE1Mjg2OTA3NTAsCiAgICAianRpIjogImMzMTk3ZGNiLWUxMTMtNDc3OC04OTc5LWI5NTZmNjg0MDA3ZiIsCiAgICAiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvZW1haWxhZGRyZXNzIjogImhpQG1lLmNvbSIsCiAgICAic29tZS1udW1iZXIiOiAxMi41NiwKICAgICJuZXN0ZWQtY2xhaW0iOiB7CiAgICAgICAgImhpIjogIkknbSIsCiAgICAgICAgImEiOiAibmVzdGVkIGNsYWltIgogICAgfQp9Cg==."); + return; + } - var console = new SimplifiedConsole(); - var claimsDisplayer = new ClaimsDisplayer(console, TimeZoneInfo.Local); + var console = new SimplifiedConsole(); + var claimsDisplayer = new ClaimsDisplayer(console, TimeZoneInfo.Local); - try - { - var claims = JwtClaimsDecoder.GetClaims(args[0]); - claimsDisplayer.DisplayClaims(claims); - } - catch (FormatException e) - { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(e.Message); - } - finally - { - Console.ResetColor(); - } + try + { + var claims = JwtClaimsDecoder.GetClaims(args[0]); + claimsDisplayer.DisplayClaims(claims); + } + catch (FormatException e) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(e.Message); + } + finally + { + Console.ResetColor(); } } } diff --git a/src/dotnet-decode-jwt/SimplifiedConsole.cs b/src/dotnet-decode-jwt/SimplifiedConsole.cs index 19f1c7b..fb3636f 100644 --- a/src/dotnet-decode-jwt/SimplifiedConsole.cs +++ b/src/dotnet-decode-jwt/SimplifiedConsole.cs @@ -1,29 +1,26 @@ -using System; +namespace DotNet.Decode.Jwt; -namespace DotNet.Decode.Jwt +public interface IConsole { - public interface IConsole + ConsoleColor ForegroundColor { set; } + void WriteLine(string value); + void ResetColor(); +} + +class SimplifiedConsole : IConsole +{ + public ConsoleColor ForegroundColor { - ConsoleColor ForegroundColor { set; } - void WriteLine(string value); - void ResetColor(); + set => Console.ForegroundColor = value; } - class SimplifiedConsole : IConsole + public void WriteLine(string value) { - public ConsoleColor ForegroundColor - { - set => Console.ForegroundColor = value; - } - - public void WriteLine(string value) - { - Console.WriteLine(value); - } + Console.WriteLine(value); + } - public void ResetColor() - { - Console.ResetColor(); - } + public void ResetColor() + { + Console.ResetColor(); } } diff --git a/src/dotnet-decode-jwt/dotnet-decode-jwt.csproj b/src/dotnet-decode-jwt/dotnet-decode-jwt.csproj index 8e37f08..edc1d5f 100644 --- a/src/dotnet-decode-jwt/dotnet-decode-jwt.csproj +++ b/src/dotnet-decode-jwt/dotnet-decode-jwt.csproj @@ -2,6 +2,8 @@ Exe netcoreapp3.1;net6.0 + latest + enable DotNet.Decode.Jwt true dotnet-decode-jwt diff --git a/tests/dotnet-decode-jwt-tests/ClaimsDisplayerTests.cs b/tests/dotnet-decode-jwt-tests/ClaimsDisplayerTests.cs index df2cdaf..aa883f2 100644 --- a/tests/dotnet-decode-jwt-tests/ClaimsDisplayerTests.cs +++ b/tests/dotnet-decode-jwt-tests/ClaimsDisplayerTests.cs @@ -1,112 +1,107 @@ -using System; -using System.Collections.Generic; -using System.Globalization; +using System.Globalization; using FluentAssertions; -using Newtonsoft.Json.Linq; -using Xunit; -namespace DotNet.Decode.Jwt.Tests +namespace DotNet.Decode.Jwt.Tests; + +public class ClaimsDisplayerTests { - public class ClaimsDisplayerTests - { - private readonly ClaimsDisplayer _target; + private readonly ClaimsDisplayer _target; - private readonly MockConsole _console; + private readonly MockConsole _console; - public ClaimsDisplayerTests() - { - _console = new MockConsole(); + public ClaimsDisplayerTests() + { + _console = new MockConsole(); - _target = new ClaimsDisplayer(_console, TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time")); - } + _target = new ClaimsDisplayer(_console, TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time")); + } - static ClaimsDisplayerTests() - { - var cultureInfo = new CultureInfo("en-AU"); + static ClaimsDisplayerTests() + { + var cultureInfo = new CultureInfo("en-AU"); - CultureInfo.DefaultThreadCurrentCulture = cultureInfo; - CultureInfo.DefaultThreadCurrentUICulture = cultureInfo; - } + CultureInfo.DefaultThreadCurrentCulture = cultureInfo; + CultureInfo.DefaultThreadCurrentUICulture = cultureInfo; + } - [Fact] - public void GivenNoClaim_WhenDisplayClaims_ThenMessage() - { - // Arrange + [Fact] + public void GivenNoClaim_WhenDisplayClaims_ThenMessage() + { + // Arrange - var claims = new JObject(); + var claims = new JObject(); - // Act + // Act - _target.DisplayClaims(claims); + _target.DisplayClaims(claims); - // Assert + // Assert - var expected = new List - { - "SET FOREGROUND COLOR: DarkGray", - "WRITE: There was no claims in the JWT.", - "RESET COLOR" - }; + var expected = new List + { + "SET FOREGROUND COLOR: DarkGray", + "WRITE: There was no claims in the JWT.", + "RESET COLOR" + }; - _console.Actions.Should().BeEquivalentTo(expected); - } + _console.Actions.Should().BeEquivalentTo(expected); + } - [Fact] - public void GivenAnyClaim_WhenDisplayClaims_ThenDisplayClaims() - { - // Arrange + [Fact] + public void GivenAnyClaim_WhenDisplayClaims_ThenDisplayClaims() + { + // Arrange - var claims = JObject.Parse(@" + var claims = JObject.Parse(@" { 'iat': 1516239022 } "); - // Act + // Act - _target.DisplayClaims(claims); + _target.DisplayClaims(claims); - // Assert + // Assert - var expected = new List - { - "WRITE: ", - "SET FOREGROUND COLOR: Yellow", - "WRITE: Expiration Time (exp): N/A", - "WRITE: Not Before (nbf): N/A", - "WRITE: Issued At (iat): Thursday, 18 January 2018 01:30:22 UTC / Thursday, 18 January 2018 12:30:22 (UTC+10:00) Canberra, Melbourne, Sydney", - "SET FOREGROUND COLOR: Green", - "WRITE: ", - "WRITE: Claims are:", - "WRITE: ", - "RESET COLOR", - "WRITE: {\r\n \"iat\": 1516239022\r\n}", - "RESET COLOR" - }; - - _console.Actions.Should().BeEquivalentTo(expected); - } + var expected = new List + { + "WRITE: ", + "SET FOREGROUND COLOR: Yellow", + "WRITE: Expiration Time (exp): N/A", + "WRITE: Not Before (nbf): N/A", + "WRITE: Issued At (iat): Thursday, 18 January 2018 01:30:22 UTC / Thursday, 18 January 2018 12:30:22 (UTC+10:00) Canberra, Melbourne, Sydney", + "SET FOREGROUND COLOR: Green", + "WRITE: ", + "WRITE: Claims are:", + "WRITE: ", + "RESET COLOR", + "WRITE: {\r\n \"iat\": 1516239022\r\n}", + "RESET COLOR" + }; + + _console.Actions.Should().BeEquivalentTo(expected); } +} - internal class MockConsole : IConsole - { - private readonly List _actions = new List(); +internal class MockConsole : IConsole +{ + private readonly List _actions = new List(); - public IReadOnlyList Actions => _actions; + public IReadOnlyList Actions => _actions; - public ConsoleColor ForegroundColor - { - set => _actions.Add($"SET FOREGROUND COLOR: {value}"); - } + public ConsoleColor ForegroundColor + { + set => _actions.Add($"SET FOREGROUND COLOR: {value}"); + } - public void WriteLine(string value) - { - _actions.Add($"WRITE: {value}"); - } + public void WriteLine(string value) + { + _actions.Add($"WRITE: {value}"); + } - public void ResetColor() - { - _actions.Add("RESET COLOR"); - } + public void ResetColor() + { + _actions.Add("RESET COLOR"); } } diff --git a/tests/dotnet-decode-jwt-tests/JwtClaimsDecoderTests.cs b/tests/dotnet-decode-jwt-tests/JwtClaimsDecoderTests.cs index d1bd3a4..e27d6f5 100644 --- a/tests/dotnet-decode-jwt-tests/JwtClaimsDecoderTests.cs +++ b/tests/dotnet-decode-jwt-tests/JwtClaimsDecoderTests.cs @@ -1,67 +1,63 @@ -using System; -using Newtonsoft.Json.Linq; -using Xunit; +namespace DotNet.Decode.Jwt.Tests; -namespace DotNet.Decode.Jwt.Tests +public class JwtClaimsDecoderTests { - public class JwtClaimsDecoderTests - { - private const string JwtWithoutPeriods = "cQ=="; - private const string JwtWithOnlyOnePeriod = "cQ==.cQ=="; + private const string JwtWithoutPeriods = "cQ=="; + private const string JwtWithOnlyOnePeriod = "cQ==.cQ=="; - [Theory] - [InlineData(JwtWithoutPeriods)] - [InlineData(JwtWithOnlyOnePeriod)] - public void GivenJwtWithLessThanTwoDots_WhenGetClaims_ThenThrows(string jwt) - { - // Act + [Theory] + [InlineData(JwtWithoutPeriods)] + [InlineData(JwtWithOnlyOnePeriod)] + public void GivenJwtWithLessThanTwoDots_WhenGetClaims_ThenThrows(string jwt) + { + // Act - var exception = Record.Exception(() => JwtClaimsDecoder.GetClaims(jwt)); + var exception = Record.Exception(() => JwtClaimsDecoder.GetClaims(jwt)); - // Assert + // Assert - Assert.IsType(exception); - Assert.Equal("The JWT should contain two periods.", exception.Message); - } + Assert.IsType(exception); + Assert.Equal("The JWT should contain two periods.", exception.Message); + } - [Fact] - public void GivenNotBase64EncodedClaimsSet_WhenGetClaims_ThenThrows() - { - // Act + [Fact] + public void GivenNotBase64EncodedClaimsSet_WhenGetClaims_ThenThrows() + { + // Act - var exception = Record.Exception(() => JwtClaimsDecoder.GetClaims("cQ==.invalid|base|64.cQ==")); + var exception = Record.Exception(() => JwtClaimsDecoder.GetClaims("cQ==.invalid|base|64.cQ==")); - // Assert + // Assert - Assert.IsType(exception); - } + Assert.IsType(exception); + } - [Fact] - public void GivenClaimsSetIsNotJson_WhenGetClaims_ThenThrows() - { - // Act + [Fact] + public void GivenClaimsSetIsNotJson_WhenGetClaims_ThenThrows() + { + // Act - var exception = Record.Exception(() => JwtClaimsDecoder.GetClaims("cQ==.cQ==.cQ==")); + var exception = Record.Exception(() => JwtClaimsDecoder.GetClaims("cQ==.cQ==.cQ==")); - // Assert + // Assert - Assert.IsType(exception); - } + Assert.IsType(exception); + } - [Fact] - public void GivenIatIsNumber_WhenGetClaims_ThenReturnClaims() - { - // Arrange + [Fact] + public void GivenIatIsNumber_WhenGetClaims_ThenReturnClaims() + { + // Arrange - const string jwt = "eyJhbGciOiJub25lIn0.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ."; + const string jwt = "eyJhbGciOiJub25lIn0.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ."; - // Act + // Act - var actualClaims = JwtClaimsDecoder.GetClaims(jwt); + var actualClaims = JwtClaimsDecoder.GetClaims(jwt); - // Assert + // Assert - var expectedClaims = JObject.Parse(@" + var expectedClaims = JObject.Parse(@" { 'sub': '1234567890', 'name': 'John Doe', @@ -69,73 +65,72 @@ public void GivenIatIsNumber_WhenGetClaims_ThenReturnClaims() } "); - Assert.Equal(expectedClaims, actualClaims); - } + Assert.Equal(expectedClaims, actualClaims); + } - [Fact] - public void GivenAudIsArrayOfString_WhenGetClaims_ThenReturnClaims() - { - // Arrange + [Fact] + public void GivenAudIsArrayOfString_WhenGetClaims_ThenReturnClaims() + { + // Arrange - const string jwt = "eyJhbGciOiJub25lIn0.eyJhdWQiOlsiYXVkaWVuY2Utb25lIiwiYXVkaWVuY2UtdHdvIl19."; + const string jwt = "eyJhbGciOiJub25lIn0.eyJhdWQiOlsiYXVkaWVuY2Utb25lIiwiYXVkaWVuY2UtdHdvIl19."; - // Act + // Act - var actualClaims = JwtClaimsDecoder.GetClaims(jwt); + var actualClaims = JwtClaimsDecoder.GetClaims(jwt); - // Assert + // Assert - var expectedClaims = JObject.Parse(@" + var expectedClaims = JObject.Parse(@" { 'aud': ['audience-one','audience-two'] } "); - Assert.Equal(expectedClaims, actualClaims); - } + Assert.Equal(expectedClaims, actualClaims); + } - [Fact] - public void GivenAudIsSingleString_WhenGetClaims_ThenReturnClaims() - { - // Arrange + [Fact] + public void GivenAudIsSingleString_WhenGetClaims_ThenReturnClaims() + { + // Arrange - const string jwt = "eyJhbGciOiJub25lIn0.eyJhdWQiOiJhdWRpZW5jZSJ9."; + const string jwt = "eyJhbGciOiJub25lIn0.eyJhdWQiOiJhdWRpZW5jZSJ9."; - // Act + // Act - var actualClaims = JwtClaimsDecoder.GetClaims(jwt); + var actualClaims = JwtClaimsDecoder.GetClaims(jwt); - // Assert + // Assert - var expectedClaims = JObject.Parse(@" + var expectedClaims = JObject.Parse(@" { 'aud': 'audience' } "); - Assert.Equal(expectedClaims, actualClaims); - } + Assert.Equal(expectedClaims, actualClaims); + } - [Fact] - public void GivenClaimKeyIsXmlNamespace_WhenGetClaims_ThenReturnClaims() - { - // Arrange + [Fact] + public void GivenClaimKeyIsXmlNamespace_WhenGetClaims_ThenReturnClaims() + { + // Arrange - const string jwt = "eyJhbGciOiJub25lIn0.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9lbWFpbGFkZHJlc3MiOiAiaGlAbWUuY29tIn0=."; + const string jwt = "eyJhbGciOiJub25lIn0.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9lbWFpbGFkZHJlc3MiOiAiaGlAbWUuY29tIn0=."; - // Act + // Act - var actualClaims = JwtClaimsDecoder.GetClaims(jwt); + var actualClaims = JwtClaimsDecoder.GetClaims(jwt); - // Assert + // Assert - var expectedClaims = JObject.Parse(@" + var expectedClaims = JObject.Parse(@" { 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress': 'hi@me.com' } "); - Assert.Equal(expectedClaims, actualClaims); - } + Assert.Equal(expectedClaims, actualClaims); } } diff --git a/tests/dotnet-decode-jwt-tests/dotnet-decode-jwt-tests.csproj b/tests/dotnet-decode-jwt-tests/dotnet-decode-jwt-tests.csproj index fc5b6c1..e19f562 100644 --- a/tests/dotnet-decode-jwt-tests/dotnet-decode-jwt-tests.csproj +++ b/tests/dotnet-decode-jwt-tests/dotnet-decode-jwt-tests.csproj @@ -1,9 +1,15 @@  net6.0 + latest + enable DotNet.Decode.Jwt.Tests false + + + +