|
| 1 | +import { |
| 2 | + allowInsecureRequests, |
| 3 | + authorizationCodeGrant, |
| 4 | + buildAuthorizationUrl, |
| 5 | + Configuration, |
| 6 | + discovery, |
| 7 | + IDToken, |
| 8 | + randomState, |
| 9 | +} from "openid-client"; |
| 10 | +import { createRemoteJWKSet, jwtVerify } from "jose"; |
| 11 | + |
| 12 | +export const OIDC_ISSUER = |
| 13 | + process.env.OIDC_ISSUER || "https://marketplace.vercel.com"; |
| 14 | + |
| 15 | +let clientPromise: Promise<Configuration> | undefined; |
| 16 | +let jwks: ReturnType<typeof createRemoteJWKSet> | undefined; |
| 17 | + |
| 18 | +export async function getOidcConfiguration(): Promise<Configuration> { |
| 19 | + if (clientPromise) { |
| 20 | + return clientPromise; |
| 21 | + } |
| 22 | + |
| 23 | + clientPromise = (async () => { |
| 24 | + try { |
| 25 | + const oidcClientId = process.env.INTEGRATION_CLIENT_ID; |
| 26 | + if (!oidcClientId) { |
| 27 | + throw new Error("Missing INTEGRATION_CLIENT_ID environment variable"); |
| 28 | + } |
| 29 | + const oidcClientSecret = process.env.INTEGRATION_CLIENT_SECRET; |
| 30 | + if (!oidcClientSecret) { |
| 31 | + throw new Error( |
| 32 | + "Missing INTEGRATION_CLIENT_SECRET environment variable" |
| 33 | + ); |
| 34 | + } |
| 35 | + const configuration = await discovery( |
| 36 | + new URL(OIDC_ISSUER), |
| 37 | + oidcClientId, |
| 38 | + { |
| 39 | + client_id: oidcClientId, |
| 40 | + client_secret: oidcClientSecret, |
| 41 | + }, |
| 42 | + undefined, |
| 43 | + { |
| 44 | + algorithm: "oidc", |
| 45 | + execute: [allowInsecureRequests], |
| 46 | + } |
| 47 | + ); |
| 48 | + console.log("Discovered configuration: ", configuration.serverMetadata()); |
| 49 | + return configuration; |
| 50 | + } catch (error) { |
| 51 | + console.error( |
| 52 | + "Error discovering OIDC issuer or initializing client:", |
| 53 | + error |
| 54 | + ); |
| 55 | + throw error; |
| 56 | + } |
| 57 | + })(); |
| 58 | + return clientPromise; |
| 59 | +} |
| 60 | + |
| 61 | +export async function createAuthorizationUrl({ |
| 62 | + callbackUrl, |
| 63 | + login_hint, |
| 64 | + v_deeplink, |
| 65 | + explicit = true, |
| 66 | +}: { |
| 67 | + callbackUrl: string; |
| 68 | + login_hint?: string; |
| 69 | + v_deeplink?: string; |
| 70 | + explicit?: boolean; |
| 71 | +}): Promise<{ |
| 72 | + redirectTo: string; |
| 73 | + state: string; |
| 74 | +}> { |
| 75 | + const config = await getOidcConfiguration(); |
| 76 | + |
| 77 | + const state = randomState(); |
| 78 | + |
| 79 | + const redirectTo = buildAuthorizationUrl(config, { |
| 80 | + redirect_uri: callbackUrl, |
| 81 | + scope: "openid", |
| 82 | + state, |
| 83 | + response_type: explicit ? "code" : "id_token", |
| 84 | + ...(login_hint ? { login_hint } : null), |
| 85 | + ...(v_deeplink ? { v_deeplink } : null), |
| 86 | + }); |
| 87 | + |
| 88 | + return { |
| 89 | + redirectTo: redirectTo.toString(), |
| 90 | + state, |
| 91 | + }; |
| 92 | +} |
| 93 | + |
| 94 | +async function getJwks() { |
| 95 | + if (!jwks) { |
| 96 | + const config = await getOidcConfiguration(); |
| 97 | + const serverMetadata = config.serverMetadata(); |
| 98 | + if (!serverMetadata.jwks_uri) { |
| 99 | + throw new Error("JWKS URI not found in server metadata."); |
| 100 | + } |
| 101 | + console.log("Creating JWKS from server metadata:", serverMetadata.jwks_uri); |
| 102 | + jwks = createRemoteJWKSet(new URL(serverMetadata.jwks_uri)); |
| 103 | + } |
| 104 | + return jwks; |
| 105 | +} |
| 106 | + |
| 107 | +async function validateIdToken(id_token: string): Promise<IDToken> { |
| 108 | + const jwks = await getJwks(); |
| 109 | + const token = await jwtVerify<IDToken>(id_token, jwks); |
| 110 | + console.log("ID Token claims:", token.payload); |
| 111 | + return token.payload; |
| 112 | +} |
| 113 | + |
| 114 | +export async function getTokens( |
| 115 | + currentUrl: string, |
| 116 | + expectedState: string | undefined |
| 117 | +): Promise<{ id_token: string; claims: IDToken } | null> { |
| 118 | + const config = await getOidcConfiguration(); |
| 119 | + |
| 120 | + const tokens = await authorizationCodeGrant(config, new URL(currentUrl), { |
| 121 | + expectedState, |
| 122 | + idTokenExpected: true, |
| 123 | + }); |
| 124 | + |
| 125 | + console.log("Token Endpoint Response", tokens); |
| 126 | + |
| 127 | + const id_token = tokens.id_token; |
| 128 | + if (!id_token) { |
| 129 | + console.warn("No ID token received from the token endpoint."); |
| 130 | + return null; |
| 131 | + } |
| 132 | + |
| 133 | + const claims2 = tokens.claims(); |
| 134 | + console.log("Claims2", claims2); |
| 135 | + |
| 136 | + const claims = await validateIdToken(id_token); |
| 137 | + console.log("Token Endpoint Response claims", claims); |
| 138 | + |
| 139 | + return { id_token, claims }; |
| 140 | +} |
0 commit comments