| 1 | import { scrypt, timingSafeEqual } from "node:crypto"; |
| 2 | import { and, eq |
| 3 | import { db } |
| 4 | import { account, servers |
| 5 | import { toUuid } |
| 6 | |
| 7 | const scryptAsync = ( |
| 8 | new Promise<Buffer |
| 9 | scrypt(password, salt |
| 10 | ); |
| 11 | |
| 12 | async function verifyBetterAuthPassword |
| 13 | const [salt, |
| 14 | if (!salt || |
| 15 | const derived = |
| 16 | N: 16384, r: |
| 17 | }); |
| 18 | return timingSafeEqual(Buffer |
| 19 | } |
| 20 | |
| 21 | export interface SftpAuthResult |
| 22 | user: string; |
| 23 | server: string; |
| 24 | permissions: string[] |
| 25 | ignored_files: string[] |
| 26 | } |
| 27 | |
| 28 | export async function authenticateSftp |
| 29 | username: string, |
| 30 | password: string, |
| 31 | ): Promise<SftpAuthResult |
| 32 | const parts = |
| 33 | if (parts.length |
| 34 | console.log("[sftp] bad format: |
| 35 | return null; |
| 36 | } |
| 37 | const uuidShort = |
| 38 | const emailPrefix = |
| 39 | console.log("[sftp] uuidShort=%s emailPrefix=%s |
| 40 | |
| 41 | const server = |
| 42 | where: eq(servers |
| 43 | }); |
| 44 | if (!server) |
| 45 | if (server.suspended |
| 46 | |
| 47 | const userRecord = |
| 48 | where: like(user |
| 49 | }); |
| 50 | if (!userRecord) |
| 51 | console.log("[sftp] no user with email prefix: |
| 52 | return null; |
| 53 | } |
| 54 | console.log("[sftp] found user: |
| 55 | |
| 56 | const credential = |
| 57 | where: and(eq |
| 58 | }); |
| 59 | if (!credential?.password |
| 60 | console.log("[sftp] no credential for user: |
| 61 | return null; |
| 62 | } |
| 63 | |
| 64 | const valid = |
| 65 | if (!valid) |
| 66 | console.log("[sftp] wrong password for user: |
| 67 | return null; |
| 68 | } |
| 69 | |
| 70 | const isOwner = |
| 71 | let permissions: |
| 72 | |
| 73 | if (isOwner) { |
| 74 | permissions = ["* |
| 75 | } else { |
| 76 | const subuserRecord = |
| 77 | where: and(eq |
| 78 | }); |
| 79 | if (!subuserRecord) |
| 80 | console.log("[sftp] not owner and not subuser |
| 81 | return null; |
| 82 | } |
| 83 | permissions = JSON. |
| 84 | const hasSftpAccess = |
| 85 | (p) => |
| 86 | ); |
| 87 | if (!hasSftpAccess) |
| 88 | console.log("[sftp] subuser has no file permission |
| 89 | return null; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | console.log("[sftp] auth ok, permissions: |
| 94 | return { user: |
| 95 | } |