-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix JWT stub compatibility with IdentityWeb (#183)
* Fix JWT stub compatibility with IdentityWeb * Version bump --------- Co-authored-by: JT <[email protected]>
- Loading branch information
Showing
7 changed files
with
84 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Alba.Security; | ||
using Microsoft.AspNetCore.Authentication.JwtBearer; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.IdentityModel.JsonWebTokens; | ||
using Microsoft.IdentityModel.Protocols; | ||
using Microsoft.IdentityModel.Protocols.OpenIdConnect; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace Alba.Testing.Security; | ||
|
@@ -39,7 +45,7 @@ await host.Scenario(s => | |
public async Task can_stub_individual_scheme_jwt() | ||
{ | ||
// This is a Alba extension that can "stub" out authentication | ||
var securityStub = new JwtSecurityStub("custom") | ||
var securityStub = new JwtSecurityStub(JwtBearerDefaults.AuthenticationScheme) | ||
.With("foo", "bar") | ||
.With(JwtRegisteredClaimNames.Email, "[email protected]") | ||
.WithName("jeremy"); | ||
|
@@ -49,15 +55,61 @@ public async Task can_stub_individual_scheme_jwt() | |
|
||
await host.Scenario(s => | ||
{ | ||
s.Get.Url("/identity2"); | ||
s.Get.Url("/identity"); | ||
s.StatusCodeShouldBeOk(); | ||
}); | ||
|
||
await host.Scenario(s => | ||
{ | ||
s.Get.Url("/identity"); | ||
s.Get.Url("/identity2"); | ||
s.StatusCodeShouldBe(HttpStatusCode.Unauthorized); | ||
}); | ||
|
||
} | ||
|
||
[Fact] | ||
public async Task can_stub_individual_schemes_microsoft_identity_web() | ||
{ | ||
var securityStub1 = new AuthenticationStub("custom") | ||
.With("foo", "bar") | ||
.With(JwtRegisteredClaimNames.Email, "[email protected]") | ||
.WithName("jeremy"); | ||
|
||
var securityStub2 = new JwtSecurityStub(JwtBearerDefaults.AuthenticationScheme) | ||
.With("foo", "bar") | ||
.With(JwtRegisteredClaimNames.Email, "[email protected]") | ||
.WithName("jeremy"); | ||
|
||
var securityStub3 = new JwtSecurityStub("AzureAuthentication") | ||
.With("iss", "bar") | ||
.With("tid", "tenantid") | ||
.With("roles", "") | ||
.With(JwtRegisteredClaimNames.Email, "[email protected]") | ||
.WithName("jeremy"); | ||
|
||
// We're calling your real web service's configuration | ||
await using var host = await AlbaHost.For<WebAppSecuredWithJwt.Program>(securityStub1, securityStub2, securityStub3); | ||
var postConfigures = host.Services.GetRequiredService<IOptionsMonitor<JwtBearerOptions>>().Get("AzureAuthentication"); | ||
|
||
postConfigures.ConfigurationManager.ShouldBeOfType<StaticConfigurationManager<OpenIdConnectConfiguration>>(); | ||
|
||
await host.Scenario(s => | ||
{ | ||
s.Get.Url("/identity"); | ||
s.StatusCodeShouldBeOk(); | ||
}); | ||
|
||
await host.Scenario(s => | ||
{ | ||
s.Get.Url("/identity2"); | ||
s.StatusCodeShouldBeOk(); | ||
}); | ||
|
||
await host.Scenario(s => | ||
{ | ||
s.Get.Url("/identity3"); | ||
s.StatusCodeShouldBeOk(); | ||
}); | ||
|
||
} | ||
} |
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
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
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
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
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
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