-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathutils.ts
98 lines (86 loc) · 2.57 KB
/
utils.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
import { Settings } from "../../providers/SettingsProvider/types"
import { StoreKey } from "../../utils/localStorage/types"
type TokenPayload = Partial<{
grant_type: string
code: string
code_verifier: string
client_id: string
redirect_uri: string
refresh_token: string
}>
const getBaseURL = (settings: Settings) => {
// if there is no host in settings, no need to construct base URL at all
if (!settings["acl.oidc.host"]) {
return ""
}
// if there is host in settings, we are in legacy mode, and we should construct the base URL
return `${settings["acl.oidc.tls.enabled"] ? "https" : "http"}://${
settings["acl.oidc.host"]
}:${settings["acl.oidc.port"]}`
}
export const getAuthorisationURL = ({
settings,
code_challenge = null,
state = null,
loginWithDifferentAccount,
redirect_uri,
}: {
settings: Settings
code_challenge: string | null
state: string | null
loginWithDifferentAccount?: boolean
redirect_uri: string
}) => {
const params = {
client_id: settings["acl.oidc.client.id"] || "",
response_type: "code",
scope: settings["acl.oidc.scope"] || "openid",
redirect_uri,
}
const urlParams = new URLSearchParams(params)
if (code_challenge) {
urlParams.append("code_challenge", code_challenge)
urlParams.append("code_challenge_method", "S256")
}
if (state) {
urlParams.append("state", state)
}
if (loginWithDifferentAccount) {
urlParams.append("prompt", "login")
}
return (
getBaseURL(settings) +
settings["acl.oidc.authorization.endpoint"] +
"?" +
urlParams
)
}
export const getTokenExpirationDate = (expires_in: number) => {
return new Date(new Date().getTime() + expires_in * 1000)
}
export const getAuthToken = async (
settings: Settings,
payload: TokenPayload,
) => {
return fetch(
`${getBaseURL(settings)}${settings["acl.oidc.token.endpoint"]}`,
{
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams(payload),
},
)
}
export const hasUIAuth = (settings: Settings) =>
settings["acl.enabled"] && !settings["acl.basic.auth.realm.enabled"]
export const getSSOUserNameWithClientID = (clientId: string) => {
return localStorage.getItem(`${StoreKey.SSO_USERNAME}.${clientId}`) ?? ""
}
export const setSSOUserNameWithClientID = (clientId: string, value: string) => {
localStorage.setItem(`${StoreKey.SSO_USERNAME}.${clientId}`, value)
}
export const removeSSOUserNameWithClientID = (clientId: string) => {
localStorage.removeItem(`${StoreKey.SSO_USERNAME}.${clientId}`)
}