|
| 1 | +import pkceChallenge from 'pkce-challenge' |
| 2 | +import jwt from '../../lib/jwt' |
| 3 | +import * as cookie from '../lib/cookie' |
| 4 | +import logger from 'src/lib/logger' |
| 5 | + |
| 6 | +const PKCE_LENGTH = 64 |
| 7 | +const PKCE_CODE_CHALLENGE_METHOD = 'S256' // can be 'plain', not recommended https://tools.ietf.org/html/rfc7636#section-4.2 |
| 8 | +const PKCE_MAX_AGE = 60 * 15 // 15 minutes in seconds |
| 9 | + |
| 10 | +/** Adds `code_verifier` to `req.options.pkce`, and removes the corresponding cookie */ |
| 11 | +export async function handleCallback (req, res) { |
| 12 | + const { cookies, provider, baseUrl, basePath } = req.options |
| 13 | + try { |
| 14 | + if (provider.protection !== 'pkce') { // Provider does not support PKCE, nothing to do. |
| 15 | + return |
| 16 | + } |
| 17 | + |
| 18 | + if (!(cookies.pkceCodeVerifier.name in req.cookies)) { |
| 19 | + throw new Error('The code_verifier cookie was not found.') |
| 20 | + } |
| 21 | + const pkce = await jwt.decode({ |
| 22 | + ...req.options.jwt, |
| 23 | + token: req.cookies[cookies.pkceCodeVerifier.name], |
| 24 | + maxAge: PKCE_MAX_AGE, |
| 25 | + encryption: true |
| 26 | + }) |
| 27 | + cookie.set(res, cookies.pkceCodeVerifier.name, null, { maxAge: 0 }) // remove PKCE after it has been used |
| 28 | + req.options.pkce = pkce |
| 29 | + } catch (error) { |
| 30 | + logger.error('PKCE_ERROR', error) |
| 31 | + return `${baseUrl}${basePath}/error?error=Configuration` |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/** Adds `code_challenge` and `code_challenge_method` to `req.options.pkce`. */ |
| 36 | +export async function handleSignin (req, res) { |
| 37 | + const { cookies, provider, baseUrl, basePath } = req.options |
| 38 | + try { |
| 39 | + if (provider.protection !== 'pkce') { // Provider does not support PKCE, nothing to do. |
| 40 | + return |
| 41 | + } |
| 42 | + // Started login flow, add generated pkce to req.options and (encrypted) code_verifier to a cookie |
| 43 | + const pkce = pkceChallenge(PKCE_LENGTH) |
| 44 | + req.options.pkce = { |
| 45 | + code_challenge: pkce.code_challenge, |
| 46 | + code_challenge_method: PKCE_CODE_CHALLENGE_METHOD |
| 47 | + } |
| 48 | + const encryptedCodeVerifier = await jwt.encode({ |
| 49 | + ...req.options.jwt, |
| 50 | + maxAge: PKCE_MAX_AGE, |
| 51 | + token: { code_verifier: pkce.code_verifier }, |
| 52 | + encryption: true |
| 53 | + }) |
| 54 | + |
| 55 | + const cookieExpires = new Date() |
| 56 | + cookieExpires.setTime(cookieExpires.getTime() + (PKCE_MAX_AGE * 1000)) |
| 57 | + cookie.set(res, cookies.pkceCodeVerifier.name, encryptedCodeVerifier, { |
| 58 | + expires: cookieExpires.toISOString(), |
| 59 | + ...cookies.pkceCodeVerifier.options |
| 60 | + }) |
| 61 | + } catch (error) { |
| 62 | + logger.error('PKCE_ERROR', error) |
| 63 | + return `${baseUrl}${basePath}/error?error=Configuration` |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +export default { |
| 68 | + handleSignin, handleCallback |
| 69 | +} |
0 commit comments