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 Logs |
22 | * Search tenant log events. Use `q` (Lucene query, e.g. type:"f" for failed logins) and `sort` (e.g. date:-1). Standard pagination caps at 1000 results — use the New Log Event trigger to stream beyond that. |
23 | */ |
24 | export async function main( |
25 | auth: RT.Auth0, |
26 | q: string | undefined, |
27 | sort: string | undefined, |
28 | page: number | undefined, |
29 | per_page: number | undefined |
30 | ) { |
31 | const token = await getManagementToken(auth) |
32 | const url = new URL(`https://${auth.domain}/api/v2/logs`) |
33 | if (q !== undefined && q !== "") url.searchParams.append("q", q) |
34 | if (sort !== undefined && sort !== "") url.searchParams.append("sort", sort) |
35 | if (page !== undefined) url.searchParams.append("page", String(page)) |
36 | if (per_page !== undefined) |
37 | url.searchParams.append("per_page", String(per_page)) |
38 |
|
39 | const response = await fetch(url, { |
40 | method: "GET", |
41 | headers: { |
42 | Authorization: `Bearer ${token}`, |
43 | Accept: "application/json", |
44 | }, |
45 | }) |
46 |
|
47 | if (!response.ok) { |
48 | throw new Error(`${response.status} ${await response.text()}`) |
49 | } |
50 |
|
51 | return await response.json() |
52 | } |
53 |
|