|
| 1 | +import type { H3Event } from 'h3' |
| 2 | +import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3' |
| 3 | +import { withQuery, parsePath } from 'ufo' |
| 4 | +import { defu } from 'defu' |
| 5 | +import { useRuntimeConfig } from '#imports' |
| 6 | +import type { OAuthConfig } from '#auth-utils' |
| 7 | + |
| 8 | +export interface OAuthXSUAAConfig { |
| 9 | + /** |
| 10 | + * XSUAA OAuth Client ID |
| 11 | + * @default process.env.NUXT_OAUTH_XSUAA_CLIENT_ID |
| 12 | + */ |
| 13 | + clientId?: string |
| 14 | + /** |
| 15 | + * XSUAA OAuth Client Secret |
| 16 | + * @default process.env.NUXT_OAUTH_XSUAA_CLIENT_SECRET |
| 17 | + */ |
| 18 | + clientSecret?: string |
| 19 | + /** |
| 20 | + * XSUAA OAuth Issuer |
| 21 | + * @default process.env.NUXT_OAUTH_XSUAA_DOMAIN |
| 22 | + */ |
| 23 | + domain?: string |
| 24 | + /** |
| 25 | + * XSUAA OAuth Scope |
| 26 | + * @default [] |
| 27 | + * @see https://sap.github.io/cloud-sdk/docs/java/guides/cloud-foundry-xsuaa-service |
| 28 | + * @example ['openid'] |
| 29 | + */ |
| 30 | + scope?: string[] |
| 31 | +} |
| 32 | + |
| 33 | +export function xsuaaEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthXSUAAConfig>) { |
| 34 | + return eventHandler(async (event: H3Event) => { |
| 35 | + config = defu(config, useRuntimeConfig(event).oauth?.xsuaa) as OAuthXSUAAConfig |
| 36 | + const { code } = getQuery(event) |
| 37 | + |
| 38 | + if (!config.clientId || !config.clientSecret || !config.domain) { |
| 39 | + const error = createError({ |
| 40 | + statusCode: 500, |
| 41 | + message: 'Missing NUXT_OAUTH_XSUAA_CLIENT_ID or NUXT_OAUTH_XSUAA_CLIENT_SECRET or NUXT_OAUTH_XSUAA_DOMAIN env variables.', |
| 42 | + }) |
| 43 | + if (!onError) throw error |
| 44 | + return onError(event, error) |
| 45 | + } |
| 46 | + const authorizationURL = `https://${config.domain}/oauth/authorize` |
| 47 | + const tokenURL = `https://${config.domain}/oauth/token` |
| 48 | + |
| 49 | + const redirectUrl = getRequestURL(event).href |
| 50 | + if (!code) { |
| 51 | + config.scope = config.scope || [] |
| 52 | + // Redirect to XSUAA Oauth page |
| 53 | + return sendRedirect( |
| 54 | + event, |
| 55 | + withQuery(authorizationURL as string, { |
| 56 | + response_type: 'code', |
| 57 | + client_id: config.clientId, |
| 58 | + redirect_uri: redirectUrl, |
| 59 | + scope: config.scope.join(' '), |
| 60 | + }), |
| 61 | + ) |
| 62 | + } |
| 63 | + |
| 64 | + // TODO: improve typing |
| 65 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 66 | + const tokens: any = await $fetch( |
| 67 | + tokenURL as string, |
| 68 | + { |
| 69 | + method: 'POST', |
| 70 | + headers: { |
| 71 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 72 | + }, |
| 73 | + body: new URLSearchParams({ |
| 74 | + grant_type: 'authorization_code', |
| 75 | + client_id: config.clientId, |
| 76 | + client_secret: config.clientSecret, |
| 77 | + redirect_uri: parsePath(redirectUrl).pathname, |
| 78 | + code: `${code}`, |
| 79 | + }), |
| 80 | + }, |
| 81 | + ).catch((error) => { |
| 82 | + return { error } |
| 83 | + }) |
| 84 | + if (tokens.error) { |
| 85 | + const error = createError({ |
| 86 | + statusCode: 401, |
| 87 | + message: `XSUAA login failed: ${tokens.error?.data?.error_description || 'Unknown error'}`, |
| 88 | + data: tokens, |
| 89 | + }) |
| 90 | + if (!onError) throw error |
| 91 | + return onError(event, error) |
| 92 | + } |
| 93 | + |
| 94 | + const tokenType = tokens.token_type |
| 95 | + const accessToken = tokens.access_token |
| 96 | + |
| 97 | + // TODO: improve typing |
| 98 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 99 | + const user: any = await $fetch(`https://${config.domain}/userinfo`, { |
| 100 | + headers: { |
| 101 | + Authorization: `${tokenType} ${accessToken}`, |
| 102 | + }, |
| 103 | + }) |
| 104 | + |
| 105 | + return onSuccess(event, { |
| 106 | + tokens, |
| 107 | + user, |
| 108 | + }) |
| 109 | + }) |
| 110 | +} |
0 commit comments