| import crypto from "crypto"; |
|
|
| const API_KEY_SECRET = process.env.API_KEY_SECRET || "endpoint-proxy-api-key-secret"; |
|
|
| |
| |
| |
| function generateKeyId() { |
| const chars = "abcdefghijklmnopqrstuvwxyz0123456789"; |
| let result = ""; |
| for (let i = 0; i < 6; i++) { |
| result += chars.charAt(Math.floor(Math.random() * chars.length)); |
| } |
| return result; |
| } |
|
|
| |
| |
| |
| function generateCrc(machineId, keyId) { |
| return crypto |
| .createHmac("sha256", API_KEY_SECRET) |
| .update(machineId + keyId) |
| .digest("hex") |
| .slice(0, 8); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function generateApiKeyWithMachine(machineId) { |
| const keyId = generateKeyId(); |
| const crc = generateCrc(machineId, keyId); |
| const key = `sk-${machineId}-${keyId}-${crc}`; |
| return { key, keyId }; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export function parseApiKey(apiKey) { |
| if (!apiKey || !apiKey.startsWith("sk-")) return null; |
|
|
| const parts = apiKey.split("-"); |
| |
| |
| if (parts.length === 4) { |
| const [, machineId, keyId, crc] = parts; |
| |
| |
| const expectedCrc = generateCrc(machineId, keyId); |
| if (crc !== expectedCrc) return null; |
| |
| return { machineId, keyId, isNewFormat: true }; |
| } |
| |
| |
| if (parts.length === 2) { |
| return { machineId: null, keyId: parts[1], isNewFormat: false }; |
| } |
| |
| return null; |
| } |
|
|
| |
| |
| |
| |
| |
| export function verifyApiKeyCrc(apiKey) { |
| const parsed = parseApiKey(apiKey); |
| if (!parsed) return false; |
| |
| |
| if (!parsed.isNewFormat) return true; |
| |
| |
| return true; |
| } |
|
|
| |
| |
| |
| |
| |
| export function isNewFormatKey(apiKey) { |
| const parsed = parseApiKey(apiKey); |
| return parsed?.isNewFormat === true; |
| } |
|
|
|
|