From c7052abd175085bb26a0964247dbce1d13c587bd Mon Sep 17 00:00:00 2001 From: Jason Raimondi Date: Fri, 5 Jul 2024 10:59:04 -0400 Subject: [PATCH] fix: move all crypto imports to import directly crypto not node:crypto --- README.md | 2 +- docs/docs/grants/authorization_code.mdx | 4 ++-- src/code_verifiers/S256.verifier.ts | 4 ++-- src/utils/token.ts | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 12fe5727..e56fd4b7 100644 --- a/README.md +++ b/README.md @@ -327,7 +327,7 @@ Before initializing [Part One](#part-one) of the authorization code flow, the cl We can do this in Node using the native crypto package and a `base64urlencode` function: ```typescript -import crypto from "node:crypto"; +import crypto from "crypto"; const code_verifier = crypto.randomBytes(43).toString("hex"); ``` diff --git a/docs/docs/grants/authorization_code.mdx b/docs/docs/grants/authorization_code.mdx index 11d015e7..792360c0 100644 --- a/docs/docs/grants/authorization_code.mdx +++ b/docs/docs/grants/authorization_code.mdx @@ -143,9 +143,9 @@ Before initializing [Part One](#part-one) of the authorization code flow, the cl We can do this in Node using the native crypto package and a `base64urlencode` function: ```ts -import crypto from "node:crypto"; +import { randomBytes } from "crypto"; -const code_verifier = crypto.randomBytes(43).toString("hex"); +const code_verifier = randomBytes(43).toString("hex"); ``` @see [https://www.oauth.com/oauth2-servers/pkce/authorization-request/](https://www.oauth.com/oauth2-servers/pkce/authorization-request/) diff --git a/src/code_verifiers/S256.verifier.ts b/src/code_verifiers/S256.verifier.ts index 0ac05ca1..e11ee4cc 100644 --- a/src/code_verifiers/S256.verifier.ts +++ b/src/code_verifiers/S256.verifier.ts @@ -1,4 +1,4 @@ -import crypto from "node:crypto"; +import { createHash } from "crypto"; import { base64urlencode } from "../utils/base64.js"; import { ICodeChallenge } from "./verifier.js"; @@ -7,7 +7,7 @@ export class S256Verifier implements ICodeChallenge { public readonly method = "S256"; verifyCodeChallenge(codeVerifier: string, codeChallenge: string): boolean { - const codeHash = crypto.createHash("sha256").update(codeVerifier).digest(); + const codeHash = createHash("sha256").update(codeVerifier).digest(); return codeChallenge === base64urlencode(codeHash); } } diff --git a/src/utils/token.ts b/src/utils/token.ts index bd1df1f6..5411d45a 100644 --- a/src/utils/token.ts +++ b/src/utils/token.ts @@ -1,5 +1,5 @@ -import crypto from "node:crypto"; +import { randomBytes } from "crypto"; export function generateRandomToken(len = 80): string { - return crypto.randomBytes(len / 2).toString("hex"); + return randomBytes(len / 2).toString("hex"); }