Skip to content

Commit

Permalink
Added tokenProvider functionality (#196)
Browse files Browse the repository at this point in the history
* Added tokenProvider functionality

* applied feedback

* Update yarn.lock

* Make audience of management api optional.
When its not provided is inferred from domain's

* Clean a bit the README.md
  • Loading branch information
dctoon authored and hzalaz committed Sep 30, 2017
1 parent 74ede96 commit c8ce74f
Show file tree
Hide file tree
Showing 24 changed files with 1,026 additions and 74 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ var auth0 = new AuthenticationClient({
## Management API Client
The Auth0 Management API is meant to be used by back-end servers or trusted parties performing administrative tasks. Generally speaking, anything that can be done through the Auth0 dashboard (and more) can also be done through this API.


Initialize your client class with an API v2 token and a domain.

```js
Expand All @@ -43,7 +42,22 @@ var management = new ManagementClient({
});
```

> When using at browser you should use `telemetry: false`.
> Note: When using at browser you should use `telemetry: false`.
To obtain **automatically** a Management API token via the ManagementClient, you can specify the parameters `clientId`, `clientSecret` (use a Non Interactive Client) and optionally `scope`.
Behind the scenes the Client Credentials Grant is used to obtain the `access_token` and is by default cached for the duration of the returned `expires_in` value.

```js
var ManagementClient = require('auth0').ManagementClient;
var auth0 = new ManagementClient({
domain: '{YOUR_ACCOUNT}.auth0.com',
clientId: '{YOUR_NON_INTERACTIVE_CLIENT_ID}',
clientSecret: '{YOUR_NON_INTERACTIVE_CLIENT_SECRET}',
scope: "read:users write:users",
});
```

> Make sure your ClientId is allowed to request tokens from Management API in [Auth0 Dashboard](https://manage.auth0.com/#/apis)
To obtain a Management API token from your node backend, you can use Client Credentials Grant using your registered Auth0 Non Interactive Clients

Expand All @@ -67,8 +81,6 @@ auth0.clientCredentialsGrant({
});
```

> Make sure your ClientId is allowed to request tokens from Management API in [Auth0 Dashboard](https://manage.auth0.com/#/apis)
Also you can request a token when the user authenticates using any of our client side SDKs, e.g. [auth0.js](https://github.com/auth0/auth0.js).

## Promises and callbacks
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"dependencies": {
"bluebird": "^2.10.2",
"request": "^2.83.0",
"lru-memoizer": "^1.11.1",
"object.assign": "^4.0.4",
"rest-facade": "^1.5.0"
},
"devDependencies": {
Expand Down
71 changes: 71 additions & 0 deletions src/Auth0RestClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
var RestClient = require('rest-facade').Client;
var Promise = require('bluebird');
var ArgumentError = require('rest-facade').ArgumentError;

var Auth0RestClient = function (resourceUrl, options, provider) {
if (resourceUrl === null || resourceUrl === undefined) {
throw new ArgumentError('Must provide a Resource Url');
}

if ('string' !== typeof resourceUrl || resourceUrl.length === 0) {
throw new ArgumentError('The provided Resource Url is invalid');
}

if (options === null || typeof options !== 'object') {
throw new ArgumentError('Must provide options');
}

this.options = options;
this.provider = provider;
this.restClient = new RestClient(resourceUrl, options);

this.wrappedProvider = function (method, args) {
if (!this.provider) {
return this.restClient[method].apply(this.restClient, args);
}

var callback;
if(args && args[args.length -1] instanceof Function){
callback = args[args.length -1];
}

var self = this;
return this.provider.getAccessToken()
.then(function (access_token) {
self.options.headers['Authorization'] = 'Bearer ' + access_token;
return self.restClient[method].apply(self.restClient, args);
}).catch(function(err){
if(callback){
return callback(err);
}
return Promise.reject(err);
});
}
};

Auth0RestClient.prototype.getAll = function ( /* [params], [callback] */ ) {
return this.wrappedProvider('getAll', arguments);
};


Auth0RestClient.prototype.get = function ( /* [params], [callback] */ ) {
return this.wrappedProvider('get', arguments);
}

Auth0RestClient.prototype.create = function ( /* [params], [callback] */ ) {
return this.wrappedProvider('create', arguments);
}

Auth0RestClient.prototype.patch = function ( /* [params], [callback] */ ) {
return this.wrappedProvider('patch', arguments);
}

Auth0RestClient.prototype.update = function ( /* [params], [callback] */ ) {
return this.wrappedProvider('update', arguments);
}

Auth0RestClient.prototype.delete = function ( /* [params], [callback] */ ) {
return this.wrappedProvider('delete', arguments);
}

module.exports = Auth0RestClient;
5 changes: 2 additions & 3 deletions src/management/BlacklistedTokensManager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var RestClient = require('rest-facade').Client;
var ArgumentError = require('rest-facade').ArgumentError;
var utils = require('../utils');

var Auth0RestClient = require('../Auth0RestClient');

/**
* @class BlacklistedTokensManager
Expand Down Expand Up @@ -44,7 +43,7 @@ var BlacklistedTokensManager = function (options) {
*
* @type {external:RestClient}
*/
this.resource = new RestClient(options.baseUrl + '/blacklists/tokens', clientOptions);
this.resource = new Auth0RestClient(options.baseUrl + '/blacklists/tokens', clientOptions, options.tokenProvider);
};


Expand Down
5 changes: 2 additions & 3 deletions src/management/ClientGrantsManager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var RestClient = require('rest-facade').Client;
var ArgumentError = require('rest-facade').ArgumentError;
var utils = require('../utils');

var Auth0RestClient = require('../Auth0RestClient');

/**
* @class ClientGrantsManager
Expand Down Expand Up @@ -46,7 +45,7 @@ var ClientGrantsManager = function (options) {
*
* @type {external:RestClient}
*/
this.resource = new RestClient(options.baseUrl + '/client-grants/:id', clientOptions);
this.resource = new Auth0RestClient(options.baseUrl + '/client-grants/:id', clientOptions, options.tokenProvider);
};


Expand Down
5 changes: 2 additions & 3 deletions src/management/ClientsManager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var RestClient = require('rest-facade').Client;
var ArgumentError = require('rest-facade').ArgumentError;
var utils = require('../utils');

var Auth0RestClient = require('../Auth0RestClient');

/**
* @class ClientsManager
Expand Down Expand Up @@ -49,7 +48,7 @@ var ClientsManager = function (options) {
*
* @type {external:RestClient}
*/
this.resource = new RestClient(options.baseUrl + '/clients/:client_id', clientOptions);
this.resource = new Auth0RestClient(options.baseUrl + '/clients/:client_id', clientOptions, options.tokenProvider);
};


Expand Down
5 changes: 2 additions & 3 deletions src/management/ConnectionsManager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var RestClient = require('rest-facade').Client;
var ArgumentError = require('rest-facade').ArgumentError;
var utils = require('../utils');

var Auth0RestClient = require('../Auth0RestClient');

/**
* @class ConnectionsManager
Expand Down Expand Up @@ -43,7 +42,7 @@ var ConnectionsManager = function (options) {
*
* @type {external:RestClient}
*/
this.resource = new RestClient(options.baseUrl + '/connections/:id ', apiOptions);
this.resource = new Auth0RestClient(options.baseUrl + '/connections/:id ', apiOptions, options.tokenProvider);
};


Expand Down
7 changes: 3 additions & 4 deletions src/management/DeviceCredentialsManager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var RestClient = require('rest-facade').Client;
var ArgumentError = require('rest-facade').ArgumentError;
var utils = require('../utils');

var Auth0RestClient = require('../Auth0RestClient');

/**
* Simple facade for consuming a REST API endpoint.
Expand Down Expand Up @@ -49,9 +48,9 @@ var DeviceCredentialsManager = function (options) {
* {@link https://auth0.com/docs/api/v2#!/Device_Credentials
* Auth0 DeviceCredentialsManagers endpoint}.
*
* @type {external:RestDeviceCredentialsManager}
* @type {external:RestClient}
*/
this.resource = new RestClient(options.baseUrl + '/device-credentials/:id', clientOptions);
this.resource = new Auth0RestClient(options.baseUrl + '/device-credentials/:id', clientOptions, options.tokenProvider);
};


Expand Down
5 changes: 2 additions & 3 deletions src/management/EmailProviderManager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var RestClient = require('rest-facade').Client;
var ArgumentError = require('rest-facade').ArgumentError;
var utils = require('../utils');

var Auth0RestClient = require('../Auth0RestClient');

/**
* Simple facade for consuming a REST API endpoint.
Expand Down Expand Up @@ -50,7 +49,7 @@ var EmailProviderManager = function (options) {
*
* @type {external:RestClient}
*/
this.resource = new RestClient(options.baseUrl + '/emails/provider', clientOptions);
this.resource = new Auth0RestClient(options.baseUrl + '/emails/provider', clientOptions, options.tokenProvider);
};


Expand Down
5 changes: 2 additions & 3 deletions src/management/JobsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ var extend = require('util')._extend;
var Promise = require('bluebird');
var fs = require('fs');

var RestClient = require('rest-facade').Client;
var ArgumentError = require('rest-facade').ArgumentError;

var Auth0RestClient = require('../Auth0RestClient');

/**
* Simple facade for consuming a REST API endpoint.
Expand Down Expand Up @@ -51,7 +50,7 @@ var JobsManager = function (options){
*
* @type {external:RestClient}
*/
this.jobs = new RestClient(options.baseUrl + '/jobs/:id', clientOptions);
this.jobs = new Auth0RestClient(options.baseUrl + '/jobs/:id', clientOptions, options.tokenProvider);
};


Expand Down
5 changes: 2 additions & 3 deletions src/management/LogsManager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var RestClient = require('rest-facade').Client;
var ArgumentError = require('rest-facade').ArgumentError;
var utils = require('../utils');

var Auth0RestClient = require('../Auth0RestClient');

/**
* @class LogsManager
Expand Down Expand Up @@ -43,7 +42,7 @@ var LogsManager = function (options) {
*
* @type {external:RestClient}
*/
this.resource = new RestClient(options.baseUrl + '/logs/:id ', apiOptions);
this.resource = new Auth0RestClient(options.baseUrl + '/logs/:id ', apiOptions, options.tokenProvider);
};

/**
Expand Down
Loading

0 comments on commit c8ce74f

Please sign in to comment.