|
| 1 | +import type { H3Event } from 'h3' |
| 2 | +import { eventHandler, getRequestHeader, readBody, sendRedirect } from 'h3' |
| 3 | +import { withQuery } from 'ufo' |
| 4 | +import { defu } from 'defu' |
| 5 | +import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken, signJwt, verifyJwt } from '../utils' |
| 6 | +import { useRuntimeConfig } from '#imports' |
| 7 | +import type { OAuthConfig } from '#auth-utils' |
| 8 | + |
| 9 | +export interface OAuthAppleConfig { |
| 10 | + /** |
| 11 | + * Apple OAuth Client ID |
| 12 | + * @default process.env.NUXT_OAUTH_APPLE_CLIENT_ID |
| 13 | + */ |
| 14 | + clientId?: string |
| 15 | + |
| 16 | + /** |
| 17 | + * Apple OAuth team ID |
| 18 | + * @default process.env.NUXT_OAUTH_APPLE_TEAM_ID |
| 19 | + */ |
| 20 | + teamId?: string |
| 21 | + |
| 22 | + /** |
| 23 | + * Apple OAuth key identifier |
| 24 | + * @default process.env.NUXT_OAUTH_APPLE_KEY_ID |
| 25 | + */ |
| 26 | + keyId?: string |
| 27 | + |
| 28 | + /** |
| 29 | + * Apple OAuth Private Key. Linebreaks must be replaced with \n |
| 30 | + * @example '-----BEGIN PRIVATE KEY-----\nMIGTAgEAMBMGByqGSM...\n-----END PRIVATE KEY-----' |
| 31 | + * @default process.env.NUXT_OAUTH_APPLE_PRIVATE_KEY |
| 32 | + */ |
| 33 | + privateKey?: string |
| 34 | + |
| 35 | + /** |
| 36 | + * Apple OAuth Scope. Apple wants this to be a string separated by spaces, but for consistency with other providers, we also allow an array of strings. |
| 37 | + * @default '' |
| 38 | + * @see https://developer.apple.com/documentation/sign_in_with_apple/clientconfigi/3230955-scope |
| 39 | + * @example 'name email' |
| 40 | + */ |
| 41 | + scope?: string | string[] |
| 42 | + |
| 43 | + /** |
| 44 | + * Apple OAuth Authorization URL |
| 45 | + * @default 'https://appleid.apple.com/auth/authorize' |
| 46 | + */ |
| 47 | + authorizationURL?: string |
| 48 | + |
| 49 | + /** |
| 50 | + * Extra authorization parameters to provide to the authorization URL |
| 51 | + * @see https://developer.apple.com/documentation/sign_in_with_apple/clientconfigi/3230955-scope |
| 52 | + * @example { usePop: true } |
| 53 | + */ |
| 54 | + authorizationParams?: Record<string, string | boolean> |
| 55 | + |
| 56 | + /** |
| 57 | + * Apple OAuth Token URL |
| 58 | + * @default 'https://appleid.apple.com/auth/token' |
| 59 | + */ |
| 60 | + tokenURL?: string |
| 61 | + |
| 62 | + /** |
| 63 | + * Redirect URL to to allow overriding for situations like prod failing to determine public hostname |
| 64 | + * @default process.env.NUXT_OAUTH_APPLE_REDIRECT_URL or current URL |
| 65 | + */ |
| 66 | + redirectURL?: string |
| 67 | +} |
| 68 | + |
| 69 | +export interface OAuthAppleTokens { |
| 70 | + iss: string |
| 71 | + aud: string |
| 72 | + exp: number |
| 73 | + iat: number |
| 74 | + sub: string |
| 75 | + at_hash: string |
| 76 | + email: string |
| 77 | + email_verified: boolean |
| 78 | + is_private_email: boolean |
| 79 | + auth_time: number |
| 80 | + nonce_supported: boolean |
| 81 | +} |
| 82 | + |
| 83 | +export interface OAuthAppleUser { |
| 84 | + name?: { |
| 85 | + firstName?: string |
| 86 | + lastName?: string |
| 87 | + } |
| 88 | + email?: string |
| 89 | +} |
| 90 | + |
| 91 | +export function defineOAuthAppleEventHandler({ |
| 92 | + config, |
| 93 | + onSuccess, |
| 94 | + onError, |
| 95 | +}: OAuthConfig<OAuthAppleConfig>) { |
| 96 | + return eventHandler(async (event: H3Event) => { |
| 97 | + config = defu(config, useRuntimeConfig(event).oauth?.apple, { |
| 98 | + authorizationURL: config?.authorizationURL || 'https://appleid.apple.com/auth/authorize', |
| 99 | + authorizationParams: {}, |
| 100 | + }) as OAuthAppleConfig |
| 101 | + |
| 102 | + if (!config.teamId || !config.keyId || !config.privateKey || !config.clientId) { |
| 103 | + return handleMissingConfiguration(event, 'apple', ['teamId', 'keyId', 'privateKey', 'clientId'], onError) |
| 104 | + } |
| 105 | + |
| 106 | + // instead of a query, apple sends a form post back after login |
| 107 | + const isPost = getRequestHeader(event, 'content-type') === 'application/x-www-form-urlencoded' |
| 108 | + |
| 109 | + let code: string | undefined |
| 110 | + let user: OAuthAppleUser | undefined |
| 111 | + |
| 112 | + if (isPost) { |
| 113 | + // `user` will only be available the first time a user logs in. |
| 114 | + ({ code, user } = await readBody<{ code: string, user?: OAuthAppleUser }>(event)) |
| 115 | + } |
| 116 | + |
| 117 | + // Send user to apple login page. |
| 118 | + if (!isPost || !code) { |
| 119 | + const redirectURL = config.redirectURL || getOAuthRedirectURL(event) |
| 120 | + |
| 121 | + config.scope = Array.isArray(config.scope) |
| 122 | + ? config.scope.join(' ') |
| 123 | + : (config.scope || 'name email') |
| 124 | + |
| 125 | + return sendRedirect( |
| 126 | + event, |
| 127 | + withQuery(config.authorizationURL as string, { |
| 128 | + response_type: 'code', |
| 129 | + response_mode: 'form_post', |
| 130 | + client_id: config.clientId, |
| 131 | + redirect_uri: redirectURL, |
| 132 | + scope: config.scope, |
| 133 | + ...config.authorizationParams, |
| 134 | + }), |
| 135 | + ) |
| 136 | + } |
| 137 | + |
| 138 | + // Verify the form post data we got back from apple |
| 139 | + try { |
| 140 | + const secret = await signJwt( |
| 141 | + { |
| 142 | + iss: config.teamId, |
| 143 | + aud: 'https://appleid.apple.com', |
| 144 | + sub: config.clientId, |
| 145 | + }, |
| 146 | + { |
| 147 | + privateKey: config.privateKey, |
| 148 | + keyId: config.keyId, |
| 149 | + teamId: config.teamId, |
| 150 | + clientId: config.clientId, |
| 151 | + expiresIn: '5m', |
| 152 | + }, |
| 153 | + ) |
| 154 | + |
| 155 | + const accessTokenResult = await requestAccessToken(config.tokenURL || 'https://appleid.apple.com/auth/token', { |
| 156 | + params: { |
| 157 | + client_id: config.clientId, |
| 158 | + client_secret: secret, |
| 159 | + code, |
| 160 | + grant_type: 'authorization_code', |
| 161 | + redirect_uri: config.redirectURL, |
| 162 | + }, |
| 163 | + }) |
| 164 | + |
| 165 | + const tokens = await verifyJwt<OAuthAppleTokens>(accessTokenResult.id_token, { |
| 166 | + publicJwkUrl: 'https://appleid.apple.com/auth/keys', |
| 167 | + audience: config.clientId, |
| 168 | + issuer: 'https://appleid.apple.com', |
| 169 | + }) |
| 170 | + |
| 171 | + if (!tokens) { |
| 172 | + return handleAccessTokenErrorResponse(event, 'apple', tokens, onError) |
| 173 | + } |
| 174 | + |
| 175 | + return onSuccess(event, { user, tokens }) |
| 176 | + } |
| 177 | + catch (error) { |
| 178 | + return handleAccessTokenErrorResponse(event, 'apple', error, onError) |
| 179 | + } |
| 180 | + }) |
| 181 | +} |
0 commit comments