|
| 1 | +import type { H3Event } from 'h3' |
| 2 | +import { |
| 3 | + eventHandler, |
| 4 | + createError, |
| 5 | + getQuery, |
| 6 | + getRequestURL, |
| 7 | + sendRedirect, |
| 8 | +} from 'h3' |
| 9 | +import { ofetch } from 'ofetch' |
| 10 | +import { withQuery, parsePath } from 'ufo' |
| 11 | +import { defu } from 'defu' |
| 12 | +import { useRuntimeConfig } from '#imports' |
| 13 | +import type { OAuthConfig } from '#auth-utils' |
| 14 | + |
| 15 | +export interface OAuthKeycloakConfig { |
| 16 | + /** |
| 17 | + * Keycloak OAuth Client ID |
| 18 | + * @default process.env.NUXT_OAUTH_KEYCLOAK_CLIENT_ID |
| 19 | + */ |
| 20 | + clientId?: string |
| 21 | + /** |
| 22 | + * Keycloak OAuth Client Secret |
| 23 | + * @default process.env.NUXT_OAUTH_KEYCLOAK_CLIENT_SECRET |
| 24 | + */ |
| 25 | + clientSecret?: string |
| 26 | + /** |
| 27 | + * Keycloak OAuth Server URL |
| 28 | + * @example http://192.168.1.10:8080/auth |
| 29 | + * @default process.env.NUXT_OAUTH_KEYCLOAK_SERVER_URL |
| 30 | + */ |
| 31 | + serverUrl?: string |
| 32 | + /** |
| 33 | + * Keycloak OAuth Realm |
| 34 | + * @default process.env.NUXT_OAUTH_KEYCLOAK_REALM |
| 35 | + */ |
| 36 | + realm?: string |
| 37 | + /** |
| 38 | + * Keycloak OAuth Scope |
| 39 | + * @default [] |
| 40 | + * @see https://www.keycloak.org/docs/latest/authorization_services/ |
| 41 | + * @example ['openid'] |
| 42 | + */ |
| 43 | + scope?: string[] |
| 44 | +} |
| 45 | + |
| 46 | +export function keycloakEventHandler({ |
| 47 | + config, |
| 48 | + onSuccess, |
| 49 | + onError, |
| 50 | +}: OAuthConfig<OAuthKeycloakConfig>) { |
| 51 | + return eventHandler(async (event: H3Event) => { |
| 52 | + config = defu( |
| 53 | + config, |
| 54 | + // @ts-ignore |
| 55 | + useRuntimeConfig(event).oauth?.keycloak |
| 56 | + ) as OAuthKeycloakConfig |
| 57 | + |
| 58 | + const query = getQuery(event) |
| 59 | + const { code } = query |
| 60 | + |
| 61 | + if (query.error) { |
| 62 | + const error = createError({ |
| 63 | + statusCode: 401, |
| 64 | + message: `Keycloak login failed: ${query.error || 'Unknown error'}`, |
| 65 | + data: query, |
| 66 | + }) |
| 67 | + if (!onError) throw error |
| 68 | + return onError(event, error) |
| 69 | + } |
| 70 | + |
| 71 | + if ( |
| 72 | + !config.clientId || |
| 73 | + !config.clientSecret || |
| 74 | + !config.serverUrl || |
| 75 | + !config.realm |
| 76 | + ) { |
| 77 | + const error = createError({ |
| 78 | + statusCode: 500, |
| 79 | + message: |
| 80 | + 'Missing NUXT_OAUTH_KEYCLOAK_CLIENT_ID or NUXT_OAUTH_KEYCLOAK_CLIENT_SECRET or NUXT_OAUTH_KEYCLOAK_SERVER_URL or NUXT_OAUTH_KEYCLOAK_REALM env variables.', |
| 81 | + }) |
| 82 | + if (!onError) throw error |
| 83 | + return onError(event, error) |
| 84 | + } |
| 85 | + |
| 86 | + const realmURL = `${config.serverUrl}/realms/${config.realm}` |
| 87 | + |
| 88 | + const authorizationURL = `${realmURL}/protocol/openid-connect/auth` |
| 89 | + const tokenURL = `${realmURL}/protocol/openid-connect/token` |
| 90 | + const redirectUrl = getRequestURL(event).href |
| 91 | + |
| 92 | + if (!code) { |
| 93 | + config.scope = config.scope || ['openid'] |
| 94 | + |
| 95 | + // Redirect to Keycloak Oauth page |
| 96 | + return sendRedirect( |
| 97 | + event, |
| 98 | + withQuery(authorizationURL, { |
| 99 | + client_id: config.clientId, |
| 100 | + redirect_uri: redirectUrl, |
| 101 | + scope: config.scope.join(' '), |
| 102 | + response_type: 'code', |
| 103 | + }) |
| 104 | + ) |
| 105 | + } |
| 106 | + |
| 107 | + config.scope = config.scope || [] |
| 108 | + if (!config.scope.includes('openid')) { |
| 109 | + config.scope.push('openid') |
| 110 | + } |
| 111 | + |
| 112 | + const tokens: any = await ofetch(tokenURL, { |
| 113 | + method: 'POST', |
| 114 | + headers: { |
| 115 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 116 | + }, |
| 117 | + body: new URLSearchParams({ |
| 118 | + client_id: config.clientId, |
| 119 | + client_secret: config.clientSecret, |
| 120 | + grant_type: 'authorization_code', |
| 121 | + redirect_uri: parsePath(redirectUrl).pathname, |
| 122 | + code: code as string, |
| 123 | + }).toString(), |
| 124 | + }).catch((error) => { |
| 125 | + return { error } |
| 126 | + }) |
| 127 | + |
| 128 | + if (tokens.error) { |
| 129 | + const error = createError({ |
| 130 | + statusCode: 401, |
| 131 | + message: `Keycloak login failed: ${ |
| 132 | + tokens.error?.data?.error_description || 'Unknown error' |
| 133 | + }`, |
| 134 | + data: tokens, |
| 135 | + }) |
| 136 | + if (!onError) throw error |
| 137 | + return onError(event, error) |
| 138 | + } |
| 139 | + |
| 140 | + const accessToken = tokens.access_token |
| 141 | + |
| 142 | + const user: any = await ofetch( |
| 143 | + `${realmURL}/protocol/openid-connect/userinfo`, |
| 144 | + { |
| 145 | + headers: { |
| 146 | + Authorization: `Bearer ${accessToken}`, |
| 147 | + Accept: 'application/json', |
| 148 | + }, |
| 149 | + } |
| 150 | + ) |
| 151 | + |
| 152 | + if (!user) { |
| 153 | + const error = createError({ |
| 154 | + statusCode: 500, |
| 155 | + message: 'Could not get Keycloak user', |
| 156 | + data: tokens, |
| 157 | + }) |
| 158 | + if (!onError) throw error |
| 159 | + return onError(event, error) |
| 160 | + } |
| 161 | + |
| 162 | + return onSuccess(event, { |
| 163 | + user, |
| 164 | + tokens, |
| 165 | + }) |
| 166 | + }) |
| 167 | +} |
0 commit comments