-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqueryParams.ts
89 lines (82 loc) · 2.48 KB
/
queryParams.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { QueryParams, TrackingQueryParams } from '@/shared/model/QueryParams';
import { validateReturnUrl, validateRefUrl } from '@/server/lib/validateUrl';
import { validateClientId } from '@/server/lib/validateClientId';
import { isStringBoolean } from './isStringBoolean';
import { validateFromUri } from './validateFromUri';
const validateGetOnlyError = (
method: string,
error?: string,
): boolean | undefined => {
// The error parameter causes an error message to be rendered
// Only render this error message for GET pages
// On POST with the error, we redirect to the GET URL of the form with the error parameter
if (method === 'GET' && error === 'true') {
return true;
}
};
const stringToNumber = (
maybeNumber: string | undefined,
): number | undefined => {
if (!maybeNumber) {
return undefined;
}
const parsedNumber = Number(maybeNumber);
if (isNaN(parsedNumber)) {
return undefined;
}
return parsedNumber;
};
export const parseExpressQueryParams = (
method: string,
{
returnUrl,
clientId,
emailVerified,
emailSentSuccess,
csrfError,
recaptchaError,
refViewId,
ref,
listName,
encryptedEmail,
error,
error_description,
componentEventParams,
fromURI,
appClientId,
maxAge,
useOktaClassic,
usePasswordSignIn,
signInEmail,
}: Record<keyof QueryParams, string | undefined>, // parameters from req.query
// some parameters may be manually passed in req.body too,
// generally for tracking purposes
bodyParams: TrackingQueryParams = {},
): QueryParams => {
return {
returnUrl: validateReturnUrl(returnUrl),
clientId: validateClientId(clientId),
emailVerified: isStringBoolean(emailVerified),
emailSentSuccess: isStringBoolean(emailSentSuccess),
csrfError: validateGetOnlyError(method, csrfError),
recaptchaError: validateGetOnlyError(method, recaptchaError),
refViewId: refViewId || bodyParams.refViewId,
ref: (ref || bodyParams.ref) && validateRefUrl(ref || bodyParams.ref),
componentEventParams:
componentEventParams || bodyParams.componentEventParams,
encryptedEmail,
error,
listName: listName,
error_description,
fromURI: validateFromUri(fromURI),
appClientId,
maxAge: stringToNumber(maxAge),
useOktaClassic: isStringBoolean(useOktaClassic),
usePasswordSignIn: isStringBoolean(usePasswordSignIn),
signInEmail,
};
};
export const addReturnUrlToPath = (path: string, returnUrl: string): string => {
const divider = path.includes('?') ? '&' : '?';
return `${path}${divider}returnUrl=${encodeURIComponent(returnUrl)}`;
};