-
Notifications
You must be signed in to change notification settings - Fork 2
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(openIntProxyLink): Adding Openint proxy link #39
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
12d4e43
adding openIntProxyLink
pellicceama 8340ea4
migratinng sdk-qbo to new auth paradigm
pellicceama 4feb3cf
adding qbo proxy spec
pellicceama 00f37c1
migrating apollo
pellicceama 02d314a
adding authHeadersLink
pellicceama 5e03a24
lints in proxyLink
pellicceama f34d9ad
supporting new auth parameter paradigm
pellicceama e20fed1
adding new authLink
pellicceama 4434f24
qbo proxy tests
pellicceama 9e79f96
lint
pellicceama 9fbcc5a
fix: some pr review changes
tonyxiao 1356b24
fix: type error
tonyxiao 2787124
fix: type error
tonyxiao bc25c89
fix: test
tonyxiao 01ebc72
fixing circular dependency
pellicceama File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,51 @@ | ||
import type {UnionToIntersection} from 'type-fest' | ||
import {openIntProxyLink, OpenIntProxyLinkOptions} from './openIntProxyLink.js' | ||
import {mergeHeaders, modifyRequest} from '../modifyRequestResponse.js' | ||
import type {Link} from '../link.js' | ||
|
||
export type ClientAuthOptions = | ||
| {openInt: OpenIntProxyLinkOptions} | ||
/** to be passed as Authorization header as a bearer token, Should handle automatic refreshing */ | ||
| {oauth: {accessToken: string; refreshToken?: string; expiresAt?: number}} | ||
| {basic: {username: string; password: string}} | ||
/** non oauth / directly specifying bearer token */ | ||
| {bearer: string} | ||
|
||
type Indexify<T> = T & Record<string, undefined> | ||
type AllUnionKeys<T> = keyof UnionToIntersection<{[K in keyof T]: undefined}> | ||
type NonDiscriminatedUnion<T> = { | ||
[K in AllUnionKeys<T> & string]: Indexify<T>[K] | ||
} | ||
|
||
export function authLink(_auth: ClientAuthOptions, baseUrl: string): Link { | ||
if (!_auth) { | ||
// No Op | ||
return (req, next) => next(req) | ||
} | ||
const auth = _auth as NonDiscriminatedUnion<ClientAuthOptions> | ||
|
||
if (auth.openInt) { | ||
return openIntProxyLink(auth.openInt, baseUrl) | ||
} | ||
|
||
const headers = { | ||
['authorization']: auth.oauth?.accessToken | ||
? `Bearer ${auth.oauth.accessToken}` | ||
: auth?.bearer | ||
? `Bearer ${auth.bearer}` | ||
: auth?.basic | ||
? `Basic ${btoa(`${auth.basic?.username}:${auth.basic?.password}`)}` | ||
: '', | ||
} satisfies HeadersInit | ||
|
||
return async (req, next) => { | ||
req.headers.delete('authorization') | ||
const res = await next( | ||
modifyRequest(req, { | ||
headers: mergeHeaders(req.headers, headers, {}), | ||
body: req.body, | ||
}), | ||
) | ||
return res | ||
} | ||
} |
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,120 @@ | ||
import {mergeHeaders, modifyRequest} from '../index.js' | ||
import type {Link} from '../link.js' | ||
|
||
export type OpenIntProxyLinkOptions = { | ||
token?: string | ||
apiKey?: string | ||
resourceId?: string | ||
endUserId?: string | ||
connectorName?: string | ||
} | ||
|
||
// TODO: Use something like this to validate and generate the types too | ||
// const AuthClientOptionsSchema = z.union([ | ||
// z.object({ | ||
// openInt: z.object({ | ||
// // Add OpenIntProxyLinkOptions fields here | ||
// }).optional() | ||
// }), | ||
// z.object({ | ||
// oauth: z.object({ | ||
// accessToken: z.string(), | ||
// refreshToken: z.string().optional(), | ||
// expiresAt: z.number().optional() | ||
// }).optional() | ||
// }), | ||
// z.object({ | ||
// basic: z.object({ | ||
// username: z.string(), | ||
// password: z.string() | ||
// }).optional() | ||
// }), | ||
// z.object({ | ||
// bearer: z.string().optional() | ||
// }) | ||
// ]) | ||
|
||
export function validateOpenIntProxyLinkOptions( | ||
options: OpenIntProxyLinkOptions, | ||
): boolean { | ||
const {token, apiKey, resourceId, endUserId, connectorName} = options ?? {} | ||
|
||
const hasToken = !!token | ||
const hasApiKey = !!apiKey | ||
const hasResourceId = !!resourceId | ||
const hasEndUserId = !!endUserId | ||
const hasConnectorName = !!connectorName | ||
|
||
const expectsAuthProxy = | ||
hasToken || hasApiKey || hasResourceId || hasEndUserId || hasConnectorName | ||
if ( | ||
expectsAuthProxy && | ||
!( | ||
(hasToken && hasResourceId) || | ||
(hasToken && hasConnectorName) || | ||
(hasApiKey && hasResourceId) || | ||
(hasApiKey && hasEndUserId && hasConnectorName) | ||
) | ||
) { | ||
throw new Error( | ||
'Invalid configuration for proxy authentication. You must provide one of the following combinations: ' + | ||
'1) token AND resourceId, ' + | ||
'2) token AND connectorName, ' + | ||
'3) apiKey AND resourceId, ' + | ||
'4) apiKey AND endUserId AND connectorName, ' + | ||
'or none of these options and instead authenticate directly.', | ||
) | ||
} | ||
return expectsAuthProxy | ||
} | ||
|
||
interface OpenIntProxyHeaders { | ||
authorization?: `Bearer ${string}` | ||
'x-apikey'?: string | ||
'x-resource-id'?: string | ||
'x-resource-end-user-id'?: string | ||
'x-resource-connector-name'?: string | ||
} | ||
|
||
function removeEmptyHeaders(headers: OpenIntProxyHeaders): HeadersInit { | ||
return Object.fromEntries( | ||
Object.entries(headers).filter(([_, value]) => value && value !== ''), | ||
) satisfies HeadersInit | ||
} | ||
|
||
export function openIntProxyLink( | ||
opts: OpenIntProxyLinkOptions, | ||
baseUrl: string, | ||
): Link { | ||
validateOpenIntProxyLinkOptions(opts) | ||
const {apiKey, token, resourceId, endUserId, connectorName} = opts | ||
|
||
const headers = removeEmptyHeaders({ | ||
['x-apikey']: apiKey || '', | ||
['authorization']: token ? `Bearer ${token}` : undefined, | ||
['x-resource-id']: resourceId || '', | ||
['x-resource-end-user-id']: endUserId || '', | ||
['x-resource-connector-name']: connectorName || '', | ||
}) satisfies HeadersInit | ||
|
||
return async (req, next) => { | ||
// TODO: Check if we are already proxying and throw an error if so | ||
// if (req.url.includes(proxyUrl)) { | ||
// // Was previously necessary as link called twice leading to /api/proxy/api/proxy/? | ||
// return next(req) | ||
// } | ||
const proxyUrl = 'https://api.openint.dev/proxy' | ||
|
||
// Remove the authorization header because it will be overwritten by the proxy anyways | ||
// TODO: Think about using non-standard header for auth to avoid this maybe? | ||
req.headers.delete('authorization') | ||
const res = await next( | ||
modifyRequest(req, { | ||
url: req.url.replace(baseUrl, proxyUrl), | ||
headers: mergeHeaders(req.headers, headers, {}), | ||
body: req.body, | ||
}), | ||
) | ||
return res | ||
} | ||
} |
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
OpenIntProxyHeaders
interface is defined twice in this file. Consider removing the duplicate definition to avoid confusion.