-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathurl.ts
345 lines (289 loc) · 10.1 KB
/
url.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import { camelToSnake, createDevOrStagingUrlCache, globs } from '@clerk/shared';
import type { SignUpResource } from '@clerk/types';
import { joinPaths } from './path';
import { getQueryParams } from './querystring';
declare global {
export interface Window {
tldts: {
getDomain(hostname: string, { allowPrivateDomains }: { allowPrivateDomains: boolean }): string;
};
}
}
// This is used as a dummy base when we need to invoke "new URL()" but we don't care about the URL origin.
const DUMMY_URL_BASE = 'http://clerk-dummy';
export const DEV_OR_STAGING_SUFFIXES = [
'.lcl.dev',
'.stg.dev',
'.lclstage.dev',
'.stgstage.dev',
'.dev.lclclerk.com',
'.stg.lclclerk.com',
'.accounts.lclclerk.com',
'accountsstage.dev',
'accounts.dev',
];
const BANNED_URI_PROTOCOLS = ['javascript:'] as const;
const { isDevOrStagingUrl } = createDevOrStagingUrlCache();
export { isDevOrStagingUrl };
const accountsCache = new Map<string, boolean>();
export function isAccountsHostedPages(url: string | URL = window.location.hostname): boolean {
if (!url) {
return false;
}
const hostname = typeof url === 'string' ? url : url.hostname;
let res = accountsCache.get(hostname);
if (res === undefined) {
res = DEV_OR_STAGING_SUFFIXES.some(s => /^(https?:\/\/)?accounts\./.test(hostname) && hostname.endsWith(s));
accountsCache.set(hostname, res);
}
return res;
}
export function getETLDPlusOneFromFrontendApi(frontendApi: string): string {
return frontendApi.replace('clerk.', '');
}
export function getAllETLDs(hostname: string = window.location.hostname): string[] {
const parts = hostname.split('.');
const memo = [];
const domains = [];
for (let i = parts.length - 1; i > 0; i--) {
memo.unshift(parts[i]);
domains.push(memo.join('.'));
}
return domains;
}
interface BuildURLParams extends Partial<URL> {
base?: string;
hashPath?: string;
hashSearch?: string;
}
interface BuildURLOptions<T> {
skipOrigin?: boolean;
stringify?: T;
}
/**
*
* buildURL(params: URLParams, options: BuildURLOptions): string
*
* Builds a URL safely by using the native URL() constructor. It can
* also build a secondary path and search URL that lives inside the hash
* of the main URL. For example:
*
* https://foo.com/bar?qux=42#/hash-bar?hash-qux=42
*
* References:
* https://developer.mozilla.org/en-US/docs/Web/API/URL
*
* @param {BuildURLParams} params
* @param {BuildURLOptions} options
* @returns {URL | string} Returns the URL href
*/
export function buildURL<B extends boolean>(
params: BuildURLParams,
options?: BuildURLOptions<B>,
): B extends true ? string : URL;
export function buildURL(params: BuildURLParams, options: BuildURLOptions<boolean> = {}): URL | string {
const { base, hashPath, hashSearch, searchParams, ...rest } = params;
let fallbackBase = '';
// This check is necessary for React native environments where window is undefined.
// TODO: Refactor all window and document usages in clerk-js and import them from
// a single file that will be mocked easily in React native environments.
if (typeof window !== 'undefined' && !!window.location) {
fallbackBase = window.location.href;
} else {
fallbackBase = 'http://react-native-fake-base-url';
}
const url = new URL(base || '', fallbackBase);
// Properly copy URLSearchParams
if (searchParams instanceof URLSearchParams) {
searchParams.forEach((value, key) => {
url.searchParams.set(key, value);
});
}
Object.assign(url, rest);
// Treat that hash part of the main URL as if it's another URL with a pathname and a search.
// Another nested hash inside the top level hash (e.g. #my-hash#my-second-hash) is currently
// not supported as there is no use case for it yet.
if (hashPath || hashSearch) {
// Parse the hash to a URL object
const dummyUrlForHash = new URL(DUMMY_URL_BASE + url.hash.substring(1));
// Join the current hash path and with the provided one
dummyUrlForHash.pathname = joinPaths(dummyUrlForHash.pathname, hashPath || '');
// Merge search params
const hashSearchParams = getQueryParams(hashSearch || '');
for (const [key, val] of Object.entries(hashSearchParams)) {
dummyUrlForHash.searchParams.append(key, val as string);
}
// Keep just the pathname and the search
const newHash = dummyUrlForHash.href.replace(DUMMY_URL_BASE, '');
// Assign them to the hash of the main url
url.hash = newHash;
}
const { stringify, skipOrigin } = options;
if (stringify) {
return skipOrigin ? url.href.replace(url.origin, '') : url.href;
}
return url;
}
export function toURL(url: string | URL): URL {
return new URL(url.toString(), window.location.origin);
}
/**
*
* stripOrigin(url: URL | string): string
*
* Strips the origin part of a URL and preserves path, search and hash is applicable
*
* References:
* https://developer.mozilla.org/en-US/docs/Web/API/URL
*
* @param {URL | string} url
* @returns {string} Returns the URL href without the origin
*/
export function stripOrigin(url: URL | string): string {
url = toURL(url);
return url.href.replace(url.origin, '');
}
/**
* trimTrailingSlash(path: string): string
*
* Strips the trailing slashes from a string
*
* @returns {string} Returns the string without trailing slashes
* @param path
*/
export const trimTrailingSlash = (path: string): string => {
return (path || '').replace(/\/+$/, '');
};
export const appendAsQueryParams = (
baseUrl: string | URL,
urls: Record<string, string | URL | null | undefined> = {},
): string => {
const base = toURL(baseUrl);
const params = new URLSearchParams();
for (const [key, val] of Object.entries(urls)) {
if (!val) {
continue;
}
const url = toURL(val);
const sameOrigin = base.origin === url.origin;
params.append(camelToSnake(key), sameOrigin ? stripOrigin(url) : url + '');
}
// The following line will prepend the hash with a `/`.
// This is required for ClerkJS Components Hash router to work as expected
// as it treats the hash as sub-path with its nested querystring parameters.
return base + (params.toString() ? '#/?' + params.toString() : '');
};
export const hasExternalAccountSignUpError = (signUp: SignUpResource): boolean => {
const { externalAccount } = signUp.verifications;
return !!externalAccount.error;
};
export function getSearchParameterFromHash({
hash = window.location.hash,
paramName,
}: {
hash?: string;
paramName: string;
}) {
const h = hash.startsWith('#') ? hash.substring(1) : hash;
const dummyUrlForHash = new URL(h, DUMMY_URL_BASE);
return dummyUrlForHash.searchParams.get(paramName);
}
export function isValidUrl(val: unknown, opts?: { includeRelativeUrls?: boolean }): val is string {
const { includeRelativeUrls = false } = opts || {};
if (!val && !includeRelativeUrls) {
return false;
}
try {
new URL(val as string, includeRelativeUrls ? DUMMY_URL_BASE : undefined);
return true;
} catch (e) {
return false;
}
}
export function isDataUri(val?: string): val is string {
if (!isValidUrl(val)) {
return false;
}
return new URL(val).protocol === 'data:';
}
export function hasBannedProtocol(val: string | URL) {
if (!isValidUrl(val)) {
return false;
}
const protocol = new URL(val).protocol;
return BANNED_URI_PROTOCOLS.some(bp => bp === protocol);
}
export const hasUrlInFragment = (_url: URL | string) => {
return new URL(_url, DUMMY_URL_BASE).hash.startsWith('#/');
};
/**
* Creates a new URL by merging a fragment-based URL, if found.
* The result URL has the original and the fragment pathnames appended
* and also includes all search params from both places.
*
* @example
* Input
* https://accounts.clerk.com/sign-in?user_param=hello#/verify/factor-one?redirect_url=/protected
* Return value:
* https://accounts.clerk.com/sign-in/verify/factor-one?user_param=hello&redirect_url=/protected
*/
export const mergeFragmentIntoUrl = (_url: string | URL): URL => {
const url = new URL(_url);
if (!hasUrlInFragment(url)) {
return url;
}
const fragmentUrl = new URL(url.hash.replace('#/', '/'), url.href);
const mergedPathname = [url.pathname, fragmentUrl.pathname]
.map(s => s.split('/'))
.flat()
.filter(Boolean)
.join('/');
const mergedUrl = new URL(mergedPathname, url.origin);
url.searchParams.forEach((val, key) => {
mergedUrl.searchParams.set(key, val);
});
fragmentUrl.searchParams.forEach((val, key) => {
mergedUrl.searchParams.set(key, val);
});
return mergedUrl;
};
export const pathFromFullPath = (fullPath: string) => {
return fullPath.replace(/CLERK-ROUTER\/(.*?)\//, '');
};
const frontendApiRedirectPathsWithUserInput: string[] = [
'/oauth/authorize', // OAuth2 identify provider flow
];
const frontendApiRedirectPathsNoUserInput: string[] = [
'/v1/verify', // magic links
'/v1/tickets/accept', // ticket flow
];
export function isRedirectForFAPIInitiatedFlow(frontendApi: string, redirectUrl: string): boolean {
const url = new URL(redirectUrl, DUMMY_URL_BASE);
const path = url.pathname;
const isValidFrontendRedirectPath =
frontendApiRedirectPathsWithUserInput.includes(path) || frontendApiRedirectPathsNoUserInput.includes(path);
return frontendApi === url.host && isValidFrontendRedirectPath;
}
export function requiresUserInput(redirectUrl: string): boolean {
const url = new URL(redirectUrl, DUMMY_URL_BASE);
return frontendApiRedirectPathsWithUserInput.includes(url.pathname);
}
export const isAllowedRedirectOrigin = (_url: string, allowedRedirectOrigins: Array<string | RegExp> | undefined) => {
if (!allowedRedirectOrigins) {
return true;
}
const url = new URL(_url, DUMMY_URL_BASE);
const isRelativeUrl = url.origin === DUMMY_URL_BASE;
if (isRelativeUrl) {
return true;
}
const isAllowed = allowedRedirectOrigins
.map(origin => (typeof origin === 'string' ? globs.toRegexp(trimTrailingSlash(origin)) : origin))
.some(origin => origin.test(trimTrailingSlash(url.origin)));
if (!isAllowed) {
console.warn(
`Clerk: Redirect URL ${url} is not on one of the allowedRedirectOrigins, falling back to the default redirect URL.`,
);
}
return isAllowed;
};