[backend/federation] Fix sporadic key fetch failures (ISH-194)
This commit is contained in:
parent
4c1e03ccc1
commit
4878d82463
2 changed files with 21 additions and 0 deletions
|
@ -67,6 +67,9 @@ public class AuthorizedFetchMiddleware(
|
|||
var user = await userResolver.ResolveAsync(sig.KeyId).WaitAsync(ct);
|
||||
key = await db.UserPublickeys.Include(p => p.User)
|
||||
.FirstOrDefaultAsync(p => p.User == user, ct);
|
||||
|
||||
// If the key is still null here, we have a data consistency issue and need to update the key manually
|
||||
key ??= await userSvc.UpdateUserPublicKeyAsync(user).WaitAsync(ct);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
|
|
@ -432,6 +432,24 @@ public class UserService(
|
|||
return key;
|
||||
}
|
||||
|
||||
public async Task<UserPublickey> UpdateUserPublicKeyAsync(User user)
|
||||
{
|
||||
var uri = user.Uri ?? throw new Exception("Can't update public key of user without Uri");
|
||||
var actor = await fetchSvc.FetchActorAsync(uri);
|
||||
|
||||
if (actor.PublicKey?.PublicKey == null)
|
||||
throw new Exception("Failed to update user public key: Invalid or missing public key");
|
||||
|
||||
var key = await db.UserPublickeys.FirstOrDefaultAsync(p => p.User == user) ?? new UserPublickey { User = user };
|
||||
|
||||
key.KeyId = actor.PublicKey.Id;
|
||||
key.KeyPem = actor.PublicKey.PublicKey;
|
||||
|
||||
db.Update(key);
|
||||
await db.SaveChangesAsync();
|
||||
return key;
|
||||
}
|
||||
|
||||
public async Task DeleteUserAsync(ASActor actor)
|
||||
{
|
||||
var user = await db.Users
|
||||
|
|
Loading…
Add table
Reference in a new issue