| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { Inject, Injectable |
| 3 | import { and, eq |
| 4 | import { nanoid } |
| 5 | |
| 6 | import { DATABASE } |
| 7 | import { isoTimestamp } |
| 8 | import { readPublicKey } |
| 9 | import { UsersService } |
| 10 | import type { GpgKeyDTO, |
| 11 | import { |
| 12 | GpgKeyAlreadyExistsError, |
| 13 | GpgKeyEmailNotVerifiedError, |
| 14 | GpgKeyNotFoundError, |
| 15 | InvalidGpgKeyError, |
| 16 | } from './gpg-keys.errors.js'; |
| 17 | |
| 18 | /** |
| 19 | * OpenPGP public keys an account uploads so its signed commits read as verified. |
| 20 | * |
| 21 | * A key is only accepted once one of its user ids is an address the account has already verified: the badge claims the commit's author signed it, and without that check anyone could upload a key naming someone else's address. |
| 22 | */ |
| 23 | const keyColumns = { |
| 24 | id: schema.userGpgKey |
| 25 | keyId: schema.userGpgKey |
| 26 | fingerprint: schema.userGpgKey |
| 27 | publicKey: schema.userGpgKey |
| 28 | createdAt: isoTimestamp(schema |
| 29 | }; |
| 30 | |
| 31 | @Injectable() |
| 32 | export class GpgKeysService |
| 33 | constructor( |
| 34 | @Inject(DATABASE) |
| 35 | private readonly users |
| 36 | ) {} |
| 37 | |
| 38 | async list(userId |
| 39 | const rows = |
| 40 | .select(keyColumns) |
| 41 | .from(schema.userGpgKey |
| 42 | .where(eq(schema |
| 43 | .orderBy(schema.userGpgKey |
| 44 | |
| 45 | return { |
| 46 | keys: await Promise |
| 47 | rows.map(async |
| 48 | ...key, |
| 49 | emails: await addressesOn |
| 50 | })), |
| 51 | ), |
| 52 | }; |
| 53 | } |
| 54 | |
| 55 | async add(userId |
| 56 | const key = |
| 57 | throw new InvalidGpgKeyError( |
| 58 | }); |
| 59 | |
| 60 | if (key.revoked |
| 61 | if (key.expired |
| 62 | |
| 63 | const user = |
| 64 | const owned = |
| 65 | if (!key.emails |
| 66 | throw new GpgKeyEmailNotVerifiedError( |
| 67 | } |
| 68 | |
| 69 | const [row] |
| 70 | .insert(schema.userGpgKey |
| 71 | .values({ |
| 72 | id: `gpg_${nanoid |
| 73 | userId, |
| 74 | keyId: key.keyId |
| 75 | fingerprint: key.fingerprint |
| 76 | publicKey: armoredKey.trim |
| 77 | }) |
| 78 | .onConflictDoNothing() |
| 79 | .returning({ |
| 80 | id: schema.userGpgKey |
| 81 | keyId: schema.userGpgKey |
| 82 | fingerprint: schema.userGpgKey |
| 83 | createdAt: isoTimestamp(schema |
| 84 | }); |
| 85 | |
| 86 | // A key id belongs to one account, so a clash is someone else's key - or this account adding the same one twice. |
| 87 | if (!row) |
| 88 | |
| 89 | return { ...row |
| 90 | } |
| 91 | |
| 92 | async remove(userId |
| 93 | // Ownership is part of the match, so another account's key id deletes nothing rather than deleting theirs. |
| 94 | const [row] |
| 95 | .delete(schema.userGpgKey |
| 96 | .where( |
| 97 | and(eq(schema |
| 98 | ) |
| 99 | .returning({ id: |
| 100 | |
| 101 | if (!row) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Read back from the armor rather than stored twice: a key's user ids are only ever as current as the armor that was uploaded. |
| 106 | async function addressesOn( |
| 107 | return readPublicKey(armoredKey |
| 108 | .then((key) |
| 109 | .catch(() => |
| 110 | } |