| | | 1 | | using Anichron.API.Infrastructure; |
| | | 2 | | using Anichron.API.Services; |
| | | 3 | | using Anichron.Core.Data.Repository; |
| | | 4 | | using Anichron.Core.Domain; |
| | | 5 | | using System.Security.Claims; |
| | | 6 | | |
| | | 7 | | namespace Anichron.API.Endpoints; |
| | | 8 | | |
| | | 9 | | public static class UserEndpoints |
| | | 10 | | { |
| | | 11 | | public static IEndpointRouteBuilder MapUserEndpoints(this IEndpointRouteBuilder app) |
| | 74 | 12 | | { |
| | 74 | 13 | | var group = app.MapGroup(ApiPaths.Users.Group).WithTags("Users"); |
| | 74 | 14 | | group.MapGet(ApiPaths.Users.Me, GetMeAsync) |
| | 74 | 15 | | .RequireUserAllowedWhileMustChangePassword() |
| | 74 | 16 | | .Produces<UserProfileResponse>() |
| | 74 | 17 | | .Produces(StatusCodes.Status404NotFound); |
| | 74 | 18 | | group.MapPost(ApiPaths.Users.ChangePassword, ChangePasswordAsync) |
| | 74 | 19 | | .RequireUserAllowedWhileMustChangePassword() |
| | 74 | 20 | | .RequireRateLimit(AuthRateLimitPolicies.Sensitive) |
| | 74 | 21 | | .Produces<SessionResponse>() |
| | 74 | 22 | | .ProducesValidationProblem() |
| | 74 | 23 | | .ProducesValidationProblem(StatusCodes.Status422UnprocessableEntity); |
| | 74 | 24 | | return app; |
| | 74 | 25 | | } |
| | | 26 | | |
| | | 27 | | internal static async Task<IResult> GetMeAsync( |
| | | 28 | | ClaimsPrincipal user, |
| | | 29 | | IUserRepository users, |
| | | 30 | | IUserStorageConfigRepository storageConfigs, |
| | | 31 | | CancellationToken ct) |
| | 5 | 32 | | { |
| | 5 | 33 | | if (!Guid.TryParse(user.FindFirstValue(ClaimTypes.NameIdentifier), out var userId)) |
| | 2 | 34 | | return Results.Unauthorized(); |
| | | 35 | | |
| | 3 | 36 | | var found = await users.FindByIdAsync(userId, ct); |
| | 3 | 37 | | if (found is null) |
| | 1 | 38 | | return Results.NotFound(); |
| | | 39 | | |
| | 2 | 40 | | return Results.Ok(UserProfileResponse.From(found, await storageConfigs.AnyActiveByUserIdAsync(userId, ct))); |
| | 5 | 41 | | } |
| | | 42 | | |
| | | 43 | | internal static async Task<IResult> ChangePasswordAsync( |
| | | 44 | | ChangePasswordRequest req, |
| | | 45 | | ClaimsPrincipal user, |
| | | 46 | | HttpContext http, |
| | | 47 | | IAuthService auth, |
| | | 48 | | ISessionResponseMapper mapper, |
| | | 49 | | CancellationToken ct) |
| | 11 | 50 | | { |
| | 11 | 51 | | if (!Guid.TryParse(user.FindFirstValue(ClaimTypes.NameIdentifier), out var userId)) |
| | 2 | 52 | | return Results.Unauthorized(); |
| | | 53 | | |
| | 9 | 54 | | var result = await auth.ChangePasswordAsync(userId, req.CurrentPassword, req.NewPassword, ct); |
| | 9 | 55 | | return mapper.GetChangePasswordResult(result, http); |
| | 11 | 56 | | } |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | public sealed record ChangePasswordRequest(string CurrentPassword, string NewPassword); |
| | | 60 | | public sealed record UserProfileResponse( |
| | | 61 | | Guid Id, string Username, string Email, bool IsAdmin, bool MustChangePassword, bool HasStorageConfig) |
| | | 62 | | { |
| | | 63 | | internal static UserProfileResponse From(User user, bool hasStorageConfig) |
| | | 64 | | => new(user.Id, user.Username, user.Email, user.IsAdmin, user.MustChangePassword, hasStorageConfig); |
| | | 65 | | } |