Skip to content
Open
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
68 changes: 51 additions & 17 deletions packages/core/src/lib/actions/callback/oauth/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,24 +156,58 @@ export async function handleOAuth(
redirect_uri = provider.redirectProxyUrl
}

let codeGrantResponse = await o.authorizationCodeGrantRequest(
as,
client,
clientAuth,
codeGrantParams,
redirect_uri,
codeVerifier ?? "decoy",
{
// TODO: move away from allowing insecure HTTP requests
[o.allowInsecureRequests]: true,
[o.customFetch]: (...args) => {
if (!provider.checks.includes("pkce")) {
args[1].body.delete("code_verifier")
}
return (provider[customFetch] ?? fetch)(...args)
},
let codeGrantResponse: Response

// Check if there is a custom token request method
if (provider.token?.request && typeof provider.token.request === 'function') {
try {
const tokenResult = await provider.token.request({
params: Object.fromEntries(codeGrantParams.entries()),
checks: {},
provider: provider
})

if (tokenResult && tokenResult.tokens) {
codeGrantResponse = new Response(JSON.stringify(tokenResult.tokens), {
status: 200,
headers: { 'Content-Type': 'application/json' }
})
} else {
throw new Error('Invalid token response from custom request')
}
} catch (error) {
logger.error(
new OAuthCallbackError("Custom token request failed", {
providerId: provider.id,
error: error instanceof Error ? error.message : 'Unknown error'
})
)
throw new OAuthCallbackError("Custom token request failed", {
providerId: provider.id,
error: error instanceof Error ? error.message : 'Unknown error'
})
}
)
} else {
// Use standard OAuth2 process
codeGrantResponse = await o.authorizationCodeGrantRequest(
as,
client,
clientAuth,
codeGrantParams,
redirect_uri,
codeVerifier ?? "decoy",
{
// TODO: move away from allowing insecure HTTP requests
[o.allowInsecureRequests]: true,
[o.customFetch]: (...args) => {
if (!provider.checks.includes("pkce")) {
args[1].body.delete("code_verifier")
}
return (provider[customFetch] ?? fetch)(...args)
},
}
)
}

if (provider.token?.conform) {
codeGrantResponse =
Expand Down