0

List Application Users

by
Published 4 days ago

List the users assigned to an application.

Script okta Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 5 days ago
1
//native
2

3
export type DynSelect_app_id = string
4

5
// Dropdown of the org's applications so users pick an app instead of typing its opaque ID.
6
export async function app_id(auth: RT.Okta) {
7
  const url = new URL(`${auth.org_url}/api/v1/apps`)
8
  url.searchParams.append("limit", "200")
9
  const response = await fetch(url, {
10
    headers: {
11
      Authorization: `SSWS ${auth.token}`,
12
      Accept: "application/json",
13
    },
14
  })
15
  if (!response.ok) {
16
    throw new Error(`${response.status} ${await response.text()}`)
17
  }
18
  const apps = (await response.json()) as {
19
    id: string
20
    label: string
21
    name: string
22
  }[]
23
  return apps.map((a) => ({
24
    value: a.id,
25
    label: a.label ? `${a.label} (${a.id})` : a.id,
26
  }))
27
}
28

29
/**
30
 * List Application Users
31
 * List the users assigned to an application. Page with `limit` and the `after` cursor from the previous response's Link header.
32
 */
33
export async function main(
34
  auth: RT.Okta,
35
  app_id: DynSelect_app_id,
36
  q: string | undefined,
37
  limit: number | undefined,
38
  after: string | undefined
39
) {
40
  const url = new URL(`${auth.org_url}/api/v1/apps/${app_id}/users`)
41
  if (q !== undefined && q !== "") url.searchParams.append("q", q)
42
  if (limit !== undefined) url.searchParams.append("limit", String(limit))
43
  if (after !== undefined && after !== "")
44
    url.searchParams.append("after", after)
45

46
  const response = await fetch(url, {
47
    method: "GET",
48
    headers: {
49
      Authorization: `SSWS ${auth.token}`,
50
      Accept: "application/json",
51
    },
52
  })
53

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

58
  return await response.json()
59
}
60