Skip to content

Commit

Permalink
General code rafactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
abatishchev committed May 19, 2015
1 parent d716f5f commit 549db01
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions JWT/JWT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ public enum JwtHashAlgorithm
/// </summary>
public static class JsonWebToken
{
private static Dictionary<JwtHashAlgorithm, Func<byte[], byte[], byte[]>> HashAlgorithms;
private static readonly IDictionary<JwtHashAlgorithm, Func<byte[], byte[], byte[]>> HashAlgorithms;

/// <summary>
/// Pluggable JSON Serializer
/// </summary>
public static IJsonSerializer JsonSerializer = new DefaultJsonSerializer();
/// </summary>
public static readonly IJsonSerializer JsonSerializer = new DefaultJsonSerializer();

private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

static JsonWebToken()
{
Expand All @@ -47,8 +49,8 @@ public static string Encode(IDictionary<string, object> extraHeaders, object pay
var segments = new List<string>();
var header = new Dictionary<string, object>(extraHeaders)
{
{"typ", "JWT"},
{"alg", algorithm.ToString()}
{ "typ", "JWT" },
{ "alg", algorithm.ToString() }
};

byte[] headerBytes = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(header));
Expand Down Expand Up @@ -121,7 +123,7 @@ public static string Decode(string token, byte[] key, bool verify = true)
}
var header = parts[0];
var payload = parts[1];
byte[] crypto = Base64UrlDecode(parts[2]);
var crypto = Base64UrlDecode(parts[2]);

var headerJson = Encoding.UTF8.GetString(Base64UrlDecode(header));
var payloadJson = Encoding.UTF8.GetString(Base64UrlDecode(payload));
Expand Down Expand Up @@ -154,7 +156,7 @@ public static string Decode(string token, byte[] key, bool verify = true)
throw new SignatureVerificationException("Claim 'exp' must be an integer.");
}

var secondsSinceEpoch = Math.Round((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds);
var secondsSinceEpoch = Math.Round((DateTime.UtcNow - UnixEpoch).TotalSeconds);
if (secondsSinceEpoch >= exp)
{
throw new SignatureVerificationException("Token has expired.");
Expand Down Expand Up @@ -188,7 +190,7 @@ public static string Decode(string token, string key, bool verify = true)
/// <exception cref="SignatureVerificationException">Thrown if the verify parameter was true and the signature was NOT valid or if the JWT was signed with an unsupported algorithm.</exception>
public static object DecodeToObject(string token, string key, bool verify = true)
{
var payloadJson = JsonWebToken.Decode(token, key, verify);
var payloadJson = Decode(token, key, verify);
var payloadData = JsonSerializer.Deserialize<Dictionary<string, object>>(payloadJson);
return payloadData;
}
Expand All @@ -204,7 +206,7 @@ public static object DecodeToObject(string token, string key, bool verify = true
/// <exception cref="SignatureVerificationException">Thrown if the verify parameter was true and the signature was NOT valid or if the JWT was signed with an unsupported algorithm.</exception>
public static T DecodeToObject<T>(string token, string key, bool verify = true)
{
var payloadJson = JsonWebToken.Decode(token, key, verify);
var payloadJson = Decode(token, key, verify);
var payloadData = JsonSerializer.Deserialize<T>(payloadJson);
return payloadData;
}
Expand Down Expand Up @@ -240,8 +242,8 @@ public static byte[] Base64UrlDecode(string input)
{
case 0: break; // No pad chars in this case
case 2: output += "=="; break; // Two pad chars
case 3: output += "="; break; // One pad char
default: throw new System.Exception("Illegal base64url string!");
case 3: output += "="; break; // One pad char
default: throw new Exception("Illegal base64url string!");
}
var converted = Convert.FromBase64String(output); // Standard base64 decoder
return converted;
Expand Down

0 comments on commit 549db01

Please sign in to comment.