1
- import { subtle , getRandomValues } from "uncrypto" ;
2
- import { AuthUrlOptions , PKCEChallenge , IssuerRouteTypes } from "./types" ;
1
+ // import { getRandomValues,subtle } from "uncrypto";
2
+ import { IssuerRouteTypes , LoginOptions } from "./types" ;
3
3
4
4
/**
5
5
*
@@ -16,142 +16,119 @@ export const base64UrlEncode = (str: string): string => {
16
16
. replace ( / = + $ / , "" ) ;
17
17
} ;
18
18
19
+ //function to remove trailing slash
20
+ export const sanitizeRedirect = ( url : string ) : string => {
21
+ return url . replace ( / \/ $ / , "" ) ;
22
+ } ;
23
+
19
24
/**
20
25
*
21
26
* @param options
22
27
* @param type
23
28
* @returns URL to redirect to
24
29
*/
25
30
export const generateAuthUrl = (
26
- options : AuthUrlOptions ,
31
+ domain : string ,
27
32
type : IssuerRouteTypes = IssuerRouteTypes . login ,
33
+ options : LoginOptions ,
28
34
) : URL => {
29
- const authUrl = new URL ( options . issuerURL + options . issuerRoutes [ type ] ) ;
35
+ console . log ( "generateAuthUrl" , options ) ;
36
+
37
+ const authUrl = new URL ( `${ domain } /oauth2/auth` ) ;
30
38
31
39
const searchParams : Record < string , string > = {
32
- redirect_uri : generateCallbackUrl (
33
- options . redirectURL ,
34
- options . redirectRoutes . callback ,
35
- ) ,
40
+ redirect_uri : sanitizeRedirect ( options . callbackURL ) ,
36
41
client_id : options . clientID ,
37
- response_type : options . responseType ,
38
- scope : options . scope ,
39
- code_challenge : options . code_challenge ,
40
- code_challenge_method : options . codeChallengeMethod ,
42
+ response_type : options . responseType || "code" ,
43
+ scope : options . scope . join ( " " ) ,
41
44
state : options . state ,
42
- audience : options . audience ,
43
- start_page : type === IssuerRouteTypes . register ? "registration" : "" ,
45
+ start_page : type ,
44
46
} ;
45
47
46
- for ( const [ key , value ] of Object . entries ( options ) ) {
47
- if ( key === "kindeAuth" || searchParams [ key ] ) continue ;
48
- if ( value !== null && value !== undefined ) {
49
- searchParams [ key ] = value ;
50
- }
48
+ if ( options . codeChallenge ) {
49
+ searchParams [ "code_challenge" ] = options . codeChallenge ;
50
+ searchParams [ "code_challenge_method" ] = "S256" ;
51
+ }
52
+
53
+ if ( options . codeChallengeMethod ) {
54
+ searchParams [ "code_challenge_method" ] = options . codeChallengeMethod ;
55
+ }
56
+
57
+ if ( options . audience ) {
58
+ searchParams [ "audience" ] = options . audience ;
51
59
}
52
60
53
61
authUrl . search = new URLSearchParams ( searchParams ) . toString ( ) ;
54
62
return authUrl ;
55
63
} ;
56
64
57
- /**
58
- *
59
- * @param base Base domain URL
60
- * @param path Path to append to the base URL
61
- * @returns
62
- */
63
- const generateCallbackUrl = ( base : string , path : string ) : string => {
64
- const siteUrl = base . endsWith ( "/" ) ? base . slice ( 0 , - 1 ) : base ;
65
- const callbackPath = path . startsWith ( "/" ) ? path . substring ( 1 ) : path ;
66
- return `${ siteUrl } /${ callbackPath } ` ;
67
- } ;
68
-
69
- /**
70
- *
71
- * @param code_verifier Verifier to generate challenge from
72
- * @returns URL safe base64 encoded string
73
- */
74
- export async function pkceChallengeFromVerifier (
75
- code_verifier : string ,
76
- ) : Promise < string > {
77
- const hashed = await sha256 ( code_verifier ) ;
78
- const hashedString = Array . from ( new Uint8Array ( hashed ) )
79
- . map ( ( byte ) => String . fromCharCode ( byte ) )
80
- . join ( "" ) ;
81
- return base64UrlEncode ( hashedString ) ;
82
- }
83
-
84
- /**
85
- * Creates a random string of provided length.
86
- * @param {number } length
87
- * @returns {string } required secret
88
- */
89
- export const generateRandomString = ( length : number = 28 ) : string => {
90
- const bytesNeeded = Math . ceil ( length / 2 ) ;
91
- const array = new Uint32Array ( bytesNeeded ) ;
92
- getRandomValues ( array ) ;
93
- let result = Array . from ( array , ( dec ) =>
94
- ( "0" + dec . toString ( 16 ) ) . slice ( - 2 ) ,
95
- ) . join ( "" ) ;
96
- if ( length % 2 !== 0 ) {
97
- // If the requested length is odd, remove the last character to adjust the length
98
- result = result . slice ( 0 , - 1 ) ;
99
- }
100
- return result ;
101
- } ;
65
+ // /**
66
+ // * Creates a random string of provided length.
67
+ // * @param {number } length
68
+ // * @returns {string } required secret
69
+ // */
70
+ // export const generateRandomString = (length: number = 28): string => {
71
+ // const bytesNeeded = Math.ceil(length / 2);
72
+ // const array = new Uint32Array(bytesNeeded);
73
+ // getRandomValues(array);
74
+ // let result = Array.from(array, (dec) =>
75
+ // ("0" + dec.toString(16)).slice(-2),
76
+ // ).join("");
77
+ // if (length % 2 !== 0) {
78
+ // // If the requested length is odd, remove the last character to adjust the length
79
+ // result = result.slice(0, -1);
80
+ // }
81
+ // return result;
82
+ // };
102
83
103
- /**
104
- * Sanitizes the redirect URL
105
- * @param param0 {baseUrl: string, url: string}
106
- * @returns URL
107
- */
108
- export const sanitizeRedirect = ( {
109
- baseUrl,
110
- url,
111
- } : {
112
- baseUrl : string ;
113
- url : string ;
114
- } ) : string => {
115
- if ( url . startsWith ( "/" ) ) {
116
- return `${ baseUrl } ${ url } ` ;
117
- } else if ( new URL ( url ) . origin === baseUrl ) {
118
- return url ;
119
- }
84
+ // //////
120
85
121
- return baseUrl ;
122
- } ;
86
+ // /**
87
+ // *
88
+ // * @param code_verifier Verifier to generate challenge from
89
+ // * @returns URL safe base64 encoded string
90
+ // */
91
+ // export async function pkceChallengeFromVerifier(
92
+ // code_verifier: string,
93
+ // ): Promise<string> {
94
+ // const hashed = await sha256(code_verifier);
95
+ // const hashedString = Array.from(new Uint8Array(hashed))
96
+ // .map((byte) => String.fromCharCode(byte))
97
+ // .join("");
98
+ // return base64UrlEncode(hashedString);
99
+ // }
123
100
124
- /**
125
- * setups up PKCE challenge
126
- * @returns
127
- */
128
- export const setupChallenge = ( ) => {
129
- return { state : generateRandomString ( ) , ...pkceChallenge ( ) } ;
130
- } ;
101
+ // / **
102
+ // * setups up PKCE challenge
103
+ // * @returns
104
+ // */
105
+ // export const setupChallenge = () => {
106
+ // return { state: generateRandomString(), ...pkceChallenge() };
107
+ // };
131
108
132
- /**
133
- * Calculate the SHA256 hash of the input text.
134
- * @param plain the text to hash
135
- * @returns a promise that resolves to an ArrayBuffer
136
- */
137
- export const sha256 = ( plain : string ) => {
138
- const encoder = new TextEncoder ( ) ;
139
- const data = encoder . encode ( plain ) ;
140
- return subtle . digest ( "SHA-256" , data ) ;
141
- } ;
109
+ // / **
110
+ // * Calculate the SHA256 hash of the input text.
111
+ // * @param plain the text to hash
112
+ // * @returns a promise that resolves to an ArrayBuffer
113
+ // */
114
+ // export const sha256 = (plain: string) => {
115
+ // const encoder = new TextEncoder();
116
+ // const data = encoder.encode(plain);
117
+ // return subtle.digest("SHA-256", data);
118
+ // };
142
119
143
- export async function generateChallenge ( code_verifier : string ) {
144
- return ( await sha256 ( code_verifier ) ) . toString ( ) ;
145
- }
120
+ // export async function generateChallenge(code_verifier: string) {
121
+ // return (await sha256(code_verifier)).toString();
122
+ // }
146
123
147
- /**
148
- *
149
- * @returns
150
- */
151
- export const pkceChallenge = async ( ) : Promise < PKCEChallenge > => {
152
- const codeVerifier = generateRandomString ( ) ;
153
- return {
154
- codeVerifier,
155
- codeChallenge : await generateChallenge ( codeVerifier ) ,
156
- } ;
157
- } ;
124
+ // / **
125
+ // *
126
+ // * @returns
127
+ // */
128
+ // export const pkceChallenge = async (): Promise<PKCEChallenge> => {
129
+ // const codeVerifier = generateRandomString();
130
+ // return {
131
+ // codeVerifier,
132
+ // codeChallenge: await generateChallenge(codeVerifier),
133
+ // };
134
+ // };
0 commit comments