Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk-openint): fixing auth #44

Merged
merged 9 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion packages/fetch-links/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type {HTTPMethod} from './link.js'
import {applyLinks} from './link.js'
import {axiosLink} from './links/axiosLink.js'
import {fetchLink, logLink, retryLink, throwLink} from './links/index.js'
import {authLink, echoLink, fetchLink, logLink, retryLink, throwLink} from './links/index.js'
import {modifyRequest, modifyResponse} from './modifyRequestResponse.js'

const req = new Request('https://httpbin.org/anything', {method: 'GET'})
Expand Down Expand Up @@ -152,3 +152,66 @@

// TODO: Test error handling for both 4xx and 5xx errors
})

describe('authLink', () => {
test('openInt user auth fails with no resourceId', async () => {
try {
await applyLinks(req, [authLink({openInt: {token: '123'}}, 'https://httpbin.org'), fetchLink()])
} catch (e) {
expect(e).toBeInstanceOf(Error)

Check failure on line 161 in packages/fetch-links/index.spec.ts

View workflow job for this annotation

GitHub Actions / Run type checks, lint, and tests

Avoid calling `expect` conditionally`

Check failure on line 161 in packages/fetch-links/index.spec.ts

View workflow job for this annotation

GitHub Actions / Run type checks, lint, and tests

Avoid calling `expect` conditionally`
}
})

test('OpenInt user auth succeeds with resourceId', async () => {
const res = await applyLinks(req, [authLink({openInt: {token: '123', resourceId: '123'}}, 'https://httpbin.org'), echoLink()])
const json: any = await res.json()
expect(json.headers['authorization']).toEqual('Bearer 123')
expect(json.headers['x-resource-id']).toEqual('123')
expect(json.headers['x-resource-connector-name']).toEqual(undefined)
expect(json.headers['x-resource-connector-config-id']).toEqual(undefined)
expect(json.headers['x-resource-end-user-id']).toEqual(undefined)
expect(json.url).toContain('openint.dev')
})

test('openInt admin auth', async () => {
const res = await applyLinks(req, [authLink({openInt: {apiKey: '123', resourceId: '456'}}, 'https://httpbin.org'), echoLink()])
const json: any = await res.json()
expect(json.headers['x-apikey']).toEqual('123')
expect(json.headers['x-resource-id']).toEqual('456')
expect(json.headers['x-resource-connector-name']).toEqual(undefined)
expect(json.headers['x-resource-connector-config-id']).toEqual(undefined)
expect(json.headers['x-resource-end-user-id']).toEqual(undefined)
expect(json.url).toContain('openint.dev')
})

test('openInt user auth fails with token and no connectorName', async () => {
try {
await applyLinks(req, [authLink({openInt: {token: '123'}}, 'https://httpbin.org'), fetchLink()])
} catch (e) {
expect(e).toBeInstanceOf(Error)

Check failure on line 191 in packages/fetch-links/index.spec.ts

View workflow job for this annotation

GitHub Actions / Run type checks, lint, and tests

Avoid calling `expect` conditionally`

Check failure on line 191 in packages/fetch-links/index.spec.ts

View workflow job for this annotation

GitHub Actions / Run type checks, lint, and tests

Avoid calling `expect` conditionally`
}
})

test('openInt user auth succeeds with token and connectorName', async () => {
const res = await applyLinks(req, [authLink({openInt: {token: '123', connectorName: 'myConnector'}}, 'https://httpbin.org'), echoLink()])
const json: any = await res.json()
expect(json.headers['authorization']).toEqual('Bearer 123')
expect(json.headers['x-resource-connector-name']).toEqual('myConnector')
})

test('openInt admin auth fails with apiKey and no endUserId or connectorName', async () => {
try {
await applyLinks(req, [authLink({openInt: {apiKey: '123'}}, 'https://httpbin.org'), fetchLink()])
} catch (e) {
expect(e).toBeInstanceOf(Error)

Check failure on line 206 in packages/fetch-links/index.spec.ts

View workflow job for this annotation

GitHub Actions / Run type checks, lint, and tests

Avoid calling `expect` conditionally`

Check failure on line 206 in packages/fetch-links/index.spec.ts

View workflow job for this annotation

GitHub Actions / Run type checks, lint, and tests

Avoid calling `expect` conditionally`
}
})

test('openInt admin auth succeeds with apiKey, endUserId, and connectorName', async () => {
const res = await applyLinks(req, [authLink({openInt: {apiKey: '123', endUserId: '789', connectorName: 'myConnector'}}, 'https://httpbin.org'), echoLink()])
const json: any = await res.json()
expect(json.headers['x-apikey']).toEqual('123')
expect(json.headers['x-resource-end-user-id']).toEqual('789')
expect(json.headers['x-resource-connector-name']).toEqual('myConnector')
})
})
8 changes: 8 additions & 0 deletions packages/fetch-links/links/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ export * from './authLink.js'

// MARK: Built-in links

export function echoLink(): Link {
return async (req) => Response.json({
url: req.url,
headers: Object.fromEntries(req.headers.entries()),
bodyText: await req.text(),
})
}

export function fetchLink({
fetch = globalThis.fetch.bind(globalThis),
}: {
Expand Down
2 changes: 1 addition & 1 deletion sdks/sdk-openint/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@opensdks/sdk-openint",
"version": "0.0.14",
"version": "0.0.15",
"type": "module",
"exports": {
".": {
Expand Down
42 changes: 32 additions & 10 deletions sdks/sdk-openint/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export type OpenIntSDKTypes = SDKTypes<
openintTypes,
Omit<ClientOptions, 'headers'> & {
headers: {
/** organization auth */
'x-apikey'?: string
/** Bearer token, for end user auth */
authorization?: `Bearer ${string}`
/** For passthrough and resource specific api */
'x-resource-id'?: string
/** Alternative ways to pass the resource id, works in case there is a single connector */
Expand All @@ -27,19 +31,37 @@ export const openintSdkDef = {
oasMeta: openintOasMeta,
} satisfies SdkDefinition<OpenIntSDKTypes>

function generateHeaders(opts: OpenIntSDKTypes['options']) {
const headers: Record<string, string> = {};

if (opts.apiKey) {
headers['x-apikey'] = opts.apiKey;
}
if (opts.token) {
headers['authorization'] = `Bearer ${opts.token}`;
}
if (opts.resourceId) {
headers['x-resource-id'] = opts.resourceId;
}
if (opts.endUserId) {
headers['x-resource-end-user-id'] = opts.endUserId;
}
if (opts.connectorName) {
headers['x-resource-connector-name'] = opts.connectorName;
}

return headers;
}

export function initOpenIntSDK(opts: OpenIntSDKTypes['options']) {
// @ts-expect-error
const headers = {...opts.headers, ...generateHeaders({...opts, ...opts?.auth?.openInt})}
// @ts-expect-error
delete opts?.auth?.openInt;

return initSDK(openintSdkDef, {
...opts,
// new way of passing auth options
auth: {
openInt: {
token: opts.token,
apiKey: opts.apiKey,
resourceId: opts.resourceId,
endUserId: opts.endUserId,
connectorName: opts.connectorName,
}
}
headers
})
}

Expand Down
Loading