|
| 1 | +import type { H3Event } from 'h3' |
| 2 | +import { eventHandler, getQuery, sendRedirect } from 'h3' |
| 3 | +import { withQuery } from 'ufo' |
| 4 | +import { defu } from 'defu' |
| 5 | +import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken, handleState, handleInvalidState } from '../utils' |
| 6 | +import { useRuntimeConfig, createError } from '#imports' |
| 7 | +import type { OAuthConfig } from '#auth-utils' |
| 8 | + |
| 9 | +export interface OAuthHerokuConfig { |
| 10 | + /** |
| 11 | + * Heroku OAuth Client ID |
| 12 | + * @default process.env.NUXT_OAUTH_HEROKU_CLIENT_ID |
| 13 | + */ |
| 14 | + clientId?: string |
| 15 | + /** |
| 16 | + * Heroku OAuth Client Secret |
| 17 | + * @default process.env.NUXT_OAUTH_HEROKU_CLIENT_SECRET |
| 18 | + */ |
| 19 | + clientSecret?: string |
| 20 | + /** |
| 21 | + * Heroku OAuth Scope |
| 22 | + * @default ['identity'] |
| 23 | + * @see https://devcenter.heroku.com/articles/oauth#scopes |
| 24 | + * @example ['identity'] |
| 25 | + */ |
| 26 | + scope?: string[] |
| 27 | + /** |
| 28 | + * Heroku OAuth Authorization URL |
| 29 | + * @default 'https://id.heroku.com/oauth/authorize' |
| 30 | + */ |
| 31 | + authorizationURL?: string |
| 32 | + /** |
| 33 | + * Heroku OAuth Authorization URL |
| 34 | + * @default 'https://id.heroku.com/oauth/token' |
| 35 | + */ |
| 36 | + tokenURL?: string |
| 37 | + /** |
| 38 | + * Extra authorization parameters to provide to the authorization URL |
| 39 | + * @default {} |
| 40 | + */ |
| 41 | + authorizationParams?: Record<string, string> |
| 42 | + /** |
| 43 | + * Redirect URL to allow overriding for situations like prod failing to determine public hostname |
| 44 | + * @default process.env.NUXT_OAUTH_HEROKU_REDIRECT_URL or current URL |
| 45 | + */ |
| 46 | + redirectURL?: string |
| 47 | +} |
| 48 | + |
| 49 | +export function defineOAuthHerokuEventHandler({ |
| 50 | + config, |
| 51 | + onSuccess, |
| 52 | + onError, |
| 53 | +}: OAuthConfig<OAuthHerokuConfig>) { |
| 54 | + return eventHandler(async (event: H3Event) => { |
| 55 | + const runtimeConfig = useRuntimeConfig(event).oauth?.heroku |
| 56 | + const baseURL = 'https://id.heroku.com' |
| 57 | + config = defu(config, runtimeConfig, { |
| 58 | + authorizationURL: `${baseURL}/oauth/authorize`, |
| 59 | + tokenURL: `${baseURL}/oauth/token`, |
| 60 | + authorizationParams: {}, |
| 61 | + }) as OAuthHerokuConfig |
| 62 | + |
| 63 | + const query = getQuery<{ code?: string, state?: string, error?: string }>(event) |
| 64 | + |
| 65 | + if (query.error) { |
| 66 | + const error = createError({ |
| 67 | + statusCode: 401, |
| 68 | + message: `Heroku login failed: ${query.error || 'Unknown error'}`, |
| 69 | + data: query, |
| 70 | + }) |
| 71 | + if (!onError) throw error |
| 72 | + return onError(event, error) |
| 73 | + } |
| 74 | + |
| 75 | + if (!config.clientId || !config.clientSecret) { |
| 76 | + return handleMissingConfiguration(event, 'heroku', ['clientId', 'clientSecret'], onError) |
| 77 | + } |
| 78 | + |
| 79 | + const redirectURL = config.redirectURL || getOAuthRedirectURL(event) |
| 80 | + const state = await handleState(event) |
| 81 | + |
| 82 | + if (!query.code) { |
| 83 | + config.scope = config.scope || ['identity'] |
| 84 | + return sendRedirect( |
| 85 | + event, |
| 86 | + withQuery(config.authorizationURL as string, { |
| 87 | + response_type: 'code', |
| 88 | + client_id: config.clientId, |
| 89 | + redirect_uri: redirectURL, |
| 90 | + scope: config.scope.join(' '), |
| 91 | + state, |
| 92 | + ...config.authorizationParams, |
| 93 | + }), |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + if (query.state !== state) { |
| 98 | + handleInvalidState(event, 'heroku', onError) |
| 99 | + } |
| 100 | + |
| 101 | + const tokens = await requestAccessToken(config.tokenURL as string, { |
| 102 | + body: { |
| 103 | + grant_type: 'authorization_code', |
| 104 | + client_id: config.clientId, |
| 105 | + client_secret: config.clientSecret, |
| 106 | + redirect_uri: redirectURL, |
| 107 | + code: query.code, |
| 108 | + }, |
| 109 | + }) |
| 110 | + |
| 111 | + if (tokens.error) { |
| 112 | + return handleAccessTokenErrorResponse(event, 'heroku', tokens, onError) |
| 113 | + } |
| 114 | + |
| 115 | + const accessToken = tokens.access_token |
| 116 | + const user = await $fetch(`https://api.heroku.com/account`, { |
| 117 | + headers: { |
| 118 | + Accept: 'application/vnd.heroku+json; version=3', |
| 119 | + Authorization: `Bearer ${accessToken}`, |
| 120 | + }, |
| 121 | + }) |
| 122 | + |
| 123 | + return onSuccess(event, { |
| 124 | + user, |
| 125 | + tokens, |
| 126 | + }) |
| 127 | + }) |
| 128 | +} |
0 commit comments