Skip to content

Commit

Permalink
[SDK-3868] Add support for managing client credentials (#774)
Browse files Browse the repository at this point in the history
Co-authored-by: Rita Zerrizuela <[email protected]>
Co-authored-by: Jake Lacey <[email protected]>
  • Loading branch information
3 people authored Jan 12, 2023
1 parent 292829a commit b89b864
Show file tree
Hide file tree
Showing 3 changed files with 469 additions and 1 deletion.
112 changes: 112 additions & 0 deletions src/management/ClientCredentialsManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const BaseManager = require('./BaseManager');

/**
* The client credentials class provides a simple abstraction for performing CRUD operations
* on Auth0 ClientCredentials.
*/
class ClientCredentialsManager extends BaseManager {
/**
* @param {object} options The client options.
* @param {string} options.baseUrl The URL of the API.
* @param {object} [options.headers] Headers to be included in all requests.
* @param {object} [options.retry] Retry Policy Config
*/
constructor(options) {
super(options);

/**
* @type {external:RestClient}
*/
this.resource = this._getRestClient('/clients/:client_id/credentials/:credential_id');
}

/**
* Create a new client credential.
*
* @example
* var params = { client_id: CLIENT_ID };
*
* management.clientCredentials.create(params, data, function (err, credential) {
* if (err) {
* // Handle error.
* }
*
* // The credential created.
* console.log({credential});
* });
* @param {string} params.client_id The client id.
* @param {object} data Client Credential data object.
* @param {Function} [cb] Callback function.
* @returns {Promise|undefined}
*/
create(...args) {
return this.resource.create(...args);
}

/**
* Return a list of credentials for a given client.
*
* @example
* var params = { client_id: CLIENT_ID };
*
* management.clientCredentials.getAll(params, function (err, credentials) {
* if (err) {
* // Handle error.
* }
*
* // A list of credentials associated with that client.
* console.log({credentials});
* });
* @param {string} params.client_id The client id.
* @param {Function} [cb] Callback function.
* @returns {Promise|undefined}
*/
getAll(...args) {
return this.resource.getAll(...args);
}

/**
* Return a credential for a given client.
*
* @example
* var params = { client_id: CLIENT_ID, credential_id: CREDENTIAL_ID };
*
* management.clientCredentials.getById(data, function (err, credential) {
* if (err) {
* // Handle error.
* }
*
* // A specific credential associated with that credential.
* console.log({credential});
* });
* @param {string} params.client_id The client id.
* @param {string} params.credential_id The credential id.
* @param {Function} [cb] Callback function.
* @returns {Promise|undefined}
*/
get(...args) {
return this.resource.get(...args);
}

/**
* Delete a credential for a given client.
*
* @example
* var params = { client_id: CLIENT_ID, credential_id: CREDENTIAL_ID };
*
* management.clientCredentials.delete(params, data, function (err) {
* if (err) {
* // Handle error.
* }
* });
* @param {string} params.client_id The client id.
* @param {string} params.credential_id The credential id.
* @param {Function} [cb] Callback function.
* @returns {Promise|undefined}
*/
delete(...args) {
return this.resource.delete(...args);
}
}

module.exports = ClientCredentialsManager;
10 changes: 9 additions & 1 deletion src/management/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { jsonToBase64, generateClientInfo } = utils;
const { ArgumentError } = require('rest-facade');

// Managers.
const ClientCredentialsManager = require('./ClientCredentialsManager');
const ClientsManager = require('./ClientsManager');
const ClientGrantsManager = require('./ClientGrantsManager');
const GrantsManager = require('./GrantsManager');
Expand Down Expand Up @@ -73,7 +74,6 @@ const MANAGEMENT_API_AUD_FORMAT = 'https://%s/api/v2/';
* cacheTTLInSeconds: 10
* }
* });
*
* @example <caption>
* Initialize your client class, by using a Non Interactive Client to fetch an access_token
* via the Client Credentials Grant, providing a Private Key using the private_key_jwt token
Expand Down Expand Up @@ -177,6 +177,14 @@ class ManagementClient {
*/
this.clients = new ClientsManager(managerOptions);

/**
* Simple abstraction for performing CRUD operations on the
* client credentials endpoint.
*
* @type {module:management.ClientCredentialsManager}
*/
this.clientCredentials = new ClientCredentialsManager(managerOptions);

/**
* Simple abstraction for performing CRUD operations on the client grants
* endpoint.
Expand Down
Loading

0 comments on commit b89b864

Please sign in to comment.