Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonWebToken exposes Header Claims #3170

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Microsoft.IdentityModel.JsonWebTokens/JsonWebToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,15 @@ public virtual IEnumerable<Claim> Claims
}
}

/// <summary>
/// Gets an <see cref="IReadOnlyList{Claim}"/> where each claim in the JWT header { name, value } is returned as a <see cref="Claim"/>.
/// </summary>
/// <remarks>
/// A <see cref="Claim"/> requires each value to be represented as a string. If the value was not a string, then <see cref="Claim.Type"/> contains the json type.
/// <see cref="JsonClaimValueTypes"/> and <see cref="ClaimValueTypes"/> to determine the json type.
/// </remarks>
public IReadOnlyList<Claim> HeaderClaims => Header.Claims(Issuer ?? ClaimsIdentity.DefaultIssuer);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatives: Only expose header names, and then call GetHeaderValue(key) with each key from the header names, or reparse the encoded header


/// <summary>
/// Gets a <see cref="Claim"/> representing the { key, 'value' } pair corresponding to the provided <paramref name="key"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Microsoft.IdentityModel.JsonWebTokens.JsonWebToken.HeaderClaims.get -> System.Collections.Generic.IReadOnlyList<System.Security.Claims.Claim>
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ public class JsonWebTokenTests
new Claim("dateTimeIso8061", dateTime.ToUniversalTime().ToString("o", CultureInfo.InvariantCulture), ClaimValueTypes.DateTime, "LOCAL AUTHORITY", "LOCAL AUTHORITY"),
};

[Fact]
public void JsonWebToken_HeaderClaims_ReturnsExpectedClaims()
{
var jsonWebTokenHandler = new JsonWebTokenHandler();
var jsonWebTokenString = jsonWebTokenHandler.CreateToken(Default.PayloadString, KeyingMaterial.JsonWebKeyRsa256SigningCredentials);
var jsonWebToken = new JsonWebToken(jsonWebTokenString);
var claims = jsonWebToken.HeaderClaims;

// Header should have three default claims:
Assert.Equal(3, claims.Count);
Assert.Contains(claims, c => c.Type == "alg");
Assert.Contains(claims, c => c.Type == "typ");
Assert.Contains(claims, c => c.Type == "kid");
}

[Fact]
public void ByteArrayClaimsEncodedAsExpected()
{
Expand Down
Loading