0

List Grants

by
Published 4 days ago

List user consent grants (issued authorizations). Filter by user_id, client_id or audience.

Script auth0 Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 5 days ago
1
//native
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 Grants
22
 * List user consent grants (issued authorizations). Filter by user_id, client_id or audience.
23
 */
24
export async function main(
25
  auth: RT.Auth0,
26
  user_id: string | undefined,
27
  client_id: string | undefined,
28
  audience: string | undefined,
29
  page: number | undefined,
30
  per_page: number | undefined
31
) {
32
  const token = await getManagementToken(auth)
33
  const url = new URL(`https://${auth.domain}/api/v2/grants`)
34
  if (user_id !== undefined && user_id !== "")
35
    url.searchParams.append("user_id", user_id)
36
  if (client_id !== undefined && client_id !== "")
37
    url.searchParams.append("client_id", client_id)
38
  if (audience !== undefined && audience !== "")
39
    url.searchParams.append("audience", audience)
40
  if (page !== undefined) url.searchParams.append("page", String(page))
41
  if (per_page !== undefined)
42
    url.searchParams.append("per_page", String(per_page))
43

44
  const response = await fetch(url, {
45
    method: "GET",
46
    headers: {
47
      Authorization: `Bearer ${token}`,
48
      Accept: "application/json",
49
    },
50
  })
51

52
  if (!response.ok) {
53
    throw new Error(`${response.status} ${await response.text()}`)
54
  }
55

56
  return await response.json()
57
}
58