0

List Group Members

by
Published 4 days ago

List the users that belong to a group.

Script okta Verified

The script

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

3
export type DynSelect_group_id = string
4

5
// Dropdown of the org's groups so users pick a group instead of typing its opaque ID.
6
export async function group_id(auth: RT.Okta) {
7
  const url = new URL(`${auth.org_url}/api/v1/groups`)
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 groups = (await response.json()) as {
19
    id: string
20
    profile: { name: string }
21
  }[]
22
  return groups.map((g) => ({
23
    value: g.id,
24
    label: g.profile?.name ? `${g.profile.name} (${g.id})` : g.id,
25
  }))
26
}
27

28
/**
29
 * List Group Members
30
 * List the users that belong to a group. Page with `limit` and the `after` cursor from the previous response's Link header.
31
 */
32
export async function main(
33
  auth: RT.Okta,
34
  group_id: DynSelect_group_id,
35
  limit: number | undefined,
36
  after: string | undefined
37
) {
38
  const url = new URL(`${auth.org_url}/api/v1/groups/${group_id}/users`)
39
  if (limit !== undefined) url.searchParams.append("limit", String(limit))
40
  if (after !== undefined && after !== "")
41
    url.searchParams.append("after", after)
42

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

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

55
  return await response.json()
56
}
57