mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
f7644bec68
* feat(api): add middleware example * feat(api): add `@fastify/middie`, reorder for alphabetness * [skip-ci] [skip ci]
20 lines
621 B
TypeScript
20 lines
621 B
TypeScript
import { randomBytes, createHash } from 'crypto';
|
|
|
|
export function base64URLEncode(buf: Buffer) {
|
|
return buf
|
|
.toString('base64')
|
|
.replace(/\+/g, '-')
|
|
.replace(/\//g, '_')
|
|
.replace(/=/g, '');
|
|
}
|
|
export const verifier = base64URLEncode(randomBytes(32));
|
|
|
|
function sha256(buf: Buffer) {
|
|
return createHash('sha256').update(buf).digest();
|
|
}
|
|
export const challenge = base64URLEncode(sha256(Buffer.from(verifier)));
|
|
|
|
// This is used for Fastify middleware, but is not exported from Fastify itself.
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
export type NextFunction = (err?: any) => void;
|