Skip to content

Commit

Permalink
Custom typ header (#285)
Browse files Browse the repository at this point in the history
* Checking if 'typ' header is presented in extra headers before adding default value
* Update tests
* Bumped version to 7.2.1
  • Loading branch information
altafard authored May 21, 2020
1 parent 9e8a083 commit d9670e2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/JWT/JWT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<Authors>Alexander Batishchev, John Sheehan, Michael Lehenbauer</Authors>
<PackageTags>jwt;json</PackageTags>
<PackageLicense>CC0-1.0</PackageLicense>
<Version>7.2.0</Version>
<Version>7.2.1</Version>
<FileVersion>7.0.0.0</FileVersion>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<RootNamespace>JWT</RootNamespace>
Expand Down
10 changes: 7 additions & 3 deletions src/JWT/JwtEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ public string Encode(IDictionary<string, object> extraHeaders, object payload, b

var segments = new List<string>(3);

var header = extraHeaders is null ? new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase) : new Dictionary<string, object>(extraHeaders, StringComparer.OrdinalIgnoreCase);
header.Add("typ", "JWT");
var header = extraHeaders is null ?
new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase) :
new Dictionary<string, object>(extraHeaders, StringComparer.OrdinalIgnoreCase);

if (!header.ContainsKey("typ"))
header.Add("typ", "JWT");
header.Add("alg", _algorithm.Name);

var headerBytes = GetBytes(_jsonSerializer.Serialize(header));
Expand All @@ -58,4 +62,4 @@ public string Encode(IDictionary<string, object> extraHeaders, object payload, b
return String.Join(".", segments.ToArray());
}
}
}
}
22 changes: 22 additions & 0 deletions tests/JWT.Tests.Common/JwtEncoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,27 @@ public void Encode_Should_Encode_To_Token_With_Extra_Headers()
actual.Should()
.Be(expected, "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_Type_Headers()
{
var extraHeaders = new Dictionary<string, object>
{
{ "typ", "foo" }
};
const string key = TestData.Secret;
var toEncode = TestData.Customer;
const string expected = TestData.TokenWithCustomTypeHeader;

var algorithm = new HMACSHA256Algorithm();
var urlEncoder = new JwtBase64UrlEncoder();
var serializer = new JsonNetSerializer();
var encoder = new JwtEncoder(algorithm, serializer, urlEncoder);

var actual = encoder.Encode(extraHeaders, toEncode, key);

actual.Should()
.Be(expected, "because the same data encoded with the same key must result in the same token");
}
}
}
1 change: 1 addition & 0 deletions tests/JWT.Tests.Common/Models/TestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static class TestData

public const string Token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJGaXJzdE5hbWUiOiJKZXN1cyIsIkFnZSI6MzN9.jBdQNPhChZpZSMZX6Z5okc7YJ3dc5esWp4YCtasYXFU";
public const string TokenWithExtraHeaders = "eyJmb28iOiJiYXIiLCJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJGaXJzdE5hbWUiOiJKZXN1cyIsIkFnZSI6MzN9.QQJaPxDE6E7l-zC-LKTbEgPfId5FDvowRKww1o6jdwU";
public const string TokenWithCustomTypeHeader = "eyJ0eXAiOiJmb28iLCJhbGciOiJIUzI1NiJ9.eyJGaXJzdE5hbWUiOiJKZXN1cyIsIkFnZSI6MzN9.vubwuLxx_7AWGvo-Y8XF_l7XP1WOv5obJulIk3RlVdk";

public const string TokenWithIncorrectSignature = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJGaXJzdE5hbWUiOiJCb2IiLCJBZ2UiOjM3fQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";
public const string TokenWithoutHeader = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9eyJGaXJzdE5hbWUiOiJCb2IiLCJBZ2UiOjM3fQ.oj1ROhq6SyGDG3C0WIPe8wDuMJjA47uKwXCHkxl6Zy0";
Expand Down

0 comments on commit d9670e2

Please sign in to comment.