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

Adds JsonWebToken Constructor that accepts Memory<char> #2458

Merged
merged 44 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
f5017ab
Add JWT ctor that accepts a span
Jan 24, 2024
01cf389
Update YAML to build previews
Jan 25, 2024
c7744a0
Add ReadOnlySpan overloads
Jan 27, 2024
594a95a
Add benchmark
Jan 29, 2024
af1d85e
Add ValidateAndGetOutputSize for spans
Jan 29, 2024
308943f
Remove duplication
Jan 30, 2024
3c187b0
Update benchmark
Jan 31, 2024
4c26b84
Remove duplicates
Jan 31, 2024
b545908
Update
Jan 31, 2024
ca13536
Add benchmarks
Jan 31, 2024
9419c44
Update
Feb 1, 2024
6b3f8db
Replace ReadyOnlySpan with ReadOnlyMemory
Feb 2, 2024
b9aacb0
Update test
Feb 2, 2024
e3c9e99
Update benchmark naming
Feb 2, 2024
abdd893
Move ArrayPool to JsonWebToken level
Feb 2, 2024
add4daa
Update CreateHeaderClaimSet
Feb 2, 2024
1b917c3
Update benchmark for JWE to resolve error
Feb 5, 2024
5c9304b
Merge branch 'dev' into sruthi/JsonWebTokenSpan
Feb 5, 2024
d9d9644
Reduce duplication
Feb 7, 2024
40fa97d
Fix comment
Feb 7, 2024
bde27eb
Set _encodedTokenMemory as readonly
Feb 7, 2024
6bea783
Remove unnecessary private method
Feb 7, 2024
2f06e7f
Use Base64UrlEncoding.Decode with action
Feb 9, 2024
eea955e
Use ValidateAndGetOutputSize(ReadOnlySpan<char> strSpan..) thoughout
Feb 9, 2024
095fe77
Add tests
Feb 10, 2024
7423282
Revert change
Feb 10, 2024
c78454e
Temporarily allow publishing to NuGet
Feb 10, 2024
222216a
use just `1` for preview
jennyf19 Feb 10, 2024
6d6298c
up version to 7.4.0 for preview
jennyf19 Feb 10, 2024
d574485
Merge with dev
Feb 13, 2024
3dea6b2
Separate JWS and JWE benchmarks
Feb 13, 2024
8338b9e
Merge dev into branch
Feb 23, 2024
9d427f3
Re-add variable
Feb 23, 2024
9c644bc
Update test/Microsoft.IdentityModel.JsonWebTokens.Tests/JsonWebTokenT…
sruke Feb 24, 2024
ee24470
Update src/Microsoft.IdentityModel.JsonWebTokens/JsonWebToken.cs
sruke Feb 24, 2024
a2b40e9
Update test/Microsoft.IdentityModel.Tokens.Tests/Base64UrlEncodingTes…
sruke Feb 24, 2024
135f3d3
Removed private method
Feb 24, 2024
5416f0a
Store ReadOnlySpan is a variable
Feb 24, 2024
dabdc8c
Update benchmark
Feb 24, 2024
94a4281
Merge branch 'dev' into sruthi/JsonWebTokenSpan
Feb 24, 2024
d6c089d
Cleanup
Feb 24, 2024
e1d6bc6
Fix CreateHeaderClaimSet and CreatePayloadClaimSet to avoid reading t…
jennyf19 Feb 25, 2024
2beabaa
Add comment
Feb 25, 2024
e5bdcd0
Update benchmark
Feb 25, 2024
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
4 changes: 4 additions & 0 deletions benchmark/Microsoft.IdentityModel.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public static void Main(string[] args)
}
private static void DebugThroughTests()
{
ReadJWETokenTests readTokenTests = new ReadJWETokenTests();
readTokenTests.Setup();
readTokenTests.ReadJWE_FromMemory();

AsymmetricAdapterSignatures asymmetricAdapter = new AsymmetricAdapterSignatures();
asymmetricAdapter.Setup();
asymmetricAdapter.SignDotnetCreatingBufferRSA();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using BenchmarkDotNet.Attributes;
using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.Tokens;

namespace Microsoft.IdentityModel.Benchmarks
{
// dotnet run -c release -f net8.0 --filter Microsoft.IdentityModel.Benchmarks.ReadTokenTests*

public class ReadJWETokenTests
jennyf19 marked this conversation as resolved.
Show resolved Hide resolved
{
string _encryptedJWE;
ReadOnlyMemory<char> _encryptedJWEAsMemory;

[GlobalSetup]
public void Setup()
{
var jsonWebTokenHandler = new JsonWebTokenHandler();
var jweTokenDescriptor = new SecurityTokenDescriptor
{
SigningCredentials = BenchmarkUtils.SigningCredentialsRsaSha256,
EncryptingCredentials = BenchmarkUtils.EncryptingCredentialsAes256Sha512,
TokenType = JwtHeaderParameterNames.Jwk,
Claims = BenchmarkUtils.Claims
};

_encryptedJWE = jsonWebTokenHandler.CreateToken(jweTokenDescriptor);
_encryptedJWEAsMemory = _encryptedJWE.AsMemory();
}

[Benchmark]
public JsonWebToken ReadJWE_FromString()
{
return new JsonWebToken(_encryptedJWE);
}

[Benchmark]
public JsonWebToken ReadJWE_FromMemory()
{
return new JsonWebToken(_encryptedJWEAsMemory);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using BenchmarkDotNet.Attributes;
using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.Tokens;

namespace Microsoft.IdentityModel.Benchmarks
{
// dotnet run -c release -f net8.0 --filter Microsoft.IdentityModel.Benchmarks.ReadJWSTokenTests*

public class ReadJWSTokenTests
{
string _encodedJWS;
ReadOnlyMemory<char> _encodedJWSAsMemory;

[GlobalSetup]
public void Setup()
{
var jsonWebTokenHandler = new JsonWebTokenHandler();
var jwsTokenDescriptor = new SecurityTokenDescriptor
{
SigningCredentials = BenchmarkUtils.SigningCredentialsRsaSha256,
TokenType = JwtHeaderParameterNames.Jwk,
Claims = BenchmarkUtils.Claims
};

_encodedJWS = jsonWebTokenHandler.CreateToken(jwsTokenDescriptor);
_encodedJWSAsMemory = _encodedJWS.AsMemory();
}

[Benchmark]
public JsonWebToken ReadJWS_FromString()
{
return new JsonWebToken(_encodedJWS);
}

[Benchmark]
pmaytak marked this conversation as resolved.
Show resolved Hide resolved
public JsonWebToken ReadJWS_FromMemory()
{
return new JsonWebToken(_encodedJWSAsMemory);
}
}
}
9 changes: 9 additions & 0 deletions build/releaseBuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ jobs:
BuildDropPath: '$(Build.SourcesDirectory)\src'
ManifestDirPath: '$(Build.SourcesDirectory)\artifacts'

- task: NuGetCommand@2
sruke marked this conversation as resolved.
Show resolved Hide resolved
sruke marked this conversation as resolved.
Show resolved Hide resolved
displayName: 'Upload NuGet Package to VSTS NuGet'
condition: eq(variables['NugetPackageType'], 'preview')
inputs:
command: push
packagesToPush: '$(Build.Repository.LocalPath)\artifacts\*.nupkg'
publishVstsFeed: '46419298-b96c-437f-bd4c-12c8df7f868d'
allowPackageConflicts: true

- task: PublishBuildArtifacts@1
displayName: 'Publish NuGet Package Artifact'
inputs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ public partial class JsonWebToken
{
internal JsonClaimSet CreateHeaderClaimSet(byte[] bytes)
{
return CreateHeaderClaimSet(bytes, bytes.Length);
return CreateHeaderClaimSet(bytes.AsSpan());
}

internal JsonClaimSet CreateHeaderClaimSet(byte[] bytes, int length)
{
Utf8JsonReader reader = new(bytes.AsSpan().Slice(0, length));
return CreateHeaderClaimSet(bytes.AsSpan(0, length));
}

internal JsonClaimSet CreateHeaderClaimSet(ReadOnlySpan<byte> byteSpan)
{
Utf8JsonReader reader = new(byteSpan);
if (!JsonSerializerPrimitives.IsReaderAtTokenType(ref reader, JsonTokenType.StartObject, true))
throw LogHelper.LogExceptionMessage(
new JsonException(
Expand Down Expand Up @@ -73,7 +78,7 @@ internal JsonClaimSet CreateHeaderClaimSet(byte[] bytes, int length)
}
}
// We read a JsonTokenType.StartObject above, exiting and positioning reader at next token.
else if (JsonSerializerPrimitives.IsReaderAtTokenType(ref reader, JsonTokenType.EndObject, true))
else if (JsonSerializerPrimitives.IsReaderAtTokenType(ref reader, JsonTokenType.EndObject, false))
break;
else if (!reader.Read())
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ public partial class JsonWebToken
{
internal JsonClaimSet CreatePayloadClaimSet(byte[] bytes, int length)
pmaytak marked this conversation as resolved.
Show resolved Hide resolved
{
Utf8JsonReader reader = new(bytes.AsSpan().Slice(0, length));
return CreatePayloadClaimSet(bytes.AsSpan(0, length));
}

internal JsonClaimSet CreatePayloadClaimSet(ReadOnlySpan<byte> byteSpan)
{
Utf8JsonReader reader = new(byteSpan);
if (!JsonSerializerPrimitives.IsReaderAtTokenType(ref reader, JsonTokenType.StartObject, true))
throw LogHelper.LogExceptionMessage(
new JsonException(
Expand All @@ -27,14 +32,14 @@ internal JsonClaimSet CreatePayloadClaimSet(byte[] bytes, int length)
LogHelper.MarkAsNonPII(reader.CurrentDepth),
LogHelper.MarkAsNonPII(reader.BytesConsumed))));

Dictionary<string, object> claims = new();
Dictionary<string, object> claims = [];
while (true)
{
if (reader.TokenType == JsonTokenType.PropertyName)
{
if (reader.ValueTextEquals(JwtPayloadUtf8Bytes.Aud))
{
_audiences = new List<string>();
_audiences = [];
reader.Read();
if (reader.TokenType == JsonTokenType.StartArray)
{
Expand Down Expand Up @@ -99,7 +104,7 @@ internal JsonClaimSet CreatePayloadClaimSet(byte[] bytes, int length)
}
}
// We read a JsonTokenType.StartObject above, exiting and positioning reader at next token.
else if (JsonSerializerPrimitives.IsReaderAtTokenType(ref reader, JsonTokenType.EndObject, true))
else if (JsonSerializerPrimitives.IsReaderAtTokenType(ref reader, JsonTokenType.EndObject, false))
break;
else if (!reader.Read())
break;
Expand Down
Loading
Loading