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
6 changes: 2 additions & 4 deletions src/auth/cloudflare-auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { z } from 'zod'

import type { AuthRequest } from '@cloudflare/workers-oauth-provider'

const PKCE_CHARSET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~'
const CODE_VERIFIER_LENGTH = 96

Expand Down Expand Up @@ -44,15 +42,15 @@ export async function generatePKCECodes(): Promise<PKCECodes> {
export async function getAuthorizationURL(params: {
client_id: string
redirect_uri: string
state: AuthRequest
stateToken: string
scopes: string[]
codeChallenge: string
}): Promise<{ authUrl: string }> {
const urlParams = new URLSearchParams({
response_type: 'code',
client_id: params.client_id,
redirect_uri: params.redirect_uri,
state: btoa(JSON.stringify(params.state)),
state: params.stateToken,
code_challenge: params.codeChallenge,
code_challenge_method: 'S256',
scope: params.scopes.join(' ')
Expand Down
22 changes: 4 additions & 18 deletions src/auth/oauth-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,15 @@ export async function handleTokenExchangeCallback(
*/
async function redirectToCloudflare(
requestUrl: string,
oauthReqInfo: AuthRequest,
stateToken: string,
codeChallenge: string,
scopes: string[],
additionalHeaders: Record<string, string> = {}
): Promise<Response> {
const stateWithToken: AuthRequest = {
...oauthReqInfo,
state: stateToken
}

const { authUrl } = await getAuthorizationURL({
client_id: env.CLOUDFLARE_CLIENT_ID,
redirect_uri: new URL('/oauth/callback', requestUrl).href,
state: stateWithToken,
stateToken,
scopes,
codeChallenge
})
Expand Down Expand Up @@ -193,16 +187,9 @@ export function createAuthHandlers() {
const stateToken = await createOAuthState(oauthReqInfo, env.OAUTH_KV, codeVerifier)
const { setCookie: sessionCookie } = await bindStateToSession(stateToken)

return redirectToCloudflare(
c.req.url,
oauthReqInfo,
stateToken,
codeChallenge,
defaultScopes,
{
'Set-Cookie': sessionCookie
}
)
return redirectToCloudflare(c.req.url, stateToken, codeChallenge, defaultScopes, {
'Set-Cookie': sessionCookie
})
}

// Client not approved - show consent dialog with scope selection
Expand Down Expand Up @@ -263,7 +250,6 @@ export function createAuthHandlers() {

const redirectResponse = await redirectToCloudflare(
c.req.url,
oauthReqInfo,
stateToken,
codeChallenge,
scopesToRequest
Expand Down
12 changes: 1 addition & 11 deletions src/auth/workers-oauth-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1101,17 +1101,7 @@ export async function validateOAuthState(
throw new OAuthError('invalid_request', 'Missing state parameter')
}

// Decode state to extract embedded stateToken
let stateToken: string
try {
const decodedState = JSON.parse(atob(stateFromQuery))
stateToken = decodedState.state
if (!stateToken) {
throw new Error('State token not found')
}
} catch {
throw new OAuthError('invalid_request', 'Failed to decode state')
}
const stateToken = stateFromQuery

// Validate state exists in KV
const storedDataJson = await kv.get(`oauth:state:${stateToken}`)
Expand Down