-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathurls.ts
82 lines (72 loc) · 2.22 KB
/
urls.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
import { LOGOUT_ENDPOINT, log } from '../globals'
import { isBlank } from './strings'
/**
* Join the supplied strings together using '/', stripping any leading/ending '/'
* from the supplied strings if needed, except the first and last string.
*/
export function joinPaths(...paths: string[]): string {
const tmp: string[] = []
paths.forEach((path, index) => {
if (isBlank(path)) {
return
}
if (path === '/') {
tmp.push('')
return
}
if (index !== 0 && path.match(/^\//)) {
path = path.slice(1)
}
if (index < paths.length - 1 && path.match(/\/$/)) {
path = path.slice(0, path.length - 1)
}
if (!isBlank(path)) {
tmp.push(path)
}
})
return tmp.join('/')
}
async function logoutRedirectAvailable(): Promise<boolean> {
const response = await fetch(LOGOUT_ENDPOINT)
if (!response || response.status >= 400) {
log.debug('Warning: Server does not have a logout page. Redirecting to login provider directly ...')
return false
}
return true
}
export function logoutRedirect(redirectUri: URL): void {
// Have a logout page so append redirect uri to its url
const logoutUri = new URL(relToAbsUrl(LOGOUT_ENDPOINT))
logoutUri.searchParams.append('redirect_uri', redirectUri.toString())
logoutRedirectAvailable().then(exists => {
if (exists) redirect(logoutUri)
else redirect(redirectUri)
})
}
export function validateRedirectURI(redirectUri: URL) {
const currentUrl = new URL(window.location.href)
const { hostname, port, protocol } = redirectUri
return (
hostname === currentUrl.hostname &&
port === currentUrl.port &&
protocol === currentUrl.protocol &&
['http:', 'https:'].includes(protocol)
)
}
export function sanitizeUri(url: URL) {
const searchParams = url.searchParams
if (searchParams.toString() !== '') {
searchParams.forEach((value, key) => {
searchParams.set(key, encodeURIComponent(value))
})
}
return url.href
}
export function redirect(target: URL) {
log.debug('Redirecting to URI:', target)
// Redirect to the target URI
window.location.href = target.toString()
}
export function relToAbsUrl(relativeUrl: string): string {
return new URL(relativeUrl, window.location.origin).href
}