-
-
Notifications
You must be signed in to change notification settings - Fork 748
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
16 changed files
with
199 additions
and
35 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
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
17 changes: 17 additions & 0 deletions
17
...hocolate/AspNetCore/src/AspNetCore.Authorization.Opa/Result/DefaultPolicyResultHandler.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,17 @@ | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace HotChocolate.AspNetCore.Authorization; | ||
|
||
public class DefaultPolicyResultHandler : PolicyResultHandlerBase<QueryResponse<bool?>, ResponseBase> | ||
{ | ||
public DefaultPolicyResultHandler(IOptions<OpaOptions> options) : base(options) { } | ||
protected override Task<ResponseBase> ProcessAsync(PolicyResultContext<QueryResponse<bool?>> context) | ||
{ | ||
return Task.FromResult<ResponseBase>(context.Result switch | ||
{ | ||
{ Result: null } when context.PolicyPath.Equals(string.Empty) => NoDefaultPolicy.Response, | ||
{ Result: null } => PolicyNotFound.Response, | ||
_ => new QueryResponse<bool?> { Result = false } | ||
}); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ocolate/AspNetCore/src/AspNetCore.Authorization.Opa/Result/DelegatePolicyResultHandler.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,11 @@ | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace HotChocolate.AspNetCore.Authorization; | ||
|
||
public class DelegatePolicyResultHandler<T, TOutput> : PolicyResultHandlerBase<T, TOutput> | ||
where TOutput : ResponseBase | ||
{ | ||
private readonly Func<PolicyResultContext<T>, Task<TOutput>> _process; | ||
public DelegatePolicyResultHandler(Func<PolicyResultContext<T>, Task<TOutput>> process, IOptions<OpaOptions> options) : base(options) => _process = process; | ||
protected override Task<TOutput> ProcessAsync(PolicyResultContext<T> context) => _process(context); | ||
} |
8 changes: 8 additions & 0 deletions
8
src/HotChocolate/AspNetCore/src/AspNetCore.Authorization.Opa/Result/IPolicyResultHandler.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,8 @@ | ||
using HotChocolate.Resolvers; | ||
|
||
namespace HotChocolate.AspNetCore.Authorization; | ||
|
||
public interface IPolicyResultHandler | ||
{ | ||
Task<ResponseBase?> HandleAsync(string policyPath, HttpResponseMessage response, IMiddlewareContext context); | ||
} |
16 changes: 16 additions & 0 deletions
16
src/HotChocolate/AspNetCore/src/AspNetCore.Authorization.Opa/Result/PolicyResultContext.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,16 @@ | ||
using HotChocolate.Resolvers; | ||
|
||
namespace HotChocolate.AspNetCore.Authorization; | ||
|
||
public class PolicyResultContext<T> | ||
{ | ||
public PolicyResultContext(string policyPath, T? result, IMiddlewareContext context) | ||
{ | ||
PolicyPath = policyPath; | ||
Result = result; | ||
MiddlewareContext = context; | ||
} | ||
public string PolicyPath { get; } | ||
public T? Result { get; } | ||
public IMiddlewareContext MiddlewareContext { get; } | ||
} |
21 changes: 21 additions & 0 deletions
21
...otChocolate/AspNetCore/src/AspNetCore.Authorization.Opa/Result/PolicyResultHandlerBase.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,21 @@ | ||
using HotChocolate.Resolvers; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace HotChocolate.AspNetCore.Authorization; | ||
|
||
public abstract class PolicyResultHandlerBase<T, TOutput> : IPolicyResultHandler | ||
where TOutput : ResponseBase | ||
{ | ||
private readonly IOptions<OpaOptions> _options; | ||
protected PolicyResultHandlerBase(IOptions<OpaOptions> options) => _options = options; | ||
protected abstract Task<TOutput> ProcessAsync(PolicyResultContext<T> context); | ||
|
||
public async Task<ResponseBase?> HandleAsync(string policyPath, HttpResponseMessage response, | ||
IMiddlewareContext context) | ||
{ | ||
QueryResponse<T?> responseObj = await response.Content | ||
.FromJsonAsync<QueryResponse<T?>>(_options.Value.JsonSerializerOptions, context.RequestAborted) | ||
.ConfigureAwait(false); | ||
return await ProcessAsync(new PolicyResultContext<T>(policyPath, responseObj.Result, context)); | ||
} | ||
} |
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