Skip to content

Commit

Permalink
Added method to add several claims (#210)
Browse files Browse the repository at this point in the history
* Added method to add several claims
* Added unit test for AddClaims method
* Bumped the minor version
  • Loading branch information
pBouillon authored and abatishchev committed Aug 17, 2019
1 parent aa3d71b commit a1eb810
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/JWT/Builder/JwtBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JWT.Algorithms;
using JWT.Serializers;
Expand Down Expand Up @@ -59,6 +60,20 @@ public JwtBuilder AddClaim(string name, object value)
public JwtBuilder AddClaim(string name, string value) =>
AddClaim(name, (object)value);

/// <summary>
/// Add several claims to the JWT
/// </summary>
/// <param name="claims">IDictionary of claims to be added</param>
/// <returns>Current builder instance</returns>
public JwtBuilder AddClaims(IDictionary<string, object> claims)
{
foreach (var entry in claims)
{
AddClaim(entry.Key, entry.Value);
}
return this;
}

/// <summary>
/// Adds well-known claim to the JWT.
/// </summary>
Expand Down
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 @@
<PackageProjectUrl>https://github.com/jwt-dotnet/jwt</PackageProjectUrl>
<Authors>Alexander Batishchev, John Sheehan, Michael Lehenbauer</Authors>
<PackageLicenseUrl>https://creativecommons.org/publicdomain/zero/1.0/</PackageLicenseUrl>
<Version>5.2.2</Version>
<Version>5.2.3</Version>
<PackageTags>jwt json</PackageTags>
<FileVersion>5.0.0.0</FileVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
Expand Down
22 changes: 21 additions & 1 deletion tests/JWT.Tests.Common/JwtBuilderEncodeTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using JWT.Algorithms;
Expand Down Expand Up @@ -34,6 +35,25 @@ public void Build_WithPayload()
Assert.True(decodedToken.Contains("exp") && decodedToken.Contains(testtime));
}

[Fact]
public void Build_WithPayloadWithClaims()
{
var token = new JwtBuilder()
.WithAlgorithm(new HMACSHA256Algorithm())
.WithSecret("gsdhjfkhdfjklhjklgfsdhgfbsdgfvsdvfghjdjfgb")
.AddClaims(new Dictionary<string, object>
{
{ "key-1", "value-1" },
{ "key-2", "value-2" }
})
.Build();
Assert.True(token.Length > 0 && token.Split('.').Length == 3);

var decodedToken = Encoding.UTF8.GetString(new JwtBase64UrlEncoder().Decode(token.Split('.')[1]));
Assert.True(decodedToken.Contains("key-1") && decodedToken.Contains("value-1"));
Assert.True(decodedToken.Contains("key-2") && decodedToken.Contains("value-2"));
}

[Fact]
public void Build_WithoutDependencies_Should_Throw_Exception()
{
Expand Down Expand Up @@ -71,4 +91,4 @@ public void Build_WithMultipleSecrets_Should_Throw_Exception()
.Build());
}
}
}
}

0 comments on commit a1eb810

Please sign in to comment.