| 1 | import * as jose from "jose"; |
| 2 | import { createHash, randomUUID |
| 3 | import { env } |
| 4 | import { getEffectiveAppUrl } |
| 5 | |
| 6 | let _privateKey: jose |
| 7 | let _publicKey: jose |
| 8 | |
| 9 | async function getPrivateKey() |
| 10 | if (!_privateKey) |
| 11 | const pem = |
| 12 | _privateKey = await |
| 13 | } |
| 14 | return _privateKey; |
| 15 | } |
| 16 | |
| 17 | async function getPublicKey() |
| 18 | if (!_publicKey) |
| 19 | const pem = |
| 20 | _publicKey = await |
| 21 | } |
| 22 | return _publicKey; |
| 23 | } |
| 24 | |
| 25 | export interface WsTokenPayload |
| 26 | user_uuid: string; |
| 27 | server_uuid: string; |
| 28 | permissions: string[] |
| 29 | } |
| 30 | |
| 31 | export function toUuid(id |
| 32 | const h = |
| 33 | return `${h. |
| 34 | } |
| 35 | |
| 36 | export async function signWsToken |
| 37 | payload: WsTokenPayload |
| 38 | nodeToken: string, |
| 39 | ): Promise<string |
| 40 | const secret = |
| 41 | const appUrl = |
| 42 | return new jose |
| 43 | user_uuid: toUuid(payload |
| 44 | server_uuid: payload.server_uuid |
| 45 | permissions: payload.permissions |
| 46 | scope: "websocket", |
| 47 | }) |
| 48 | .setProtectedHeader({ alg: |
| 49 | .setIssuedAt() |
| 50 | .setExpirationTime("15m") |
| 51 | .setIssuer(appUrl) |
| 52 | .setAudience(["*"]) |
| 53 | .setJti(randomUUID()) |
| 54 | .sign(secret); |
| 55 | } |
| 56 | |
| 57 | export async function signBackupDownloadToken |
| 58 | userId: string, |
| 59 | serverUuid: string, |
| 60 | backupUuid: string, |
| 61 | nodeToken: string, |
| 62 | ): Promise<string |
| 63 | const secret = |
| 64 | const appUrl = |
| 65 | return new jose |
| 66 | user_uuid: toUuid(userId |
| 67 | server_uuid: serverUuid, |
| 68 | backup_uuid: backupUuid, |
| 69 | unique_id: randomUUID(), |
| 70 | scope: "backup-download", |
| 71 | }) |
| 72 | .setProtectedHeader({ alg: |
| 73 | .setIssuedAt() |
| 74 | .setExpirationTime("15m") |
| 75 | .setIssuer(appUrl) |
| 76 | .setAudience(["*"]) |
| 77 | .setJti(randomUUID()) |
| 78 | .sign(secret); |
| 79 | } |
| 80 | |
| 81 | export async function signFileDownloadToken |
| 82 | userId: string, |
| 83 | serverUuid: string, |
| 84 | filePath: string, |
| 85 | nodeToken: string, |
| 86 | ): Promise<string |
| 87 | const secret = |
| 88 | const appUrl = |
| 89 | return new jose |
| 90 | user_uuid: toUuid(userId |
| 91 | server_uuid: serverUuid, |
| 92 | file_path: filePath, |
| 93 | unique_id: randomUUID(), |
| 94 | scope: "file-download", |
| 95 | }) |
| 96 | .setProtectedHeader({ alg: |
| 97 | .setIssuedAt() |
| 98 | .setExpirationTime("15m") |
| 99 | .setIssuer(appUrl) |
| 100 | .setAudience(["*"]) |
| 101 | .setJti(randomUUID()) |
| 102 | .sign(secret); |
| 103 | } |
| 104 | |
| 105 | export async function getPublicKeyJwk |
| 106 | const key = |
| 107 | return jose.exportJWK |
| 108 | } |
| 109 | |
| 110 | export async function verifyWsToken |
| 111 | token: string, |
| 112 | ): Promise<jose |
| 113 | const key = |
| 114 | const { payload |
| 115 | return payload as |
| 116 | } |