Skip to content

Commit 5ddf74d

Browse files
committed
allow custom params in helpers
1 parent da2f6ca commit 5ddf74d

File tree

1 file changed

+81
-36
lines changed

1 file changed

+81
-36
lines changed

src/static/helpers/slasHelper.ts

Lines changed: 81 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ export const generateCodeChallenge = async (
104104
* @param parameters.redirectURI - the location the client will be returned to after successful login with 3rd party IDP. Must be registered in SLAS.
105105
* @param parameters.hint? - optional string to hint at a particular IDP. Guest sessions are created by setting this to 'guest'
106106
* @param parameters.usid? - optional saved SLAS user id to link the new session to a previous session
107+
* @param privateClient - flag to indicate if the client is private or not. Defaults to false.
108+
* @param headers - optional headers to pass in the 'authorizeCustomer` endpoint.
107109
* @returns login url, user id and authorization code if available
108110
*/
109111
export async function authorize(
@@ -118,7 +120,8 @@ export async function authorize(
118120
redirectURI: string;
119121
hint?: string;
120122
usid?: string;
121-
},
123+
} & {[key in `c_${string}`]: any},
124+
headers?: {[key: string]: string},
122125
privateClient = false
123126
): Promise<{code: string; url: string; usid: string}> {
124127
interface ClientOptions {
@@ -153,7 +156,10 @@ export async function authorize(
153156
redirect_uri: parameters.redirectURI,
154157
response_type: 'code',
155158
...(parameters.usid && {usid: parameters.usid}),
159+
// we don't need to validate c_params because shopperLogin func will do that
160+
...parameters,
156161
},
162+
headers,
157163
};
158164

159165
const response = await slasClientCopy.authorizeCustomer(options, true);
@@ -265,23 +271,27 @@ export async function loginIDPUser(
265271
code: string;
266272
usid?: string;
267273
dnt?: boolean;
268-
}
274+
} & {[key in `c_${string}`]: any},
275+
headers?: {[key: string]: string}
269276
): Promise<TokenResponse> {
270277
const privateClient = !!credentials.clientSecret;
278+
const {code, dnt, usid, ...restOfParams} = parameters;
271279

272280
const tokenBody: TokenRequest = {
273281
client_id: slasClient.clientConfig.parameters.clientId,
274282
channel_id: slasClient.clientConfig.parameters.siteId,
275-
code: parameters.code,
283+
code,
276284
organizationId: slasClient.clientConfig.parameters.organizationId,
277285
...(!privateClient &&
278286
credentials.codeVerifier && {code_verifier: credentials.codeVerifier}),
279287
grant_type: privateClient
280288
? 'authorization_code'
281289
: 'authorization_code_pkce',
282290
redirect_uri: parameters.redirectURI,
283-
...(parameters.dnt !== undefined && {dnt: parameters.dnt.toString()}),
284-
...(parameters.usid && {usid: parameters.usid}),
291+
...(dnt !== undefined && {dnt: dnt.toString()}),
292+
...(usid && {usid: parameters.usid}),
293+
// no need to validate here since `slasClient.getAccessToken` will do that
294+
...restOfParams,
285295
};
286296
// Using slas private client
287297
if (credentials.clientSecret) {
@@ -298,7 +308,7 @@ export async function loginIDPUser(
298308
return slasClient.getAccessToken(optionsToken);
299309
}
300310
// default is to use slas public client
301-
return slasClient.getAccessToken({body: tokenBody});
311+
return slasClient.getAccessToken({body: tokenBody, headers});
302312
}
303313

304314
/**
@@ -310,6 +320,7 @@ export async function loginIDPUser(
310320
* @param parameters - parameters to pass in the API calls.
311321
* @param parameters.usid? - Unique Shopper Identifier to enable personalization.
312322
* @param parameters.dnt? - Optional parameter to enable Do Not Track (DNT) for the user.
323+
* @param headers - optional headers to pass in the 'getAccessToken` endpoint.
313324
* @returns TokenResponse
314325
*/
315326
export async function loginGuestUserPrivate(
@@ -322,10 +333,11 @@ export async function loginGuestUserPrivate(
322333
parameters: {
323334
usid?: string;
324335
dnt?: boolean;
325-
},
336+
} & {[key in `c_${string}`]: any},
326337
credentials: {
327338
clientSecret: string;
328-
}
339+
},
340+
headers?: {[key: string]: string}
329341
): Promise<TokenResponse> {
330342
if (!slasClient.clientConfig.parameters.siteId) {
331343
throw new Error(
@@ -336,16 +348,19 @@ export async function loginGuestUserPrivate(
336348
const authorization = `Basic ${stringToBase64(
337349
`${slasClient.clientConfig.parameters.clientId}:${credentials.clientSecret}`
338350
)}`;
339-
351+
const {usid, dnt, ...restOfParams} = parameters;
340352
const options = {
341353
headers: {
342354
Authorization: authorization,
355+
...headers,
343356
},
344357
body: {
345358
grant_type: 'client_credentials',
346359
channel_id: slasClient.clientConfig.parameters.siteId,
347360
...(parameters.usid && {usid: parameters.usid}),
348361
...(parameters.dnt !== undefined && {dnt: parameters.dnt.toString()}),
362+
// no need to validate here since `slasClient.getAccessToken` will do that
363+
...restOfParams,
349364
},
350365
};
351366

@@ -359,6 +374,7 @@ export async function loginGuestUserPrivate(
359374
* @param parameters.redirectURI - Per OAuth standard, a valid app route. Must be listed in your SLAS configuration. On server, this will not be actually called. On browser, this will be called, but ignored.
360375
* @param parameters.usid? - Unique Shopper Identifier to enable personalization.
361376
* @param parameters.dnt? - Optional parameter to enable Do Not Track (DNT) for the user.
377+
* @param headers - optional headers to pass in the 'getAccessToken` and authorize` endpoints.
362378
* @returns TokenResponse
363379
*/
364380
export async function loginGuestUser(
@@ -372,28 +388,35 @@ export async function loginGuestUser(
372388
redirectURI: string;
373389
usid?: string;
374390
dnt?: boolean;
375-
}
391+
} & {[key in `c_${string}`]: any},
392+
headers?: {[key: string]: string}
376393
): Promise<TokenResponse> {
377394
const codeVerifier = createCodeVerifier();
378395

379-
const authResponse = await authorize(slasClient, codeVerifier, {
380-
redirectURI: parameters.redirectURI,
381-
hint: 'guest',
382-
...(parameters.usid && {usid: parameters.usid}),
383-
});
384-
396+
const authResponse = await authorize(
397+
slasClient,
398+
codeVerifier,
399+
{
400+
redirectURI: parameters.redirectURI,
401+
hint: 'guest',
402+
...(parameters.usid && {usid: parameters.usid}),
403+
},
404+
headers
405+
);
406+
const {dnt, redirectURI, ...restOfParams} = parameters;
385407
const tokenBody: TokenRequest = {
386408
client_id: slasClient.clientConfig.parameters.clientId,
387409
channel_id: slasClient.clientConfig.parameters.siteId,
388410
code: authResponse.code,
389411
code_verifier: codeVerifier,
390412
grant_type: 'authorization_code_pkce',
391-
redirect_uri: parameters.redirectURI,
413+
redirect_uri: redirectURI,
392414
usid: authResponse.usid,
393-
...(parameters.dnt !== undefined && {dnt: parameters.dnt.toString()}),
415+
...(dnt !== undefined && {dnt: dnt.toString()}),
416+
...restOfParams,
394417
};
395418

396-
return slasClient.getAccessToken({body: tokenBody});
419+
return slasClient.getAccessToken({body: tokenBody, headers});
397420
}
398421

399422
/**
@@ -408,6 +431,8 @@ export async function loginGuestUser(
408431
* @param parameters.redirectURI - Per OAuth standard, a valid app route. Must be listed in your SLAS configuration. On server, this will not be actually called. On browser, this will be called, but ignored.
409432
* @param parameters.usid? - Unique Shopper Identifier to enable personalization.
410433
* @param parameters.dnt? - Optional parameter to enable Do Not Track (DNT) for the user.
434+
* @param headers - optional headers to pass in the 'getAccessToken' and 'authenticateCustomer' endpoints.
435+
* @param body - optional body parameters to pass in the 'authenticateCustomer' endpoint.
411436
* @returns TokenResponse
412437
*/
413438
export async function loginRegisteredUserB2C(
@@ -426,7 +451,9 @@ export async function loginRegisteredUserB2C(
426451
redirectURI: string;
427452
usid?: string;
428453
dnt?: boolean;
429-
}
454+
} & {[key in `c_${string}`]: any},
455+
headers?: {[key: string]: string},
456+
body?: {[key: string]: string}
430457
): Promise<TokenResponse> {
431458
const codeVerifier = createCodeVerifier();
432459
const codeChallenge = await generateCodeChallenge(codeVerifier);
@@ -446,20 +473,23 @@ export async function loginRegisteredUserB2C(
446473
const authorization = `Basic ${stringToBase64(
447474
`${credentials.username}:${credentials.password}`
448475
)}`;
449-
476+
const {dnt, usid, redirectURI, ...restOfParams} = parameters;
450477
const options = {
451478
headers: {
452479
Authorization: authorization,
480+
...headers,
453481
},
454482
parameters: {
455483
organizationId: slasClient.clientConfig.parameters.organizationId,
484+
...restOfParams,
456485
},
457486
body: {
458-
redirect_uri: parameters.redirectURI,
487+
redirect_uri: redirectURI,
459488
client_id: slasClient.clientConfig.parameters.clientId,
460489
code_challenge: codeChallenge,
461490
channel_id: slasClient.clientConfig.parameters.siteId,
462-
...(parameters.usid && {usid: parameters.usid}),
491+
...(usid && {usid}),
492+
...body,
463493
},
464494
};
465495

@@ -482,7 +512,7 @@ export async function loginRegisteredUserB2C(
482512
organizationId: slasClient.clientConfig.parameters.organizationId,
483513
redirect_uri: parameters.redirectURI,
484514
usid: authResponse.usid,
485-
...(parameters.dnt !== undefined && {dnt: parameters.dnt.toString()}),
515+
...(dnt !== undefined && {dnt: dnt.toString()}),
486516
};
487517
// using slas private client
488518
if (credentials.clientSecret) {
@@ -493,13 +523,14 @@ export async function loginRegisteredUserB2C(
493523
const optionsToken = {
494524
headers: {
495525
Authorization: authHeaderIdSecret,
526+
...headers,
496527
},
497528
body: tokenBody,
498529
};
499530
return slasClient.getAccessToken(optionsToken);
500531
}
501532
// default is to use slas public client
502-
return slasClient.getAccessToken({body: tokenBody});
533+
return slasClient.getAccessToken({body: tokenBody, headers});
503534
}
504535

505536
/* Function to send passwordless login token
@@ -531,7 +562,8 @@ export async function authorizePasswordless(
531562
userid: string;
532563
locale?: string;
533564
mode: string;
534-
}
565+
} & {[key in `c_${string}`]: any},
566+
headers?: {[key: string]: string}
535567
): Promise<Response> {
536568
if (!credentials.clientSecret) {
537569
throw new Error('Required argument client secret is not provided');
@@ -554,22 +586,25 @@ export async function authorizePasswordless(
554586
const authHeaderIdSecret = `Basic ${stringToBase64(
555587
`${slasClient.clientConfig.parameters.clientId}:${credentials.clientSecret}`
556588
)}`;
589+
const {userid, mode, locale, usid, callbackURI, ...restOfParams} = parameters;
557590
const tokenBody = {
558-
user_id: parameters.userid,
559-
mode: parameters.mode,
560-
...(parameters.locale && {locale: parameters.locale}),
561-
...(parameters.usid && {usid: parameters.usid}),
591+
user_id: userid,
592+
mode,
593+
...(locale && {locale}),
594+
...(usid && {usid}),
562595
channel_id: slasClient.clientConfig.parameters.siteId,
563-
...(parameters.callbackURI && {callback_uri: parameters.callbackURI}),
596+
...(callbackURI && {callback_uri: callbackURI}),
564597
};
565598

566599
return slasClient.authorizePasswordlessCustomer(
567600
{
568601
headers: {
569602
Authorization: authHeaderIdSecret,
603+
...headers,
570604
},
571605
parameters: {
572606
organizationId: slasClient.clientConfig.parameters.organizationId,
607+
...restOfParams,
573608
},
574609
body: tokenBody,
575610
},
@@ -587,6 +622,7 @@ export async function authorizePasswordless(
587622
* @param parameters.callbackURI? - URI to send the passwordless login token to. Must be listed in your SLAS configuration. Required when mode is callback
588623
* @param parameters.pwdlessLoginToken - Passwordless login token
589624
* @param parameters.dnt? - Optional parameter to enable Do Not Track (DNT) for the user.
625+
* @param headers - optional headers to pass in the 'getPasswordLessAccessToken'
590626
* @returns Promise of Response or Object
591627
*/
592628
export async function getPasswordLessAccessToken(
@@ -602,7 +638,8 @@ export async function getPasswordLessAccessToken(
602638
parameters: {
603639
pwdlessLoginToken: string;
604640
dnt?: string;
605-
}
641+
},
642+
headers?: {[key: string]: string}
606643
): Promise<TokenResponse> {
607644
if (!credentials.clientSecret) {
608645
throw new Error('Required argument client secret is not provided');
@@ -622,19 +659,22 @@ export async function getPasswordLessAccessToken(
622659
`${slasClient.clientConfig.parameters.clientId}:${credentials.clientSecret}`
623660
)}`;
624661

662+
const {dnt, ...restOfParams} = parameters;
625663
const tokenBody = {
626664
grant_type: 'client_credentials',
627665
hint: 'pwdless_login',
628666
pwdless_login_token: parameters.pwdlessLoginToken,
629667
code_verifier: codeVerifier,
630-
...(parameters.dnt && {dnt: parameters.dnt}),
668+
...(dnt && {dnt}),
631669
};
632670
return slasClient.getPasswordLessAccessToken({
633671
headers: {
634672
Authorization: authHeaderIdSecret,
673+
...headers,
635674
},
636675
parameters: {
637676
organizationId: slasClient.clientConfig.parameters.organizationId,
677+
...restOfParams,
638678
},
639679
body: tokenBody,
640680
});
@@ -648,6 +688,7 @@ export async function getPasswordLessAccessToken(
648688
* @param parameters.refreshToken - a valid refresh token to exchange for a new access token (and refresh token).
649689
* @param credentials - the clientSecret (if applicable) to login with.
650690
* @param credentials.clientSecret - secret associated with client ID
691+
* @param headers - optional headers to pass in the 'get
651692
* @returns TokenResponse
652693
*/
653694
export function refreshAccessToken(
@@ -660,8 +701,9 @@ export function refreshAccessToken(
660701
parameters: {
661702
refreshToken: string;
662703
dnt?: boolean;
663-
},
664-
credentials?: {clientSecret?: string}
704+
} & {[key in `c_${string}`]: any},
705+
credentials?: {clientSecret?: string},
706+
headers?: {[key: string]: string}
665707
): Promise<TokenResponse> {
666708
const body = {
667709
grant_type: 'refresh_token',
@@ -678,6 +720,7 @@ export function refreshAccessToken(
678720
const options = {
679721
headers: {
680722
Authorization: authorization,
723+
...headers,
681724
},
682725
body,
683726
};
@@ -693,6 +736,7 @@ export function refreshAccessToken(
693736
* @param parameters - parameters to pass in the API calls.
694737
* @param parameters.accessToken - a valid access token to exchange for a new access token (and refresh token).
695738
* @param parameters.refreshToken - a valid refresh token to exchange for a new access token (and refresh token).
739+
* @param headers - optional headers to pass in the 'logoutCustomer` endpoint.
696740
* @returns TokenResponse
697741
*/
698742
export function logout(
@@ -705,7 +749,8 @@ export function logout(
705749
parameters: {
706750
accessToken: string;
707751
refreshToken: string;
708-
}
752+
} & {[key in `c_${string}`]: any},
753+
headers?: {[key: string]: string}
709754
): Promise<TokenResponse> {
710755
return slasClient.logoutCustomer({
711756
headers: {

0 commit comments

Comments
 (0)