Skip to content

Commit

Permalink
Merge pull request #1006 from Azure/master
Browse files Browse the repository at this point in the history
[NodeJS] Redesigned the structure of AzureEnvironment and updated env…
  • Loading branch information
NiklasGustafsson committed May 4, 2016
2 parents f8d693a + 0142862 commit 3dbbebb
Show file tree
Hide file tree
Showing 8 changed files with 400 additions and 116 deletions.
176 changes: 143 additions & 33 deletions ClientRuntimes/NodeJS/ms-rest-azure/lib/azureEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,158 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

'use strict';

var _ = require('underscore');

var util = require('util');
/**
* @class
* Initializes a new instance of the AzureEnvironment class.
* @constructor
* @param {string} authenticationEndpoint - ActiveDirectory Endpoint for the Azure Environment.
* @param {string} tokenAudience - Token audience for an endpoint.
* @param {bool} [validateAuthority] - Determines whether the authentication endpoint should
* @param {string} parameters.name - The Environment name
* @param {string} parameters.portalUrl - the management portal URL
* @param {string} parameters.managementEndpointUrl - the management service endpoint
* @param {string} parameters.resourceManagerEndpointUrl - the resource management endpoint
* @param {string} parameters.activeDirectoryEndpointUrl - the Active Directory login endpoint
* @param {string} parameters.activeDirectoryResourceId - The resource ID to obtain AD tokens for
* @param {string} [parameters.publishingProfileUrl] - the publish settings file URL
* @param {string} [parameters.sqlManagementEndpointUrl] - the sql server management endpoint for mobile commands
* @param {string} [parameters.sqlServerHostnameSuffix] - the dns suffix for sql servers
* @param {string} [parameters.galleryEndpointUrl] - the template gallery endpoint
* @param {string} [parameters.activeDirectoryGraphResourceId] - the Active Directory resource ID
* @param {string} [parameters.activeDirectoryGraphApiVersion] - the Active Directory api version
* @param {string} [parameters.storageEndpointSuffix] - the endpoint suffix for storage accounts
* @param {string} [parameters.keyVaultDnsSuffix] - the keyvault service dns suffix
* @param {string} [parameters.azureDataLakeStoreFileSystemEndpointSuffix] - the data lake store filesystem service dns suffix
* @param {string} [parameters.azureDataLakeAnalyticsCatalogAndJobEndpointSuffix] - the data lake analytics job and catalog service dns suffix
* @param {bool} [parameters.validateAuthority] - Determines whether the authentication endpoint should
* be validated with Azure AD. Default value is true.
*/
function AzureEnvironment(authenticationEndpoint, tokenAudience, validateAuthority) {
this.authenticationEndpoint = authenticationEndpoint;
this.tokenAudience = tokenAudience;
this.validateAuthority = validateAuthority;
}
function AzureEnvironment(parameters) {
//Set defaults.
this.validateAuthority = true;

/**
* Provides the settings for authentication with Azure
*/
var Azure = new AzureEnvironment('https://login.microsoftonline.com/',
'https://management.core.windows.net/',
true);
if (parameters) {
//Validate required parameters
var requiredParams = [ 'name', 'portalUrl', 'managementEndpointUrl', 'resourceManagerEndpointUrl',
'activeDirectoryEndpointUrl', 'activeDirectoryResourceId'];
requiredParams.forEach(function (param) {
if (!parameters[param] || typeof parameters[param].valueOf() !== 'string') {
throw new Error(util.format('Please provide "%s" for the environment and it must be of type "string".', param));
}
});
//Assign provided parameters
for (var prop in parameters) {
this[prop] = parameters[prop];
}
}
}
var supportedEnvironments = {
Azure: {
name: 'Azure',
portalUrl: 'http://go.microsoft.com/fwlink/?LinkId=254433',
publishingProfileUrl: 'http://go.microsoft.com/fwlink/?LinkId=254432',
managementEndpointUrl: 'https://management.core.windows.net',
resourceManagerEndpointUrl: 'https://management.azure.com/',
sqlManagementEndpointUrl: 'https://management.core.windows.net:8443/',
sqlServerHostnameSuffix: '.database.windows.net',
galleryEndpointUrl: 'https://gallery.azure.com/',
activeDirectoryEndpointUrl: 'https://login.microsoftonline.com',
activeDirectoryResourceId: 'https://management.core.windows.net/',
activeDirectoryGraphResourceId: 'https://graph.windows.net/',
activeDirectoryGraphApiVersion: '2013-04-05',
storageEndpointSuffix: '.core.windows.net',
keyVaultDnsSuffix: '.vault.azure.net',
azureDataLakeStoreFileSystemEndpointSuffix: 'azuredatalakestore.net',
azureDataLakeAnalyticsCatalogAndJobEndpointSuffix: 'azuredatalakeanalytics.net'
},
AzureChina: {
name: 'AzureChina',
portalUrl: 'http://go.microsoft.com/fwlink/?LinkId=301902',
publishingProfileUrl: 'http://go.microsoft.com/fwlink/?LinkID=301774',
managementEndpointUrl: 'https://management.core.chinacloudapi.cn',
resourceManagerEndpointUrl: 'https://management.chinacloudapi.cn',
sqlManagementEndpointUrl: 'https://management.core.chinacloudapi.cn:8443/',
sqlServerHostnameSuffix: '.database.chinacloudapi.cn',
galleryEndpointUrl: 'https://gallery.chinacloudapi.cn/',
activeDirectoryEndpointUrl: 'https://login.chinacloudapi.cn',
activeDirectoryResourceId: 'https://management.core.chinacloudapi.cn/',
activeDirectoryGraphResourceId: 'https://graph.chinacloudapi.cn/',
activeDirectoryGraphApiVersion: '2013-04-05',
storageEndpointSuffix: '.core.chinacloudapi.cn',
keyVaultDnsSuffix: '.vault.azure.cn',
// TODO: add dns suffixes for the china cloud for datalake store and datalake analytics once they are defined.
azureDataLakeStoreFileSystemEndpointSuffix: 'N/A',
azureDataLakeAnalyticsCatalogAndJobEndpointSuffix: 'N/A'
},
AzureUSGovernment: {
name: 'AzureUSGovernment',
portalUrl: 'https://manage.windowsazure.us',
publishingProfileUrl: 'https://manage.windowsazure.us/publishsettings/index',
managementEndpointUrl: 'https://management.core.usgovcloudapi.net',
resourceManagerEndpointUrl: 'https://management.usgovcloudapi.net',
sqlManagementEndpointUrl: 'https://management.core.usgovcloudapi.net:8443/',
sqlServerHostnameSuffix: '.database.usgovcloudapi.net',
galleryEndpointUrl: 'https://gallery.usgovcloudapi.net/',
activeDirectoryEndpointUrl: 'https://login.microsoftonline.com',
activeDirectoryResourceId: 'https://management.core.usgovcloudapi.net/',
activeDirectoryGraphResourceId: 'https://graph.windows.net/',
activeDirectoryGraphApiVersion: '2013-04-05',
storageEndpointSuffix: '.core.usgovcloudapi.net',
keyVaultDnsSuffix: '.vault.usgovcloudapi.net',
// TODO: add dns suffixes for the US government for datalake store and datalake analytics once they are defined.
azureDataLakeStoreFileSystemEndpointSuffix: 'N/A',
azureDataLakeAnalyticsCatalogAndJobEndpointSuffix: 'N/A'
},
AzureGermanCloud: {
name: 'AzureGermanCloud',
portalUrl: 'http://portal.microsoftazure.de/',
publishingProfileUrl: 'https://manage.microsoftazure.de/publishsettings/index',
managementEndpointUrl: 'https://management.core.cloudapi.de',
resourceManagerEndpointUrl: 'https://management.microsoftazure.de',
sqlManagementEndpointUrl: 'https://management.core.cloudapi.de:8443/',
sqlServerHostnameSuffix: '.database.cloudapi.de',
galleryEndpointUrl: 'https://gallery.cloudapi.de/',
activeDirectoryEndpointUrl: 'https://login.microsoftonline.de',
activeDirectoryResourceId: 'https://management.core.cloudapi.de/',
activeDirectoryGraphResourceId: 'https://graph.cloudapi.de/',
activeDirectoryGraphApiVersion: '2013-04-05',
storageEndpointSuffix: '.core.cloudapi.de',
keyVaultDnsSuffix: '.vault.microsoftazure.de',
// TODO: add dns suffixes for the US government for datalake store and datalake analytics once they are defined.
azureDataLakeStoreFileSystemEndpointSuffix: 'N/A',
azureDataLakeAnalyticsCatalogAndJobEndpointSuffix: 'N/A'
}
};

/**
* Provides the settings for authentication with Azure China
* Adds a new instance of the AzureEnvironment to the prototype.
* @param {string} parameters.name - The Environment name
* @param {string} parameters.portalUrl - the management portal URL
* @param {string} parameters.managementEndpointUrl - the management service endpoint
* @param {string} parameters.resourceManagerEndpointUrl - the resource management endpoint
* @param {string} parameters.activeDirectoryEndpointUrl - the Active Directory login endpoint
* @param {string} parameters.activeDirectoryResourceId - The resource ID to obtain AD tokens for
* @param {string} [parameters.publishingProfileUrl] - the publish settings file URL
* @param {string} [parameters.sqlManagementEndpointUrl] - the sql server management endpoint for mobile commands
* @param {string} [parameters.sqlServerHostnameSuffix] - the dns suffix for sql servers
* @param {string} [parameters.galleryEndpointUrl] - the template gallery endpoint
* @param {string} [parameters.activeDirectoryGraphResourceId] - the Active Directory resource ID
* @param {string} [parameters.activeDirectoryGraphApiVersion] - the Active Directory api version
* @param {string} [parameters.storageEndpointSuffix] - the endpoint suffix for storage accounts
* @param {string} [parameters.keyVaultDnsSuffix] - the keyvault service dns suffix
* @param {string} [parameters.azureDataLakeStoreFileSystemEndpointSuffix] - the data lake store filesystem service dns suffix
* @param {string} [parameters.azureDataLakeAnalyticsCatalogAndJobEndpointSuffix] - the data lake analytics job and catalog service dns suffix
* @param {bool} [parameters.validateAuthority] - Determines whether the authentication endpoint should
* be validated with Azure AD. Default value is true.
* @return {AzureEnvironment} - Reference to the newly added Environment
*/
var AzureChina = new AzureEnvironment('https://login.chinacloudapi.cn/',
'https://management.core.chinacloudapi.cn/',
true);
AzureEnvironment.prototype.add = function(parameters) {
var _environment = new AzureEnvironment(parameters);
AzureEnvironment.prototype[_environment.name] = _environment;
return _environment;
};

/**
* Provides the settings for authentication with Azure US Government
*/
var AzureUSGovernment = new AzureEnvironment('https://login.microsoftonline.com/',
'https://management.core.usgovcloudapi.net/',
true);
//Adding the supported environments
for(var key in supportedEnvironments) {
AzureEnvironment.prototype.add(supportedEnvironments[key]);
}

_.extend(module.exports, {
Azure: Azure,
AzureChina: AzureChina,
AzureEnvironment: AzureEnvironment,
AzureUSGovernment: AzureUSGovernment
});
module.exports = new AzureEnvironment();
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ function ApplicationTokenCredentials(clientId, domain, secret, options) {
*/
ApplicationTokenCredentials.prototype.signRequest = function (webResource, callback) {
var self = this;
var authorityUrl = self.environment.authenticationEndpoint + self.domain;
var authorityUrl = self.environment.activeDirectoryEndpointUrl + self.domain;
var context = new adal.AuthenticationContext(authorityUrl, self.environment.validateAuthority, self.tokenCache);

context.acquireTokenWithClientCredentials(self.environment.tokenAudience, self.clientId, self.secret, function (err, result) {
context.acquireTokenWithClientCredentials(self.environment.activeDirectoryResourceId, self.clientId, self.secret, function (err, result) {
if (err) {
return callback(new Error('Failed to acquire token for application. \n' + err));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ function UserTokenCredentials(clientId, domain, username, password, clientRedire
*/
UserTokenCredentials.prototype.signRequest = function (webResource, callback) {
var self = this;
var authorityUrl = self.environment.authenticationEndpoint + self.domain;
var authorityUrl = self.environment.activeDirectoryEndpointUrl + self.domain;
var context = new adal.AuthenticationContext(authorityUrl, self.environment.validateAuthority, self.tokenCache);

context.acquireTokenWithUsernamePassword(self.environment.tokenAudience, self.username, self.password, self.clientId, function (err, result) {
context.acquireTokenWithUsernamePassword(self.environment.activeDirectoryResourceId, self.username, self.password, self.clientId, function (err, result) {
if (err) {
return callback(new Error('Failed to acquire token. \n' + err));
}
Expand Down
Loading

0 comments on commit 3dbbebb

Please sign in to comment.