Skip to content

Commit bc9d9d2

Browse files
Made ctor of ValidationParameters public (#480)
* 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 <abatishchev@gmail.com>
1 parent 2c76ee0 commit bc9d9d2

4 files changed

Lines changed: 78 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Unreleased
22

3+
# 10.1.1
4+
5+
- Made ctor of ValidationParameters public, set default values for boolean properties to true
6+
37
# 10.1.0
48

59
- Unmarked HMAC SHA based algorithms as insecure and obsolete (was done in 9.0.0-beta4)

src/JWT/JWT.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
</PropertyGroup>
2626

2727
<PropertyGroup>
28-
<Version>10.1.0</Version>
28+
<Version>10.1.1</Version>
2929
<FileVersion>10.0.0.0</FileVersion>
3030
<AssemblyVersion>10.0.0.0</AssemblyVersion>
3131
</PropertyGroup>

src/JWT/ValidationParameters.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,28 @@ namespace JWT
88
public class ValidationParameters
99
{
1010
/// <remarks>
11+
/// By default, all peroperties are set to <c>true</c> to ensure validation is enabled.
1112
/// Use <see cref="Default"/> if you'd like to set all properties set to <see langword="true" />
1213
/// or use <see cref="None"/> if you'd like to set all properties set to <see langword="false" />.
1314
/// </remarks>>
14-
private ValidationParameters()
15+
public ValidationParameters()
1516
{
1617
}
1718

1819
/// <summary>
1920
/// Gets or sets whether to validate the validity of the token's signature.
2021
/// </summary>
21-
public bool ValidateSignature { get; set; }
22+
public bool ValidateSignature { get; set; } = true;
2223

2324
/// <summary>
2425
/// Gets or sets whether to validate the validity of the token's expiration time.
2526
/// </summary>
26-
public bool ValidateExpirationTime { get; set; }
27+
public bool ValidateExpirationTime { get; set; } = true;
2728

2829
/// <summary>
2930
/// Gets or sets whether to validate the validity of the token's issued time.
3031
/// </summary>
31-
public bool ValidateIssuedTime { get; set; }
32+
public bool ValidateIssuedTime { get; set; } = true;
3233

3334
/// <summary>
3435
/// Gets or sets the time margin in seconds for exp and nbf during token validation.
@@ -38,13 +39,7 @@ private ValidationParameters()
3839
/// <summary>
3940
/// Returns a <see cref="ValidationParameters" /> with all properties set to <see langword="true" />.
4041
/// </summary>
41-
public static ValidationParameters Default => new ValidationParameters
42-
{
43-
ValidateSignature = true,
44-
ValidateExpirationTime = true,
45-
ValidateIssuedTime = true,
46-
TimeMargin = 0
47-
};
42+
public static ValidationParameters Default => new ValidationParameters();
4843

4944
/// <summary>
5045
/// Returns a <see cref="ValidationParameters" /> with all properties set to <see langword="false" />.

tests/JWT.Tests.Common/JwtValidatorTests.cs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,73 @@ public void TryValidate_Should_Return_True_And_Exception_Null_When_Token_Is_Not_
378378
.BeTrue("because token should be valid");
379379

380380
}
381-
381+
382+
[TestMethod]
383+
public void ValidationParameters_Ctor_Should_Set_All_Validation_To_True()
384+
{
385+
var valParams = new ValidationParameters();
386+
387+
valParams.ValidateSignature.Should()
388+
.BeTrue("because ValidationParameters constructor should set ValidateSignature to true");
389+
valParams.ValidateExpirationTime.Should()
390+
.BeTrue("because ValidationParameters constructor should set ValidateExpirationTime to true");
391+
valParams.ValidateIssuedTime.Should()
392+
.BeTrue("because ValidationParameters constructor should set ValidateIssuedTime to true");
393+
valParams.TimeMargin.Should()
394+
.Be(0, "because ValidationParameters constructor should set TimeMargin to 0");
395+
}
396+
397+
[TestMethod]
398+
public void ValidationParameters_Ctor_Should_Allow_Default_Values_To_Be_Overriden()
399+
{
400+
var valParams = new ValidationParameters
401+
{
402+
ValidateSignature = false,
403+
ValidateExpirationTime = false,
404+
ValidateIssuedTime = false,
405+
TimeMargin = 300
406+
};
407+
408+
valParams.ValidateSignature.Should()
409+
.BeFalse("because ValidationParameters constructor should allow ValidateSignature to be overridden");
410+
valParams.ValidateExpirationTime.Should()
411+
.BeFalse("because ValidationParameters constructor should allow ValidateExpirationTime to be overridden");
412+
valParams.ValidateIssuedTime.Should()
413+
.BeFalse("because ValidationParameters constructor should allow ValidateIssuedTime to be overridden");
414+
valParams.TimeMargin.Should()
415+
.Be(300, "because ValidationParameters constructor should allow TimeMargin to be overridden");
416+
}
417+
418+
[TestMethod]
419+
public void ValidationParameters_Default_Should_Set_All_Validation_To_True()
420+
{
421+
var valParams = ValidationParameters.Default;
422+
423+
valParams.ValidateSignature.Should()
424+
.BeTrue("because ValidationParameters.Default should set ValidateSignature to true");
425+
valParams.ValidateExpirationTime.Should()
426+
.BeTrue("because ValidationParameters.Default should set ValidateExpirationTime to true");
427+
valParams.ValidateIssuedTime.Should()
428+
.BeTrue("because ValidationParameters.Default should set ValidateIssuedTime to true");
429+
valParams.TimeMargin.Should()
430+
.Be(0, "because ValidationParameters.Default should set TimeMargin to 0");
431+
}
432+
433+
[TestMethod]
434+
public void ValidationParameters_None_Should_Set_All_Validation_To_False()
435+
{
436+
var valParams = ValidationParameters.None;
437+
438+
valParams.ValidateSignature.Should()
439+
.BeFalse("because ValidationParameters.None should set ValidateSignature to false");
440+
valParams.ValidateExpirationTime.Should()
441+
.BeFalse("because ValidationParameters.DefaNoneult should set ValidateExpirationTime to false");
442+
valParams.ValidateIssuedTime.Should()
443+
.BeFalse("because ValidationParameters.None should set ValidateIssuedTime to false");
444+
valParams.TimeMargin.Should()
445+
.Be(0, "because ValidationParameters.Default should set TimeMargin to 0");
446+
}
447+
382448
private static IJsonSerializer CreateSerializer() =>
383449
new DefaultJsonSerializerFactory().Create();
384450
}

0 commit comments

Comments
 (0)