@outlinerisk/auth0-tools
Advanced tools
Comparing version 0.1.13 to 0.1.14
@@ -105,2 +105,8 @@ import { logger } from '../../logger.js'; | ||
} | ||
// check if grant already exists | ||
const grants = await this.getAppGrantsByName(appName, apiName); | ||
if (grants.length > 0) { | ||
logger.info(`M2M app with name '${appName}' is already authorized on API with name: '${apiName}', exiting.`); | ||
return; | ||
} | ||
// authorize m2m app on api | ||
@@ -107,0 +113,0 @@ let apiScopes = []; |
@@ -1,2 +0,2 @@ | ||
import { Client, ManagementClient, ResourceServer } from 'auth0'; | ||
import { Client, ClientGrant, ManagementClient, ResourceServer } from 'auth0'; | ||
/** | ||
@@ -12,2 +12,3 @@ * Pathpoint's Auth0 client that wraps Auth0's ManagementClient. | ||
apiManager: any; | ||
m2mGrantsManager: any; | ||
prefix: string; | ||
@@ -68,2 +69,9 @@ /** | ||
/** | ||
* Retrieves the grants for the given M2M app on the given API. | ||
* | ||
* @param appName The human readable name of the M2M app. | ||
* @param apiName The human readable name of the API. | ||
*/ | ||
getAppGrantsByName: (appName: string, apiName?: string) => Promise<ClientGrant[]>; | ||
/** | ||
* Retrieves the API(s) with the given name. | ||
@@ -70,0 +78,0 @@ * Returns an array because it's possible to have multiple APIs with the same name, |
@@ -14,2 +14,3 @@ import { ManagementClient } from 'auth0'; | ||
apiManager; | ||
m2mGrantsManager; | ||
prefix; | ||
@@ -38,2 +39,4 @@ /** | ||
this.appManager = this.managementClient.clients; | ||
// @ts-ignore - auth0 doesn't provide types for their managers | ||
this.m2mGrantsManager = this.managementClient.clientGrants; | ||
this.prefix = prefix; | ||
@@ -171,2 +174,46 @@ } | ||
/** | ||
* Retrieves the grants for the given M2M app on the given API. | ||
* | ||
* @param appName The human readable name of the M2M app. | ||
* @param apiName The human readable name of the API. | ||
*/ | ||
getAppGrantsByName = async (appName, apiName = '') => { | ||
let grants = []; | ||
fetchAppGrants: try { | ||
const app = await this.getAppByName(appName); | ||
if (!app) { | ||
break fetchAppGrants; | ||
} | ||
// set up base params | ||
const perPage = 50; | ||
let params = { | ||
client_id: app.client_id, | ||
include_totals: true, | ||
page: 0, | ||
per_page: perPage, | ||
}; | ||
// check for api | ||
const api = await this.getAPIByName(apiName); | ||
if (api) { | ||
params['audience'] = api.identifier; | ||
} | ||
// get first page | ||
let res = await this.m2mGrantsManager.getAll(params); | ||
grants = res.client_grants; | ||
// iterate through remaining pages | ||
const total = res.total; | ||
for (let page = 1; page * perPage < total; page++) { | ||
// get page | ||
params['page'] = page; | ||
res = await this.m2mGrantsManager.getAll(params); | ||
grants = grants.concat(res.client_grants); | ||
} | ||
} | ||
catch (err) { | ||
logger.error(`Error while trying to get grants for M2M app with name: '${appName}' on API with name: '${apiName}'.\n${err}`); | ||
throw err; | ||
} | ||
return grants; | ||
}; | ||
/** | ||
* Retrieves the API(s) with the given name. | ||
@@ -173,0 +220,0 @@ * Returns an array because it's possible to have multiple APIs with the same name, |
{ | ||
"name": "@outlinerisk/auth0-tools", | ||
"version": "0.1.13", | ||
"version": "0.1.14", | ||
"description": "Pathpoint's internal Auth0 tooling.", | ||
@@ -5,0 +5,0 @@ "author": "Pathpoint", |
61049
1249