-
Notifications
You must be signed in to change notification settings - Fork 186
feat: add Cloudflare OAuth provider #529
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
Open
franklin-tina
wants to merge
2
commits into
atinux:main
Choose a base branch
from
franklin-tina:feat/cloudflare-oauth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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,17 @@ | ||
| export default defineOAuthCloudflareEventHandler({ | ||
| config: { | ||
| // Cloudflare has no default scope — set the client's registered API-permission ID(s) | ||
| // (from GET /client/v4/oauth/scopes). `openid` is not a Cloudflare scope. | ||
| scope: ['user-details.read'], | ||
| }, | ||
| async onSuccess(event, { user }) { | ||
| await setUserSession(event, { | ||
| user: { | ||
| cloudflare: user.sub, | ||
| }, | ||
| loggedInAt: Date.now(), | ||
| }) | ||
|
|
||
| return sendRedirect(event, '/') | ||
| }, | ||
| }) |
This file contains hidden or 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 hidden or 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 hidden or 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,172 @@ | ||
| import type { H3Event } from 'h3' | ||
| import { eventHandler, getQuery, sendRedirect, createError } from 'h3' | ||
| import { withQuery } from 'ufo' | ||
| import { defu } from 'defu' | ||
| import { getOAuthRedirectURL, handleAccessTokenErrorResponse, handleInvalidState, handleMissingConfiguration, handlePkceVerifier, handleState, requestAccessToken } from '../utils' | ||
| import { useRuntimeConfig } from '#imports' | ||
| import type { OAuthConfig } from '#auth-utils' | ||
|
|
||
| /** | ||
| * Cloudflare OAuth provider — Authorization Code flow (the only flow Cloudflare supports). | ||
| * | ||
| * References: | ||
| * - Endpoints: https://developers.cloudflare.com/fundamentals/oauth/integrate-with-cloudflare/ | ||
| * - Flow/PKCE: https://developers.cloudflare.com/fundamentals/oauth/create-an-oauth-client/#supported-oauth-flows | ||
| * - Scopes: https://developers.cloudflare.com/fundamentals/oauth/create-an-oauth-client/#select-scopes | ||
| * | ||
| * Scopes are dot-notation API-permission names (e.g. `workers-platform.read`) fetched from | ||
| * `GET /client/v4/oauth/scopes` — `openid` is NOT a Cloudflare scope. PKCE (S256) is required | ||
| * for public clients (auth method `none`) and optional for confidential clients (client secret). | ||
| */ | ||
|
|
||
| export interface OAuthCloudflareConfig { | ||
| /** | ||
| * Cloudflare OAuth Client ID | ||
| * @default process.env.NUXT_OAUTH_CLOUDFLARE_CLIENT_ID | ||
| */ | ||
| clientId?: string | ||
| /** | ||
| * Cloudflare OAuth Client Secret | ||
| * @default process.env.NUXT_OAUTH_CLOUDFLARE_CLIENT_SECRET | ||
| */ | ||
| clientSecret?: string | ||
| /** | ||
| * Cloudflare OAuth scopes — dot-notation API token permission IDs from | ||
| * `GET /client/v4/oauth/scopes`, matching the scopes the client was registered with. | ||
| * Cloudflare requires at least one; there is no default (`openid` is not a Cloudflare scope). | ||
| * @see https://developers.cloudflare.com/fundamentals/oauth/create-an-oauth-client/#select-scopes | ||
| * @example ['workers-platform.read'] | ||
| */ | ||
| scope?: string[] | ||
| /** | ||
| * Cloudflare OAuth Authorization URL | ||
| * @default 'https://dash.cloudflare.com/oauth2/auth' | ||
| */ | ||
| authorizationURL?: string | ||
| /** | ||
| * Cloudflare OAuth Token URL | ||
| * @default 'https://dash.cloudflare.com/oauth2/token' | ||
| */ | ||
| tokenURL?: string | ||
| /** | ||
| * Cloudflare OIDC userinfo URL. Returns standard claims (`sub`, `iss`, `aud`, …); | ||
| * use `GET /client/v4/user` for email/name. | ||
| * @default 'https://dash.cloudflare.com/oauth2/userinfo' | ||
| */ | ||
| userURL?: string | ||
| /** | ||
| * Extra authorization parameters to provide to the authorization URL | ||
| */ | ||
| authorizationParams?: Record<string, string> | ||
| /** | ||
| * Redirect URL to allow overriding for situations like prod failing to determine public hostname | ||
| * @default process.env.NUXT_OAUTH_CLOUDFLARE_REDIRECT_URL or current URL | ||
| */ | ||
| redirectURL?: string | ||
| } | ||
|
|
||
| /** | ||
| * Claims returned by Cloudflare's `/oauth2/userinfo` endpoint. `sub` is the stable | ||
| * Cloudflare user ID; the rest are standard OIDC token claims. There is no email/name | ||
| * here — for richer profile data call `GET /client/v4/user` with the access token. | ||
| */ | ||
| export interface CloudflareUser { | ||
| /** Stable Cloudflare user ID. */ | ||
| sub: string | ||
| aud?: string[] | ||
| iss?: string | ||
| iat?: number | ||
| auth_time?: number | ||
| rat?: number | ||
| } | ||
|
|
||
| export interface CloudflareTokens { | ||
| access_token: string | ||
| token_type: string | ||
| expires_in: number | ||
| /** Present when scope includes `offline_access` */ | ||
| refresh_token?: string | ||
| /** Present when scope includes `openid` */ | ||
| id_token?: string | ||
| scope: string | ||
| } | ||
|
|
||
| export function defineOAuthCloudflareEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthCloudflareConfig, { user: CloudflareUser, tokens: CloudflareTokens }>) { | ||
| return eventHandler(async (event: H3Event) => { | ||
| config = defu(config, useRuntimeConfig(event).oauth?.cloudflare, { | ||
| authorizationURL: 'https://dash.cloudflare.com/oauth2/auth', | ||
| tokenURL: 'https://dash.cloudflare.com/oauth2/token', | ||
| userURL: 'https://dash.cloudflare.com/oauth2/userinfo', | ||
| authorizationParams: {}, | ||
| }) as OAuthCloudflareConfig | ||
|
|
||
| const query = getQuery<{ code?: string, error?: string, error_description?: string, state?: string }>(event) | ||
|
|
||
| if (query.error) { | ||
| const error = createError({ | ||
| statusCode: 401, | ||
| message: `Cloudflare login failed: ${query.error_description || query.error}`, | ||
| data: query, | ||
| }) | ||
| if (!onError) throw error | ||
| return onError(event, error) | ||
| } | ||
|
|
||
| if (!config.clientId || !config.clientSecret) { | ||
| return handleMissingConfiguration(event, 'cloudflare', ['clientId', 'clientSecret'], onError) | ||
| } | ||
|
|
||
| const redirectURL = config.redirectURL || getOAuthRedirectURL(event) | ||
| const state = await handleState(event) | ||
| // PKCE (S256) is supported and recommended by Cloudflare even for confidential clients. | ||
| const pkce = await handlePkceVerifier(event) | ||
|
|
||
| if (!query.code) { | ||
| // Cloudflare requires ≥1 scope (a dot-notation API permission); there is no universal | ||
| // default, so send exactly what's configured and omit the param when none is set. | ||
| return sendRedirect( | ||
| event, | ||
| withQuery(config.authorizationURL as string, { | ||
| response_type: 'code', | ||
| client_id: config.clientId, | ||
| redirect_uri: redirectURL, | ||
| scope: config.scope?.length ? config.scope.join(' ') : undefined, | ||
| state, | ||
| code_challenge: pkce.code_challenge, | ||
| code_challenge_method: pkce.code_challenge_method, | ||
| ...config.authorizationParams, | ||
| }), | ||
| ) | ||
| } | ||
|
|
||
| if (query.state !== state) { | ||
| return handleInvalidState(event, 'cloudflare', onError) | ||
| } | ||
|
|
||
| const tokens = await requestAccessToken(config.tokenURL as string, { | ||
| body: { | ||
| grant_type: 'authorization_code', | ||
| client_id: config.clientId, | ||
| client_secret: config.clientSecret, | ||
| redirect_uri: redirectURL, | ||
| code: query.code, | ||
| code_verifier: pkce.code_verifier, | ||
| }, | ||
| }) | ||
|
|
||
| if (tokens.error) { | ||
| return handleAccessTokenErrorResponse(event, 'cloudflare', tokens, onError) | ||
| } | ||
|
|
||
| const user = await $fetch<CloudflareUser>(config.userURL as string, { | ||
| headers: { | ||
| Authorization: `Bearer ${tokens.access_token}`, | ||
| }, | ||
| }) | ||
|
|
||
| return onSuccess(event, { | ||
| user, | ||
| tokens, | ||
| }) | ||
| }) | ||
| } | ||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.