Skip to content
Open
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
62 changes: 58 additions & 4 deletions packages/core/src/lib/actions/callback/oauth/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,60 @@ export async function handleOAuth(
) {
const { logger, provider } = options

/**
* If the provider supplies a custom `token.request` function, call it
* directly and skip the standard OAuth 2.0 authorization-code grant entirely.
* This is required for protocols that differ from OAuth 2.0, such as
* Steam's OpenID 2.0 flow, where there is no authorization code to exchange
* and no token/issuer endpoint to discover.
*
* This check must happen before the authorization-server setup block below,
* which would otherwise crash for providers that have no `token.url` or `issuer`.
*/
if (provider.token?.request) {
const resCookies: Cookie[] = []
const state = await checks.state.use(cookies, resCookies, options)
const codeVerifier = await checks.pkce.use(cookies, resCookies, options)
const { userinfo } = provider

const tokenResponse = await provider.token.request({
params: params as Record<string, string | undefined>,
checks: { pkce: codeVerifier, state },
provider,
})

if (!tokenResponse) {
throw new OAuthCallbackError(
`Provider "${provider.id}" token.request returned no tokens`
)
}

const tokens: TokenSet & Pick<Account, "expires_at"> =
tokenResponse.tokens as TokenSet & Pick<Account, "expires_at">

let profile: Profile = {}

if (userinfo?.request) {
const _profile = await userinfo.request({ tokens, provider })
if (_profile instanceof Object) profile = _profile
} else if (userinfo?.url) {
const userinfoResponse = await fetch(userinfo.url, {
headers: { Authorization: `Bearer ${tokens.access_token}` },
})
profile = await userinfoResponse.json()
} else {
throw new TypeError("No userinfo endpoint configured")
}

const profileResult = await getUserAndAccount(
profile,
provider,
tokens,
logger
)
return { ...profileResult, profile, cookies: resCookies }
}

let as: o.AuthorizationServer

const { token, userinfo } = provider
Expand Down Expand Up @@ -156,6 +210,10 @@ export async function handleOAuth(
redirect_uri = provider.redirectProxyUrl
}

let profile: Profile = {}

const requireIdToken = isOIDCProvider(provider)

let codeGrantResponse = await o.authorizationCodeGrantRequest(
as,
client,
Expand All @@ -181,10 +239,6 @@ export async function handleOAuth(
codeGrantResponse
}

let profile: Profile = {}

const requireIdToken = isOIDCProvider(provider)

if (provider[conformInternal]) {
switch (provider.id) {
case "microsoft-entra-id":
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/lib/pages/error.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { JSX } from "preact"
import type { ErrorPageParam, Theme } from "../../types.js"

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/lib/utils/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ export function assertConfig(

let key
if (typeof a !== "string" && !a?.url) key = "authorization"
else if (typeof t !== "string" && !t?.url) key = "token"
else if (typeof u !== "string" && !u?.url) key = "userinfo"
else if (typeof t !== "string" && !t?.url && !t?.request) key = "token"
else if (typeof u !== "string" && !u?.url && !u?.request) key = "userinfo"

if (key) {
return new InvalidEndpoints(
Expand Down
Loading
Loading