Skip to content

Connection validation and making expires_at optional #244

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

Merged
merged 6 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
50 changes: 32 additions & 18 deletions kits/cdk/internal/oauthConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ export const zOauthConnectionError = z.object({
message: z.string().nullish(),
})

const zOauthCredentials = z.object({
type: zAuthMode,
/** For API key auth... */
api_key: z.string().nullish(),
access_token: z.string().optional(),
refresh_token: z.string().optional(),
// sometimes this is missing from the response
expires_at: z.string().datetime().optional(),
raw: z.object({
access_token: z.string(),
expires_in: z.number(),
// sometimes this is missing from the response
expires_at: z.string().datetime().optional(),
/** Refresh token (Only returned if the REFRESH_TOKEN boolean parameter is set to true and the refresh token is available) */
refresh_token: z.string().nullish(),
refresh_token_expires_in: z.number().nullish(),
token_type: z.string(), //'bearer',
scope: z.string().optional(),
}),
});
export const oauthBaseSchema = {
name: z.literal('__oauth__'), // TODO: This is a noop
connectorConfig: z.object({
Expand All @@ -40,24 +60,7 @@ export const oauthBaseSchema = {
connectionSettings: z.object({
// equivalent to nango /v1/connections data.connection object with certain fields removed like id
oauth: z.object({
credentials: z.object({
type: zAuthMode,
/** For API key auth... */
api_key: z.string().nullish(),
access_token: z.string().optional(),
refresh_token: z.string().optional(),
expires_at: z.string().datetime(),
raw: z.object({
access_token: z.string(),
expires_in: z.number(),
expires_at: z.string().datetime(),
/** Refresh token (Only returned if the REFRESH_TOKEN boolean parameter is set to true and the refresh token is available) */
refresh_token: z.string().nullish(),
refresh_token_expires_in: z.number().nullish(),
token_type: z.string(), //'bearer',
scope: z.string().optional(),
}),
}),
credentials: zOauthCredentials,
connection_config: z
.object({
portalId: z.number().nullish(),
Expand Down Expand Up @@ -156,6 +159,17 @@ export function makeOauthConnectorServer({
},
})
.then((r) => r.data as OauthBaseTypes['connectionSettings'])


const parsed = zOauthCredentials.safeParse(res)
if (!parsed.success) {
console.error(
'Provider did not return valid connection settings',
parsed?.error?.format(),
)
throw new Error('Provider did not return valid connection settings')
}

return {
connectionExternalId: extractId(connId)[2],
settings: {
Expand Down
3 changes: 1 addition & 2 deletions packages/api/proxyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export const proxyHandler = async (req: Request) => {
const protectedContext = getProtectedContext(ctx)
const remoteContext = await getRemoteContext(protectedContext)

const credentialsExpired = remoteContext.remote.settings.oauth?.credentials
.expires_at
const credentialsExpired = remoteContext.remote.settings.oauth?.credentials?.expires_at
? new Date(remoteContext.remote.settings.oauth.credentials.expires_at) <
new Date()
: false
Expand Down