-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
6 changed files
with
89 additions
and
10 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
5 changes: 0 additions & 5 deletions
5
TeachingRecordSystem/src/TeachingRecordSystem.Api/appsettings.Testing.json
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
61 changes: 61 additions & 0 deletions
61
...System/tests/TeachingRecordSystem.Api.Tests/Infrastructure/Security/TestAuthentication.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,61 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Text.Encodings.Web; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.Extensions.Options; | ||
using TeachingRecordSystem.Api.Infrastructure.Security; | ||
|
||
namespace TeachingRecordSystem.Api.Tests.Infrastructure.Security; | ||
|
||
public class TestAuthenticationHandler : AuthenticationHandler<TestAuthenticationOptions> | ||
{ | ||
private readonly CurrentApiClientProvider _currentApiClientProvider; | ||
|
||
public TestAuthenticationHandler( | ||
CurrentApiClientProvider currentApiClientProvider, | ||
IOptionsMonitor<TestAuthenticationOptions> options, | ||
ILoggerFactory logger, | ||
UrlEncoder encoder, | ||
ISystemClock clock) : | ||
base(options, logger, encoder, clock) | ||
{ | ||
_currentApiClientProvider = currentApiClientProvider; | ||
} | ||
|
||
protected override Task<AuthenticateResult> HandleAuthenticateAsync() | ||
{ | ||
if (!Request.Headers.TryGetValue("X-Use-CurrentClientIdProvider", out var useCurrentClientIdProvider) || | ||
useCurrentClientIdProvider != "true") | ||
{ | ||
return Task.FromResult(AuthenticateResult.NoResult()); | ||
} | ||
|
||
var currentApiClientId = _currentApiClientProvider.CurrentApiClientId; | ||
|
||
if (currentApiClientId is not null) | ||
{ | ||
var principal = ApiKeyAuthenticationHandler.CreatePrincipal(currentApiClientId); | ||
|
||
var ticket = new AuthenticationTicket(principal, Scheme.Name); | ||
|
||
return Task.FromResult(AuthenticateResult.Success(ticket)); | ||
} | ||
else | ||
{ | ||
return Task.FromResult(AuthenticateResult.NoResult()); | ||
} | ||
} | ||
} | ||
|
||
public class TestAuthenticationOptions : AuthenticationSchemeOptions { } | ||
|
||
public class CurrentApiClientProvider | ||
{ | ||
private readonly AsyncLocal<string> _currentApiClientId = new(); | ||
|
||
[DisallowNull] | ||
public string? CurrentApiClientId | ||
{ | ||
get => _currentApiClientId.Value; | ||
set => _currentApiClientId.Value = value; | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/xunit.runner.json
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,4 @@ | ||
{ | ||
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json", | ||
"parallelizeTestCollections": false | ||
} |