0

Get device

by
Published today

Get the monitoring details of one access point, switch or gateway by serial number.

Script aruba_central_onprem Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 6 hours ago
1
//native
2

3
/**
4
 * Get device
5
 * Get the monitoring details of one access point, switch or gateway by serial number.
6
 */
7
export async function main(
8
  auth: RT.ArubaCentralOnprem,
9
  device_type: "aps" | "switches" | "gateways",
10
  serial: string
11
) {
12
  const token = await getAccessToken(auth, "read")
13
  const base = auth.base_url.replace(/\/+$/, "")
14
  const url = new URL(
15
    `${base}/monitoring/v1/${device_type}/${encodeURIComponent(serial)}`
16
  )
17

18
  const response = await fetch(url, {
19
    headers: {
20
      Authorization: `Bearer ${token}`,
21
      Accept: "application/json",
22
    },
23
  })
24
  if (!response.ok) {
25
    throw new Error(`${response.status} ${await response.text()}`)
26
  }
27

28
  return await response.json()
29
}
30

31
// OAuth grant mechanism documented for Central On-Premises (API Gateway > OAuth grant):
32
// login -> authorization code -> access token. Tokens last 2 hours, so a fresh one is minted per run.
33
async function getAccessToken(
34
  auth: RT.ArubaCentralOnprem,
35
  scope: "read" | "all"
36
): Promise<string> {
37
  const base = auth.base_url.replace(/\/+$/, "")
38

39
  const loginUrl = new URL(`${base}/oauth2/authorize/central/api/login`)
40
  loginUrl.searchParams.append("client_id", auth.client_id)
41
  const login = await fetch(loginUrl, {
42
    method: "POST",
43
    headers: { "Content-Type": "application/json", Accept: "application/json" },
44
    body: JSON.stringify({ username: auth.username, password: auth.password }),
45
  })
46
  if (!login.ok) {
47
    throw new Error(`login: ${login.status} ${await login.text()}`)
48
  }
49
  const cookies = login.headers.get("set-cookie") ?? ""
50
  const csrf = cookies.match(/csrftoken=([^;,\s]+)/)?.[1]
51
  const session = cookies.match(/session=([^;,\s]+)/)?.[1]
52
  if (!csrf || !session) {
53
    throw new Error(
54
      "login: response did not set the csrftoken and session cookies"
55
    )
56
  }
57

58
  const authorizeUrl = new URL(`${base}/oauth2/authorize/central/api`)
59
  authorizeUrl.searchParams.append("client_id", auth.client_id)
60
  authorizeUrl.searchParams.append("response_type", "code")
61
  authorizeUrl.searchParams.append("scope", scope)
62
  const authorize = await fetch(authorizeUrl, {
63
    method: "POST",
64
    headers: {
65
      "Content-Type": "application/json",
66
      Accept: "application/json",
67
      Cookie: `session=${session}`,
68
      "X-CSRF-Token": csrf,
69
    },
70
    body: JSON.stringify({ customer_id: auth.customer_id }),
71
  })
72
  if (!authorize.ok) {
73
    throw new Error(`authorize: ${authorize.status} ${await authorize.text()}`)
74
  }
75
  const { auth_code } = (await authorize.json()) as { auth_code: string }
76

77
  const tokenUrl = new URL(`${base}/oauth2/token`)
78
  tokenUrl.searchParams.append("client_id", auth.client_id)
79
  tokenUrl.searchParams.append("client_secret", auth.client_secret)
80
  tokenUrl.searchParams.append("grant_type", "authorization_code")
81
  tokenUrl.searchParams.append("code", auth_code)
82
  const token = await fetch(tokenUrl, {
83
    method: "POST",
84
    headers: { Accept: "application/json" },
85
  })
86
  if (!token.ok) {
87
    throw new Error(`token: ${token.status} ${await token.text()}`)
88
  }
89
  const { access_token } = (await token.json()) as { access_token: string }
90
  return access_token
91
}
92