|
1 | 1 | // Copyright 2021 Twitter, Inc.
|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 |
|
4 |
| -import crypto from "crypto"; |
5 |
| -import { buildQueryString, basicAuthHeader } from "./utils"; |
| 4 | +import { buildQueryString, basicAuthHeader, base64Encode } from "./utils"; |
6 | 5 | import { AuthClient, AuthHeader } from "./types";
|
7 | 6 | import { RequestOptions, rest } from "./request";
|
8 | 7 |
|
@@ -63,13 +62,12 @@ export interface RevokeAccessTokenParams {
|
63 | 62 | client_id: string;
|
64 | 63 | }
|
65 | 64 |
|
66 |
| -function sha256(buffer: string) { |
67 |
| - return crypto.createHash("sha256").update(buffer).digest(); |
| 65 | +async function sha256(buffer: string): Promise<Uint8Array> { |
| 66 | + return new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(buffer))); |
68 | 67 | }
|
69 | 68 |
|
70 |
| -function base64URLEncode(str: Buffer) { |
71 |
| - return str |
72 |
| - .toString("base64") |
| 69 | +function base64URLEncode(str: Uint8Array) { |
| 70 | + return base64Encode(str) |
73 | 71 | .replace(/\+/g, "-")
|
74 | 72 | .replace(/\//g, "_")
|
75 | 73 | .replace(/=/g, "");
|
@@ -247,9 +245,9 @@ export class OAuth2User implements AuthClient {
|
247 | 245 | if (!callback) throw new Error("callback required");
|
248 | 246 | if (!scopes) throw new Error("scopes required");
|
249 | 247 | if (options.code_challenge_method === "s256") {
|
250 |
| - const code_verifier = base64URLEncode(crypto.randomBytes(32)); |
| 248 | + const code_verifier = base64URLEncode(crypto.getRandomValues(new Uint8Array(32))); |
251 | 249 | this.#code_verifier = code_verifier;
|
252 |
| - this.#code_challenge = base64URLEncode(sha256(code_verifier)); |
| 250 | + this.#code_challenge = base64URLEncode(await sha256(code_verifier)); |
253 | 251 | } else {
|
254 | 252 | this.#code_challenge = options.code_challenge;
|
255 | 253 | this.#code_verifier = options.code_challenge;
|
|
0 commit comments