Skip to content

Commit

Permalink
Fix JWT token decrypt
Browse files Browse the repository at this point in the history
Change decrypt method
  • Loading branch information
nRafinia committed Mar 15, 2023
1 parent 2d5ed3a commit 64a4ab1
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 31 deletions.
24 changes: 24 additions & 0 deletions src/nHash/Providers/JsonBeautifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Text.Json;

namespace nHash.Providers;

public class JsonBeautifier
{
private readonly JsonSerializerOptions _serializerOptions;

public JsonBeautifier()
{
_serializerOptions = new JsonSerializerOptions
{
WriteIndented = true,
PropertyNameCaseInsensitive = true
};
}

public string Set(string text)
{
var jsonElement = JsonDocument.Parse(text).RootElement;
var prettyJson = JsonSerializer.Serialize(jsonElement, _serializerOptions);
return prettyJson;
}
}
93 changes: 65 additions & 28 deletions src/nHash/SubFeatures/Encodes/JwtTokenDecodeFeature.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.IdentityModel.Tokens.Jwt;
using System.Text;
using System.Text.Json.Nodes;
using System.Web;
using nHash.Providers;

namespace nHash.SubFeatures.Encodes;

Expand All @@ -17,7 +20,7 @@ public JwtTokenDecodeFeature()

private Command GetFeatureCommand()
{
var command = new Command("jwt", "JWT token decode")
var command = new Command("jwt", "JWT token decode (Comply with GDPR rules)")
{
_noWriteInformation
};
Expand All @@ -29,17 +32,30 @@ private Command GetFeatureCommand()

private static void DecodeJwtToken(string text, bool noWriteInformation)
{
var tokenHandler = new JwtSecurityTokenHandler();
var jwt = tokenHandler.ReadJwtToken(text);
var parts = text.Split('.');
var header = parts[0];
var payload = parts[1];
//var signature = parts[2];


var decodedHeader = HttpUtility.UrlDecode(Encoding.UTF8.GetString(Convert.FromBase64String(header)));
payload = payload.PadRight(payload.Length + (payload.Length * 3) % 4, '=');
var decodedPayload = HttpUtility.UrlDecode(Encoding.UTF8.GetString(Convert.FromBase64String(payload)));

//Console.WriteLine("JWT payload: " + decodedPayload);
var jsonBeautifier = new JsonBeautifier();
var prettyHeader = jsonBeautifier.Set(decodedHeader);
var prettyPayload = jsonBeautifier.Set(decodedPayload);


Console.WriteLine();
Console.WriteLine("Header: (ALGORITHM & TOKEN TYPE)");
WriteHeaders(jwt);

Console.WriteLine(prettyHeader);
Console.WriteLine();
Console.WriteLine("Payload: (DATA)");
WritePayload(jwt);
Console.WriteLine(prettyPayload);

//WritePayload(jwt);

if (noWriteInformation)
{
Expand All @@ -48,10 +64,12 @@ private static void DecodeJwtToken(string text, bool noWriteInformation)

Console.WriteLine();
Console.WriteLine("Summary:");
WriteSummary(jwt);
WriteSummary(decodedHeader, decodedPayload);
//Console.WriteLine("JWT algorithm: " + JsonObject.Parse(decodedHeader)["alg"]);
//jwtObject.has
}

private static void WriteHeaders(JwtSecurityToken jwt)
/*private static void WriteHeaders(JwtSecurityToken jwt)
{
foreach (var header in jwt.Header)
{
Expand All @@ -65,45 +83,64 @@ private static void WritePayload(JwtSecurityToken jwt)
{
Console.WriteLine(" " + claim.Key + ": " + claim.Value);
}
}
}*/

private static void WriteSummary(JwtSecurityToken jwt)
private static void WriteSummary(string header, string payload)
{
// Get algorithm and other data from JWT token
Console.WriteLine(" Algorithm: " + jwt.Header.Alg);
if (!string.IsNullOrWhiteSpace(jwt.Issuer))
var jwtObjectHeader = JsonNode.Parse(header);
if (jwtObjectHeader is null)
{
Console.WriteLine(" Issuer: " + jwt.Issuer);
return;
}

var algorithm = jwtObjectHeader["alg"];
if (algorithm is not null)
{
Console.WriteLine(" Algorithm: " + algorithm);
}

var jwtObjectPayload = JsonNode.Parse(payload);
if (jwtObjectPayload is null)
{
return;
}

if (jwt.IssuedAt != DateTime.MinValue)
var issuer = jwtObjectPayload["iss"];
if (issuer is not null)
{
Console.WriteLine(" Issued at: " + jwt.IssuedAt);
Console.WriteLine(" Issuer: " + issuer);
}

if (!string.IsNullOrWhiteSpace(jwt.Id))
var issuedAt = jwtObjectPayload["iat"];
if (issuedAt is not null)
{
Console.WriteLine(" Id: " + jwt.Id);
var issueValue = Convert.ToInt64(issuedAt.ToString());
Console.WriteLine(" Issued at: " + DateTimeOffset.FromUnixTimeSeconds(issueValue).DateTime);
}

if (jwt.Audiences.Any())
var id = jwtObjectPayload["id"];
if (id is not null)
{
Console.WriteLine(" Audience: " + string.Join(", ", jwt.Audiences));
Console.WriteLine(" Id: " + id);
}

if (!string.IsNullOrWhiteSpace(jwt.Subject))
var audience = jwtObjectPayload["aud"];
if (audience is not null)
{
Console.WriteLine(" Subject: " + jwt.Subject);
Console.WriteLine(" Audience: " + audience);
}

if (!string.IsNullOrWhiteSpace(jwt.Actor))
var subject = jwtObjectPayload["sub"];
if (subject is not null)
{
Console.WriteLine(" Actor: " + jwt.Actor);
Console.WriteLine(" Subject: " + subject);
}

if (jwt.ValidTo != DateTime.MinValue)
var expirationAt = jwtObjectPayload["exp"];
if (expirationAt is not null)
{
Console.WriteLine(" Expiration: " + jwt.ValidTo);
var expirationValue = Convert.ToInt64(expirationAt.ToString());
Console.WriteLine(" Expiration: " + DateTimeOffset.FromUnixTimeSeconds(expirationValue).DateTime);
}
}
}
6 changes: 6 additions & 0 deletions src/nHash/SubFeatures/Texts/JsonFeature.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace nHash.SubFeatures.Texts;

public class JsonFeature
{

}
4 changes: 1 addition & 3 deletions src/nHash/nHash.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<Version>1.3</Version>
<Version>1.3.2</Version>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latestmajor</LangVersion>
Expand All @@ -16,7 +16,5 @@
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="MlkPwgen" Version="0.3.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.27.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>
</Project>

0 comments on commit 64a4ab1

Please sign in to comment.