-
-
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
8 changed files
with
127 additions
and
89 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
44 changes: 44 additions & 0 deletions
44
...spNetCore/src/AspNetCore.Authorization.Opa/HotChocolateAuthorizeRequestExecutorBuilder.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,44 @@ | ||
using HotChocolate.AspNetCore.Authorization; | ||
using HotChocolate.Execution.Configuration; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
/// <summary> | ||
/// Provides extension methods for the GraphQL builder. | ||
/// </summary> | ||
public static class HotChocolateAuthorizeRequestExecutorBuilder | ||
{ | ||
/// <summary> | ||
/// Adds OPA authorization handler. | ||
/// </summary> | ||
/// <param name="builder"> | ||
/// The <see cref="IRequestExecutorBuilder"/>. | ||
/// </param> | ||
/// <param name="configure"> | ||
/// Configure <see cref="OpaOptions"/>. | ||
/// </param> | ||
/// <returns> | ||
/// Returns the <see cref="IRequestExecutorBuilder"/> for chaining in more configurations. | ||
/// </returns> | ||
public static IRequestExecutorBuilder AddOpaAuthorizationHandler( | ||
this IRequestExecutorBuilder builder, Action<IConfiguration, OpaOptions>? configure = null) | ||
{ | ||
builder.AddAuthorization(); | ||
builder.AddAuthorizationHandler<OpaAuthorizationHandler>(); | ||
builder.Services.AddSingleton<IOpaDecision, OpaDecision>(); | ||
builder.Services.AddHttpClient<OpaService>((f, c) => | ||
{ | ||
OpaOptions? options = f.GetRequiredService<OpaOptions>(); | ||
c.BaseAddress = options.BaseAddress; | ||
c.Timeout = options.ConnectionTimeout; | ||
}); | ||
builder.Services.AddSingleton(f => | ||
{ | ||
var options = new OpaOptions(); | ||
configure?.Invoke(f.GetRequiredService<IConfiguration>(), options); | ||
return options; | ||
}); | ||
return builder; | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
src/HotChocolate/AspNetCore/src/AspNetCore.Authorization.Opa/OpaJsonExtensions.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,28 @@ | ||
#if NET6_0 | ||
using System.Net.Http.Json; | ||
#endif | ||
using System.Text.Json; | ||
|
||
namespace HotChocolate.AspNetCore.Authorization; | ||
|
||
internal static class OpaJsonExtensions | ||
{ | ||
internal static HttpContent ToJsonContent(this QueryRequest request, JsonSerializerOptions options) | ||
{ | ||
#if NET6_0 | ||
return JsonContent.Create(request, options: options); | ||
#else | ||
var body = JsonSerializer.Serialize(request, options); | ||
return new StringContent(body, System.Text.Encoding.UTF8, "application/json"); | ||
#endif | ||
} | ||
|
||
internal static async Task<QueryResponse?> QueryResponseFromJsonAsync(this HttpContent content, JsonSerializerOptions options, CancellationToken token) | ||
{ | ||
#if NET6_0 | ||
return await content.ReadFromJsonAsync<QueryResponse>(options, token); | ||
#else | ||
return await JsonSerializer.DeserializeAsync<QueryResponse>(await content.ReadAsStreamAsync(), options, token); | ||
#endif | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/HotChocolate/AspNetCore/src/AspNetCore.Authorization.Opa/OpaService.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,23 @@ | ||
namespace HotChocolate.AspNetCore.Authorization; | ||
|
||
public sealed class OpaService : IOpaService | ||
{ | ||
private readonly HttpClient _httpClient; | ||
private readonly OpaOptions _options; | ||
|
||
public OpaService(HttpClient httpClient, OpaOptions options) | ||
{ | ||
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient)); | ||
_options = options ?? throw new ArgumentNullException(nameof(options)); | ||
} | ||
|
||
public async Task<QueryResponse?> QueryAsync(string policyPath, QueryRequest request, CancellationToken token) | ||
{ | ||
if (policyPath is null) throw new ArgumentNullException(nameof(policyPath)); | ||
if (request is null) throw new ArgumentNullException(nameof(request)); | ||
|
||
HttpResponseMessage? response = await _httpClient.PostAsync(policyPath, request.ToJsonContent(_options.JsonSerializerOptions), token); | ||
response.EnsureSuccessStatusCode(); | ||
return await response.Content.QueryResponseFromJsonAsync(_options.JsonSerializerOptions, token); | ||
} | ||
} |
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
45 changes: 0 additions & 45 deletions
45
...Chocolate/AspNetCore/test/AspNetCore.Authorization.Opa.Tests/AuthorizationHandlerTests.cs
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