Skip to content

Commit

Permalink
Merge branch 'Zagitta-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Devin Rader committed May 26, 2015
2 parents d716f5f + 6398313 commit f032ce8
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions JWT/JWT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,26 @@ public static string Decode(string token, string key, bool verify = true)
/// <param name="verify">Whether to verify the signature (default is true).</param>
/// <returns>An object representing the payload.</returns>
/// <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)
public static object DecodeToObject(string token, byte[] key, bool verify = true)
{
var payloadJson = JsonWebToken.Decode(token, key, verify);
var payloadData = JsonSerializer.Deserialize<Dictionary<string, object>>(payloadJson);
return payloadData;
}

/// <summary>
/// Given a JWT, decode it and return the payload as an object (by deserializing it with <see cref="System.Web.Script.Serialization.JavaScriptSerializer"/>).
/// </summary>
/// <param name="token">The JWT.</param>
/// <param name="key">The key that was used to sign the JWT.</param>
/// <param name="verify">Whether to verify the signature (default is true).</param>
/// <returns>An object representing the payload.</returns>
/// <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)
{
return DecodeToObject(token, Encoding.UTF8.GetBytes(key), verify);
}

/// <summary>
/// Given a JWT, decode it and return the payload as an object (by deserializing it with <see cref="System.Web.Script.Serialization.JavaScriptSerializer"/>).
/// </summary>
Expand All @@ -202,13 +215,27 @@ public static object DecodeToObject(string token, string key, bool verify = true
/// <param name="verify">Whether to verify the signature (default is true).</param>
/// <returns>An object representing the payload.</returns>
/// <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)
public static T DecodeToObject<T>(string token, byte[] key, bool verify = true)
{
var payloadJson = JsonWebToken.Decode(token, key, verify);
var payloadData = JsonSerializer.Deserialize<T>(payloadJson);
return payloadData;
}

/// <summary>
/// Given a JWT, decode it and return the payload as an object (by deserializing it with <see cref="System.Web.Script.Serialization.JavaScriptSerializer"/>).
/// </summary>
/// <typeparam name="T">The <see cref="Type"/> to return</typeparam>
/// <param name="token">The JWT.</param>
/// <param name="key">The key that was used to sign the JWT.</param>
/// <param name="verify">Whether to verify the signature (default is true).</param>
/// <returns>An object representing the payload.</returns>
/// <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)
{
return DecodeToObject<T>(token, Encoding.UTF8.GetBytes(key), verify);
}

private static JwtHashAlgorithm GetHashAlgorithm(string algorithm)
{
switch (algorithm)
Expand Down

0 comments on commit f032ce8

Please sign in to comment.