1
- // import { getRandomValues,subtle } from "uncrypto";
2
- import { IssuerRouteTypes , LoginOptions } from "./types" ;
1
+ import { IssuerRouteTypes , LoginMethodParams , LoginOptions } from "./types" ;
3
2
4
3
/**
5
4
*
@@ -21,6 +20,28 @@ export const sanitizeRedirect = (url: string): string => {
21
20
return url . replace ( / \/ $ / , "" ) ;
22
21
} ;
23
22
23
+ export const mapLoginMethodParamsForUrl = (
24
+ options : Partial < LoginMethodParams > ,
25
+ ) : Record < string , string > => {
26
+ const translate : Record < string , string | undefined > = {
27
+ login_hint : options . loginHint ,
28
+ is_create_org : options . isCreateOrg ?. toString ( ) ,
29
+ connection_id : options . connectionId ,
30
+ redirect_uri : options . redirectURL ? sanitizeRedirect ( options . redirectURL ) : undefined ,
31
+ audience : options . audience ,
32
+ scope : options . scope ?. join ( " " ) || 'email profile openid offline' ,
33
+ prompt : options . prompt ,
34
+ lang : options . lang ,
35
+ org_code : options . orgCode ,
36
+ org_name : options . orgName ,
37
+ } ;
38
+
39
+ Object . keys ( translate ) . forEach (
40
+ ( key ) => translate [ key ] === undefined && delete translate [ key ] ,
41
+ ) ;
42
+ return translate as Record < string , string > ;
43
+ } ;
44
+
24
45
/**
25
46
*
26
47
* @param options
@@ -35,14 +56,16 @@ export const generateAuthUrl = (
35
56
const authUrl = new URL ( `${ domain } /oauth2/auth` ) ;
36
57
37
58
const searchParams : Record < string , string > = {
38
- redirect_uri : sanitizeRedirect ( options . callbackURL ) ,
39
59
client_id : options . clientId ,
40
60
response_type : options . responseType || "code" ,
41
- scope : options . scope . join ( " " ) ,
42
- state : options . state ,
43
61
start_page : type ,
62
+ ...mapLoginMethodParamsForUrl ( options ) ,
44
63
} ;
45
64
65
+ if ( options . state ) {
66
+ searchParams [ "state" ] = options . state ;
67
+ }
68
+
46
69
if ( options . codeChallenge ) {
47
70
searchParams [ "code_challenge" ] = options . codeChallenge ;
48
71
searchParams [ "code_challenge_method" ] = "S256" ;
@@ -52,81 +75,7 @@ export const generateAuthUrl = (
52
75
searchParams [ "code_challenge_method" ] = options . codeChallengeMethod ;
53
76
}
54
77
55
- if ( options . audience ) {
56
- searchParams [ "audience" ] = options . audience ;
57
- }
58
-
59
78
authUrl . search = new URLSearchParams ( searchParams ) . toString ( ) ;
60
79
return authUrl ;
61
80
} ;
62
81
63
- // /**
64
- // * Creates a random string of provided length.
65
- // * @param {number } length
66
- // * @returns {string } required secret
67
- // */
68
- // export const generateRandomString = (length: number = 28): string => {
69
- // const bytesNeeded = Math.ceil(length / 2);
70
- // const array = new Uint32Array(bytesNeeded);
71
- // getRandomValues(array);
72
- // let result = Array.from(array, (dec) =>
73
- // ("0" + dec.toString(16)).slice(-2),
74
- // ).join("");
75
- // if (length % 2 !== 0) {
76
- // // If the requested length is odd, remove the last character to adjust the length
77
- // result = result.slice(0, -1);
78
- // }
79
- // return result;
80
- // };
81
-
82
- // //////
83
-
84
- // /**
85
- // *
86
- // * @param code_verifier Verifier to generate challenge from
87
- // * @returns URL safe base64 encoded string
88
- // */
89
- // export async function pkceChallengeFromVerifier(
90
- // code_verifier: string,
91
- // ): Promise<string> {
92
- // const hashed = await sha256(code_verifier);
93
- // const hashedString = Array.from(new Uint8Array(hashed))
94
- // .map((byte) => String.fromCharCode(byte))
95
- // .join("");
96
- // return base64UrlEncode(hashedString);
97
- // }
98
-
99
- // /**
100
- // * setups up PKCE challenge
101
- // * @returns
102
- // */
103
- // export const setupChallenge = () => {
104
- // return { state: generateRandomString(), ...pkceChallenge() };
105
- // };
106
-
107
- // /**
108
- // * Calculate the SHA256 hash of the input text.
109
- // * @param plain the text to hash
110
- // * @returns a promise that resolves to an ArrayBuffer
111
- // */
112
- // export const sha256 = (plain: string) => {
113
- // const encoder = new TextEncoder();
114
- // const data = encoder.encode(plain);
115
- // return subtle.digest("SHA-256", data);
116
- // };
117
-
118
- // export async function generateChallenge(code_verifier: string) {
119
- // return (await sha256(code_verifier)).toString();
120
- // }
121
-
122
- // /**
123
- // *
124
- // * @returns
125
- // */
126
- // export const pkceChallenge = async (): Promise<PKCEChallenge> => {
127
- // const codeVerifier = generateRandomString();
128
- // return {
129
- // codeVerifier,
130
- // codeChallenge: await generateChallenge(codeVerifier),
131
- // };
132
- // };
0 commit comments