fix: add transaction to avoid not updating the password
All checks were successful
Build, Package and Push Images / preprocess (push) Successful in 2s
Build, Package and Push Images / build (push) Successful in 28s
Build, Package and Push Images / sonarqube (push) Has been skipped
Build, Package and Push Images / sbom-scan (push) Successful in 33s
Build, Package and Push Images / container-build (push) Successful in 3m6s
Build, Package and Push Images / container-sbom-scan (push) Successful in 39s

This commit is contained in:
Timothy Schenk 2023-11-12 13:18:36 +01:00
parent 497d415fb6
commit dc7daaef5c
2 changed files with 28 additions and 10 deletions

View file

@ -28,6 +28,8 @@ insert_final_newline = true
indent_size = 4 indent_size = 4
dotnet_sort_system_directives_first = true dotnet_sort_system_directives_first = true
MA0004.report = DetectContext # (default) Try to detect the current context and report only if it considers ConfigureAwait is needed
MA0004.report = Always # Always report missing ConfigureAwait whatever the context
# Don't use this. qualifier # Don't use this. qualifier
dotnet_style_qualification_for_field = false:suggestion dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion dotnet_style_qualification_for_property = false:suggestion

View file

@ -44,16 +44,32 @@ public class LoginHandler : IPacketHandler<LoginInfoPacket>
{ {
if (this._configuration.GetSection("Testing").GetValue<bool>("CreateAccountOnLogin")) if (this._configuration.GetSection("Testing").GetValue<bool>("CreateAccountOnLogin"))
{ {
argon2Id.Salt = RandomNumberGenerator.GetBytes(16); var transaction =
var finalAccount = await _wonderkingContext.Database.BeginTransactionAsync().ConfigureAwait(true);
await this._wonderkingContext.Accounts.AddAsync(new Account(packet.Username, Array.Empty<byte>(), "", await using (transaction.ConfigureAwait(true))
0, argon2Id.Salt)).ConfigureAwait(true); {
await this._wonderkingContext.SaveChangesAsync().ConfigureAwait(true); try
argon2Id.AssociatedData = finalAccount.Entity.Id.ToByteArray(); {
finalAccount.Entity.Password = await argon2Id.GetBytesAsync(16).ConfigureAwait(true); argon2Id.Salt = RandomNumberGenerator.GetBytes(16);
this._wonderkingContext.Accounts.Update(finalAccount.Entity); var finalAccount =
loginResponseReason = LoginResponseReason.Ok; await this._wonderkingContext.Accounts.AddAsync(new Account(packet.Username,
await this._wonderkingContext.SaveChangesAsync().ConfigureAwait(true); Array.Empty<byte>(), "",
0, argon2Id.Salt)).ConfigureAwait(true);
await this._wonderkingContext.SaveChangesAsync().ConfigureAwait(true);
argon2Id.AssociatedData = finalAccount.Entity.Id.ToByteArray();
finalAccount.Entity.Password = await argon2Id.GetBytesAsync(16).ConfigureAwait(true);
this._wonderkingContext.Accounts.Update(finalAccount.Entity);
loginResponseReason = LoginResponseReason.Ok;
await this._wonderkingContext.SaveChangesAsync().ConfigureAwait(true);
await transaction.CommitAsync().ConfigureAwait(true);
}
catch (Exception)
{
await transaction.RollbackAsync().ConfigureAwait(true); // Rollback the transaction on error
throw;
}
}
} }
else else
{ {