Every failure the server answers with names its own error code
Each refusal and failure the client API sends now starts with a code no other failure uses, such as "[ERR0030006] That code is not valid.", so a screenshot or a report points at the exact place it came from. Nothing another program reads changes: ActivityPub, the Mastodon API's errors, sign-in tokens and the error kinds decePubClient branches on stay as they were. The house-rules checks move to 1.1.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W4YtEJrgxG12Pc39Fai8Bd
This commit is contained in:
1 parent
f3fdeaf038
commit
c2f439d1e9
25 files changed
+268
-224
No files matched your search
@@ -43,7 +43,7 @@ namespace PrivaPub.Services
|
||||
{
|
||||
signUpForm.UserName = signUpForm.UserName.ToLower();
|
||||
if (await dbEntities.RootUsers.Match(u => u.UserName == signUpForm.UserName).ExecuteAnyAsync(token))
|
||||
return result.Invalidate(localizer["Username '{0}' already taken.", signUpForm.UserName]);
|
||||
return result.Invalidate(30001.ToCodedError(localizer["Username '{0}' already taken.", signUpForm.UserName]));
|
||||
|
||||
var signUpPasswordHashed = passwordHasher.Hash(signUpForm.Password);
|
||||
var newUser = new RootUser
|
||||
@@ -74,7 +74,7 @@ namespace PrivaPub.Services
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(SignUpAsync)}()");
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
return result.Invalidate(30002.ToCodedError(localizer["Something went wrong."]), exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,16 +89,16 @@ namespace PrivaPub.Services
|
||||
var user = await dbEntities.RootUsers.Match(u => u.UserName == loginForm.UserName && u.DeletedAt == null).ExecuteFirstAsync(token);
|
||||
var (verified, needsUpgrade) = passwordHasher.Check(user?.HashedPassword ?? Decoy, loginForm.Password);
|
||||
if (user?.HashedPassword == default || !verified)
|
||||
return result.Invalidate(localizer[NoMatch]);
|
||||
return result.Invalidate(30003.ToCodedError(localizer[NoMatch]));
|
||||
if (user.IsBanned)
|
||||
return result.Invalidate(localizer["User '{0}' banned.", user.UserName]);
|
||||
return result.Invalidate(30004.ToCodedError(localizer["User '{0}' banned.", user.UserName]));
|
||||
// only a correct password learns that two-factor is on: it is asked for with a 428
|
||||
if (user.Totp?.EnabledAt != default)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(loginForm.OneTimeCode))
|
||||
return result.Invalidate(localizer[TwoFactor.CodeNeeded], StatusCodes.Status428PreconditionRequired);
|
||||
return result.Invalidate(30005.ToCodedError(localizer[TwoFactor.CodeNeeded]), StatusCodes.Status428PreconditionRequired);
|
||||
if (!await TwoFactor.Consume(user, loginForm.OneTimeCode, CancellationToken.None))
|
||||
return result.Invalidate(localizer[TwoFactor.CodeInvalid]);
|
||||
return result.Invalidate(30006.ToCodedError(localizer[TwoFactor.CodeInvalid]));
|
||||
}
|
||||
|
||||
// hashed again at today's iterations; matched on the old hash, so two sign-ins at once never undo each other
|
||||
@@ -122,7 +122,7 @@ namespace PrivaPub.Services
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(LoginAsync)}()");
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
return result.Invalidate(30007.ToCodedError(localizer["Something went wrong."]), exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,12 +133,12 @@ namespace PrivaPub.Services
|
||||
{
|
||||
var isSupportedLanguage = appConfigurationService.AppConfiguration.SupportedLanguages.Contains(userSettings.LanguageCode);
|
||||
if (!isSupportedLanguage)
|
||||
return result.Invalidate(localizer["Language code '{0}' unsupported.", userSettings.LanguageCode]);
|
||||
return result.Invalidate(30008.ToCodedError(localizer["Language code '{0}' unsupported.", userSettings.LanguageCode]));
|
||||
|
||||
var languageCodeExists = await dbEntities.Languages
|
||||
.Match(l => l.International2Code == userSettings.LanguageCode).ExecuteAnyAsync(token);
|
||||
if (!languageCodeExists)
|
||||
return result.Invalidate(localizer["Language code '{0}' doesn't exist.", userSettings.LanguageCode]);
|
||||
return result.Invalidate(30009.ToCodedError(localizer["Language code '{0}' doesn't exist.", userSettings.LanguageCode]));
|
||||
|
||||
var language = await dbEntities.Languages.Match(l => l.International2Code == userSettings.LanguageCode)
|
||||
.ExecuteFirstAsync(token);
|
||||
@@ -161,7 +161,7 @@ namespace PrivaPub.Services
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(UpdateUserSettingsAsync)}()");
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
return result.Invalidate(30010.ToCodedError(localizer["Something went wrong."]), exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -173,12 +173,12 @@ namespace PrivaPub.Services
|
||||
var user = await dbEntities.RootUsers.Match(u => u.ID == userId && u.DeletedAt == null).ExecuteFirstAsync(token);
|
||||
|
||||
if (user == null)
|
||||
return result.Invalidate(localizer["Username '{0}' not found.", userId]);
|
||||
return result.Invalidate(30011.ToCodedError(localizer["Username '{0}' not found.", userId]));
|
||||
|
||||
var (verified, needsUpgrade) = passwordHasher.Check(user.HashedPassword, userPasswordForm.OldPassword);
|
||||
|
||||
if (!verified)
|
||||
return result.Invalidate(localizer["Wrong password."]);
|
||||
return result.Invalidate(30012.ToCodedError(localizer["Wrong password."]));
|
||||
|
||||
await DB.Default.Update<RootUser>().MatchID(user.ID)
|
||||
.Modify(u => u.HashedPassword, passwordHasher.Hash(userPasswordForm.NewPassword))
|
||||
@@ -193,7 +193,7 @@ namespace PrivaPub.Services
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(UpdateUserPasswordAsync)}()");
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
return result.Invalidate(30013.ToCodedError(localizer["Something went wrong."]), exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,13 +207,13 @@ namespace PrivaPub.Services
|
||||
if (await removal.Remove(id, CancellationToken.None))
|
||||
removed++;
|
||||
if (removed == 0)
|
||||
return result.Invalidate(localizer["User already deleted."]);
|
||||
return result.Invalidate(30014.ToCodedError(localizer["User already deleted."]));
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(RemoveUserAsync)}()");
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
return result.Invalidate(30015.ToCodedError(localizer["Something went wrong."]), exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,14 +226,14 @@ namespace PrivaPub.Services
|
||||
var user = await dbEntities.RootUsers.MatchID(rootId).Match(u => u.DeletedAt == null).ExecuteFirstAsync(token);
|
||||
var (verified, _) = passwordHasher.Check(user?.HashedPassword ?? Decoy, password ?? string.Empty);
|
||||
if (user?.HashedPassword == default || !verified)
|
||||
return result.Invalidate(localizer[NoMatch], StatusCodes.Status403Forbidden);
|
||||
return result.Invalidate(30016.ToCodedError(localizer[NoMatch]), StatusCodes.Status403Forbidden);
|
||||
await removal.Remove(user.ID, CancellationToken.None);
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(RemoveSelfAsync)}()");
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
return result.Invalidate(30017.ToCodedError(localizer["Something went wrong."]), exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -260,7 +260,7 @@ namespace PrivaPub.Services
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(BanUserAsync)}()");
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
return result.Invalidate(30018.ToCodedError(localizer["Something went wrong."]), exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -279,7 +279,7 @@ namespace PrivaPub.Services
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(UnbanUserAsync)}()");
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
return result.Invalidate(30019.ToCodedError(localizer["Something went wrong."]), exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -290,7 +290,7 @@ namespace PrivaPub.Services
|
||||
{
|
||||
var user = await dbEntities.RootUsers.MatchID(userId).ExecuteFirstAsync(token);
|
||||
if (user == default)
|
||||
return result.Invalidate(localizer["User not found."], StatusCodes.Status404NotFound);
|
||||
return result.Invalidate(30020.ToCodedError(localizer["User not found."]), StatusCodes.Status404NotFound);
|
||||
|
||||
if (loginForm != default && loginForm.ThemeIsDarkMode != user.Settings.ThemeIsDarkMode)
|
||||
{
|
||||
@@ -307,7 +307,7 @@ namespace PrivaPub.Services
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(GetUserSettingsAsync)}()");
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
return result.Invalidate(30021.ToCodedError(localizer["Something went wrong."]), exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -332,7 +332,7 @@ namespace PrivaPub.Services
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(SetupAndSendRecoveryEmail)}()");
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
return result.Invalidate(30022.ToCodedError(localizer["Something went wrong."]), exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -353,7 +353,7 @@ namespace PrivaPub.Services
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
return result.Invalidate(30023.ToCodedError(localizer["Something went wrong."]), exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -367,7 +367,7 @@ namespace PrivaPub.Services
|
||||
var user = recovery == default ? default
|
||||
: await dbEntities.RootUsers.MatchID(recovery.RootUserId).Match(u => u.DeletedAt == null && !u.IsBanned).ExecuteFirstAsync(token);
|
||||
if (user == default)
|
||||
return result.Invalidate(localizer["Invalid recovery code."], StatusCodes.Status404NotFound);
|
||||
return result.Invalidate(30024.ToCodedError(localizer["Invalid recovery code."]), StatusCodes.Status404NotFound);
|
||||
|
||||
await DB.Default.Update<RootUser>().MatchID(user.ID)
|
||||
.Modify(u => u.HashedPassword, passwordHasher.Hash(newPasswordForm.NewPassword))
|
||||
@@ -380,7 +380,7 @@ namespace PrivaPub.Services
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(ChangePassword)}()");
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
return result.Invalidate(30025.ToCodedError(localizer["Something went wrong."]), exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -407,7 +407,7 @@ namespace PrivaPub.Services
|
||||
{
|
||||
var user = await dbEntities.RootUsers.MatchID(rootId).Match(u => u.DeletedAt == null).ExecuteFirstAsync(token);
|
||||
if (user == default)
|
||||
return result.Invalidate(localizer["User not found."], StatusCodes.Status404NotFound);
|
||||
return result.Invalidate(30026.ToCodedError(localizer["User not found."]), StatusCodes.Status404NotFound);
|
||||
var now = DateTime.UtcNow;
|
||||
var pending = await DB.Default.Find<EmailVerification>().Match(v => v.RootUserId == rootId && v.ExpiresAt > now).ExecuteFirstAsync(token);
|
||||
result.Data = new ViewAccount
|
||||
@@ -425,7 +425,7 @@ namespace PrivaPub.Services
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(AccountAsync)}()");
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
return result.Invalidate(30027.ToCodedError(localizer["Something went wrong."]), exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -439,7 +439,7 @@ namespace PrivaPub.Services
|
||||
var user = await dbEntities.RootUsers.MatchID(rootId).Match(u => u.DeletedAt == null).ExecuteFirstAsync(token);
|
||||
var (verified, _) = passwordHasher.Check(user?.HashedPassword ?? Decoy, form.Password ?? string.Empty);
|
||||
if (user?.HashedPassword == default || !verified)
|
||||
return result.Invalidate(localizer["Wrong password."], StatusCodes.Status403Forbidden);
|
||||
return result.Invalidate(30028.ToCodedError(localizer["Wrong password."]), StatusCodes.Status403Forbidden);
|
||||
|
||||
await DB.Default.DeleteAsync<EmailVerification>(v => v.RootUserId == user.ID);
|
||||
var pending = new EmailVerification
|
||||
@@ -457,7 +457,7 @@ namespace PrivaPub.Services
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(RequestEmailChangeAsync)}()");
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
return result.Invalidate(30029.ToCodedError(localizer["Something went wrong."]), exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -470,18 +470,18 @@ namespace PrivaPub.Services
|
||||
var now = DateTime.UtcNow;
|
||||
var pending = await DB.Default.Find<EmailVerification>().Match(v => v.RootUserId == rootId && v.ExpiresAt > now).ExecuteFirstAsync(token);
|
||||
if (pending == default || string.IsNullOrEmpty(pending.CodeHash))
|
||||
return result.Invalidate(localizer[EmailCodeInvalid]);
|
||||
return result.Invalidate(30030.ToCodedError(localizer[EmailCodeInvalid]));
|
||||
if (pending.CodeHash != RecoveryJob.Hash(code?.Trim()))
|
||||
{
|
||||
if (pending.Attempts + 1 >= EmailVerification.MaxAttempts)
|
||||
await DB.Default.DeleteAsync<EmailVerification>(pending.ID);
|
||||
else
|
||||
await DB.Default.Update<EmailVerification>().MatchID(pending.ID).Modify(b => b.Inc(v => v.Attempts, 1)).ExecuteAsync(token);
|
||||
return result.Invalidate(localizer[EmailCodeInvalid]);
|
||||
return result.Invalidate(30031.ToCodedError(localizer[EmailCodeInvalid]));
|
||||
}
|
||||
await DB.Default.DeleteAsync<EmailVerification>(pending.ID);
|
||||
if (await dbEntities.RootUsers.Match(u => u.Email == pending.Email && u.IsEmailValidated && u.ID != rootId && u.DeletedAt == null).ExecuteAnyAsync(token))
|
||||
return result.Invalidate(localizer[EmailCodeInvalid]);
|
||||
return result.Invalidate(30032.ToCodedError(localizer[EmailCodeInvalid]));
|
||||
|
||||
await DB.Default.Update<RootUser>().MatchID(rootId)
|
||||
.Modify(u => u.Email, pending.Email)
|
||||
@@ -493,7 +493,7 @@ namespace PrivaPub.Services
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(VerifyEmailAsync)}()");
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
return result.Invalidate(30033.ToCodedError(localizer["Something went wrong."]), exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -507,9 +507,9 @@ namespace PrivaPub.Services
|
||||
var user = await dbEntities.RootUsers.MatchID(rootId).Match(u => u.DeletedAt == null).ExecuteFirstAsync(token);
|
||||
var (verified, _) = passwordHasher.Check(user?.HashedPassword ?? Decoy, password ?? string.Empty);
|
||||
if (user?.HashedPassword == default || !verified)
|
||||
return result.Invalidate(localizer["Wrong password."], StatusCodes.Status403Forbidden);
|
||||
return result.Invalidate(30034.ToCodedError(localizer["Wrong password."]), StatusCodes.Status403Forbidden);
|
||||
if (user.Totp?.EnabledAt != default)
|
||||
return result.Invalidate(localizer["Two-factor sign-in is on already: turn it off first."], StatusCodes.Status409Conflict);
|
||||
return result.Invalidate(30035.ToCodedError(localizer["Two-factor sign-in is on already: turn it off first."]), StatusCodes.Status409Conflict);
|
||||
|
||||
var secret = TwoFactor.NewSecret();
|
||||
await DB.Default.Update<RootUser>().MatchID(rootId)
|
||||
@@ -522,7 +522,7 @@ namespace PrivaPub.Services
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(TotpSetupAsync)}()");
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
return result.Invalidate(30036.ToCodedError(localizer["Something went wrong."]), exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -533,9 +533,9 @@ namespace PrivaPub.Services
|
||||
{
|
||||
var user = await dbEntities.RootUsers.MatchID(rootId).Match(u => u.DeletedAt == null).ExecuteFirstAsync(token);
|
||||
if (user?.Totp is not { EnabledAt: null, PendingSecret: not null } pending || pending.PendingSince < DateTime.UtcNow.AddHours(-1))
|
||||
return result.Invalidate(localizer["Set up two-factor sign-in again: that setup expired."], StatusCodes.Status409Conflict);
|
||||
return result.Invalidate(30037.ToCodedError(localizer["Set up two-factor sign-in again: that setup expired."]), StatusCodes.Status409Conflict);
|
||||
if (TwoFactor.Step(pending.PendingSecret, code) is not { } step)
|
||||
return result.Invalidate(localizer[TwoFactor.CodeInvalid]);
|
||||
return result.Invalidate(30038.ToCodedError(localizer[TwoFactor.CodeInvalid]));
|
||||
|
||||
var codes = TwoFactor.NewRecoveryCodes();
|
||||
await DB.Default.Update<RootUser>().MatchID(rootId)
|
||||
@@ -555,7 +555,7 @@ namespace PrivaPub.Services
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(TotpEnableAsync)}()");
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
return result.Invalidate(30039.ToCodedError(localizer["Something went wrong."]), exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -567,11 +567,11 @@ namespace PrivaPub.Services
|
||||
var user = await dbEntities.RootUsers.MatchID(rootId).Match(u => u.DeletedAt == null).ExecuteFirstAsync(token);
|
||||
var (verified, _) = passwordHasher.Check(user?.HashedPassword ?? Decoy, password ?? string.Empty);
|
||||
if (user?.HashedPassword == default || !verified)
|
||||
return result.Invalidate(localizer["Wrong password."], StatusCodes.Status403Forbidden);
|
||||
return result.Invalidate(30040.ToCodedError(localizer["Wrong password."]), StatusCodes.Status403Forbidden);
|
||||
if (user.Totp?.EnabledAt == default)
|
||||
return await AccountAsync(rootId, token);
|
||||
if (!await TwoFactor.Consume(user, code, CancellationToken.None))
|
||||
return result.Invalidate(localizer[TwoFactor.CodeInvalid]);
|
||||
return result.Invalidate(30041.ToCodedError(localizer[TwoFactor.CodeInvalid]));
|
||||
|
||||
await DB.Default.Update<RootUser>().MatchID(rootId).Modify(u => u.Totp, null).ExecuteAsync(token);
|
||||
return await AccountAsync(rootId, token);
|
||||
@@ -579,7 +579,7 @@ namespace PrivaPub.Services
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(TotpDisableAsync)}()");
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
return result.Invalidate(30042.ToCodedError(localizer["Something went wrong."]), exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -591,7 +591,7 @@ namespace PrivaPub.Services
|
||||
var user = await dbEntities.RootUsers.MatchID(rootId).Match(u => u.DeletedAt == null).ExecuteFirstAsync(token);
|
||||
var (verified, _) = passwordHasher.Check(user?.HashedPassword ?? Decoy, password ?? string.Empty);
|
||||
if (user?.HashedPassword == default || !verified)
|
||||
return result.Invalidate(localizer["Wrong password."], StatusCodes.Status403Forbidden);
|
||||
return result.Invalidate(30043.ToCodedError(localizer["Wrong password."]), StatusCodes.Status403Forbidden);
|
||||
|
||||
await DB.Default.Update<RootUser>().MatchID(rootId)
|
||||
.Modify(u => u.Email, null)
|
||||
@@ -605,7 +605,7 @@ namespace PrivaPub.Services
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"{nameof(RootUsersService)}.{nameof(RemoveEmailAsync)}()");
|
||||
return result.Invalidate(localizer["Something went wrong."], exception: ex);
|
||||
return result.Invalidate(30044.ToCodedError(localizer["Something went wrong."]), exception: ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user