forked from Azure/autorest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
interactive login in the node runtime (ms-rest-azure) (Azure#1066)
* inital commit * minor update * some more updates
- Loading branch information
1 parent
d25043a
commit 3cb1bec
Showing
14 changed files
with
370 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
ClientRuntimes/NodeJS/ms-rest-azure/lib/credentials/deviceTokenCredentials.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
var util = require('util'); | ||
var msrest = require('ms-rest'); | ||
var adal = require('adal-node'); | ||
var Constants = msrest.Constants; | ||
|
||
var azureConstants = require('../constants'); | ||
var AzureEnvironment = require('../azureEnvironment'); | ||
|
||
/** | ||
* Creates a new DeviceTokenCredentials object that gets a new access token using userCodeInfo (contains user_code, device_code) | ||
* for authenticating user on device. | ||
* | ||
* When this credential is used, the script will provide a url and code. The user needs to copy the url and the code, paste it | ||
* in a browser and authenticate over there. If successful, the script will get the access token. | ||
* | ||
* @constructor | ||
* @param {object} [options] Object representing optional parameters. | ||
* @param {string} [options.username] The user name for account in the form: 'user@example.com'. | ||
* @param {AzureEnvironment} [options.environment] The azure environment to authenticate with. Default environment is "Azure" popularly known as "Public Azure Cloud". | ||
* @param {string} [options.domain] The domain or tenant id containing this application. Default value is 'common' | ||
* @param {string} [options.clientId] The active directory application client id. | ||
* See {@link https://azure.microsoft.com/en-us/documentation/articles/active-directory-devquickstarts-dotnet/ Active Directory Quickstart for .Net} | ||
* for an example. | ||
* @param {string} [options.authorizationScheme] The authorization scheme. Default value is 'bearer'. | ||
* @param {object} [options.tokenCache] The token cache. Default value is the MemoryCache object from adal. | ||
*/ | ||
function DeviceTokenCredentials(options) { | ||
if (!options) { | ||
options = {}; | ||
} | ||
|
||
if (!options.username) { | ||
options.username = 'user@example.com'; | ||
} | ||
|
||
if (!options.environment) { | ||
options.environment = AzureEnvironment.Azure; | ||
} | ||
|
||
if (!options.domain) { | ||
options.domain = azureConstants.AAD_COMMON_TENANT; | ||
} | ||
|
||
if (!options.clientId) { | ||
options.clientId = azureConstants.DEFAULT_ADAL_CLIENT_ID; | ||
} | ||
|
||
if (!options.authorizationScheme) { | ||
options.authorizationScheme = Constants.HeaderConstants.AUTHORIZATION_SCHEME; | ||
} | ||
|
||
if (!options.tokenCache) { | ||
options.tokenCache = new adal.MemoryCache(); | ||
} | ||
|
||
this.username = options.username; | ||
this.environment = options.environment; | ||
this.domain = options.domain; | ||
this.clientId = options.clientId; | ||
this.authorizationScheme = options.authorizationScheme; | ||
this.tokenCache = options.tokenCache; | ||
var authorityUrl = this.environment.activeDirectoryEndpointUrl + this.domain; | ||
this.context = new adal.AuthenticationContext(authorityUrl, this.environment.validateAuthority, this.tokenCache); | ||
} | ||
|
||
DeviceTokenCredentials.prototype.retrieveTokenFromCache = function (callback) { | ||
var self = this; | ||
self.context.acquireToken(self.environment.activeDirectoryResourceId, self.username, self.clientId, function (err, result) { | ||
if (err) return callback(err); | ||
return callback(null, result.tokenType, result.accessToken); | ||
}); | ||
}; | ||
|
||
|
||
/** | ||
* Signs a request with the Authentication header. | ||
* | ||
* @param {webResource} The WebResource to be signed. | ||
* @param {function(error)} callback The callback function. | ||
* @return {undefined} | ||
*/ | ||
DeviceTokenCredentials.prototype.signRequest = function (webResource, callback) { | ||
return this.retrieveTokenFromCache(function(err, scheme, token) { | ||
if (err) return callback(err); | ||
webResource.headers[Constants.HeaderConstants.AUTHORIZATION] = util.format('%s %s', scheme, token); | ||
return callback(null); | ||
}); | ||
}; | ||
|
||
module.exports = DeviceTokenCredentials; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.