|
| 1 | +import type { H3Event } from 'h3' |
| 2 | +import { eventHandler, getQuery, sendRedirect, getRequestIP, getRequestHeader } from 'h3' |
| 3 | +import { withQuery } from 'ufo' |
| 4 | +import { defu } from 'defu' |
| 5 | +import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from '../utils' |
| 6 | +import { useRuntimeConfig } from '#imports' |
| 7 | +import type { OAuthConfig } from '#auth-utils' |
| 8 | + |
| 9 | +/** |
| 10 | + * WorkOS OAuth Configuration |
| 11 | + * @see https://workos.com/docs/reference/user-management/authentication |
| 12 | + */ |
| 13 | +export interface OAuthWorkOSConfig { |
| 14 | + /** |
| 15 | + * WorkOS OAuth Client ID |
| 16 | + * @default process.env.NUXT_OAUTH_WORKOS_CLIENT_ID |
| 17 | + */ |
| 18 | + clientId?: string |
| 19 | + /** |
| 20 | + * WorkOS OAuth Client Secret (API Key) |
| 21 | + * @default process.env.NUXT_OAUTH_WORKOS_CLIENT_SECRET |
| 22 | + */ |
| 23 | + clientSecret?: string |
| 24 | + /** |
| 25 | + * WorkOS OAuth Connection ID (Not required for WorkOS) |
| 26 | + * @default process.env.NUXT_OAUTH_WORKOS_CONNECTION_ID |
| 27 | + */ |
| 28 | + connectionId?: string |
| 29 | + /** |
| 30 | + * WorkOS OAuth screen hint |
| 31 | + * @default 'sign-in' |
| 32 | + */ |
| 33 | + screenHint?: 'sign-in' | 'sign-up' |
| 34 | + /** |
| 35 | + * Redirect URL to to allow overriding for situations like prod failing to determine public hostname |
| 36 | + * @default process.env.NUXT_OAUTH_WORKOS_REDIRECT_URL or current URL |
| 37 | + */ |
| 38 | + redirectURL?: string |
| 39 | +} |
| 40 | +export interface OAuthWorkOSUser { |
| 41 | + object: 'user' |
| 42 | + id: string |
| 43 | + email: string |
| 44 | + first_name: string | null |
| 45 | + last_name: string | null |
| 46 | + email_verified: boolean |
| 47 | + profile_picture_url: string | null |
| 48 | + created_at: string |
| 49 | + updated_at: string |
| 50 | +} |
| 51 | + |
| 52 | +export type OAuthWorkOSAuthenticationMethod = 'SSO' | 'Password' | 'AppleOAuth' | 'GitHubOAuth' | 'GoogleOAuth' | 'MicrosoftOAuth' | 'MagicAuth' | 'Impersonation' |
| 53 | + |
| 54 | +export interface OAuthWorkOSAuthenticateResponse { |
| 55 | + user: OAuthWorkOSUser |
| 56 | + organization_id: string | null |
| 57 | + access_token: string |
| 58 | + refresh_token: string |
| 59 | + error: string | null |
| 60 | + error_description: string | null |
| 61 | + authentication_method: OAuthWorkOSAuthenticationMethod |
| 62 | +} |
| 63 | + |
| 64 | +export interface OAuthWorkOSTokens { |
| 65 | + access_token: string |
| 66 | + refresh_token: string |
| 67 | +} |
| 68 | + |
| 69 | +export function defineOAuthWorkOSEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthWorkOSConfig, OAuthWorkOSUser, OAuthWorkOSTokens>) { |
| 70 | + return eventHandler(async (event: H3Event) => { |
| 71 | + config = defu(config, useRuntimeConfig(event).oauth?.workos, { screen_hint: 'sign-in' }) as OAuthWorkOSConfig |
| 72 | + |
| 73 | + if (!config.clientId || !config.clientSecret) { |
| 74 | + return handleMissingConfiguration(event, 'workos', ['clientId', 'clientSecret'], onError) |
| 75 | + } |
| 76 | + |
| 77 | + const query = getQuery<{ code?: string, state?: string, error?: string, error_description?: string, returnURL?: string }>(event) |
| 78 | + const redirectURL = config.redirectURL || getOAuthRedirectURL(event) |
| 79 | + |
| 80 | + if (query.error) { |
| 81 | + return handleAccessTokenErrorResponse(event, 'workos', query, onError) |
| 82 | + } |
| 83 | + |
| 84 | + if (!query.code) { |
| 85 | + // Redirect to WorkOS Oauth page |
| 86 | + return sendRedirect( |
| 87 | + event, |
| 88 | + withQuery('https://api.workos.com/user_management/authorize', { |
| 89 | + response_type: 'code', |
| 90 | + provider: 'authkit', |
| 91 | + client_id: config.clientId, |
| 92 | + redirect_uri: redirectURL, |
| 93 | + connection_id: config.connectionId, |
| 94 | + screen_hint: config.screenHint, |
| 95 | + }), |
| 96 | + ) |
| 97 | + } |
| 98 | + |
| 99 | + const ip_address = getRequestIP(event) |
| 100 | + const user_agent = getRequestHeader(event, 'user-agent') |
| 101 | + |
| 102 | + const authenticateResponse: OAuthWorkOSAuthenticateResponse = await requestAccessToken('https://api.workos.com/user_management/authenticate', { |
| 103 | + headers: { |
| 104 | + 'Content-Type': 'application/json', |
| 105 | + }, |
| 106 | + body: { |
| 107 | + grant_type: 'authorization_code', |
| 108 | + client_id: config.clientId, |
| 109 | + client_secret: config.clientSecret, |
| 110 | + redirect_uri: redirectURL, |
| 111 | + ip_address, |
| 112 | + user_agent, |
| 113 | + code: query.code, |
| 114 | + }, |
| 115 | + }) |
| 116 | + |
| 117 | + if (authenticateResponse.error) { |
| 118 | + return handleAccessTokenErrorResponse(event, 'workos', authenticateResponse, onError) |
| 119 | + } |
| 120 | + |
| 121 | + return onSuccess(event, { |
| 122 | + tokens: { access_token: authenticateResponse.access_token, refresh_token: authenticateResponse.refresh_token }, |
| 123 | + user: authenticateResponse.user, |
| 124 | + }) |
| 125 | + }) |
| 126 | +} |
0 commit comments