| 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 '../../lib/ssh-keys/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 | const type = |
| 39 | return { ...key |
| 40 | }), |
| 41 | }; |
| 42 | } |
| 43 | |
| 44 | async add( |
| 45 | userId: string, |
| 46 | line: string, |
| 47 | title?: string |
| 48 | oauthClientId?: string |
| 49 | ): Promise<SshKeyDTO |
| 50 | const key = |
| 51 | try { |
| 52 | return readPublicKey(line |
| 53 | } catch (error) |
| 54 | throw new InvalidSshKeyError(( |
| 55 | } |
| 56 | })(); |
| 57 | |
| 58 | const [row] |
| 59 | .insert(schema.userSshKey |
| 60 | .values({ |
| 61 | id: `ssh_${nanoid |
| 62 | userId, |
| 63 | title: |
| 64 | (title ?? key |
| 65 | key.type, |
| 66 | fingerprint: key.fingerprint |
| 67 | publicKey: key.blob |
| 68 | oauthClientId, |
| 69 | }) |
| 70 | .onConflictDoNothing() |
| 71 | .returning({ |
| 72 | id: schema.userSshKey |
| 73 | title: schema.userSshKey |
| 74 | fingerprint: schema.userSshKey |
| 75 | lastUsedAt: isoTimestamp(schema |
| 76 | createdAt: isoTimestamp(schema |
| 77 | }); |
| 78 | |
| 79 | // A fingerprint belongs to one account, so a clash is someone else's key - or this account adding the same one twice. |
| 80 | if (!row) |
| 81 | |
| 82 | return { ...row |
| 83 | } |
| 84 | |
| 85 | async remove(userId |
| 86 | // Ownership is part of the match, so another account's key id deletes nothing rather than deleting theirs. |
| 87 | const [row] |
| 88 | .delete(schema.userSshKey |
| 89 | .where( |
| 90 | and(eq(schema |
| 91 | ) |
| 92 | .returning({ id: |
| 93 | |
| 94 | if (!row) |
| 95 | } |
| 96 | |
| 97 | /** The SSH server's whole identity lookup: one indexed read per authentication attempt. */ |
| 98 | async findByFingerprint(fingerprint |
| 99 | const [row] |
| 100 | .select({ |
| 101 | id: schema.userSshKey |
| 102 | userId: schema.userSshKey |
| 103 | username: schema.user |
| 104 | publicKey: schema.userSshKey |
| 105 | oauthClientId: schema.userSshKey |
| 106 | legacy: schema.userSshKey |
| 107 | }) |
| 108 | .from(schema.userSshKey |
| 109 | .innerJoin(schema.user |
| 110 | .where(eq(schema |
| 111 | .limit(1); |
| 112 | |
| 113 | return row ?? |
| 114 | } |
| 115 | |
| 116 | async markUsed(id |
| 117 | await this.db |
| 118 | .update(schema.userSshKey |
| 119 | .set({ lastUsedAt: |
| 120 | .where(eq(schema |
| 121 | } |
| 122 | } |