-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhelpers.ts
More file actions
54 lines (49 loc) · 1.39 KB
/
helpers.ts
File metadata and controls
54 lines (49 loc) · 1.39 KB
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
import { HttpsProxyAgent } from 'https-proxy-agent'
import { InvalidTokenError, jwtDecode } from 'jwt-decode'
import z, { ZodError } from 'zod'
export const jwtClaimsPrincipalSchema = z.object({
id: z.string(),
username: z.string(),
role: z.string(),
status: z.string(),
account: z.object({ id: z.string(), name: z.string() }),
permission: z.object({
id: z.string(),
workspaceId: z.string(),
workspaceName: z.string(),
role: z.string(),
status: z.string()
})
})
export const jwtClaimsSchema = z.object({
sub: z.string(),
principal: jwtClaimsPrincipalSchema,
iss: z.url(),
jti: z.string(),
exp: z.number(),
instanceUrl: z.url(),
region: z.string()
})
export type TJwtClaims = z.infer<typeof jwtClaimsSchema>
export const getJwtClaims = (bearerToken: string): TJwtClaims => {
let claims: TJwtClaims
let claimsUnchecked: unknown
try {
claimsUnchecked = jwtDecode(bearerToken)
claims = jwtClaimsSchema.parse(claimsUnchecked)
}
catch (err) {
if (err instanceof InvalidTokenError) {
console.error('could not decode jwt token', err)
}
if (err instanceof ZodError) {
console.error('unexpected jwt claims schema', err.message)
}
throw err
}
return claims
}
export const getAxiosProxyConfiguration = (proxyConfig: string): HttpsProxyAgent<string> => {
const httpsAgent = new HttpsProxyAgent(proxyConfig)
return httpsAgent
}