-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
322 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import type { | ||
SeamHttpOptionsWithApiKey, | ||
SeamHttpOptionsWithClientSessionToken, | ||
} from './client-options.js' | ||
|
||
type Headers = Record<string, string> | ||
|
||
export const getAuthHeadersForApiKey = ({ | ||
apiKey, | ||
}: SeamHttpOptionsWithApiKey): Headers => { | ||
if (isClientSessionToken(apiKey)) { | ||
throw new InvalidSeamTokenError( | ||
'A Client Session Token cannot be used as an apiKey', | ||
) | ||
} | ||
|
||
if (isAccessToken(apiKey)) { | ||
throw new InvalidSeamTokenError( | ||
'An access token cannot be used as an apiKey without specifying a workspaceId', | ||
) | ||
} | ||
|
||
if (isJwt(apiKey) || !isSeamToken(apiKey)) { | ||
throw new InvalidSeamTokenError( | ||
`Unknown or Invalid apiKey format, expected token to start with ${tokenPrefix}`, | ||
) | ||
} | ||
|
||
return { | ||
authorization: `Bearer ${apiKey}`, | ||
} | ||
} | ||
|
||
export const getAuthHeadersForClientSessionToken = ({ | ||
clientSessionToken, | ||
}: SeamHttpOptionsWithClientSessionToken): Headers => { | ||
if (!isClientSessionToken(clientSessionToken)) { | ||
throw new InvalidSeamTokenError( | ||
`Unknown or invalid clientSessionToken format, expected token to start with ${clientSessionTokenPrefix}`, | ||
) | ||
} | ||
|
||
return { | ||
authorization: `Bearer ${clientSessionToken}`, | ||
'client-session-token': clientSessionToken, | ||
} | ||
} | ||
|
||
export class InvalidSeamTokenError extends Error { | ||
constructor(message: string) { | ||
super(`SeamHttp received an authorization invalid token: ${message}`) | ||
this.name = this.constructor.name | ||
Error.captureStackTrace(this, this.constructor) | ||
} | ||
} | ||
|
||
const tokenPrefix = 'seam_' | ||
|
||
const clientSessionTokenPrefix = 'seam_cst' | ||
|
||
const isClientSessionToken = (token: string): boolean => | ||
token.startsWith(clientSessionTokenPrefix) | ||
|
||
const isAccessToken = (token: string): boolean => token.startsWith('seam_at') | ||
|
||
const isJwt = (token: string): boolean => token.startsWith('ey') | ||
|
||
const isSeamToken = (token: string): boolean => token.startsWith(tokenPrefix) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import type { AxiosRequestConfig } from 'axios' | ||
|
||
export type SeamHttpOptions = | ||
| SeamHttpOptionsWithApiKey | ||
| SeamHttpOptionsWithClientSessionToken | ||
|
||
interface SeamHttpCommonOptions { | ||
endpoint?: string | ||
axiosOptions?: AxiosRequestConfig | ||
} | ||
|
||
export interface SeamHttpOptionsWithApiKey extends SeamHttpCommonOptions { | ||
apiKey: string | ||
} | ||
|
||
export const isSeamHttpOptionsWithApiKey = ( | ||
options: SeamHttpOptions, | ||
): options is SeamHttpOptionsWithApiKey => { | ||
if (!('apiKey' in options)) return false | ||
|
||
if ('clientSessionToken' in options && options.clientSessionToken != null) { | ||
throw new InvalidSeamHttpOptionsError( | ||
'The clientSessionToken option cannot be used with the apiKey option.', | ||
) | ||
} | ||
|
||
return true | ||
} | ||
|
||
export interface SeamHttpOptionsWithClientSessionToken | ||
extends SeamHttpCommonOptions { | ||
clientSessionToken: string | ||
} | ||
|
||
export const isSeamHttpOptionsWithClientSessionToken = ( | ||
options: SeamHttpOptions, | ||
): options is SeamHttpOptionsWithClientSessionToken => { | ||
if (!('clientSessionToken' in options)) return false | ||
|
||
if ('apiKey' in options && options.apiKey != null) { | ||
throw new InvalidSeamHttpOptionsError( | ||
'The clientSessionToken option cannot be used with the apiKey option.', | ||
) | ||
} | ||
|
||
return true | ||
} | ||
|
||
export class InvalidSeamHttpOptionsError extends Error { | ||
constructor(message: string) { | ||
super(`SeamHttp received invalid options: ${message}`) | ||
this.name = this.constructor.name | ||
Error.captureStackTrace(this, this.constructor) | ||
} | ||
} |
Oops, something went wrong.