Skip to content

Commit

Permalink
Make audience of management api optional.
Browse files Browse the repository at this point in the history
When its not provided is inferred from domain's
  • Loading branch information
hzalaz committed Sep 30, 2017
1 parent 5b19669 commit 15935c1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 49 deletions.
23 changes: 11 additions & 12 deletions src/management/ManagementTokenProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ var DEFAULT_OPTIONS = { enableCache: true };
* @param {String} options.domain ManagementClient server domain.
* @param {String} options.clientId Non Interactive Client Id.
* @param {String} options.clientSecret Non Interactive Client Secret.
* @param {String} options.audience Audience of the Management API.
* @param {String} options.scope Non Interactive Client Scope.
* @param {String} options.audience Audience of the Management API.
* @param {Boolean} [options.enableCache=true] Enabled or Disable Cache
* @param {Number} [options.cacheTTLInSeconds] By default the `expires_in` value will be used to determine the cached time of the token, this can be overridden.
*/
Expand All @@ -27,7 +27,7 @@ var ManagementTokenProvider = function (options) {
}

var params = assign({}, DEFAULT_OPTIONS, options);

if (!params.domain || params.domain.length === 0) {
throw new ArgumentError('Must provide a domain');
}
Expand All @@ -44,32 +44,31 @@ var ManagementTokenProvider = function (options) {
throw new ArgumentError('Must provide a audience');
}

if(typeof params.enableCache !== 'boolean'){
throw new ArgumentError('enableCache must be a boolean');
if (typeof params.enableCache !== 'boolean'){
throw new ArgumentError('enableCache must be a boolean');
}
if(params.enableCache && params.cacheTTLInSeconds){
if(typeof params.cacheTTLInSeconds !== 'number'){

if (params.enableCache && params.cacheTTLInSeconds) {
if (typeof params.cacheTTLInSeconds !== 'number') {
throw new ArgumentError('cacheTTLInSeconds must be a number');
}
if(params.cacheTTLInSeconds <= 0) {

if (params.cacheTTLInSeconds <= 0) {
throw new ArgumentError('cacheTTLInSeconds must be a greater than 0');
}
}

if(params.scope && typeof params.scope !== 'string'){
if (params.scope && typeof params.scope !== 'string'){
throw new ArgumentError('scope must be a string');
}

this.options = params;
var authenticationClientOptions = {
domain: this.options.domain,
clientId: this.options.clientId,
clientSecret: this.options.clientSecret,
telemetry: this.options.telemetry
};
//console.log('authenticationClientOptions', authenticationClientOptions);
this.authenticationClient = new AuthenticationClient(authenticationClientOptions);
}

Expand Down
45 changes: 23 additions & 22 deletions src/management/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var ResourceServersManager = require('./ResourceServersManager');
var ManagementTokenProvider = require('./ManagementTokenProvider');

var BASE_URL_FORMAT = 'https://%s/api/v2';

var MANAGEMENT_API_AUD_FORMAT = 'https://%s/api/v2/';

/**
* @class ManagementClient
Expand All @@ -49,8 +49,8 @@ var BASE_URL_FORMAT = 'https://%s/api/v2';
* domain: '{YOUR_ACCOUNT}.auth0.com',
* token: '{YOUR_API_V2_TOKEN}'
* });
*
*
*
*
* @example <caption>
* Initialize your client class, by using a Non Interactive Client to fetch an access_token
* via the Client Credentials Grant.
Expand All @@ -69,18 +69,18 @@ var BASE_URL_FORMAT = 'https://%s/api/v2';
* }
* });
*
* @param {Object} options Options for the ManagementClient SDK.
* @param {Object} options Options for the ManagementClient SDK.
* If a token is provided only the domain is required, other parameters are ignored.
* If no token is provided domain, clientId, clientSecret and audience are required
* @param {String} options.domain ManagementClient server domain.
* @param {String} [options.token] API access token.
* @param {String} [options.clientId] Management API Non Interactive Client Id.
* @param {String} [options.clientSecret] Management API Non Interactive Client Secret.
* @param {String} [options.audience] Management API Audience.
* @param {String} [options.scope] Management API Scopes.
* @param {String} [options.tokenProvider.enableCache=true] Enabled or Disable Cache.
* @param {Number} [options.cacheTTLInSeconds] By default the `expires_in` value will be used to determine the cached time of the token, this can be overridden.
*
* If no token is provided domain, clientId, clientSecret and scopes are required
* @param {String} options.domain ManagementClient server domain.
* @param {String} [options.token] API access token.
* @param {String} [options.clientId] Management API Non Interactive Client Id.
* @param {String} [options.clientSecret] Management API Non Interactive Client Secret.
* @param {String} [options.audience] Management API Audience. By default is your domain's, e.g. the domain is `tenant.auth0.com` and the audience is `http://tenant.auth0.com/api/v2/`
* @param {String} [options.scope] Management API Scopes.
* @param {String} [options.tokenProvider.enableCache=true] Enabled or Disable Cache.
* @param {Number} [options.tokenProvider.cacheTTLInSeconds] By default the `expires_in` value will be used to determine the cached time of the token, this can be overridden.
*
*/
var ManagementClient = function (options) {
if (!options || typeof options !== 'object') {
Expand All @@ -91,28 +91,29 @@ var ManagementClient = function (options) {
throw new ArgumentError('Must provide a domain');
}

var baseUrl = util.format(BASE_URL_FORMAT, options.domain);
var managerOptions = {
headers: {
'User-agent': 'node.js/' + process.version.replace('v', ''),
'Content-Type': 'application/json'
},
baseUrl: util.format(BASE_URL_FORMAT, options.domain)
baseUrl: baseUrl
};

if(options.token === undefined){
var config = assign({}, options);
if(options.tokenProvider){
if (options.token === undefined) {
var config = assign({ audience: util.format(MANAGEMENT_API_AUD_FORMAT, options.domain) }, options);

if (options.tokenProvider) {
config.enableCache = options.tokenProvider.enableCache;
config.cacheTTLInSeconds = options.tokenProvider.cacheTTLInSeconds;
delete config.tokenProvider;
delete config.tokenProvider;
}

this.tokenProvider = new ManagementTokenProvider(config);
managerOptions.tokenProvider = this.tokenProvider;
}else if(typeof options.token !== 'string' || options.token.length === 0){
} else if (typeof options.token !== 'string' || options.token.length === 0) {
throw new ArgumentError('Must provide a token');
}else{
} else {
managerOptions.headers['Authorization'] = 'Bearer ' + options.token;
}

Expand Down
27 changes: 12 additions & 15 deletions test/management/management-client.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ var StatsManager = require('../../src/management/StatsManager');
var TenantManager = require('../../src/management/TenantManager');

describe('ManagementClient', function () {
var withTokenProviderConfig = {
clientId: 'clientId',
clientSecret: 'clientSecret',
domain: 'auth0-node-sdk.auth0.com',
audience: 'https://auth0-node-sdk.auth0.com/api/v2/'
var withTokenProviderConfig = {
clientId: 'clientId',
clientSecret: 'clientSecret',
domain: 'auth0-node-sdk.auth0.com'
};

var withTokenConfig = {
var withTokenConfig = {
domain: 'auth0-node-sdk.auth0.com',
token: 'fake-token'
};
Expand All @@ -42,6 +41,13 @@ describe('ManagementClient', function () {
.to.be.an.instanceOf(ManagementClient);
});

it('should expose an instance of ManagementClient when withTokenProviderConfig and audience is passed', function () {
var config = assign({audience: 'https://auth0-node-sdk.auth0.com/api/v2/'}, withTokenConfig);
expect(new ManagementClient(config))
.to.exist
.to.be.an.instanceOf(ManagementClient);
});

it('should raise an error when no options object is provided', function () {
expect(ManagementClient)
.to.throw(ArgumentError, 'Management API SDK options must be an object');
Expand Down Expand Up @@ -92,15 +98,6 @@ describe('ManagementClient', function () {
.to.throw(ArgumentError, 'Must provide a clientSecret');
});

it('should raise an error when the token and audience are not set', function () {
var config = assign({}, withTokenProviderConfig);
delete config.audience;
var client = ManagementClient.bind(null, config);

expect(client)
.to.throw(ArgumentError, 'Must provide a audience');
});

describe('instance properties', function () {
var manager;
var managers = {
Expand Down

0 comments on commit 15935c1

Please sign in to comment.