Skip to content

Commit

Permalink
Fixed JwtBuilder.Encode() with custom factory (#409)
Browse files Browse the repository at this point in the history
* Fixed JwtBuilder.Encode() with custom factory
* Updated tests
* Bumped version to 10.0.0-beta2
  • Loading branch information
abatishchev authored Jun 9, 2022
1 parent 070389b commit 98619ef
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 29 deletions.
5 changes: 4 additions & 1 deletion src/JWT/Builder/JwtBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,10 @@ private void TryCreateEncoder()
if (_urlEncoder is null)
throw new InvalidOperationException($"Can't instantiate {nameof(JwtEncoder)}. Call {nameof(WithUrlEncoder)}.");

_encoder = new JwtEncoder(_algorithm, _serializer, _urlEncoder);
if (_algorithm is object)
_encoder = new JwtEncoder(_algorithm, _serializer, _urlEncoder);
else if (_algFactory is object)
_encoder = new JwtEncoder(_algFactory, _serializer, _urlEncoder);
}

private void TryCreateDecoder()
Expand Down
4 changes: 2 additions & 2 deletions src/JWT/JWT.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net6.0;net35;net40;net462;</TargetFrameworks>
Expand All @@ -20,7 +20,7 @@
<Authors>Alexander Batishchev, John Sheehan, Michael Lehenbauer</Authors>
<PackageTags>jwt;json;authorization</PackageTags>
<PackageLicense>CC0-1.0</PackageLicense>
<Version>10.0.0-beta1</Version>
<Version>10.0.0-beta2</Version>
<FileVersion>10.0.0.0</FileVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
<RootNamespace>JWT</RootNamespace>
Expand Down
73 changes: 47 additions & 26 deletions tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class JwtBuilderEncodeTests
private static readonly Fixture _fixture = new Fixture();

[TestMethod]
public void Encode_With_Secret_Should_Return_Token()
public void Encode_With_Secret_Should_Return_Valid_Token()
{
var algorithm = new HMACSHA256Algorithm();
var secret = _fixture.Create<string>();
Expand All @@ -36,19 +36,19 @@ public void Encode_With_Secret_Should_Return_Token()
}

[TestMethod]
public void Encode_With_Secret_And_Payload_Should_Return_Token()
public void Encode_With_Secret_And_Payload_Should_Return_Valid_Token()
{
var algorithm = new HMACSHA256Algorithm();

const ClaimName claimKey = ClaimName.ExpirationTime;
var claimValue = DateTime.UtcNow.AddHours(1)
.ToString(CultureInfo.InvariantCulture);
var claimValue = DateTime.UtcNow.AddHours(1).ToString(CultureInfo.InvariantCulture);
var secret = _fixture.Create<string>();

var token = JwtBuilder.Create()
.WithAlgorithm(algorithm)
.WithSecret(secret)
.AddClaim(claimValue, claimKey)
.Encode();
.WithAlgorithm(algorithm)
.WithSecret(secret)
.AddClaim(claimKey, claimValue)
.Encode();

token.Should()
.NotBeNullOrEmpty("because the token should contains some data");
Expand Down Expand Up @@ -78,13 +78,14 @@ public void Encode_With_PayloadWithClaims_Should_Return_Token()
}

var token = JwtBuilder.Create()
.WithAlgorithm(algorithm)
.WithSecret(secret)
.AddClaims(claims)
.Encode();
.WithAlgorithm(algorithm)
.WithSecret(secret)
.AddClaims(claims)
.Encode();

var decodedToken = new UTF8Encoding(false).GetString(
new JwtBase64UrlEncoder().Decode(token.Split('.')[1]));
var decodedToken = new UTF8Encoding(false)
.GetString(new JwtBase64UrlEncoder()
.Decode(token.Split('.')[1]));

token.Should()
.NotBeNullOrEmpty("because the token should contains some data");
Expand Down Expand Up @@ -175,37 +176,57 @@ public void Encode_WithoutAlgorithm_Should_Return_Token()
}

[TestMethod]
public void Encode_Should_Encode_To_Token_With_Extra_Headers()
public void Encode_Should_Return_Token_With_Extra_Headers()
{
const string key = TestData.Secret;

var actual = JwtBuilder.Create()
var token = JwtBuilder.Create()
.WithAlgorithm(new HMACSHA256Algorithm())
.WithSecret(key)
.AddHeader(HeaderName.KeyId, "42")
.AddClaim(nameof(TestData.Customer.FirstName), TestData.Customer.FirstName)
.AddClaim(nameof(TestData.Customer.Age), TestData.Customer.Age)
.Encode();

actual.Should()
.Be(TestData.TokenWithCustomTypeHeader2, "because the same data encoded with the same key must result in the same token");
token.Should()
.Be(TestData.TokenWithCustomTypeHeader2, "because the same data encoded with the same key must result in the same token");
}

[TestMethod]
public void Encode_Should_Encode_To_Token_With_Custom_Extra_Headers()
public void Encode_Should_Return_Token_With_Custom_Extra_Headers()
{
const string key = TestData.Secret;

var actual = JwtBuilder.Create()
.WithAlgorithm(new HMACSHA256Algorithm())
var token = JwtBuilder.Create()
.WithAlgorithm(new HMACSHA256Algorithm())
.WithSecret(key)
.AddHeader("version", 1)
.AddClaim(nameof(TestData.Customer.FirstName), TestData.Customer.FirstName)
.AddClaim(nameof(TestData.Customer.Age), TestData.Customer.Age)
.Encode();

token.Should()
.Be(TestData.TokenWithCustomTypeHeader3, "because the same data encoded with the same key must result in the same token");
}

[TestMethod]
public void Encode_With_Custom_Factory_Return_Token()
{
const string key = TestData.Secret;

var token = JwtBuilder.Create()
.WithSecret(key)
.AddHeader("version", 1)
.AddClaim(nameof(TestData.Customer.FirstName), TestData.Customer.FirstName)
.AddClaim(nameof(TestData.Customer.Age), TestData.Customer.Age)
.WithAlgorithmFactory(new CustomFactory())
.Encode();

actual.Should()
.Be(TestData.TokenWithCustomTypeHeader3, "because the same data encoded with the same key must result in the same token");
token.Should()
.NotBeNullOrEmpty("because the token should contains some data");
}

private sealed class CustomFactory : IAlgorithmFactory
{
public IJwtAlgorithm Create(JwtDecoderContext context) =>
new HMACSHA256Algorithm();
}
}
}

0 comments on commit 98619ef

Please sign in to comment.