| 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 { keyTypeOf, readPublicKey |
| 8 | import { isoTimestamp } |
| 9 | import type { ListSshKeysResponseDTO, |
| 10 | import { MAX_TITLE_LENGTH } |
| 11 | import { |
| 12 | InvalidSshKeyError, |
| 13 | SshKeyAlreadyExistsError, |
| 14 | SshKeyNotFoundError, |
| 15 | } from './ssh-keys.errors.js'; |
| 16 | |
| 17 | /** OpenSSH public keys an account uploads so it can push and fetch over SSH. Unlike a signing key, nothing here is checked against an address: the key proves who is connecting, not who wrote a commit. */ |
| 18 | @Injectable() |
| 19 | export class SshKeysService |
| 20 | constructor(@Inject( |
| 21 | |
| 22 | async list(userId |
| 23 | const rows = |
| 24 | .select({ |
| 25 | id: schema.userSshKey |
| 26 | title: schema.userSshKey |
| 27 | publicKey: schema.userSshKey |
| 28 | fingerprint: schema.userSshKey |
| 29 | lastUsedAt: isoTimestamp(schema |
| 30 | createdAt: isoTimestamp(schema |
| 31 | }) |
| 32 | .from(schema.userSshKey |
| 33 | .where(eq(schema |
| 34 | .orderBy(schema.userSshKey |
| 35 | |
| 36 | return { |
| 37 | keys: rows.map |
| 38 | ...key, |
| 39 | type: keyTypeOf(publicKey |
| 40 | })), |
| 41 | }; |
| 42 | } |
| 43 | |
| 44 | async add(userId |
| 45 | const key = |
| 46 | try { |
| 47 | return readPublicKey(line |
| 48 | } catch (error) |
| 49 | throw new InvalidSshKeyError(( |
| 50 | } |
| 51 | })(); |
| 52 | |
| 53 | const [row] |
| 54 | .insert(schema.userSshKey |
| 55 | .values({ |
| 56 | id: `ssh_${nanoid |
| 57 | userId, |
| 58 | title: |
| 59 | (title ?? key |
| 60 | key.type, |
| 61 | fingerprint: key.fingerprint |
| 62 | publicKey: key.blob |
| 63 | }) |
| 64 | .onConflictDoNothing() |
| 65 | .returning({ |
| 66 | id: schema.userSshKey |
| 67 | title: schema.userSshKey |
| 68 | fingerprint: schema.userSshKey |
| 69 | lastUsedAt: isoTimestamp(schema |
| 70 | createdAt: isoTimestamp(schema |
| 71 | }); |
| 72 | |
| 73 | // A fingerprint belongs to one account, so a clash is someone else's key - or this account adding the same one twice. |
| 74 | if (!row) |
| 75 | |
| 76 | return { ...row |
| 77 | } |
| 78 | |
| 79 | async remove(userId |
| 80 | // Ownership is part of the match, so another account's key id deletes nothing rather than deleting theirs. |
| 81 | const [row] |
| 82 | .delete(schema.userSshKey |
| 83 | .where( |
| 84 | and(eq(schema |
| 85 | ) |
| 86 | .returning({ id: |
| 87 | |
| 88 | if (!row) |
| 89 | } |
| 90 | |
| 91 | /** The SSH server's whole identity lookup: one indexed read per authentication attempt. */ |
| 92 | async findByFingerprint(fingerprint |
| 93 | const [row] |
| 94 | .select({ |
| 95 | id: schema.userSshKey |
| 96 | userId: schema.userSshKey |
| 97 | username: schema.user |
| 98 | publicKey: schema.userSshKey |
| 99 | }) |
| 100 | .from(schema.userSshKey |
| 101 | .innerJoin(schema.user |
| 102 | .where(eq(schema |
| 103 | .limit(1); |
| 104 | |
| 105 | return row ?? |
| 106 | } |
| 107 | |
| 108 | async markUsed(id |
| 109 | await this.db |
| 110 | .update(schema.userSshKey |
| 111 | .set({ lastUsedAt: |
| 112 | .where(eq(schema |
| 113 | } |
| 114 | } |