-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2936014
commit 664bc73
Showing
2 changed files
with
85 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 84 additions & 2 deletions
86
test/ScottBrady.IdentityModel.Tests/Tokens/JwtBearerHandlerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,88 @@ | ||
namespace ScottBrady.IdentityModel.Tests.Tokens; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Authentication.JwtBearer; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Extensions.WebEncoders.Testing; | ||
using Microsoft.IdentityModel.JsonWebTokens; | ||
using Microsoft.IdentityModel.Tokens; | ||
using Moq; | ||
using ScottBrady.IdentityModel.Crypto; | ||
using ScottBrady.IdentityModel.Tokens; | ||
using Xunit; | ||
|
||
namespace ScottBrady.IdentityModel.Tests.Tokens; | ||
|
||
public class JwtBearerHandlerTests | ||
{ | ||
private readonly Mock<IOptionsMonitor<JwtBearerOptions>> mockOptionsMonitor; | ||
private readonly TestJwtBearerHandler sut; | ||
|
||
public JwtBearerHandlerTests() | ||
{ | ||
mockOptionsMonitor = new Mock<IOptionsMonitor<JwtBearerOptions>>(); | ||
sut = new TestJwtBearerHandler(mockOptionsMonitor.Object); | ||
|
||
} | ||
|
||
[Fact] | ||
public async Task HandleAuthenticateAsync_WhenValidEdDsaToken_ExpectSuccessResult() | ||
{ | ||
const string issuer = "https://localhost"; | ||
const string audience = "api1"; | ||
var key = EdDsa.Create(ExtendedSecurityAlgorithms.Curves.Ed25519); | ||
|
||
var handler = new JsonWebTokenHandler(); | ||
var token = handler.CreateToken(new SecurityTokenDescriptor | ||
{ | ||
Issuer = issuer, | ||
Audience = audience, | ||
Expires = DateTime.UtcNow.AddMinutes(5), | ||
Claims = new Dictionary<string, object> { { "sub", "123" } }, | ||
SigningCredentials = new SigningCredentials(new EdDsaSecurityKey(key), ExtendedSecurityAlgorithms.EdDsa) | ||
}); | ||
|
||
const string scheme = "test"; | ||
var options = new JwtBearerOptions | ||
{ | ||
TokenValidationParameters = | ||
{ | ||
IssuerSigningKey = new EdDsaSecurityKey(key), | ||
ValidIssuer = issuer, | ||
ValidAudience = audience | ||
} | ||
}; | ||
|
||
mockOptionsMonitor | ||
.Setup(x => x.Get(scheme)) | ||
.Returns(options); | ||
|
||
var context = new DefaultHttpContext(); | ||
context.Request.Headers.Authorization = "Bearer " + token; | ||
|
||
await sut.InitializeAsync(new AuthenticationScheme(scheme, scheme, typeof(TestJwtBearerHandler)), context); | ||
|
||
var authenticationResult = await sut.HandleAuthenticateAsync(); | ||
|
||
authenticationResult.Succeeded.Should().BeTrue(); | ||
} | ||
} | ||
|
||
#pragma warning disable CS0618 // Type or member is obsolete | ||
public class TestJwtBearerHandler : JwtBearerHandler | ||
{ | ||
public TestJwtBearerHandler(IOptionsMonitor<JwtBearerOptions> options) | ||
: base(options, new NullLoggerFactory(), new UrlTestEncoder(), new SystemClock()) | ||
{ | ||
} | ||
|
||
} | ||
public new Task<AuthenticateResult> HandleAuthenticateAsync() | ||
{ | ||
return base.HandleAuthenticateAsync(); | ||
} | ||
} | ||
#pragma warning restore CS0618 // Type or member is obsolete |