Skip to content

Commit

Permalink
Remove api-version parameter
Browse files Browse the repository at this point in the history
Workaround for AzureAD#128
  • Loading branch information
hashtagchris committed May 18, 2019
1 parent 89ccef0 commit b68bcef
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 181 deletions.
159 changes: 3 additions & 156 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,159 +1,6 @@
# Windows Azure Active Directory Authentication Library (ADAL) for Node.js
The ADAL for node.js library makes it easy for node.js applications to authenticate to AAD in order to access AAD protected web resources. It supports 3 authentication modes shown in the quickstart code below.
# adal-node with api-version removed

## Versions
Current version - 0.1.28
Minimum recommended version - 0.1.22
You can find the changes for each version in the [change log](https://github.com/AzureAD/azure-activedirectory-library-for-nodejs/blob/master/changelog.txt).

## Samples and Documentation

[We provide a full suite of sample applications and documentation on GitHub](https://github.com/azure-samples?q=active-directory) to help you get started with learning the Azure Identity system. This includes tutorials for native clients such as Windows, Windows Phone, iOS, OSX, Android, and Linux. We also provide full walkthroughs for authentication flows such as OAuth2, OpenID Connect, Graph API, and other awesome features.

## Community Help and Support

We leverage [Stack Overflow](http://stackoverflow.com/) to work with the community on supporting Azure Active Directory and its SDKs, including this one! We highly recommend you ask your questions on Stack Overflow (we're all on there!) Also browse existing issues to see if someone has had your question before.

We recommend you use the "adal" tag so we can see it! Here is the latest Q&A on Stack Overflow for ADAL: [http://stackoverflow.com/questions/tagged/adal](http://stackoverflow.com/questions/tagged/adal)

## Security Reporting

If you find a security issue with our libraries or services please report it to [[email protected]](mailto:[email protected]) with as much detail as possible. Your submission may be eligible for a bounty through the [Microsoft Bounty](http://aka.ms/bugbounty) program. Please do not post security issues to GitHub Issues or any other public site. We will contact you shortly upon receiving the information. We encourage you to get notifications of when security incidents occur by visiting [this page](https://technet.microsoft.com/en-us/security/dd252948) and subscribing to Security Advisory Alerts.

## Contributing

All code is licensed under the Apache 2.0 license and we triage actively on GitHub. We enthusiastically welcome contributions and feedback. You can clone the repo and start contributing now.

## Quick Start
### Installation

``` $ npm install adal-node ```

### Configure the logging

#### Personal Identifiable Information (PII) & Organizational Identifiable Information (OII)

By default, ADAL logging does not capture or log any PII or OII. The library allows app developers to turn this on by configuring the `loggingWithPII` flag in the logging options. By turning on PII or OII, the app takes responsibility for safely handling highly-sensitive data and complying with any regulatory requirements.

```javascript
var logging = require('adal-node').Logging;

//PII or OII logging disabled. Default Logger does not capture any PII or OII.
logging.setLoggingOptions({
log: function(level, message, error) {
// provide your own implementation of the log function
},
level: logging.LOGGING_LEVEL.VERBOSE, // provide the logging level
loggingWithPII: false // Determine if you want to log personal identification information. The default value is false.
});

//PII or OII logging enabled.
logging.setLoggingOptions({
log: function(level, message, error) {
// provide your own implementation of the log function
},
level: logging.LOGGING_LEVEL.VERBOSE,
loggingWithPII: true
});
```

### Authorization Code

See the [website sample](https://github.com/MSOpenTech/azure-activedirectory-library-for-nodejs/blob/master/sample/website-sample.js) for a complete bare bones express based web site that makes use of the code below.

```javascript
var AuthenticationContext = require('adal-node').AuthenticationContext;

var clientId = 'yourClientIdHere';
var clientSecret = 'yourAADIssuedClientSecretHere'
var authorityHostUrl = 'https://login.windows.net';
var tenant = 'myTenant';
var authorityUrl = authorityHostUrl + '/' + tenant;
var redirectUri = 'http://localhost:3000/getAToken';
var resource = '00000002-0000-0000-c000-000000000000';
var templateAuthzUrl = 'https://login.windows.net/' +
tenant +
'/oauth2/authorize?response_type=code&client_id=' +
clientId +
'&redirect_uri=' +
redirectUri +
'&state=<state>&resource=' +
resource;

function createAuthorizationUrl(state) {
return templateAuthzUrl.replace('<state>', state);
}

// Clients get redirected here in order to create an OAuth authorize url and redirect them to AAD.
// There they will authenticate and give their consent to allow this app access to
// some resource they own.
app.get('/auth', function(req, res) {
crypto.randomBytes(48, function(ex, buf) {
var token = buf.toString('base64').replace(/\//g,'_').replace(/\+/g,'-');

res.cookie('authstate', token);
var authorizationUrl = createAuthorizationUrl(token);

res.redirect(authorizationUrl);
});
});

// After consent is granted AAD redirects here. The ADAL library is invoked via the
// AuthenticationContext and retrieves an access token that can be used to access the
// user owned resource.
app.get('/getAToken', function(req, res) {
if (req.cookies.authstate !== req.query.state) {
res.send('error: state does not match');
}

var authenticationContext = new AuthenticationContext(authorityUrl);

authenticationContext.acquireTokenWithAuthorizationCode(
req.query.code,
redirectUri,
resource,
clientId,
clientSecret,
function(err, response) {
var errorMessage = '';
if (err) {
errorMessage = 'error: ' + err.message + '\n';
}
errorMessage += 'response: ' + JSON.stringify(response);
res.send(errorMessage);
}
);
});
```

### Server to Server via Client Credentials

See the [client credentials sample](https://github.com/MSOpenTech/azure-activedirectory-library-for-nodejs/blob/master/sample/client-credentials-sample.js).

```javascript
var AuthenticationContext = require('adal-node').AuthenticationContext;

var authorityHostUrl = 'https://login.windows.net';
var tenant = 'myTenant.onmicrosoft.com'; // AAD Tenant name.
var authorityUrl = authorityHostUrl + '/' + tenant;
var applicationId = 'yourApplicationIdHere'; // Application Id of app registered under AAD.
var clientSecret = 'yourAADIssuedClientSecretHere'; // Secret generated for app. Read this environment variable.
var resource = '00000002-0000-0000-c000-000000000000'; // URI that identifies the resource for which the token is valid.

var context = new AuthenticationContext(authorityUrl);

context.acquireTokenWithClientCredentials(resource, applicationId, clientSecret, function(err, tokenResponse) {
if (err) {
console.log('well that didn\'t work: ' + err.stack);
} else {
console.log(tokenResponse);
}
});
```
Notice: This is a fork of the official adal-node module with the api-version parameter removed, to workaround https://github.com/AzureAD/azure-activedirectory-library-for-nodejs/issues/128. See https://github.com/AzureAD/azure-activedirectory-library-for-nodejs for the official Readme.

## License
Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");

## We Value and Adhere to the Microsoft Open Source Code of Conduct

This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.
Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
40 changes: 22 additions & 18 deletions lib/oauth2client.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
* Notice: Chris Sidi (hashtagchris) changed this file to remove the api-version parameter,
* as a workaround for
* https://github.com/AzureAD/azure-activedirectory-library-for-nodejs/issues/128
*/

/*
* @copyright
* Copyright © Microsoft Open Technologies, Inc.
Expand Down Expand Up @@ -87,22 +93,20 @@ OAuth2Client.prototype._createTokenUrl = function () {
var tokenUrl = url.parse(this._tokenEndpoint);

var parameters = {};
parameters[OAuth2Parameters.AAD_API_VERSION] = '1.0';

tokenUrl.search = querystring.stringify(parameters);
return tokenUrl;
};

/**
* Constructs the user code info request url.
* @private
* Constructs the user code info request url.
* @private
* @return {URL}
*/
OAuth2Client.prototype._createDeviceCodeUrl = function () {
var deviceCodeUrl = url.parse(this._deviceCodeEndpoint);

var parameters = {};
parameters[OAuth2Parameters.AAD_API_VERSION] = '1.0';

deviceCodeUrl.search = querystring.stringify(parameters);

Expand Down Expand Up @@ -280,7 +284,7 @@ OAuth2Client.prototype._validateTokenResponse = function(body) {
if (!wireResponse[OAuth2ResponseParameters.ACCESS_TOKEN]) {
throw this._log.createError('wireResponse missing access_token');
}

mapFields(wireResponse, tokenResponse, TOKEN_RESPONSE_MAP);

if (wireResponse[OAuth2ResponseParameters.ID_TOKEN]) {
Expand Down Expand Up @@ -310,12 +314,12 @@ OAuth2Client.prototype._validateDeviceCodeResponse = function(body) {
}

var intKeys = [
DeviceCodeResponseParameters.EXPIRES_IN,
DeviceCodeResponseParameters.EXPIRES_IN,
DeviceCodeResponseParameters.INTERVAL
];

this._parseOptionalInts(wireResponse, intKeys);

if (!wireResponse[DeviceCodeResponseParameters.EXPIRES_IN]){
throw this._log.createError('wireResponse is missing expires_in');
}
Expand Down Expand Up @@ -406,16 +410,16 @@ OAuth2Client.prototype._getTokenWithPolling = function (postOptions, callback) {
callback(null, new Error('Polling_Request_Cancelled'));
return;
}

request.post(postOptions, util.createRequestHandler('Get Token', this._log, function(response, body) {
//error response callback, for error response, it's already parsed as Json.
//error response callback, for error response, it's already parsed as Json.
if (body && body.hasOwnProperty(TokenResponseFields.ERROR) && body[TokenResponseFields.ERROR] === 'authorization_pending') {
callback(new Error(body[TokenResponseFields.ERROR]), body);
}
else {
callback(null, body);
}
},
},
// success response callback
function (response, body) {
var tokenResponse;
Expand All @@ -426,7 +430,7 @@ OAuth2Client.prototype._getTokenWithPolling = function (postOptions, callback) {
callback(null, e);
return;
}

callback(null, tokenResponse);
})
);
Expand All @@ -445,7 +449,7 @@ OAuth2Client.prototype._createPostOption = function (postUrl, urlEncodedRequestF
encoding : 'utf8'
}
);

return postOptions;
};

Expand Down Expand Up @@ -479,14 +483,14 @@ OAuth2Client.prototype.getToken = function(oauthParameters, callback) {
/**
* @param {object} oauthParameters An object whose keys come from
* Constants.OAuth2.Parameters
* @param {integer} refresh_interval The interval for polling request.
* @param {integer} exipres_in The timeout for polling request.
* @param {integer} refresh_interval The interval for polling request.
* @param {integer} exipres_in The timeout for polling request.
* @param {OAuth2Client.GetTokenCallback} callback The callback function.
*/
OAuth2Client.prototype.getTokenWithPolling = function(oauthParameters, refresh_interval, expires_in, callback){
var self = this;
var self = this;
var maxTimesForRetry = Math.floor(expires_in / refresh_interval);

var tokenUrl = self._createTokenUrl();
var urlEncodedTokenRequestForm = querystring.stringify(oauthParameters);
var postOptions = self._createPostOption(tokenUrl, urlEncodedTokenRequestForm);
Expand Down Expand Up @@ -514,7 +518,7 @@ OAuth2Client.prototype.getUserCodeInfo = function(oauthParameters, callback) {
var deviceCodeUrl = self._createDeviceCodeUrl();

var urlEncodedDeviceCodeRequestForm = querystring.stringify(oauthParameters);

var postOptions = self._createPostOption(deviceCodeUrl, urlEncodedDeviceCodeRequestForm);

request.post(postOptions, util.createRequestHandler('Get Device Code ', this._log, callback,
Expand All @@ -525,7 +529,7 @@ OAuth2Client.prototype.getUserCodeInfo = function(oauthParameters, callback) {
};

/**
* Cancel the polling request made for acquiring token by device code.
* Cancel the polling request made for acquiring token by device code.
*/
OAuth2Client.prototype.cancelPollingRequest = function() {
this._cancelPollingRequest = true;
Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
{
"name": "adal-node",
"name": "@hashtagchris/adal-node-no-api-version",
"author": {
"name": "Microsoft Open Technologies Inc",
"email": "msopentech@microsoft.com",
"url": "http://msopentech.com/"
"name": "Chris Sidi",
"email": "cubandressing@gmail.com",
"url": "http://github.com/hashtagchris"
},
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "https://github.com/AzureAD/azure-activedirectory-library-for-nodejs.git"
"url": "https://github.com/hashtagchris/azure-activedirectory-library-for-nodejs.git"
},
"version": "0.1.28",
"description": "Windows Azure Active Directory Client Library for node",
"description": "Fork of `adal-node` with a workaround for the `spn:` prefix bug (#128)",
"keywords": [
"node",
"azure",
"AAD",
"adal",
"adfs",
"oauth"
"oauth",
"spn"
],
"main": "./lib/adal.js",
"types": "./lib/adal.d.ts",
Expand Down

0 comments on commit b68bcef

Please sign in to comment.