Skip to content

Commit

Permalink
Add SeamHttp.fromConsoleSessionToken
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x committed Nov 9, 2023
1 parent e6d1f5d commit a560c73
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 2 deletions.
48 changes: 47 additions & 1 deletion src/lib/seam/connect/auth.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {
isSeamHttpOptionsWithApiKey,
isSeamHttpOptionsWithClientSessionToken,
isSeamHttpOptionsWithConsoleSessionToken,
SeamHttpInvalidOptionsError,
type SeamHttpOptionsWithApiKey,
type SeamHttpOptionsWithClientSessionToken,
type SeamHttpOptionsWithConsoleSessionToken,
} from './options.js'
import type { Options } from './parse-options.js'

Expand All @@ -22,8 +24,18 @@ export const getAuthHeaders = (options: Options): Headers => {
return getAuthHeadersForClientSessionToken(options)
}

if (isSeamHttpOptionsWithConsoleSessionToken(options)) {
return getAuthHeadersForConsoleSessionToken(options)
}

throw new SeamHttpInvalidOptionsError(
'Must specify an apiKey, clientSessionToken, or publishableKey',
[
'Must specify',
'an apiKey,',
'clientSessionToken,',
'publishableKey,',
'or consoleSessionToken with a workspaceId',
].join(' '),
)
}

Expand Down Expand Up @@ -96,6 +108,40 @@ const getAuthHeadersForClientSessionToken = ({
}
}

const getAuthHeadersForConsoleSessionToken = ({
consoleSessionToken,
workspaceId,
}: SeamHttpOptionsWithConsoleSessionToken): Headers => {
if (isJwt(consoleSessionToken)) {
throw new SeamHttpInvalidTokenError(
'A JWT cannot be used as a consoleSessionToken',
)
}

if (isClientSessionToken(consoleSessionToken)) {
throw new SeamHttpInvalidTokenError(
'A Client Session Token cannot be used as a consoleSessionToken',
)
}

if (isPublishableKey(consoleSessionToken)) {
throw new SeamHttpInvalidTokenError(
'A Publishable Key cannot be used as a clientSessionToken',
)
}

if (!isAccessToken(consoleSessionToken)) {
throw new SeamHttpInvalidTokenError(
`Unknown or invalid consoleSessionToken format, expected token to start with ${accessTokenPrefix}`,
)
}

return {
authorization: `Bearer ${consoleSessionToken}`,
'seam-workspace-id': workspaceId,
}
}

const getAuthHeadersForPublishableKey = (publishableKey: string): Headers => {
if (isJwt(publishableKey)) {
throw new SeamHttpInvalidTokenError(
Expand Down
42 changes: 41 additions & 1 deletion src/lib/seam/connect/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type SeamHttpOptions =
| SeamHttpOptionsWithClient
| SeamHttpOptionsWithApiKey
| SeamHttpOptionsWithClientSessionToken
| SeamHttpOptionsWithConsoleSessionToken

interface SeamHttpCommonOptions extends ClientOptions {
endpoint?: string
Expand Down Expand Up @@ -53,6 +54,12 @@ export const isSeamHttpOptionsWithApiKey = (
)
}

if ('consoleSessionToken' in options && options.consoleSessionToken != null) {
throw new SeamHttpInvalidOptionsError(
'The consoleSessionToken option cannot be used with the apiKey option',
)
}

return true
}

Expand All @@ -69,7 +76,40 @@ export const isSeamHttpOptionsWithClientSessionToken = (

if ('apiKey' in options && options.apiKey != null) {
throw new SeamHttpInvalidOptionsError(
'The clientSessionToken option cannot be used with the apiKey option',
'The apiKey option cannot be used with the clientSessionToken option',
)
}

if ('consoleSessionToken' in options && options.consoleSessionToken != null) {
throw new SeamHttpInvalidOptionsError(
'The consoleSessionToken option cannot be used with the clientSessionToken option',
)
}

return true
}

export interface SeamHttpOptionsWithConsoleSessionToken
extends SeamHttpCommonOptions {
consoleSessionToken: string
workspaceId: string
}

export const isSeamHttpOptionsWithConsoleSessionToken = (
options: SeamHttpOptions,
): options is SeamHttpOptionsWithConsoleSessionToken => {
if (!('consoleSessionToken' in options)) return false
if (options.consoleSessionToken == null) return false

if ('apiKey' in options && options.apiKey != null) {
throw new SeamHttpInvalidOptionsError(
'The apiKey option cannot be used with the consoleSessionToken option',
)
}

if ('clientSessionToken' in options && options.clientSessionToken != null) {
throw new SeamHttpInvalidOptionsError(
'The clientSessionToken option cannot be used with the consoleSessionToken option',
)
}

Expand Down
19 changes: 19 additions & 0 deletions src/lib/seam/connect/seam-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import {
isSeamHttpOptionsWithApiKey,
isSeamHttpOptionsWithClient,
isSeamHttpOptionsWithClientSessionToken,
isSeamHttpOptionsWithConsoleSessionToken,
type SeamHttpFromPublishableKeyOptions,
SeamHttpInvalidOptionsError,
type SeamHttpOptions,
type SeamHttpOptionsWithApiKey,
type SeamHttpOptionsWithClient,
type SeamHttpOptionsWithClientSessionToken,
type SeamHttpOptionsWithConsoleSessionToken,
} from './options.js'
import { parseOptions } from './parse-options.js'
import {
Expand Down Expand Up @@ -87,6 +89,23 @@ export class SeamHttp {
return SeamHttp.fromClientSessionToken(token, options)
}

static fromConsoleSessionToken(
consoleSessionToken: SeamHttpOptionsWithConsoleSessionToken['consoleSessionToken'],
workspaceId: SeamHttpOptionsWithConsoleSessionToken['workspaceId'],
options: Omit<
SeamHttpOptionsWithConsoleSessionToken,
'consoleSessionToken' | 'workspaceId'
> = {},
): SeamHttp {
const constructorOptions = { ...options, consoleSessionToken, workspaceId }
if (!isSeamHttpOptionsWithConsoleSessionToken(constructorOptions)) {
throw new SeamHttpInvalidOptionsError(
'Missing consoleSessionToken or workspaceId',
)
}
return new SeamHttp(constructorOptions)
}

get accessCodes(): SeamHttpAccessCodes {
return SeamHttpAccessCodes.fromClient(this.client)
}
Expand Down

0 comments on commit a560c73

Please sign in to comment.