|
| 1 | +import { |
| 2 | + InvalidSeamHttpOptionsError, |
| 3 | + isSeamHttpOptionsWithApiKey, |
| 4 | + isSeamHttpOptionsWithClientSessionToken, |
| 5 | + type SeamHttpOptions, |
| 6 | + type SeamHttpOptionsWithApiKey, |
| 7 | + type SeamHttpOptionsWithClientSessionToken, |
| 8 | +} from './client-options.js' |
| 9 | + |
| 10 | +type Headers = Record<string, string> |
| 11 | + |
| 12 | +export const getAuthHeaders = (options: SeamHttpOptions): Headers => { |
| 13 | + if (isSeamHttpOptionsWithApiKey(options)) { |
| 14 | + return getAuthHeadersForApiKey(options) |
| 15 | + } |
| 16 | + |
| 17 | + if (isSeamHttpOptionsWithClientSessionToken(options)) { |
| 18 | + return getAuthHeadersForClientSessionToken(options) |
| 19 | + } |
| 20 | + |
| 21 | + throw new InvalidSeamHttpOptionsError( |
| 22 | + 'Must specify an apiKey or clientSessionToken', |
| 23 | + ) |
| 24 | +} |
| 25 | + |
| 26 | +const getAuthHeadersForApiKey = ({ |
| 27 | + apiKey, |
| 28 | +}: SeamHttpOptionsWithApiKey): Headers => { |
| 29 | + if (isClientSessionToken(apiKey)) { |
| 30 | + throw new InvalidSeamTokenError( |
| 31 | + 'A Client Session Token cannot be used as an apiKey', |
| 32 | + ) |
| 33 | + } |
| 34 | + |
| 35 | + if (isAccessToken(apiKey)) { |
| 36 | + throw new InvalidSeamTokenError( |
| 37 | + 'An access token cannot be used as an apiKey', |
| 38 | + ) |
| 39 | + } |
| 40 | + |
| 41 | + if (isJwt(apiKey) || !isSeamToken(apiKey)) { |
| 42 | + throw new InvalidSeamTokenError( |
| 43 | + `Unknown or invalid apiKey format, expected token to start with ${tokenPrefix}`, |
| 44 | + ) |
| 45 | + } |
| 46 | + |
| 47 | + return { |
| 48 | + authorization: `Bearer ${apiKey}`, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +const getAuthHeadersForClientSessionToken = ({ |
| 53 | + clientSessionToken, |
| 54 | +}: SeamHttpOptionsWithClientSessionToken): Headers => { |
| 55 | + if (!isClientSessionToken(clientSessionToken)) { |
| 56 | + throw new InvalidSeamTokenError( |
| 57 | + `Unknown or invalid clientSessionToken format, expected token to start with ${clientSessionTokenPrefix}`, |
| 58 | + ) |
| 59 | + } |
| 60 | + |
| 61 | + return { |
| 62 | + authorization: `Bearer ${clientSessionToken}`, |
| 63 | + 'client-session-token': clientSessionToken, |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +export class InvalidSeamTokenError extends Error { |
| 68 | + constructor(message: string) { |
| 69 | + super(`SeamHttp received an invalid token: ${message}`) |
| 70 | + this.name = this.constructor.name |
| 71 | + Error.captureStackTrace(this, this.constructor) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +const tokenPrefix = 'seam_' |
| 76 | + |
| 77 | +const clientSessionTokenPrefix = 'seam_cst' |
| 78 | + |
| 79 | +const isClientSessionToken = (token: string): boolean => |
| 80 | + token.startsWith(clientSessionTokenPrefix) |
| 81 | + |
| 82 | +const isAccessToken = (token: string): boolean => token.startsWith('seam_at') |
| 83 | + |
| 84 | +const isJwt = (token: string): boolean => token.startsWith('ey') |
| 85 | + |
| 86 | +const isSeamToken = (token: string): boolean => token.startsWith(tokenPrefix) |
0 commit comments