diff --git a/src/static/helpers/slasHelper.ts b/src/static/helpers/slasHelper.ts index d011fc06..8880c1b3 100644 --- a/src/static/helpers/slasHelper.ts +++ b/src/static/helpers/slasHelper.ts @@ -16,6 +16,15 @@ import { } from '../../lib/shopperLogin'; import ResponseError from '../responseError'; +interface CryptoLib { + digestStringAsync?: ( + digest: string, + data: string, + options: {encoding: string} + ) => Promise; + default: typeof import('crypto'); +} + export const stringToBase64 = isBrowser ? btoa : (unencoded: string): string => Buffer.from(unencoded).toString('base64'); @@ -78,10 +87,29 @@ export const generateCodeChallenge = async ( const base64Digest = btoa(String.fromCharCode(...new Uint8Array(digest))); challenge = urlSafe(base64Digest); } else { - const crypto = await import('crypto'); - challenge = urlSafe( - crypto.default.createHash('sha256').update(codeVerifier).digest('base64') - ); + const crypto: CryptoLib = await import('crypto'); + + if ( + typeof navigator !== 'undefined' && + navigator.product === 'ReactNative' + ) { + const base64Digest = await crypto.digestStringAsync?.( + 'SHA-256', + codeVerifier, + { + encoding: 'base64', + } + ); + + challenge = base64Digest ? urlSafe(base64Digest) : ''; + } else { + challenge = urlSafe( + crypto.default + .createHash('sha256') + .update(codeVerifier) + .digest('base64') + ); + } } /* istanbul ignore next */