| | | 1 | | using Anichron.API.Infrastructure; |
| | | 2 | | using Anichron.API.Services; |
| | | 3 | | using Anichron.API.Settings; |
| | | 4 | | using Anichron.Core.Domain; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using static System.Globalization.CultureInfo; |
| | | 7 | | |
| | | 8 | | namespace Anichron.API.Endpoints; |
| | | 9 | | |
| | | 10 | | public interface IAuthResponseMapper |
| | | 11 | | { |
| | | 12 | | IResult GetRegistrationResult(AuthResult<AuthTokens> result, HttpContext http, PasswordPolicy passwordPolicy, Userna |
| | | 13 | | IResult GetLoginResult(AuthResult<AuthTokens> result, HttpContext http, bool setCookie); |
| | | 14 | | IResult GetRefreshResult(AuthResult<AuthTokens> result, HttpContext http, bool setCookie); |
| | | 15 | | IResult GetChangePasswordResult(AuthResult result, PasswordPolicy passwordPolicy); |
| | | 16 | | IResult GetAdminCreateUserResult(AuthResult<AdminCreatedUser> result); |
| | | 17 | | IResult GetAdminResetPasswordResult(AdminUserPasswordReset? result); |
| | | 18 | | IResult GetAdminGetUsersResult(List<User> users); |
| | | 19 | | IResult GetAdminGetUserResult(User? user); |
| | | 20 | | IResult GetAdminPatchUserResult(AuthResult<User> result); |
| | | 21 | | IResult GetAdminDeleteUserResult(AuthResult result); |
| | | 22 | | IResult GetAdminGetStorageConfigsResult(AuthResult<List<UserStorageConfig>> result); |
| | | 23 | | IResult GetAdminCreateStorageConfigResult(AuthResult<UserStorageConfig> result); |
| | | 24 | | IResult GetAdminDeleteStorageConfigResult(AuthResult result); |
| | | 25 | | void ClearRefreshCookie(HttpContext http); |
| | | 26 | | } |
| | | 27 | | |
| | 66 | 28 | | public sealed class AuthResponseMapper(AuthCookieSettings cookieSettings, IClock clock) : IAuthResponseMapper |
| | | 29 | | { |
| | | 30 | | public IResult GetRegistrationResult(AuthResult<AuthTokens> result, HttpContext http, PasswordPolicy passwordPolicy, |
| | 11 | 31 | | { |
| | 11 | 32 | | if (!result.IsSuccess) |
| | 10 | 33 | | { |
| | 10 | 34 | | return result.Error switch |
| | 10 | 35 | | { |
| | 1 | 36 | | AuthError.UsernameTaken => Results.Conflict(new { error = AuthMessages.UsernameTaken }), |
| | 1 | 37 | | AuthError.EmailTaken => Results.Conflict(new { error = AuthMessages.EmailTaken }), |
| | 1 | 38 | | AuthError.InvalidUsername => Results.UnprocessableEntity(new { error = usernamePolicy.InvalidFormatMessa |
| | 1 | 39 | | AuthError.InvalidEmail => Results.UnprocessableEntity(new { error = AuthMessages.InvalidEmail }), |
| | 1 | 40 | | AuthError.PasswordTooShort => Results.UnprocessableEntity(new { error = passwordPolicy.TooShortMessage } |
| | 1 | 41 | | AuthError.PasswordTooLong => Results.UnprocessableEntity(new { error = passwordPolicy.TooLongMessage }), |
| | 1 | 42 | | AuthError.PasswordPwned => Results.UnprocessableEntity(new { error = AuthMessages.PasswordPwned }), |
| | 1 | 43 | | AuthError.InviteTokenInvalid => Results.UnprocessableEntity(new { error = AuthMessages.InviteTokenInvali |
| | 2 | 44 | | _ => throw new UnreachableException($"Unexpected AuthError in Register: {result.Error}"), |
| | 10 | 45 | | }; |
| | | 46 | | } |
| | | 47 | | |
| | 1 | 48 | | SetRefreshCookie(http, result.Value!.RefreshToken); |
| | 1 | 49 | | return Results.Ok(new { result.Value.AccessToken }); |
| | 9 | 50 | | } |
| | | 51 | | |
| | | 52 | | public IResult GetLoginResult(AuthResult<AuthTokens> result, HttpContext http, bool setCookie) |
| | 8 | 53 | | { |
| | 8 | 54 | | if (!result.IsSuccess) |
| | 6 | 55 | | { |
| | 6 | 56 | | return result.Error switch |
| | 6 | 57 | | { |
| | 2 | 58 | | AuthError.AccountTemporarilyLocked => LockedResult(http, result.RetryAfterSeconds!.Value), |
| | 1 | 59 | | AuthError.AccountDisabled => Results.Json( |
| | 1 | 60 | | data: new { error = AuthMessages.AccountDisabled }, |
| | 1 | 61 | | statusCode: StatusCodes.Status401Unauthorized), |
| | 1 | 62 | | AuthError.InvalidCredentials => Results.Json( |
| | 1 | 63 | | data: new { error = AuthMessages.InvalidCredentials }, |
| | 1 | 64 | | statusCode: StatusCodes.Status401Unauthorized), |
| | 2 | 65 | | _ => throw new UnreachableException($"Unexpected AuthError in Login: {result.Error}"), |
| | 6 | 66 | | }; |
| | | 67 | | } |
| | | 68 | | |
| | 2 | 69 | | return BuildTokenResponse(result.Value!, http, setCookie); |
| | 6 | 70 | | } |
| | | 71 | | |
| | | 72 | | public IResult GetRefreshResult(AuthResult<AuthTokens> result, HttpContext http, bool setCookie) |
| | 7 | 73 | | { |
| | 7 | 74 | | if (!result.IsSuccess) |
| | 5 | 75 | | { |
| | 5 | 76 | | return result.Error switch |
| | 5 | 77 | | { |
| | 1 | 78 | | AuthError.TokenInvalid => Results.Json( |
| | 1 | 79 | | data: new { error = AuthMessages.RefreshTokenInvalid }, |
| | 1 | 80 | | statusCode: StatusCodes.Status401Unauthorized), |
| | 1 | 81 | | AuthError.AccountDisabled => Results.Json( |
| | 1 | 82 | | data: new { error = AuthMessages.AccountDisabled }, |
| | 1 | 83 | | statusCode: StatusCodes.Status401Unauthorized), |
| | 1 | 84 | | AuthError.AccountTemporarilyLocked => LockedResult(http, result.RetryAfterSeconds!.Value), |
| | 2 | 85 | | _ => throw new UnreachableException($"Unexpected AuthError in Refresh: {result.Error}"), |
| | 5 | 86 | | }; |
| | | 87 | | } |
| | | 88 | | |
| | 2 | 89 | | return BuildTokenResponse(result.Value!, http, setCookie); |
| | 5 | 90 | | } |
| | | 91 | | |
| | | 92 | | public IResult GetChangePasswordResult(AuthResult result, PasswordPolicy passwordPolicy) |
| | 7 | 93 | | { |
| | 7 | 94 | | if (result.IsSuccess) |
| | 1 | 95 | | return Results.NoContent(); |
| | | 96 | | |
| | 6 | 97 | | return result.Error switch |
| | 6 | 98 | | { |
| | 1 | 99 | | AuthError.InvalidCredentials => Results.Json( |
| | 1 | 100 | | data: new { error = AuthMessages.InvalidCredentials }, |
| | 1 | 101 | | statusCode: StatusCodes.Status400BadRequest), |
| | 1 | 102 | | AuthError.PasswordTooShort => Results.Json( |
| | 1 | 103 | | data: new { error = passwordPolicy.TooShortMessage }, |
| | 1 | 104 | | statusCode: StatusCodes.Status422UnprocessableEntity), |
| | 1 | 105 | | AuthError.PasswordTooLong => Results.Json( |
| | 1 | 106 | | data: new { error = passwordPolicy.TooLongMessage }, |
| | 1 | 107 | | statusCode: StatusCodes.Status422UnprocessableEntity), |
| | 1 | 108 | | AuthError.PasswordPwned => Results.Json( |
| | 1 | 109 | | data: new { error = AuthMessages.PasswordPwned }, |
| | 1 | 110 | | statusCode: StatusCodes.Status422UnprocessableEntity), |
| | 2 | 111 | | _ => throw new UnreachableException($"Unexpected AuthError in ChangePassword: {result.Error}"), |
| | 6 | 112 | | }; |
| | 5 | 113 | | } |
| | | 114 | | |
| | | 115 | | public IResult GetAdminCreateUserResult(AuthResult<AdminCreatedUser> result) |
| | 5 | 116 | | { |
| | 5 | 117 | | if (!result.IsSuccess) |
| | 4 | 118 | | { |
| | 4 | 119 | | return result.Error switch |
| | 4 | 120 | | { |
| | 0 | 121 | | AuthError.InvalidUsername => Results.UnprocessableEntity(new { error = AuthMessages.InvalidUsername }), |
| | 0 | 122 | | AuthError.InvalidEmail => Results.UnprocessableEntity(new { error = AuthMessages.InvalidEmail }), |
| | 1 | 123 | | AuthError.UsernameTaken => Results.Conflict(new { error = AuthMessages.UsernameTaken }), |
| | 1 | 124 | | AuthError.EmailTaken => Results.Conflict(new { error = AuthMessages.EmailTaken }), |
| | 2 | 125 | | _ => throw new UnreachableException($"Unexpected AuthError in AdminCreateUser: {result.Error}"), |
| | 4 | 126 | | }; |
| | | 127 | | } |
| | | 128 | | |
| | 1 | 129 | | var created = result.Value!; |
| | 1 | 130 | | var location = ApiPaths.Users.UserLocation(created.Id); |
| | 1 | 131 | | return Results.Created(location, new AdminCreatedUserResponse( |
| | 1 | 132 | | created.Id, created.Username, created.Email, created.TemporaryPassword)); |
| | 3 | 133 | | } |
| | | 134 | | |
| | | 135 | | public IResult GetAdminResetPasswordResult(AdminUserPasswordReset? result) |
| | 2 | 136 | | => result is null |
| | 2 | 137 | | ? Results.NotFound() |
| | 2 | 138 | | : Results.Ok(new AdminPasswordResetResponse(result.TemporaryPassword)); |
| | | 139 | | |
| | | 140 | | public IResult GetAdminGetUsersResult(List<User> users) |
| | 2 | 141 | | => Results.Ok(users.ConvertAll(ToAdminUserResponse)); |
| | | 142 | | |
| | | 143 | | public IResult GetAdminGetUserResult(User? user) |
| | 2 | 144 | | => user is null ? Results.NotFound() : Results.Ok(ToAdminUserResponse(user)); |
| | | 145 | | |
| | | 146 | | public IResult GetAdminPatchUserResult(AuthResult<User> result) |
| | 5 | 147 | | => result.Error switch |
| | 5 | 148 | | { |
| | 1 | 149 | | AuthError.UserNotFound => Results.NotFound(), |
| | 1 | 150 | | AuthError.CannotModifySelf => Results.Json(new { error = AuthMessages.CannotModifySelf }, statusCode: Status |
| | 1 | 151 | | null => Results.Ok(ToAdminUserResponse(result.Value!)), |
| | 2 | 152 | | _ => throw new UnreachableException($"Unexpected AuthError in AdminPatchUser: {result.Error}"), |
| | 5 | 153 | | }; |
| | | 154 | | |
| | | 155 | | public IResult GetAdminDeleteUserResult(AuthResult result) |
| | 5 | 156 | | => result.Error switch |
| | 5 | 157 | | { |
| | 1 | 158 | | AuthError.UserNotFound => Results.NotFound(), |
| | 1 | 159 | | AuthError.CannotModifySelf => Results.Json(new { error = AuthMessages.CannotModifySelf }, statusCode: Status |
| | 1 | 160 | | null => Results.NoContent(), |
| | 2 | 161 | | _ => throw new UnreachableException($"Unexpected AuthError in AdminDeleteUser: {result.Error}"), |
| | 5 | 162 | | }; |
| | | 163 | | |
| | | 164 | | public IResult GetAdminGetStorageConfigsResult(AuthResult<List<UserStorageConfig>> result) |
| | 3 | 165 | | => result.Error switch |
| | 3 | 166 | | { |
| | 1 | 167 | | AuthError.UserNotFound => Results.NotFound(), |
| | 1 | 168 | | null => Results.Ok(result.Value!.ConvertAll(ToAdminStorageConfigResponse)), |
| | 1 | 169 | | _ => throw new UnreachableException($"Unexpected AuthError in AdminGetStorageConfigs: {result.Error}"), |
| | 3 | 170 | | }; |
| | | 171 | | |
| | | 172 | | public IResult GetAdminCreateStorageConfigResult(AuthResult<UserStorageConfig> result) |
| | 4 | 173 | | { |
| | 4 | 174 | | if (!result.IsSuccess) |
| | 3 | 175 | | { |
| | 3 | 176 | | return result.Error switch |
| | 3 | 177 | | { |
| | 1 | 178 | | AuthError.UserNotFound => Results.NotFound(), |
| | 0 | 179 | | AuthError.PathInvalid => Results.UnprocessableEntity(new { error = AuthMessages.PathInvalid }), |
| | 1 | 180 | | AuthError.PathAlreadyAssigned => Results.Conflict(new { error = AuthMessages.PathAlreadyAssigned }), |
| | 1 | 181 | | _ => throw new UnreachableException($"Unexpected AuthError in AdminCreateStorageConfig: {result.Error}") |
| | 3 | 182 | | }; |
| | | 183 | | } |
| | | 184 | | |
| | 1 | 185 | | var config = result.Value!; |
| | 1 | 186 | | var location = ApiPaths.Users.StorageConfigLocation(config.UserId, config.Id); |
| | 1 | 187 | | return Results.Created(location, ToAdminStorageConfigResponse(config)); |
| | 3 | 188 | | } |
| | | 189 | | |
| | | 190 | | public IResult GetAdminDeleteStorageConfigResult(AuthResult result) |
| | 4 | 191 | | => result.Error switch |
| | 4 | 192 | | { |
| | 1 | 193 | | AuthError.StorageConfigNotFound => Results.NotFound(), |
| | 1 | 194 | | null => Results.NoContent(), |
| | 2 | 195 | | _ => throw new UnreachableException($"Unexpected AuthError in AdminDeleteStorageConfig: {result.Error}"), |
| | 4 | 196 | | }; |
| | | 197 | | |
| | | 198 | | public void ClearRefreshCookie(HttpContext http) |
| | 1 | 199 | | => http.Response.Cookies.Delete(AuthMessages.RefreshTokenCookieName); |
| | | 200 | | |
| | | 201 | | private static AdminUserResponse ToAdminUserResponse(User user) |
| | 3 | 202 | | => new(user.Id, user.Username, user.Email, user.IsAdmin, user.IsDisabled, user.StorageConfigs.Count); |
| | | 203 | | |
| | | 204 | | private static AdminStorageConfigResponse ToAdminStorageConfigResponse(UserStorageConfig config) |
| | 2 | 205 | | => new(config.Id, config.UserId, config.RootPath, config.IsActive); |
| | | 206 | | |
| | | 207 | | private IResult BuildTokenResponse(AuthTokens tokens, HttpContext http, bool setCookie) |
| | 4 | 208 | | { |
| | 4 | 209 | | if (!setCookie) |
| | 2 | 210 | | return Results.Ok(new { tokens.AccessToken, tokens.RefreshToken }); |
| | | 211 | | |
| | 2 | 212 | | SetRefreshCookie(http, tokens.RefreshToken); |
| | 2 | 213 | | return Results.Ok(new { tokens.AccessToken }); |
| | 4 | 214 | | } |
| | | 215 | | |
| | | 216 | | private static IResult LockedResult(HttpContext http, int secondsRemaining) |
| | 3 | 217 | | { |
| | 3 | 218 | | http.Response.Headers.RetryAfter = secondsRemaining.ToString(InvariantCulture); |
| | 3 | 219 | | return Results.Json( |
| | 3 | 220 | | new { error = AuthMessages.AccountTemporarilyLocked(secondsRemaining) }, |
| | 3 | 221 | | statusCode: StatusCodes.Status429TooManyRequests); |
| | 3 | 222 | | } |
| | | 223 | | |
| | | 224 | | private void SetRefreshCookie(HttpContext http, string rawToken) |
| | 3 | 225 | | { |
| | 3 | 226 | | http.Response.Cookies.Append(AuthMessages.RefreshTokenCookieName, rawToken, new CookieOptions |
| | 3 | 227 | | { |
| | 3 | 228 | | HttpOnly = true, |
| | 3 | 229 | | Secure = true, |
| | 3 | 230 | | SameSite = cookieSettings.SameSite, |
| | 3 | 231 | | Expires = clock.GetCurrentInstant().ToDateTimeOffset().AddDays(cookieSettings.RefreshTokenDays), |
| | 3 | 232 | | }); |
| | 3 | 233 | | } |
| | | 234 | | } |