-
Notifications
You must be signed in to change notification settings - Fork 0
Api/Auth: Adds AuthorizationHandler and refactor authcontrollers #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 Ignis.Auth; | ||
|
|
||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Mvc; | ||
|
|
||
| namespace Ignis.Api.Controllers; | ||
|
|
||
| [ApiController] | ||
| public class AuthorizationController(AuthorizationHandler handler) : ControllerBase | ||
| { | ||
| /// <summary>Exchange credentials for an access token (OAuth 2.0 client_credentials grant).</summary> | ||
| [HttpPost("~/connect/token")] | ||
| [ProducesResponseType(typeof(object), StatusCodes.Status200OK, "application/json")] | ||
| [ProducesResponseType(StatusCodes.Status400BadRequest)] | ||
| [ProducesResponseType(StatusCodes.Status401Unauthorized)] | ||
| public Task<IActionResult> Exchange() => handler.ExchangeAsync(HttpContext); | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,76 @@ | ||
| using System.Security.Claims; | ||
|
|
||
| using Microsoft.AspNetCore; | ||
| using Microsoft.AspNetCore.Authentication; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Mvc; | ||
|
|
||
| using OpenIddict.Abstractions; | ||
| using OpenIddict.Server.AspNetCore; | ||
|
|
||
| using static OpenIddict.Abstractions.OpenIddictConstants; | ||
|
|
||
| namespace Ignis.Auth; | ||
|
|
||
| /// <summary> | ||
| /// Contains the OpenIddict token endpoint logic. | ||
| /// Designed to be called from a thin controller in the host application. | ||
| /// </summary> | ||
| public class AuthorizationHandler | ||
| { | ||
| private readonly IOpenIddictApplicationManager _applicationManager; | ||
|
|
||
| public AuthorizationHandler(IOpenIddictApplicationManager applicationManager) | ||
| { | ||
| _applicationManager = applicationManager; | ||
| } | ||
|
|
||
| public async Task<IActionResult> ExchangeAsync(HttpContext httpContext) | ||
| { | ||
| var request = httpContext.GetOpenIddictServerRequest() | ||
| ?? throw new InvalidOperationException("The OpenID Connect request cannot be retrieved."); | ||
|
|
||
| if (request.IsClientCredentialsGrantType()) | ||
| { | ||
| return await ExchangeClientCredentialsAsync(request); | ||
| } | ||
|
|
||
| return ForbidWithError(Errors.UnsupportedGrantType, "The specified grant type is not supported."); | ||
| } | ||
|
|
||
| private async Task<IActionResult> ExchangeClientCredentialsAsync(OpenIddictRequest request) | ||
| { | ||
| if (string.IsNullOrEmpty(request.ClientId)) | ||
| { | ||
| return ForbidWithError(Errors.InvalidClient, "The client identifier is missing."); | ||
| } | ||
|
|
||
| var application = await _applicationManager.FindByClientIdAsync(request.ClientId); | ||
| if (application is null) | ||
| { | ||
| return ForbidWithError(Errors.InvalidClient, "The specified client application was not found."); | ||
| } | ||
|
|
||
| var identity = new ClaimsIdentity( | ||
| OpenIddictServerAspNetCoreDefaults.AuthenticationScheme, | ||
| Claims.Name, Claims.Role); | ||
|
|
||
| identity.SetClaim(Claims.Subject, await _applicationManager.GetClientIdAsync(application)); | ||
| identity.SetClaim(Claims.Name, await _applicationManager.GetDisplayNameAsync(application)); | ||
|
|
||
| identity.SetScopes(request.GetScopes()); | ||
| identity.SetDestinations(static claim => [Destinations.AccessToken]); | ||
|
|
||
| return new SignInResult( | ||
| OpenIddictServerAspNetCoreDefaults.AuthenticationScheme, | ||
| new ClaimsPrincipal(identity)); | ||
| } | ||
|
|
||
| private static ForbidResult ForbidWithError(string error, string description) => | ||
| new([OpenIddictServerAspNetCoreDefaults.AuthenticationScheme], | ||
| new AuthenticationProperties(new Dictionary<string, string?> | ||
| { | ||
| [OpenIddictServerAspNetCoreConstants.Properties.Error] = error, | ||
| [OpenIddictServerAspNetCoreConstants.Properties.ErrorDescription] = description, | ||
| })); | ||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Controllers are now always registered, and the
/connect/tokencontroller lives inIgnis.Api, so the token endpoint will show up in API explorer/OpenAPI even whenAuthSettings.Enabledis false (it will just return 404). If the intent is to hide the token endpoint when auth is disabled (as the previous ApplicationPart removal did), consider conditionally mapping the token endpoint only when auth is enabled (e.g., via a minimal API route) or otherwise excluding it from OpenAPI when disabled.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed — the controller now uses constructor injection and AuthSettings.Enabled has been removed entirely. Auth is always registered when Ignis.Auth is referenced, so there's no longer a state where the handler is missing. The OpenAPI visibility follows naturally from that.