//native
async function getManagementToken(auth: RT.Auth0): Promise<string> {
const response = await fetch(`https://${auth.domain}/oauth/token`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
grant_type: "client_credentials",
client_id: auth.client_id,
client_secret: auth.client_secret,
audience: `https://${auth.domain}/api/v2/`,
}),
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
const { access_token } = (await response.json()) as { access_token: string }
return access_token
}
/**
* List Grants
* List user consent grants (issued authorizations). Filter by user_id, client_id or audience.
*/
export async function main(
auth: RT.Auth0,
user_id: string | undefined,
client_id: string | undefined,
audience: string | undefined,
page: number | undefined,
per_page: number | undefined
) {
const token = await getManagementToken(auth)
const url = new URL(`https://${auth.domain}/api/v2/grants`)
if (user_id !== undefined && user_id !== "")
url.searchParams.append("user_id", user_id)
if (client_id !== undefined && client_id !== "")
url.searchParams.append("client_id", client_id)
if (audience !== undefined && audience !== "")
url.searchParams.append("audience", audience)
if (page !== undefined) url.searchParams.append("page", String(page))
if (per_page !== undefined)
url.searchParams.append("per_page", String(per_page))
const response = await fetch(url, {
method: "GET",
headers: {
Authorization: `Bearer ${token}`,
Accept: "application/json",
},
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await response.json()
}
Submitted by hugo989 5 days ago