-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use implicit & global using, file-scoped namespace
- Loading branch information
1 parent
9862c34
commit 4915649
Showing
8 changed files
with
276 additions
and
288 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<int>(); | ||
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<int>(); | ||
|
||
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}"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.