1 | |
2 |
|
3 | async function getManagementToken(auth: RT.Auth0): Promise<string> { |
4 | const response = await fetch(`https://${auth.domain}/oauth/token`, { |
5 | method: "POST", |
6 | headers: { "Content-Type": "application/json" }, |
7 | body: JSON.stringify({ |
8 | grant_type: "client_credentials", |
9 | client_id: auth.client_id, |
10 | client_secret: auth.client_secret, |
11 | audience: `https://${auth.domain}/api/v2/`, |
12 | }), |
13 | }) |
14 | if (!response.ok) { |
15 | throw new Error(`${response.status} ${await response.text()}`) |
16 | } |
17 | const { access_token } = (await response.json()) as { access_token: string } |
18 | return access_token |
19 | } |
20 | |
21 | * Update Role |
22 | * Update a role's name and/or description. |
23 | */ |
24 | export async function main( |
25 | auth: RT.Auth0, |
26 | role_id: string, |
27 | name: string | undefined, |
28 | description: string | undefined |
29 | ) { |
30 | const token = await getManagementToken(auth) |
31 | const url = new URL(`https://${auth.domain}/api/v2/roles/${role_id}`) |
32 | const body: { [key: string]: any } = {} |
33 | if (name !== undefined && name !== "") body.name = name |
34 | if (description !== undefined) body.description = description |
35 |
|
36 | const response = await fetch(url, { |
37 | method: "PATCH", |
38 | headers: { |
39 | Authorization: `Bearer ${token}`, |
40 | "Content-Type": "application/json", |
41 | Accept: "application/json", |
42 | }, |
43 | body: JSON.stringify(body), |
44 | }) |
45 |
|
46 | if (!response.ok) { |
47 | throw new Error(`${response.status} ${await response.text()}`) |
48 | } |
49 |
|
50 | return await response.json() |
51 | } |
52 |
|