Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add payloadData and expiration to the TokenExpiredException exception #63

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/JWT/JWT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,11 @@ public static void Verify(string payloadJson, string decodedCrypto, string decod
var secondsSinceEpoch = Math.Round((DateTime.UtcNow - UnixEpoch).TotalSeconds);
if (secondsSinceEpoch >= expInt)
{
throw new TokenExpiredException("Token has expired.");
throw new TokenExpiredException("Token has expired.")
{
Expiration = UnixEpoch.AddSeconds(expInt),
PayloadData = payloadData
};
}
}

Expand Down
25 changes: 24 additions & 1 deletion src/JWT/TokenExpiredException.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
using System;
using System.Collections.Generic;

namespace JWT
{
public class TokenExpiredException : Exception
{
private const string PayloadDataKey = "PayloadData";
private const string ExpirationKey = "Expiration";

public TokenExpiredException(string message)
: base(message)
: base(message)
{
}

public Dictionary<string, object> PayloadData
{
get { return GetOrDefault<Dictionary<string, object>>(PayloadDataKey); }
internal set { Data.Add(PayloadDataKey, value); }
}

public DateTime? Expiration
{
get { return GetOrDefault<DateTime?>(ExpirationKey); }
internal set { Data.Add(ExpirationKey, value); }
}

private T GetOrDefault<T>(string key)
{
if (Data.Contains(key))
return (T)Data[key];
return default(T);
}
}
}