|
| 1 | +import { |
| 2 | + AuthorizationParameters, |
| 3 | + generators, |
| 4 | + OpenIDCallbackChecks, |
| 5 | +} from "openid-client" |
| 6 | +import * as jwt from "../../../jwt" |
| 7 | + |
| 8 | +import type { RequestInternal } from "../.." |
| 9 | +import type { CookiesOptions, InternalOptions } from "../../types" |
| 10 | +import type { Cookie } from "../cookie" |
| 11 | +import { OAuthChecks } from "src/providers" |
| 12 | + |
| 13 | +/** Returns a signed cookie. */ |
| 14 | +export async function signCookie( |
| 15 | + type: keyof CookiesOptions, |
| 16 | + value: string, |
| 17 | + maxAge: number, |
| 18 | + options: InternalOptions<"oauth"> |
| 19 | +): Promise<Cookie> { |
| 20 | + const { cookies, logger } = options |
| 21 | + |
| 22 | + logger.debug(`CREATE_${type.toUpperCase()}`, { value, maxAge }) |
| 23 | + |
| 24 | + const expires = new Date() |
| 25 | + expires.setTime(expires.getTime() + maxAge * 1000) |
| 26 | + return { |
| 27 | + name: cookies[type].name, |
| 28 | + value: await jwt.encode({ ...options.jwt, maxAge, token: { value } }), |
| 29 | + options: { ...cookies[type].options, expires }, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +const PKCE_MAX_AGE = 60 * 15 // 15 minutes in seconds |
| 34 | +export const PKCE_CODE_CHALLENGE_METHOD = "S256" |
| 35 | +export const pkce = { |
| 36 | + async create( |
| 37 | + options: InternalOptions<"oauth">, |
| 38 | + cookies: Cookie[], |
| 39 | + resParams: AuthorizationParameters |
| 40 | + ) { |
| 41 | + if (!options.provider?.checks?.includes("pkce")) return |
| 42 | + const code_verifier = generators.codeVerifier() |
| 43 | + const value = generators.codeChallenge(code_verifier) |
| 44 | + resParams.code_challenge = value |
| 45 | + resParams.code_challenge_method = PKCE_CODE_CHALLENGE_METHOD |
| 46 | + |
| 47 | + const maxAge = |
| 48 | + options.cookies.pkceCodeVerifier.options.maxAge ?? PKCE_MAX_AGE |
| 49 | + |
| 50 | + cookies.push( |
| 51 | + await signCookie("pkceCodeVerifier", code_verifier, maxAge, options) |
| 52 | + ) |
| 53 | + }, |
| 54 | + /** |
| 55 | + * Returns code_verifier if the provider is configured to use PKCE, |
| 56 | + * and clears the container cookie afterwards. |
| 57 | + * An error is thrown if the code_verifier is missing or invalid. |
| 58 | + * @see https://www.rfc-editor.org/rfc/rfc7636 |
| 59 | + * @see https://danielfett.de/2020/05/16/pkce-vs-nonce-equivalent-or-not/#pkce |
| 60 | + */ |
| 61 | + async use( |
| 62 | + cookies: RequestInternal["cookies"], |
| 63 | + resCookies: Cookie[], |
| 64 | + options: InternalOptions<"oauth">, |
| 65 | + checks: OAuthChecks |
| 66 | + ): Promise<string | undefined> { |
| 67 | + if (!options.provider?.checks?.includes("pkce")) return |
| 68 | + |
| 69 | + const codeVerifier = cookies?.[options.cookies.pkceCodeVerifier.name] |
| 70 | + |
| 71 | + if (!codeVerifier) |
| 72 | + throw new TypeError("PKCE code_verifier cookie was missing.") |
| 73 | + |
| 74 | + const value = (await jwt.decode({ |
| 75 | + ...options.jwt, |
| 76 | + token: codeVerifier, |
| 77 | + })) as any |
| 78 | + |
| 79 | + if (!value?.value) |
| 80 | + throw new TypeError("PKCE code_verifier value could not be parsed.") |
| 81 | + |
| 82 | + resCookies.push({ |
| 83 | + name: options.cookies.pkceCodeVerifier.name, |
| 84 | + value: "", |
| 85 | + options: { ...options.cookies.pkceCodeVerifier.options, maxAge: 0 }, |
| 86 | + }) |
| 87 | + |
| 88 | + checks.code_verifier = value.value |
| 89 | + }, |
| 90 | +} |
| 91 | + |
| 92 | +const STATE_MAX_AGE = 60 * 15 // 15 minutes in seconds |
| 93 | +export const state = { |
| 94 | + async create( |
| 95 | + options: InternalOptions<"oauth">, |
| 96 | + cookies: Cookie[], |
| 97 | + resParams: AuthorizationParameters |
| 98 | + ) { |
| 99 | + if (!options.provider.checks?.includes("state")) return |
| 100 | + const value = generators.state() |
| 101 | + resParams.state = value |
| 102 | + const maxAge = options.cookies.state.options.maxAge ?? STATE_MAX_AGE |
| 103 | + cookies.push(await signCookie("state", value, maxAge, options)) |
| 104 | + }, |
| 105 | + /** |
| 106 | + * Returns state if the provider is configured to use state, |
| 107 | + * and clears the container cookie afterwards. |
| 108 | + * An error is thrown if the state is missing or invalid. |
| 109 | + * @see https://www.rfc-editor.org/rfc/rfc6749#section-10.12 |
| 110 | + * @see https://www.rfc-editor.org/rfc/rfc6749#section-4.1.1 |
| 111 | + */ |
| 112 | + async use( |
| 113 | + cookies: RequestInternal["cookies"], |
| 114 | + resCookies: Cookie[], |
| 115 | + options: InternalOptions<"oauth">, |
| 116 | + checks: OAuthChecks |
| 117 | + ) { |
| 118 | + if (!options.provider.checks?.includes("state")) return |
| 119 | + |
| 120 | + const state = cookies?.[options.cookies.state.name] |
| 121 | + |
| 122 | + if (!state) throw new TypeError("State cookie was missing.") |
| 123 | + |
| 124 | + const value = (await jwt.decode({ ...options.jwt, token: state })) as any |
| 125 | + |
| 126 | + if (!value?.value) throw new TypeError("State value could not be parsed.") |
| 127 | + |
| 128 | + resCookies.push({ |
| 129 | + name: options.cookies.state.name, |
| 130 | + value: "", |
| 131 | + options: { ...options.cookies.state.options, maxAge: 0 }, |
| 132 | + }) |
| 133 | + |
| 134 | + checks.state = value.value |
| 135 | + }, |
| 136 | +} |
| 137 | + |
| 138 | +const NONCE_MAX_AGE = 60 * 15 // 15 minutes in seconds |
| 139 | +export const nonce = { |
| 140 | + async create( |
| 141 | + options: InternalOptions<"oauth">, |
| 142 | + cookies: Cookie[], |
| 143 | + resParams: AuthorizationParameters |
| 144 | + ) { |
| 145 | + if (!options.provider.checks?.includes("nonce")) return |
| 146 | + const value = generators.nonce() |
| 147 | + resParams.nonce = value |
| 148 | + const maxAge = options.cookies.nonce.options.maxAge ?? NONCE_MAX_AGE |
| 149 | + cookies.push(await signCookie("nonce", value, maxAge, options)) |
| 150 | + }, |
| 151 | + /** |
| 152 | + * Returns nonce if the provider is configured to use nonce, |
| 153 | + * and clears the container cookie afterwards. |
| 154 | + * An error is thrown if the nonce is missing or invalid. |
| 155 | + * @see https://openid.net/specs/openid-connect-core-1_0.html#NonceNotes |
| 156 | + * @see https://danielfett.de/2020/05/16/pkce-vs-nonce-equivalent-or-not/#nonce |
| 157 | + */ |
| 158 | + async use( |
| 159 | + cookies: RequestInternal["cookies"], |
| 160 | + resCookies: Cookie[], |
| 161 | + options: InternalOptions<"oauth">, |
| 162 | + checks: OpenIDCallbackChecks |
| 163 | + ): Promise<string | undefined> { |
| 164 | + if (!options.provider?.checks?.includes("nonce")) return |
| 165 | + |
| 166 | + const nonce = cookies?.[options.cookies.nonce.name] |
| 167 | + if (!nonce) throw new TypeError("Nonce cookie was missing.") |
| 168 | + |
| 169 | + const value = (await jwt.decode({ ...options.jwt, token: nonce })) as any |
| 170 | + |
| 171 | + if (!value?.value) throw new TypeError("Nonce value could not be parsed.") |
| 172 | + |
| 173 | + resCookies.push({ |
| 174 | + name: options.cookies.nonce.name, |
| 175 | + value: "", |
| 176 | + options: { ...options.cookies.nonce.options, maxAge: 0 }, |
| 177 | + }) |
| 178 | + |
| 179 | + checks.nonce = value.value |
| 180 | + }, |
| 181 | +} |
0 commit comments