| | | 1 | | using ArturRios.Output; |
| | | 2 | | using ArturRios.Util.WebApi.Security.Records; |
| | | 3 | | |
| | 5 | 4 | | var builder = WebApplication.CreateBuilder(args); |
| | | 5 | | |
| | 5 | 6 | | var app = builder.Build(); |
| | | 7 | | |
| | | 8 | | // Valid credentials accepted by the fake authentication route. |
| | | 9 | | const string validEmail = "user@test.com"; |
| | | 10 | | const string validPassword = "password123"; |
| | | 11 | | const string issuedToken = "test-token"; |
| | | 12 | | |
| | | 13 | | // POST /auth — issues a token for valid credentials, otherwise returns a failed output. |
| | 5 | 14 | | app.MapPost("/auth", (Credentials credentials) => |
| | 3 | 15 | | { |
| | 3 | 16 | | if (credentials.Email == validEmail && credentials.Password == validPassword) |
| | 2 | 17 | | { |
| | 2 | 18 | | var authentication = new Authentication( |
| | 2 | 19 | | issuedToken, |
| | 2 | 20 | | true, |
| | 2 | 21 | | DateTime.UtcNow.ToString("o"), |
| | 2 | 22 | | DateTime.UtcNow.AddHours(1).ToString("o")); |
| | 5 | 23 | | |
| | 2 | 24 | | return Results.Ok(DataOutput<Authentication>.New.WithData(authentication)); |
| | 5 | 25 | | } |
| | 5 | 26 | | |
| | 1 | 27 | | return Results.Ok(DataOutput<Authentication>.New.WithError("Invalid credentials")); |
| | 8 | 28 | | }); |
| | | 29 | | |
| | | 30 | | // GET /secure — succeeds only when the issued bearer token is present. |
| | 5 | 31 | | app.MapGet("/secure", (HttpContext context) => |
| | 3 | 32 | | { |
| | 3 | 33 | | var header = context.Request.Headers.Authorization.ToString(); |
| | 5 | 34 | | |
| | 3 | 35 | | return header == $"Bearer {issuedToken}" |
| | 3 | 36 | | ? Results.Ok(DataOutput<string>.New.WithData("authorized")) |
| | 3 | 37 | | : Results.Unauthorized(); |
| | 8 | 38 | | }); |
| | | 39 | | |
| | 5 | 40 | | app.Run(); |
| | | 41 | | |
| | | 42 | | /// <summary>Entry point exposed so tests can host the app with <c>WebApplicationFactory</c>.</summary> |
| | | 43 | | public partial class Program; |