(useStorage = true)
| 52 | * @returns Promise<{key: string, encryptedKey?: string}> - The plain key and optionally encrypted version |
| 53 | */ |
| 54 | export async function createApiKey(useStorage = true): Promise<{ |
| 55 | key: string |
| 56 | encryptedKey?: string |
| 57 | }> { |
| 58 | try { |
| 59 | const hasEncryptionKey = env.API_ENCRYPTION_KEY !== undefined |
| 60 | |
| 61 | const plainKey = hasEncryptionKey ? generateEncryptedApiKey() : generateApiKey() |
| 62 | |
| 63 | if (useStorage) { |
| 64 | const encryptedKey = await encryptApiKeyForStorage(plainKey) |
| 65 | return { key: plainKey, encryptedKey } |
| 66 | } |
| 67 | |
| 68 | return { key: plainKey } |
| 69 | } catch (error) { |
| 70 | logger.error('API key creation error:', { error }) |
| 71 | throw new Error('Failed to create API key') |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Decrypts an API key from storage for display purposes |
no test coverage detected