@@ -8,43 +8,23 @@ export interface AuthorizeUriOptions {
88 baseUri ?: string ;
99}
1010
11- function isNode ( ) {
12- return typeof process !== "undefined" &&
13- process . versions ?. node != null &&
14- typeof window === "undefined" ;
15- }
16-
1711async function generateCodeVerifier ( ) : Promise < string > {
18- const length = 32 ;
19- if ( isNode ( ) ) {
20- const { randomBytes } = await import ( "crypto" ) ;
21- return randomBytes ( length ) . toString ( "base64" )
22- . replace ( / \+ / g, "-" ) . replace ( / \/ / g, "_" ) . replace ( / = / g, "" ) ;
23- } else {
24- const array = new Uint8Array ( length ) ;
25- crypto . getRandomValues ( array ) ;
26- return btoa ( String . fromCharCode ( ...array ) )
27- . replace ( / \+ / g, "-" ) . replace ( / \/ / g, "_" ) . replace ( / = / g, "" ) ;
28- }
12+ const array = new Uint8Array ( 32 ) ;
13+ crypto . getRandomValues ( array ) ;
14+ return btoa ( String . fromCharCode ( ...array ) )
15+ . replace ( / \+ / g, "-" ) . replace ( / \/ / g, "_" ) . replace ( / = / g, "" ) ;
2916}
3017
3118async function generateCodeChallenge ( verifier : string ) : Promise < string > {
32- if ( isNode ( ) ) {
33- const { createHash } = await import ( "crypto" ) ;
34- const hash = createHash ( "sha256" ) . update ( verifier ) . digest ( ) ;
35- return hash . toString ( "base64" ) . replace ( / \+ / g, "-" ) . replace ( / \/ / g, "_" )
36- . replace ( / = / g, "" ) ;
37- } else {
38- const encoder = new TextEncoder ( ) ;
39- const data = encoder . encode ( verifier ) ;
40- const hashBuffer = await crypto . subtle . digest ( "SHA-256" , data ) ;
41- const hashArray = Array . from ( new Uint8Array ( hashBuffer ) ) ;
42- const hashStr = String . fromCharCode ( ...hashArray ) ;
43- return btoa ( hashStr ) . replace ( / \+ / g, "-" ) . replace ( / \/ / g, "_" ) . replace (
44- / = / g,
45- "" ,
46- ) ;
47- }
19+ const encoder = new TextEncoder ( ) ;
20+ const data = encoder . encode ( verifier ) ;
21+ const hashBuffer = await crypto . subtle . digest ( "SHA-256" , data ) ;
22+ const hashArray = Array . from ( new Uint8Array ( hashBuffer ) ) ;
23+ const hashStr = String . fromCharCode ( ...hashArray ) ;
24+ return btoa ( hashStr ) . replace ( / \+ / g, "-" ) . replace ( / \/ / g, "_" ) . replace (
25+ / = / g,
26+ "" ,
27+ ) ;
4828}
4929
5030class AuthorizeUriExtension extends SdkExtension {
0 commit comments