fix(backend): handle corner case where a user has multiple pubkeys

一人のユーザが複数の公開鍵を持つコーナーケースを処理
This commit is contained in:
洪 民憙 (Hong Minhee) 2024-06-05 21:07:45 +00:00
parent 43cccaaee9
commit a143a5cc48
1 changed files with 14 additions and 8 deletions

View File

@ -183,15 +183,18 @@ export class ApPersonService implements OnModuleInit {
} }
if (x.publicKey) { if (x.publicKey) {
if (typeof x.publicKey.id !== 'string') { const publicKeys = Array.isArray(x.publicKey) ? x.publicKey : [x.publicKey];
for (const publicKey of publicKeys) {
if (typeof publicKey.id !== 'string') {
throw new Error('invalid Actor: publicKey.id is not a string'); throw new Error('invalid Actor: publicKey.id is not a string');
} }
const publicKeyIdHost = this.punyHost(x.publicKey.id); const publicKeyIdHost = this.punyHost(publicKey.id);
if (publicKeyIdHost !== expectHost) { if (publicKeyIdHost !== expectHost) {
throw new Error('invalid Actor: publicKey.id has different host'); throw new Error('invalid Actor: publicKey.id has different host');
} }
} }
}
return x; return x;
} }
@ -356,10 +359,13 @@ export class ApPersonService implements OnModuleInit {
})); }));
if (person.publicKey) { if (person.publicKey) {
// TODO: 一人のユーザが複数の公開鍵を持っている場合があるので、MiUserPublicKeyの主キーもuserIdから(userId, keyId)に変更する必要があるかも…?
// As a user can have multiple public keys, it might be better to change MiUserPublicKey's primary key from userId to (userId, keyId).
const publicKey = Array.isArray(person.publicKey) ? person.publicKey[0] : person.publicKey;
await transactionalEntityManager.save(new MiUserPublickey({ await transactionalEntityManager.save(new MiUserPublickey({
userId: user.id, userId: user.id,
keyId: person.publicKey.id, keyId: publicKey.id,
keyPem: person.publicKey.publicKeyPem, keyPem: publicKey.publicKeyPem,
})); }));
} }
}); });