-
-
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.
Add support for stubbing a specific authentication scheme (#168)
* Add support for stubbing a specific authentication scheme * Update docs
- Loading branch information
Showing
13 changed files
with
1,742 additions
and
894 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,25 @@ the JWT tokens with a real Open Id Connect server **so you can test your service | |
The `JwtSecurityStub` will also honor the `WithClaim()` method to add additional claims on a scenario by scenario basis | ||
as shown in the previous section. | ||
|
||
## Override a specific scheme | ||
|
||
Both `AuthenticationSecurityStub` and `JwtSecuritySnub` will replace all authentication schemes by default. If you only want a single scheme to be replaced, | ||
you can pass the scheme name via the constructor: | ||
|
||
<!-- snippet: sample_bootstrapping_with_stub_scheme_extension --> | ||
<a id='snippet-sample_bootstrapping_with_stub_scheme_extension'></a> | ||
```cs | ||
// Stub out an individual scheme | ||
var securityStub = new AuthenticationStub("custom") | ||
.With("foo", "bar") | ||
.With(JwtRegisteredClaimNames.Email, "[email protected]") | ||
.WithName("jeremy"); | ||
|
||
await using var host = await AlbaHost.For<WebAppSecuredWithJwt.Program>(securityStub); | ||
``` | ||
<sup><a href='https://github.com/JasperFx/alba/blob/master/src/Alba.Testing/Security/web_api_authentication_with_individual_stub.cs#L14-L22' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_bootstrapping_with_stub_scheme_extension' title='Start of snippet'>anchor</a></sup> | ||
<!-- endSnippet --> | ||
|
||
## Integration with JWT Authentication | ||
|
||
::: tip | ||
|
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
63 changes: 63 additions & 0 deletions
63
src/Alba.Testing/Security/web_api_authentication_with_individual_stub.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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Alba.Security; | ||
using Microsoft.IdentityModel.JsonWebTokens; | ||
using Xunit; | ||
|
||
namespace Alba.Testing.Security; | ||
|
||
public class web_api_authentication_with_individual_stub | ||
{ | ||
[Fact] | ||
public async Task can_stub_individual_scheme() | ||
{ | ||
#region sample_bootstrapping_with_stub_scheme_extension | ||
// Stub out an individual scheme | ||
var securityStub = new AuthenticationStub("custom") | ||
.With("foo", "bar") | ||
.With(JwtRegisteredClaimNames.Email, "[email protected]") | ||
.WithName("jeremy"); | ||
|
||
await using var host = await AlbaHost.For<WebAppSecuredWithJwt.Program>(securityStub); | ||
#endregion | ||
|
||
await host.Scenario(s => | ||
{ | ||
s.Get.Url("/identity2"); | ||
s.StatusCodeShouldBeOk(); | ||
}); | ||
|
||
await host.Scenario(s => | ||
{ | ||
s.Get.Url("/identity"); | ||
s.StatusCodeShouldBe(HttpStatusCode.Unauthorized); | ||
}); | ||
|
||
} | ||
|
||
[Fact] | ||
public async Task can_stub_individual_scheme_jwt() | ||
{ | ||
// This is a Alba extension that can "stub" out authentication | ||
var securityStub = new JwtSecurityStub("custom") | ||
.With("foo", "bar") | ||
.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>(securityStub); | ||
|
||
await host.Scenario(s => | ||
{ | ||
s.Get.Url("/identity2"); | ||
s.StatusCodeShouldBeOk(); | ||
}); | ||
|
||
await host.Scenario(s => | ||
{ | ||
s.Get.Url("/identity"); | ||
s.StatusCodeShouldBe(HttpStatusCode.Unauthorized); | ||
}); | ||
|
||
} | ||
} |
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 |
---|---|---|
|
@@ -111,7 +111,6 @@ public async Task can_modify_claims_per_scenario() | |
} | ||
|
||
#endregion | ||
|
||
|
||
} | ||
} |
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,52 +1,101 @@ | ||
using System; | ||
using System; | ||
using System.Security.Claims; | ||
using System.Text.Encodings.Web; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Alba.Security; | ||
|
||
/// <summary> | ||
/// Stubs out security in all Alba scenarios to always authenticate | ||
/// a user on each request with the configured claims | ||
/// </summary> | ||
public class AuthenticationStub : AuthenticationExtensionBase, IAlbaExtension | ||
public sealed class AuthenticationStub : AuthenticationExtensionBase, IAlbaExtension | ||
{ | ||
private const string TestSchemaName = "Test"; | ||
|
||
internal string? OverrideSchemeTargetName { get; } | ||
|
||
/// <summary> | ||
/// Creates a new authentication stub. Will override all implementations by default. | ||
/// </summary> | ||
/// <param name="overrideSchemeTargetName">Override a specific authentication schema.</param> | ||
public AuthenticationStub(string? overrideSchemeTargetName = null) | ||
=> OverrideSchemeTargetName = overrideSchemeTargetName; | ||
|
||
void IDisposable.Dispose() | ||
{ | ||
// nothing to dispose | ||
} | ||
|
||
ValueTask IAsyncDisposable.DisposeAsync() | ||
{ | ||
return ValueTask.CompletedTask; | ||
} | ||
ValueTask IAsyncDisposable.DisposeAsync() => ValueTask.CompletedTask; | ||
|
||
Task IAlbaExtension.Start(IAlbaHost host) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
Task IAlbaExtension.Start(IAlbaHost host) => Task.CompletedTask; | ||
|
||
IHostBuilder IAlbaExtension.Configure(IHostBuilder builder) | ||
{ | ||
return builder.ConfigureServices(services => | ||
{ | ||
services.AddHttpContextAccessor(); | ||
services.AddSingleton(this); | ||
services.AddAuthentication("Test") | ||
.AddScheme<AuthenticationSchemeOptions, TestAuthHandler>( | ||
"Test", _ => {}); | ||
services.AddTransient<IAuthenticationSchemeProvider, MockSchemeProvider>(); | ||
}); | ||
} | ||
|
||
internal ClaimsPrincipal BuildPrincipal(HttpContext context) | ||
{ | ||
var claims = allClaims(context); | ||
var identity = new ClaimsIdentity(claims, "Test"); | ||
|
||
var identity = new ClaimsIdentity(claims, TestSchemaName); | ||
var principal = new ClaimsPrincipal(identity); | ||
|
||
return principal; | ||
} | ||
|
||
private sealed class MockSchemeProvider : AuthenticationSchemeProvider | ||
{ | ||
private readonly string? _overrideSchemaTarget; | ||
|
||
public MockSchemeProvider(AuthenticationStub authSchemaStub, IOptions<AuthenticationOptions> options) | ||
: base(options) | ||
{ | ||
_overrideSchemaTarget = authSchemaStub.OverrideSchemeTargetName; | ||
} | ||
|
||
public override Task<AuthenticationScheme?> GetSchemeAsync(string name) | ||
{ | ||
if(_overrideSchemaTarget == null) | ||
return Task.FromResult(new AuthenticationScheme( | ||
TestSchemaName, | ||
TestSchemaName, | ||
typeof(MockAuthenticationHandler)))!; | ||
if (name.Equals(_overrideSchemaTarget, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
var scheme = new AuthenticationScheme( | ||
TestSchemaName, | ||
TestSchemaName, | ||
typeof(MockAuthenticationHandler)); | ||
|
||
return Task.FromResult(scheme)!; | ||
} | ||
|
||
return base.GetSchemeAsync(name); | ||
} | ||
|
||
private sealed class MockAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions> | ||
{ | ||
private readonly AuthenticationStub _authenticationSchemaStub; | ||
|
||
|
||
public MockAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, AuthenticationStub authenticationSchemaStub) : base(options, logger, encoder) | ||
{ | ||
_authenticationSchemaStub = authenticationSchemaStub; | ||
} | ||
|
||
protected override Task<AuthenticateResult> HandleAuthenticateAsync() | ||
{ | ||
var principal = _authenticationSchemaStub.BuildPrincipal(Context); | ||
var ticket = new AuthenticationTicket(principal, TestSchemaName); | ||
return Task.FromResult(AuthenticateResult.Success(ticket)); | ||
} | ||
} | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.