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 | * List Applications |
22 | * List applications (clients) registered in the tenant. Reading client secrets requires the read:client_keys scope. |
23 | */ |
24 | export async function main( |
25 | auth: RT.Auth0, |
26 | fields: string | undefined, |
27 | page: number | undefined, |
28 | per_page: number | undefined |
29 | ) { |
30 | const token = await getManagementToken(auth) |
31 | const url = new URL(`https://${auth.domain}/api/v2/clients`) |
32 | if (fields !== undefined && fields !== "") |
33 | url.searchParams.append("fields", fields) |
34 | if (page !== undefined) url.searchParams.append("page", String(page)) |
35 | if (per_page !== undefined) |
36 | url.searchParams.append("per_page", String(per_page)) |
37 |
|
38 | const response = await fetch(url, { |
39 | method: "GET", |
40 | headers: { |
41 | Authorization: `Bearer ${token}`, |
42 | Accept: "application/json", |
43 | }, |
44 | }) |
45 |
|
46 | if (!response.ok) { |
47 | throw new Error(`${response.status} ${await response.text()}`) |
48 | } |
49 |
|
50 | return await response.json() |
51 | } |
52 |
|