|
| 1 | +#nullable enable |
| 2 | + |
| 3 | +using System.Security.Claims; |
| 4 | +using System.Security.Principal; |
| 5 | +using Microsoft.AspNetCore.Authorization; |
| 6 | +using Microsoft.AspNetCore.Http; |
| 7 | + |
| 8 | +namespace GraphQL.Server.Transports.AspNetCore; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Authorization parameters. |
| 12 | +/// This struct is used to group all necessary parameters together and perform arbitrary |
| 13 | +/// actions based on provided authentication properties/attributes/etc. |
| 14 | +/// It is not intended to be called from user code. |
| 15 | +/// </summary> |
| 16 | +public readonly struct AuthorizationParameters<TState> |
| 17 | +{ |
| 18 | + /// <summary> |
| 19 | + /// Initializes an instance with a specified <see cref="Microsoft.AspNetCore.Http.HttpContext"/> |
| 20 | + /// and parameters copied from the specified instance of <see cref="GraphQLHttpMiddlewareOptions"/>. |
| 21 | + /// </summary> |
| 22 | + public AuthorizationParameters( |
| 23 | + HttpContext httpContext, |
| 24 | + GraphQLHttpMiddlewareOptions middlewareOptions, |
| 25 | + Func<TState, Task>? onNotAuthenticated, |
| 26 | + Func<TState, Task>? onNotAuthorizedRole, |
| 27 | + Func<TState, AuthorizationResult, Task>? onNotAuthorizedPolicy) |
| 28 | + { |
| 29 | + HttpContext = httpContext; |
| 30 | + AuthorizationRequired = middlewareOptions.AuthorizationRequired; |
| 31 | + AuthorizedRoles = middlewareOptions.AuthorizedRoles; |
| 32 | + AuthorizedPolicy = middlewareOptions.AuthorizedPolicy; |
| 33 | + OnNotAuthenticated = onNotAuthenticated; |
| 34 | + OnNotAuthorizedRole = onNotAuthorizedRole; |
| 35 | + OnNotAuthorizedPolicy = onNotAuthorizedPolicy; |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Gets or sets the <see cref="Microsoft.AspNetCore.Http.HttpContext"/> for the request. |
| 40 | + /// </summary> |
| 41 | + public HttpContext HttpContext { get; } |
| 42 | + |
| 43 | + /// <inheritdoc cref="GraphQLHttpMiddlewareOptions.AuthorizationRequired"/> |
| 44 | + public bool AuthorizationRequired { get; } |
| 45 | + |
| 46 | + /// <inheritdoc cref="GraphQLHttpMiddlewareOptions.AuthorizedRoles"/> |
| 47 | + public List<string>? AuthorizedRoles { get; } |
| 48 | + |
| 49 | + /// <inheritdoc cref="GraphQLHttpMiddlewareOptions.AuthorizedPolicy"/> |
| 50 | + public string? AuthorizedPolicy { get; } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// A delegate which executes if <see cref="AuthorizationRequired"/> is set |
| 54 | + /// but <see cref="IIdentity.IsAuthenticated"/> returns <see langword="false"/>. |
| 55 | + /// </summary> |
| 56 | + public Func<TState, Task>? OnNotAuthenticated { get; } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// A delegate which executes if <see cref="AuthorizedRoles"/> is set but |
| 60 | + /// <see cref="ClaimsPrincipal.IsInRole(string)"/> returns <see langword="false"/> |
| 61 | + /// for all roles. |
| 62 | + /// </summary> |
| 63 | + public Func<TState, Task>? OnNotAuthorizedRole { get; } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// A delegate which executes if <see cref="AuthorizedPolicy"/> is set but |
| 67 | + /// <see cref="IAuthorizationService.AuthorizeAsync(ClaimsPrincipal, object, string)"/> |
| 68 | + /// returns an unsuccessful <see cref="AuthorizationResult"/> for the specified policy. |
| 69 | + /// </summary> |
| 70 | + public Func<TState, AuthorizationResult, Task>? OnNotAuthorizedPolicy { get; } |
| 71 | +} |
0 commit comments