Skip to content

Commit

Permalink
Added tokenProvider functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
dctoon committed Sep 25, 2017
1 parent 6a14794 commit ad047dc
Show file tree
Hide file tree
Showing 24 changed files with 734 additions and 62 deletions.
21 changes: 19 additions & 2 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,7 @@ 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 a Management API token from your node backend, you can use Client Credentials Grant using your registered Auth0 Non Interactive Clients

Expand Down Expand Up @@ -71,6 +70,24 @@ auth0.clientCredentialsGrant({
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).

Or initialize your client class with the ManagementTokenProvider.
~~~js
var ManagementClient = require('auth0').ManagementClient;
var ManagementTokenProvider = require('auth0').ManagementTokenProvider;
var auth0 = new ManagementClient({
domain: '{YOUR_ACCOUNT}.auth0.com',
tokenProvider: new ManagementTokenProvider({
clientId: '{YOUR_NON_INTERACTIVE_CLIENT_ID}',
clientSecret: '{YOUR_NON_INTERACTIVE_CLIENT_SECRET}',
domain: '{YOUR_ACCOUNT}.auth0.com',
scope: '{MANAGEMENT_API_SCOPES}',
useCache: true //default
})
});
~~~

> Note: When using at browser you should use `telemetry: false`.
## Promises and callbacks

Be aware that all methods can be used with promises or callbacks. However, when a callback is provided no promise will be returned.
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
"homepage": "https://github.com/auth0/node-auth0",
"dependencies": {
"bluebird": "^2.10.2",
"lru-memoizer": "^1.11.1",
"object.assign": "^4.0.4",
"request": "^2.81.0",
"rest-facade": "^1.5.0"
},
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;
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

module.exports = {
ManagementClient: require('./management'),
AuthenticationClient: require('./auth')
AuthenticationClient: require('./auth'),
ManagementTokenProvider: require('./management/ManagementTokenProvider')
};
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 ad047dc

Please sign in to comment.