-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
5 changed files
with
394 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
83 changes: 83 additions & 0 deletions
83
Modules/Authentication/Multi/MultiAuthenticationConcern.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,83 @@ | ||
using GenHTTP.Api.Content; | ||
using GenHTTP.Api.Protocol; | ||
|
||
namespace GenHTTP.Modules.Authentication.Multi; | ||
|
||
public sealed class MultiAuthenticationConcern : IConcern | ||
{ | ||
#region Get-/Setters | ||
|
||
public IHandler Content { get; } | ||
|
||
private readonly IConcern[] _delegatingConcerns; | ||
|
||
#endregion | ||
|
||
#region Initialization | ||
|
||
public MultiAuthenticationConcern(IHandler content, IConcern[] delegatingConcerns) | ||
{ | ||
Content = content; | ||
_delegatingConcerns = delegatingConcerns; | ||
} | ||
|
||
#endregion | ||
|
||
#region Functionality | ||
|
||
public async ValueTask<IResponse?> HandleAsync(IRequest request) | ||
{ | ||
ResponseOrException? lastResponse = null; | ||
foreach (var concern in _delegatingConcerns) | ||
{ | ||
lastResponse?.Dispose(); | ||
|
||
try | ||
{ | ||
lastResponse = new(await concern.HandleAsync(request)); | ||
} | ||
catch (ProviderException e) | ||
{ | ||
lastResponse = new(Exception: e); | ||
} | ||
|
||
if (lastResponse.Status != ResponseStatus.Unauthorized) | ||
{ | ||
return lastResponse.Get(); | ||
} | ||
} | ||
|
||
return lastResponse?.Get() ?? | ||
request.Respond() | ||
.Status(ResponseStatus.Unauthorized) | ||
.Build(); | ||
} | ||
|
||
public ValueTask PrepareAsync() => Content.PrepareAsync(); | ||
|
||
#endregion | ||
|
||
#region Helper structure | ||
|
||
private sealed record ResponseOrException(IResponse? Response = null, ProviderException? Exception = null) : IDisposable | ||
{ | ||
public IResponse? Get() | ||
{ | ||
if (Exception != null) | ||
{ | ||
throw Exception; | ||
} | ||
|
||
return Response; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Response?.Dispose(); | ||
} | ||
|
||
public ResponseStatus? Status => Exception?.Status ?? Response?.Status.KnownStatus; | ||
} | ||
|
||
#endregion | ||
} |
88 changes: 88 additions & 0 deletions
88
Modules/Authentication/Multi/MultiAuthenticationConcernBuilder.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,88 @@ | ||
using GenHTTP.Api.Content; | ||
using GenHTTP.Api.Infrastructure; | ||
using GenHTTP.Modules.Authentication.ApiKey; | ||
using GenHTTP.Modules.Authentication.Basic; | ||
using GenHTTP.Modules.Authentication.Bearer; | ||
using GenHTTP.Modules.Authentication.ClientCertificate; | ||
|
||
namespace GenHTTP.Modules.Authentication.Multi; | ||
|
||
/// <summary> | ||
/// Builder for creating a multi-concern authentication handler. | ||
/// </summary> | ||
public sealed class MultiAuthenticationConcernBuilder : IConcernBuilder | ||
{ | ||
#region Fields | ||
|
||
private readonly List<IConcernBuilder> _delegatingConcernsBuilders = []; | ||
|
||
#endregion | ||
|
||
#region Private add functionality | ||
|
||
private MultiAuthenticationConcernBuilder AddIfNotNull(IConcernBuilder concernBuilder) | ||
{ | ||
if (concernBuilder == null) return this; | ||
|
||
_delegatingConcernsBuilders.Add(concernBuilder); | ||
return this; | ||
} | ||
|
||
#endregion | ||
|
||
#region Functionality | ||
|
||
/// <summary> | ||
/// Add nested API concern builder to this multi concern. | ||
/// </summary> | ||
/// <param name="apiKeyBuilder">Nested API key concern builder</param> | ||
public MultiAuthenticationConcernBuilder Add(ApiKeyConcernBuilder apiKeyBuilder) | ||
=> AddIfNotNull(apiKeyBuilder); | ||
|
||
/// <summary> | ||
/// Add nested API concern builder to this multi concern. | ||
/// </summary> | ||
/// <param name="basicBuilder">Nested Basic concern builder</param> | ||
public MultiAuthenticationConcernBuilder Add(BasicAuthenticationConcernBuilder basicBuilder) | ||
=> AddIfNotNull(basicBuilder); | ||
|
||
/// <summary> | ||
/// Add nested API concern builder to this multi concern. | ||
/// </summary> | ||
/// <param name="basicKnownUsersBuilder">Nested Basic Known Users concern builder</param> | ||
public MultiAuthenticationConcernBuilder Add(BasicAuthenticationKnownUsersBuilder basicKnownUsersBuilder) | ||
=> AddIfNotNull(basicKnownUsersBuilder); | ||
|
||
/// <summary> | ||
/// Add nested API concern builder to this multi concern. | ||
/// </summary> | ||
/// <param name="bearerBuilder">Nested Bearer concern builder</param> | ||
public MultiAuthenticationConcernBuilder Add(BearerAuthenticationConcernBuilder bearerBuilder) | ||
=> AddIfNotNull(bearerBuilder); | ||
|
||
/// <summary> | ||
/// Add nested API concern builder to this multi concern. | ||
/// </summary> | ||
/// <param name="clientCertificateBuilder">Nested Client Certificate concern builder</param> | ||
public MultiAuthenticationConcernBuilder Add(ClientCertificateAuthenticationBuilder clientCertificateBuilder) | ||
=> AddIfNotNull(clientCertificateBuilder); | ||
|
||
/// <summary> | ||
/// Construct the multi concern. | ||
/// </summary> | ||
/// <param name="content"></param> | ||
/// <returns></returns> | ||
public IConcern Build(IHandler content) | ||
{ | ||
var delegatingConcerns = _delegatingConcernsBuilders.Select(x => x.Build(content)).ToArray(); | ||
if (delegatingConcerns.Length == 0) throw new BuilderMissingPropertyException("Concerns"); | ||
|
||
return new MultiAuthenticationConcern( | ||
content, | ||
delegatingConcerns | ||
); | ||
} | ||
|
||
#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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using GenHTTP.Modules.Authentication.Multi; | ||
|
||
namespace GenHTTP.Modules.Authentication; | ||
|
||
public static class MultiAuthentication | ||
{ | ||
#region Builder | ||
|
||
/// <summary> | ||
/// Creates a authentication handler that will use | ||
/// underlying handlers to authenticate the request. | ||
/// </summary> | ||
public static MultiAuthenticationConcernBuilder Create() => new(); | ||
|
||
#endregion | ||
} |
Oops, something went wrong.