{"version":3,"file":"PkceGenerator.js","sources":["../../src/crypto/PkceGenerator.ts"],"sourcesContent":["/*\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { PkceCodes } from \"@azure/msal-common\";\nimport { BrowserAuthError } from \"../error/BrowserAuthError\";\nimport { Base64Encode } from \"../encode/Base64Encode\";\nimport { BrowserCrypto } from \"./BrowserCrypto\";\n\n// Constant byte array length\nconst RANDOM_BYTE_ARR_LENGTH = 32;\n\n/**\n * Class which exposes APIs to generate PKCE codes and code verifiers.\n */\nexport class PkceGenerator {\n\n private base64Encode: Base64Encode;\n private cryptoObj: BrowserCrypto;\n\n constructor(cryptoObj: BrowserCrypto) {\n this.base64Encode = new Base64Encode();\n this.cryptoObj = cryptoObj;\n }\n\n /**\n * Generates PKCE Codes. See the RFC for more information: https://tools.ietf.org/html/rfc7636\n */\n async generateCodes(): Promise {\n const codeVerifier = this.generateCodeVerifier();\n const codeChallenge = await this.generateCodeChallengeFromVerifier(codeVerifier);\n return {\n verifier: codeVerifier,\n challenge: codeChallenge\n };\n }\n\n /**\n * Generates a random 32 byte buffer and returns the base64\n * encoded string to be used as a PKCE Code Verifier\n */\n private generateCodeVerifier(): string {\n try {\n // Generate random values as utf-8\n const buffer: Uint8Array = new Uint8Array(RANDOM_BYTE_ARR_LENGTH);\n this.cryptoObj.getRandomValues(buffer);\n // encode verifier as base64\n const pkceCodeVerifierB64: string = this.base64Encode.urlEncodeArr(buffer);\n return pkceCodeVerifierB64;\n } catch (e) {\n throw BrowserAuthError.createPkceNotGeneratedError(e);\n }\n }\n\n /**\n * Creates a base64 encoded PKCE Code Challenge string from the\n * hash created from the PKCE Code Verifier supplied\n */\n private async generateCodeChallengeFromVerifier(pkceCodeVerifier: string): Promise {\n try {\n // hashed verifier\n const pkceHashedCodeVerifier = await this.cryptoObj.sha256Digest(pkceCodeVerifier);\n // encode hash as base64\n return this.base64Encode.urlEncodeArr(new Uint8Array(pkceHashedCodeVerifier));\n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