| | | 1 | | using ArturRios.Heimdall.Command.Input; |
| | | 2 | | using ArturRios.Heimdall.Command.Output; |
| | | 3 | | using ArturRios.Heimdall.Query.Input; |
| | | 4 | | using ArturRios.Heimdall.Query.Output; |
| | | 5 | | using ArturRios.Heimdall.Shared.Messages; |
| | | 6 | | using ArturRios.Mediator.Command; |
| | | 7 | | using ArturRios.Mediator.Query; |
| | | 8 | | using ArturRios.Output; |
| | | 9 | | using ArturRios.Util.WebApi.AspNetCore; |
| | | 10 | | using ArturRios.Util.WebApi.Security.Attributes; |
| | | 11 | | using Microsoft.AspNetCore.Mvc; |
| | | 12 | | using Microsoft.AspNetCore.RateLimiting; |
| | | 13 | | |
| | | 14 | | namespace ArturRios.Heimdall.WebApi.Controllers; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Authentication for applications (UC-56, UC-57, FR-AP-13…FR-AP-16): exchanging a credential |
| | | 18 | | /// for an application token, and checking such a token live. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <remarks> |
| | | 21 | | /// Both actions are anonymous to <c>AuthenticationMiddleware</c>. The middleware knows only |
| | | 22 | | /// person tokens — and refuses application tokens (TH-35) — so UC-56 is authenticated by its |
| | | 23 | | /// secret and UC-57 validates the bearer token itself. |
| | | 24 | | /// </remarks> |
| | | 25 | | [Route("api/applications")] |
| | 37 | 26 | | public class ApplicationTokenController(CommandMediator commandMediator, QueryMediator queryMediator) : Controller |
| | | 27 | | { |
| | | 28 | | /// <summary> |
| | | 29 | | /// Exchanges an application's secret for a token valid for ten minutes by default (UC-56). |
| | | 30 | | /// Every refusal is the same 401, so the endpoint reveals nothing about which applications, |
| | | 31 | | /// scopes or credentials exist. Rate-limited per client address like the person sign-in. |
| | | 32 | | /// </summary> |
| | | 33 | | [HttpPost("token")] |
| | | 34 | | [AllowAnonymous] |
| | | 35 | | [EnableRateLimiting(Startup.AuthEndpointRateLimitPolicy)] |
| | | 36 | | public async Task<ActionResult<DataOutput<IssueApplicationTokenCommandOutput?>>> IssueToken( |
| | | 37 | | [FromBody] IssueApplicationTokenCommand command) |
| | | 38 | | { |
| | | 39 | | // AF-56a — a body that is not JSON, empty, or carries an id that is not a GUID never reaches |
| | | 40 | | // here: MalformedRequestFilter answers it (NFR-10). |
| | 22 | 41 | | var result = await commandMediator |
| | 22 | 42 | | .ExecuteCommandAsync<IssueApplicationTokenCommand, IssueApplicationTokenCommandOutput>(command); |
| | | 43 | | |
| | 22 | 44 | | return result.ToActionResult(statusMap: ApplicationTokenMessageMap.StatusCodes); |
| | 22 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Checks an application token live (UC-57), presented as <c>Authorization: Bearer …</c>. |
| | | 49 | | /// Answers the application, its scope and the credential when the token is valid and all |
| | | 50 | | /// three are still live; otherwise one 401. Anonymous to the middleware, which accepts only |
| | | 51 | | /// person tokens: the handler validates the token itself. |
| | | 52 | | /// </summary> |
| | | 53 | | [HttpGet("self")] |
| | | 54 | | [AllowAnonymous] |
| | | 55 | | public async Task<ActionResult<DataOutput<ApplicationTokenCheckOutput?>>> Self() |
| | | 56 | | { |
| | 12 | 57 | | var query = new CheckApplicationTokenQuery { Token = BearerToken(Request) }; |
| | | 58 | | |
| | 12 | 59 | | var result = await queryMediator |
| | 12 | 60 | | .ExecuteQueryAsync<CheckApplicationTokenQuery, ApplicationTokenCheckOutput>(query); |
| | | 61 | | |
| | 12 | 62 | | return result.ToActionResult(statusMap: ApplicationTokenMessageMap.StatusCodes); |
| | 12 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// The token of a <c>Bearer</c> <c>Authorization</c> header, or <c>null</c>. The scheme is |
| | | 67 | | /// matched case-insensitively (RFC 9110 §11.1); any other scheme, or none, is no token. |
| | | 68 | | /// </summary> |
| | | 69 | | private static string? BearerToken(HttpRequest request) |
| | | 70 | | { |
| | | 71 | | const string scheme = "Bearer "; |
| | 12 | 72 | | var header = request.Headers.Authorization.ToString(); |
| | | 73 | | |
| | 12 | 74 | | return header.StartsWith(scheme, StringComparison.OrdinalIgnoreCase) |
| | 12 | 75 | | ? header[scheme.Length..].Trim() |
| | 12 | 76 | | : null; |
| | | 77 | | } |
| | | 78 | | } |