Skip to content

Commit

Permalink
Consistent implementation of ValidateExpClaim and ValidateNbfClaim (#397
Browse files Browse the repository at this point in the history
)

* Added TokenNotYetValidException
* Updated JwtValidator to throw it
* Updated tests
* Bumped version to 9.0.2

Co-authored-by: Alexander Batishchev <[email protected]>
  • Loading branch information
abrasat and abatishchev authored May 27, 2022
1 parent 7fc7908 commit 6d14b6d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
47 changes: 47 additions & 0 deletions src/JWT/Exceptions/TokenNotYetValidException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;

#if NET35 || NET40
using IReadOnlyPayloadDictionary = System.Collections.Generic.IDictionary<string, object>;
#else
using IReadOnlyPayloadDictionary = System.Collections.Generic.IReadOnlyDictionary<string, object>;
#endif

namespace JWT.Exceptions
{
/// <summary>
/// Represents an exception thrown when a token is not yet valid.
/// </summary>
public class TokenNotYetValidException : SignatureVerificationException
{
private const string PayloadDataKey = "PayloadData";
private const string NotBeforeKey = "NotBefore";

/// <summary>
/// Creates an instance of <see cref="TokenNotYetValidException" />
/// </summary>
/// <param name="message">The error message</param>
public TokenNotYetValidException(string message)
: base(message)
{
}

/// <summary>
/// The payload.
/// </summary>
public IReadOnlyPayloadDictionary PayloadData
{
get => GetOrDefault<Dictionary<string, object>>(PayloadDataKey);
internal set => this.Data.Add(PayloadDataKey, value);
}

/// <summary>
/// The not before DateTime of the token.
/// </summary>
public DateTime? NotBefore
{
get => GetOrDefault<DateTime?>(NotBeforeKey);
internal set => this.Data.Add(NotBeforeKey, value);
}
}
}
2 changes: 1 addition & 1 deletion src/JWT/JWT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<Authors>Alexander Batishchev, John Sheehan, Michael Lehenbauer</Authors>
<PackageTags>jwt;json;authorization</PackageTags>
<PackageLicense>CC0-1.0</PackageLicense>
<Version>9.0.1</Version>
<Version>9.0.2</Version>
<FileVersion>9.0.0.0</FileVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<RootNamespace>JWT</RootNamespace>
Expand Down
8 changes: 6 additions & 2 deletions src/JWT/JwtValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,14 @@ private Exception ValidateNbfClaim(IReadOnlyPayloadDictionary payloadData, doubl

if (secondsSinceEpoch + _valParams.TimeMargin < nbfValue)
{
return new SignatureVerificationException("Token is not yet valid.");
return new TokenNotYetValidException("Token is not yet valid.")
{
NotBefore = UnixEpoch.Value.AddSeconds(nbfValue),
PayloadData = payloadData
};
}

return null;
}
}
}
}
4 changes: 2 additions & 2 deletions tests/JWT.Tests.Common/JwtValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public void TryValidate_Should_Return_False_And_Exception_Not_Null_When_Token_Is

ex.Should()
.NotBeNull("because invalid token should thrown exception")
.And.BeOfType(typeof(SignatureVerificationException), "because not yet usable token should thrown SignatureVerificationException");
.And.BeOfType(typeof(TokenNotYetValidException), "because not yet usable token should thrown TokenNotYetValidException");
}

[TestMethod]
Expand Down Expand Up @@ -378,4 +378,4 @@ public void TryValidate_Should_Return_True_And_Exception_Null_When_Token_Is_Not_

}
}
}
}

0 comments on commit 6d14b6d

Please sign in to comment.