From 1dd8f1709f85494f4f311aee0a4d875bfe434e5e Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Sat, 11 Jul 2015 12:53:38 -0700 Subject: [PATCH] Extracting SignatureVerificationException to separate file --- JWT/JWT.cs | 70 ++++++++++++--------------- JWT/JWT.csproj | 1 + JWT/SignatureVerificationException.cs | 12 +++++ 3 files changed, 44 insertions(+), 39 deletions(-) create mode 100644 JWT/SignatureVerificationException.cs diff --git a/JWT/JWT.cs b/JWT/JWT.cs index 56476951b..6f762b406 100755 --- a/JWT/JWT.cs +++ b/JWT/JWT.cs @@ -21,9 +21,9 @@ public static class JsonWebToken /// /// Pluggable JSON Serializer - /// - public static readonly IJsonSerializer JsonSerializer = new DefaultJsonSerializer(); - + /// + public static IJsonSerializer JsonSerializer = new DefaultJsonSerializer(); + private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); static JsonWebToken() @@ -145,34 +145,34 @@ public static string Decode(string token, byte[] key, bool verify = true) return payloadJson; } - private static void Verify(string decodedCrypto, string decodedSignature, string payloadJson) - { - if (decodedCrypto != decodedSignature) - { - throw new SignatureVerificationException(string.Format("Invalid signature. Expected {0} got {1}", decodedCrypto, decodedSignature)); - } - - // verify exp claim https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-32#section-4.1.4 - var payloadData = JsonSerializer.Deserialize>(payloadJson); - if (payloadData.ContainsKey("exp") && payloadData["exp"] != null) - { - // safely unpack a boxed int - int exp; - try - { - exp = Convert.ToInt32(payloadData["exp"]); - } - catch (Exception) - { - throw new SignatureVerificationException("Claim 'exp' must be an integer."); - } - - var secondsSinceEpoch = Math.Round((DateTime.UtcNow - UnixEpoch).TotalSeconds); - if (secondsSinceEpoch >= exp) - { - throw new SignatureVerificationException("Token has expired."); - } - } + private static void Verify(string decodedCrypto, string decodedSignature, string payloadJson) + { + if (decodedCrypto != decodedSignature) + { + throw new SignatureVerificationException(string.Format("Invalid signature. Expected {0} got {1}", decodedCrypto, decodedSignature)); + } + + // verify exp claim https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-32#section-4.1.4 + var payloadData = JsonSerializer.Deserialize>(payloadJson); + if (payloadData.ContainsKey("exp") && payloadData["exp"] != null) + { + // safely unpack a boxed int + int exp; + try + { + exp = Convert.ToInt32(payloadData["exp"]); + } + catch (Exception) + { + throw new SignatureVerificationException("Claim 'exp' must be an integer."); + } + + var secondsSinceEpoch = Math.Round((DateTime.UtcNow - UnixEpoch).TotalSeconds); + if (secondsSinceEpoch >= exp) + { + throw new SignatureVerificationException("Token has expired."); + } + } } /// @@ -284,12 +284,4 @@ public static byte[] Base64UrlDecode(string input) return converted; } } - - public class SignatureVerificationException : Exception - { - public SignatureVerificationException(string message) - : base(message) - { - } - } } diff --git a/JWT/JWT.csproj b/JWT/JWT.csproj index 8ec8ebe14..ecdb738fb 100644 --- a/JWT/JWT.csproj +++ b/JWT/JWT.csproj @@ -42,6 +42,7 @@ +