Skip to content

Commit

Permalink
Merge 1522d62 into 29ff98e
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielweyer authored Apr 20, 2022
2 parents 29ff98e + 1522d62 commit be7128c
Show file tree
Hide file tree
Showing 12 changed files with 306 additions and 91 deletions.
2 changes: 2 additions & 0 deletions dotnet-decode-jwt.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Gabo/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
36 changes: 12 additions & 24 deletions src/dotnet-decode-jwt/ClaimsDisplayer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Encodings.Web;

namespace DotNet.Decode.Jwt;
namespace Gabo.DotNet.DecodeJwt;

public class ClaimsDisplayer
{
Expand All @@ -26,32 +26,20 @@ public ClaimsDisplayer(IConsole console, TimeZoneInfo localTimeZone)

public void DisplayClaims(JsonElement claims)
{
try
if (claims.ValueKind == JsonValueKind.Undefined)
{
if (claims.ValueKind == JsonValueKind.Undefined)
{
_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(JsonSerializer.Serialize(claims, _serializationOptions));
}
_console.WriteDullLine("There was no claims in the JWT.");
}
finally
else
{
_console.ResetColor();
_console.WriteBoringLine(string.Empty);
_console.WriteInfoLine($"Expiration Time ({ExpirationTimeKeyName}): {FormatDateTime(claims, ExpirationTimeKeyName)}");
_console.WriteInfoLine($"Not Before ({NotBeforeKeyName}): {FormatDateTime(claims, NotBeforeKeyName)}");
_console.WriteInfoLine($"Issued At ({IssuedAtKeyName}): {FormatDateTime(claims, IssuedAtKeyName)}");
_console.WriteFancyLine(string.Empty);
_console.WriteFancyLine("Claims are:");
_console.WriteFancyLine(string.Empty);
_console.WriteBoringLine(JsonSerializer.Serialize(claims, _serializationOptions));
}
}

Expand Down
51 changes: 51 additions & 0 deletions src/dotnet-decode-jwt/Infrastructure/NonThreadSafeConsole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace Gabo.DotNet.DecodeJwt.Infrastructure;

public interface IConsole
{
void WriteBoringLine(string value);
void WriteDullLine(string value);
void WriteInfoLine(string value);
void WriteFancyLine(string value);
void WriteErrorLine(string value);
}

internal class NonThreadSafeConsole : IConsole
{
internal const ConsoleColor BoringColor = ConsoleColor.Gray;
internal const ConsoleColor DullColor = ConsoleColor.DarkGray;
internal const ConsoleColor InfoColor = ConsoleColor.Yellow;
internal const ConsoleColor FancyColor = ConsoleColor.Green;
internal const ConsoleColor ErrorColor = ConsoleColor.Red;

public void WriteBoringLine(string value)
{
WriteColoredLine(BoringColor, value);
}

public void WriteDullLine(string value)
{
WriteColoredLine(DullColor, value);
}

public void WriteInfoLine(string value)
{
WriteColoredLine(InfoColor, value);
}

public void WriteFancyLine(string value)
{
WriteColoredLine(FancyColor, value);
}

public void WriteErrorLine(string value)
{
WriteColoredLine(ErrorColor, value);
}

private static void WriteColoredLine(ConsoleColor color, string value)
{
Console.ForegroundColor = color;
Console.WriteLine(value);
Console.ResetColor();
}
}
2 changes: 1 addition & 1 deletion src/dotnet-decode-jwt/JwtClaimsDecoder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace DotNet.Decode.Jwt;
namespace Gabo.DotNet.DecodeJwt;

public static class JwtClaimsDecoder
{
Expand Down
31 changes: 17 additions & 14 deletions src/dotnet-decode-jwt/Program.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
namespace DotNet.Decode.Jwt;
namespace Gabo.DotNet.DecodeJwt;

internal class Program
internal static class Program
{
private static void Main(string[] args)
internal const int SuccessExitCode = 0;
internal const int FailureExitCode = 1;

internal static IConsole AbstractedConsole;

internal static int Main(string[] args)
{
AbstractedConsole ??= new NonThreadSafeConsole();

if (args.Length != 1)
{
Console.WriteLine("A single argument should be provided:");
Console.WriteLine("dotnet decode-jwt eyJhbGciOiJub25lIn0.ewogICAgImlzcyI6ICJiZXN0LWlzc3VlciIsCiAgICAic3ViIjogIm5pY2Utc3ViamVjdCIsCiAgICAiYXVkIjogWyJhdWRpZW5jZS1vbmUiLCAiYXVkaWVuY2UtdHdvIl0sCiAgICAiZXhwIjogMTUyODY5MTM1MCwKICAgICJuYmYiOiAxNTI4NjkwNzUwLAogICAgImlhdCI6IDE1Mjg2OTA3NTAsCiAgICAianRpIjogImMzMTk3ZGNiLWUxMTMtNDc3OC04OTc5LWI5NTZmNjg0MDA3ZiIsCiAgICAiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvZW1haWxhZGRyZXNzIjogImhpQG1lLmNvbSIsCiAgICAic29tZS1udW1iZXIiOiAxMi41NiwKICAgICJuZXN0ZWQtY2xhaW0iOiB7CiAgICAgICAgImhpIjogIkknbSIsCiAgICAgICAgImEiOiAibmVzdGVkIGNsYWltIgogICAgfQp9Cg==.");
return;
AbstractedConsole.WriteBoringLine("A single argument should be provided:");
AbstractedConsole.WriteBoringLine("dotnet decode-jwt eyJhbGciOiJub25lIn0.ewogICAgImlzcyI6ICJiZXN0LWlzc3VlciIsCiAgICAic3ViIjogIm5pY2Utc3ViamVjdCIsCiAgICAiYXVkIjogWyJhdWRpZW5jZS1vbmUiLCAiYXVkaWVuY2UtdHdvIl0sCiAgICAiZXhwIjogMTUyODY5MTM1MCwKICAgICJuYmYiOiAxNTI4NjkwNzUwLAogICAgImlhdCI6IDE1Mjg2OTA3NTAsCiAgICAianRpIjogImMzMTk3ZGNiLWUxMTMtNDc3OC04OTc5LWI5NTZmNjg0MDA3ZiIsCiAgICAiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvZW1haWxhZGRyZXNzIjogImhpQG1lLmNvbSIsCiAgICAic29tZS1udW1iZXIiOiAxMi41NiwKICAgICJuZXN0ZWQtY2xhaW0iOiB7CiAgICAgICAgImhpIjogIkknbSIsCiAgICAgICAgImEiOiAibmVzdGVkIGNsYWltIgogICAgfQp9Cg==.");
return FailureExitCode;
}

var console = new SimplifiedConsole();
var claimsDisplayer = new ClaimsDisplayer(console, TimeZoneInfo.Local);
var claimsDisplayer = new ClaimsDisplayer(AbstractedConsole, TimeZoneInfo.Local);

try
{
var claims = JwtClaimsDecoder.GetClaims(args[0]);
claimsDisplayer.DisplayClaims(claims);
return SuccessExitCode;
}
catch (FormatException e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(e.Message);
}
finally
{
Console.ResetColor();
AbstractedConsole.WriteErrorLine(e.Message);
return FailureExitCode;
}
}
}
26 changes: 0 additions & 26 deletions src/dotnet-decode-jwt/SimplifiedConsole.cs

This file was deleted.

6 changes: 5 additions & 1 deletion src/dotnet-decode-jwt/dotnet-decode-jwt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<ContinuousIntegrationBuild Condition="'$(GITHUB_ACTIONS)' == 'True'">true</ContinuousIntegrationBuild>
<RootNamespace>DotNet.Decode.Jwt</RootNamespace>
<RootNamespace>Gabo.DotNet.DecodeJwt</RootNamespace>
<PackAsTool>true</PackAsTool>
<PackageId>dotnet-decode-jwt</PackageId>
<Authors>Gabriel Weyer</Authors>
Expand All @@ -21,6 +21,10 @@
<NoWarn>CS7035</NoWarn>
</PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="Gabo.DotNet.DecodeJwt.Tests" />
</ItemGroup>
<ItemGroup>
<Using Include="Gabo.DotNet.DecodeJwt.Infrastructure" />
<Using Include="System.Text" />
<Using Include="System.Text.Json" />
</ItemGroup>
Expand Down
60 changes: 37 additions & 23 deletions tests/dotnet-decode-jwt-tests/ClaimsDisplayerTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Globalization;
using System.Runtime.InteropServices;

namespace DotNet.Decode.Jwt.Tests;
namespace Gabo.DotNet.DecodeJwt.Tests;

public class ClaimsDisplayerTests
{
Expand Down Expand Up @@ -70,16 +70,28 @@ public void GivenAnyClaim_WhenDisplayClaims_ThenDisplayClaims()
// Assert
var expected = new List<string>
{
"SET FOREGROUND COLOR: Gray",
"WRITE: ",
"RESET COLOR",
"SET FOREGROUND COLOR: Yellow",
"WRITE: Expiration Time (exp): N/A",
"RESET COLOR",
"SET FOREGROUND COLOR: Yellow",
"WRITE: Not Before (nbf): N/A",
"RESET COLOR",
"SET FOREGROUND COLOR: Yellow",
$"WRITE: Issued At (iat): Thursday, 18 January 2018 01:30:22 UTC / Thursday, 18 January 2018 12:30:22 (UTC+10:00) {_timeZoneDisplayName}",
"RESET COLOR",
"SET FOREGROUND COLOR: Green",
"WRITE: ",
"RESET COLOR",
"SET FOREGROUND COLOR: Green",
"WRITE: Claims are:",
"RESET COLOR",
"SET FOREGROUND COLOR: Green",
"WRITE: ",
"RESET COLOR",
"SET FOREGROUND COLOR: Gray",
$"WRITE: {{{Environment.NewLine} \"iat\": 1516239022{Environment.NewLine}}}",
"RESET COLOR"
};
Expand All @@ -99,16 +111,28 @@ public void GivenIatIsNotTimestamp_WhenDisplayClaims_ThenDisplayDateAsNotAvailab
// Assert
var expected = new List<string>
{
"SET FOREGROUND COLOR: Gray",
"WRITE: ",
"RESET COLOR",
"SET FOREGROUND COLOR: Yellow",
"WRITE: Expiration Time (exp): N/A",
"RESET COLOR",
"SET FOREGROUND COLOR: Yellow",
"WRITE: Not Before (nbf): N/A",
"RESET COLOR",
"SET FOREGROUND COLOR: Yellow",
"WRITE: Issued At (iat): N/A",
"RESET COLOR",
"SET FOREGROUND COLOR: Green",
"WRITE: ",
"RESET COLOR",
"SET FOREGROUND COLOR: Green",
"WRITE: Claims are:",
"RESET COLOR",
"SET FOREGROUND COLOR: Green",
"WRITE: ",
"RESET COLOR",
"SET FOREGROUND COLOR: Gray",
$"WRITE: {{{Environment.NewLine} \"iat\": \"hello\"{Environment.NewLine}}}",
"RESET COLOR"
};
Expand All @@ -128,42 +152,32 @@ public void GivenHtmlSensitiveCharacter_WhenDisplayClaims_ThenDoNotEscape()
// Assert
var expected = new List<string>
{
"SET FOREGROUND COLOR: Gray",
"WRITE: ",
"RESET COLOR",
"SET FOREGROUND COLOR: Yellow",
"WRITE: Expiration Time (exp): N/A",
"RESET COLOR",
"SET FOREGROUND COLOR: Yellow",
"WRITE: Not Before (nbf): N/A",
"RESET COLOR",
"SET FOREGROUND COLOR: Yellow",
"WRITE: Issued At (iat): N/A",
"RESET COLOR",
"SET FOREGROUND COLOR: Green",
"WRITE: ",
"RESET COLOR",
"SET FOREGROUND COLOR: Green",
"WRITE: Claims are:",
"RESET COLOR",
"SET FOREGROUND COLOR: Green",
"WRITE: ",
"RESET COLOR",
"SET FOREGROUND COLOR: Gray",
$"WRITE: {{{Environment.NewLine} \"hi\": \"I'm\"{Environment.NewLine}}}",
"RESET COLOR"
};

_console.Actions.Should().BeEquivalentTo(expected);
}
}

internal class MockConsole : IConsole
{
private readonly List<string> _actions = new List<string>();

public IReadOnlyList<string> Actions => _actions;

public ConsoleColor ForegroundColor
{
set => _actions.Add($"SET FOREGROUND COLOR: {value}");
}

public void WriteLine(string value)
{
_actions.Add($"WRITE: {value}");
}

public void ResetColor()
{
_actions.Add("RESET COLOR");
}
}
2 changes: 1 addition & 1 deletion tests/dotnet-decode-jwt-tests/JwtClaimsDecoderTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace DotNet.Decode.Jwt.Tests;
namespace Gabo.DotNet.DecodeJwt.Tests;

public class JwtClaimsDecoderTests
{
Expand Down
Loading

0 comments on commit be7128c

Please sign in to comment.