Skip to content

Commit

Permalink
Made ctor of ValidationParameters public (#480)
Browse files Browse the repository at this point in the history
* Made ctor of ValidationParameters public
* Set default values for boolean properties to true
* Update tests
* Bumped version to 10.1.1
* Update CHANGELOG.md

---------

Co-authored-by: Alexander Batishchev <[email protected]>
  • Loading branch information
sourcevelocity-andy and abatishchev authored Sep 27, 2023
1 parent 2c76ee0 commit bc9d9d2
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

# 10.1.1

- Made ctor of ValidationParameters public, set default values for boolean properties to true

# 10.1.0

- Unmarked HMAC SHA based algorithms as insecure and obsolete (was done in 9.0.0-beta4)
Expand Down
2 changes: 1 addition & 1 deletion src/JWT/JWT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</PropertyGroup>

<PropertyGroup>
<Version>10.1.0</Version>
<Version>10.1.1</Version>
<FileVersion>10.0.0.0</FileVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</PropertyGroup>
Expand Down
17 changes: 6 additions & 11 deletions src/JWT/ValidationParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,28 @@ namespace JWT
public class ValidationParameters
{
/// <remarks>
/// By default, all peroperties are set to <c>true</c> to ensure validation is enabled.
/// Use <see cref="Default"/> if you'd like to set all properties set to <see langword="true" />
/// or use <see cref="None"/> if you'd like to set all properties set to <see langword="false" />.
/// </remarks>>
private ValidationParameters()
public ValidationParameters()
{
}

/// <summary>
/// Gets or sets whether to validate the validity of the token's signature.
/// </summary>
public bool ValidateSignature { get; set; }
public bool ValidateSignature { get; set; } = true;

/// <summary>
/// Gets or sets whether to validate the validity of the token's expiration time.
/// </summary>
public bool ValidateExpirationTime { get; set; }
public bool ValidateExpirationTime { get; set; } = true;

/// <summary>
/// Gets or sets whether to validate the validity of the token's issued time.
/// </summary>
public bool ValidateIssuedTime { get; set; }
public bool ValidateIssuedTime { get; set; } = true;

/// <summary>
/// Gets or sets the time margin in seconds for exp and nbf during token validation.
Expand All @@ -38,13 +39,7 @@ private ValidationParameters()
/// <summary>
/// Returns a <see cref="ValidationParameters" /> with all properties set to <see langword="true" />.
/// </summary>
public static ValidationParameters Default => new ValidationParameters
{
ValidateSignature = true,
ValidateExpirationTime = true,
ValidateIssuedTime = true,
TimeMargin = 0
};
public static ValidationParameters Default => new ValidationParameters();

/// <summary>
/// Returns a <see cref="ValidationParameters" /> with all properties set to <see langword="false" />.
Expand Down
68 changes: 67 additions & 1 deletion tests/JWT.Tests.Common/JwtValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,73 @@ public void TryValidate_Should_Return_True_And_Exception_Null_When_Token_Is_Not_
.BeTrue("because token should be valid");

}


[TestMethod]
public void ValidationParameters_Ctor_Should_Set_All_Validation_To_True()
{
var valParams = new ValidationParameters();

valParams.ValidateSignature.Should()
.BeTrue("because ValidationParameters constructor should set ValidateSignature to true");
valParams.ValidateExpirationTime.Should()
.BeTrue("because ValidationParameters constructor should set ValidateExpirationTime to true");
valParams.ValidateIssuedTime.Should()
.BeTrue("because ValidationParameters constructor should set ValidateIssuedTime to true");
valParams.TimeMargin.Should()
.Be(0, "because ValidationParameters constructor should set TimeMargin to 0");
}

[TestMethod]
public void ValidationParameters_Ctor_Should_Allow_Default_Values_To_Be_Overriden()
{
var valParams = new ValidationParameters
{
ValidateSignature = false,
ValidateExpirationTime = false,
ValidateIssuedTime = false,
TimeMargin = 300
};

valParams.ValidateSignature.Should()
.BeFalse("because ValidationParameters constructor should allow ValidateSignature to be overridden");
valParams.ValidateExpirationTime.Should()
.BeFalse("because ValidationParameters constructor should allow ValidateExpirationTime to be overridden");
valParams.ValidateIssuedTime.Should()
.BeFalse("because ValidationParameters constructor should allow ValidateIssuedTime to be overridden");
valParams.TimeMargin.Should()
.Be(300, "because ValidationParameters constructor should allow TimeMargin to be overridden");
}

[TestMethod]
public void ValidationParameters_Default_Should_Set_All_Validation_To_True()
{
var valParams = ValidationParameters.Default;

valParams.ValidateSignature.Should()
.BeTrue("because ValidationParameters.Default should set ValidateSignature to true");
valParams.ValidateExpirationTime.Should()
.BeTrue("because ValidationParameters.Default should set ValidateExpirationTime to true");
valParams.ValidateIssuedTime.Should()
.BeTrue("because ValidationParameters.Default should set ValidateIssuedTime to true");
valParams.TimeMargin.Should()
.Be(0, "because ValidationParameters.Default should set TimeMargin to 0");
}

[TestMethod]
public void ValidationParameters_None_Should_Set_All_Validation_To_False()
{
var valParams = ValidationParameters.None;

valParams.ValidateSignature.Should()
.BeFalse("because ValidationParameters.None should set ValidateSignature to false");
valParams.ValidateExpirationTime.Should()
.BeFalse("because ValidationParameters.DefaNoneult should set ValidateExpirationTime to false");
valParams.ValidateIssuedTime.Should()
.BeFalse("because ValidationParameters.None should set ValidateIssuedTime to false");
valParams.TimeMargin.Should()
.Be(0, "because ValidationParameters.Default should set TimeMargin to 0");
}

private static IJsonSerializer CreateSerializer() =>
new DefaultJsonSerializerFactory().Create();
}
Expand Down

0 comments on commit bc9d9d2

Please sign in to comment.