|
| 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 { randomUUID } from 'uncrypto' |
| 6 | +import { handleAccessTokenErrorResponse, handleMissingConfiguration, getOAuthRedirectURL, requestAccessToken, handlePkceVerifier } from '../utils' |
| 7 | +import { useRuntimeConfig, createError } from '#imports' |
| 8 | +import type { OAuthConfig } from '#auth-utils' |
| 9 | + |
| 10 | +export interface OAuthKickConfig { |
| 11 | + /** |
| 12 | + * Kick Client ID |
| 13 | + * @default process.env.NUXT_OAUTH_KICK_CLIENT_ID |
| 14 | + */ |
| 15 | + clientId?: string |
| 16 | + |
| 17 | + /** |
| 18 | + * Kick OAuth Client Secret |
| 19 | + * @default process.env.NUXT_OAUTH_KICK_CLIENT_SECRET |
| 20 | + */ |
| 21 | + clientSecret?: string |
| 22 | + |
| 23 | + /** |
| 24 | + * Kick OAuth Scope |
| 25 | + * @default [] |
| 26 | + * @see https://docs.kick.com/getting-started/scopes |
| 27 | + * @example ['channel:read'] |
| 28 | + */ |
| 29 | + scope?: string[] |
| 30 | + |
| 31 | + /** |
| 32 | + * Kick OAuth Authorization URL |
| 33 | + * @see https://docs.kick.com/getting-started/generating-tokens-oauth2-flow#authorization-endpoint |
| 34 | + * @default 'https://id.kick.com/oauth/authorize' |
| 35 | + */ |
| 36 | + authorizationURL?: string |
| 37 | + |
| 38 | + /** |
| 39 | + * Kick OAuth Token URL |
| 40 | + * @see https://docs.kick.com/getting-started/generating-tokens-oauth2-flow#token-endpoint |
| 41 | + * @default 'https://id.kick.com/oauth/token' |
| 42 | + */ |
| 43 | + tokenURL?: string |
| 44 | + |
| 45 | + /** |
| 46 | + * Redirect URL to to allow overriding for situations like prod failing to determine public hostname |
| 47 | + * @default process.env.NUXT_OAUTH_KICK_REDIRECT_URL or current URL |
| 48 | + */ |
| 49 | + redirectURL?: string |
| 50 | +} |
| 51 | + |
| 52 | +export function defineOAuthKickEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthKickConfig>) { |
| 53 | + return eventHandler(async (event: H3Event) => { |
| 54 | + config = defu(config, useRuntimeConfig(event).oauth?.kick, { |
| 55 | + authorizationURL: 'https://id.kick.com/oauth/authorize', |
| 56 | + tokenURL: 'https://id.kick.com/oauth/token', |
| 57 | + }) as OAuthKickConfig |
| 58 | + const query = getQuery<{ code?: string }>(event) |
| 59 | + if (!config.clientId || !config.clientSecret) { |
| 60 | + return handleMissingConfiguration(event, 'kick', ['clientId', 'clientSecret'], onError) |
| 61 | + } |
| 62 | + |
| 63 | + // Create pkce verifier |
| 64 | + const verifier = await handlePkceVerifier(event) |
| 65 | + |
| 66 | + const redirectURL = config.redirectURL || getOAuthRedirectURL(event) |
| 67 | + |
| 68 | + if (!query.code) { |
| 69 | + config.scope = config.scope || [] |
| 70 | + if (!config.scope.includes('user:read')) |
| 71 | + config.scope.push('user:read') |
| 72 | + |
| 73 | + // Redirect to Kick Oauth page |
| 74 | + return sendRedirect( |
| 75 | + event, |
| 76 | + withQuery(config.authorizationURL as string, { |
| 77 | + response_type: 'code', |
| 78 | + client_id: config.clientId, |
| 79 | + redirect_uri: redirectURL, |
| 80 | + scope: config.scope.join(' '), |
| 81 | + state: randomUUID(), |
| 82 | + code_challenge: verifier.code_challenge, |
| 83 | + code_challenge_method: verifier.code_challenge_method, |
| 84 | + }), |
| 85 | + ) |
| 86 | + } |
| 87 | + |
| 88 | + const tokens = await requestAccessToken(config.tokenURL as string, { |
| 89 | + body: { |
| 90 | + grant_type: 'authorization_code', |
| 91 | + redirect_uri: redirectURL, |
| 92 | + client_id: config.clientId, |
| 93 | + client_secret: config.clientSecret, |
| 94 | + code: query.code, |
| 95 | + code_verifier: verifier.code_verifier, |
| 96 | + }, |
| 97 | + }) |
| 98 | + |
| 99 | + if (tokens.error) { |
| 100 | + return handleAccessTokenErrorResponse(event, 'kick', tokens, onError) |
| 101 | + } |
| 102 | + const accessToken = tokens.access_token |
| 103 | + |
| 104 | + // TODO: improve typing |
| 105 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 106 | + const { data }: any = await $fetch('https://api.kick.com/public/v1/users', { |
| 107 | + headers: { |
| 108 | + Authorization: `Bearer ${accessToken}`, |
| 109 | + Accept: 'application/json', |
| 110 | + }, |
| 111 | + }) |
| 112 | + |
| 113 | + if (!data || !data.length) { |
| 114 | + const error = createError({ |
| 115 | + statusCode: 500, |
| 116 | + message: 'Could not get Kick user', |
| 117 | + data: tokens, |
| 118 | + }) |
| 119 | + if (!onError) throw error |
| 120 | + return onError(event, error) |
| 121 | + } |
| 122 | + |
| 123 | + const user = data[0] |
| 124 | + |
| 125 | + return onSuccess(event, { |
| 126 | + tokens, |
| 127 | + user, |
| 128 | + }) |
| 129 | + }) |
| 130 | +} |
0 commit comments