Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

adal-node

AzureAD1.1mApache-2.0deprecated0.2.4TypeScript support: included

This package is no longer supported. Please migrate to @azure/msal-node.

Windows Azure Active Directory Client Library for node

node, azure, AAD, adal, adfs, oauth

readme


This library, ADAL for Node.js, will no longer receive new feature improvements. Instead, use the new library MSAL for Node.js.

  • If you are starting a new project, you can get started with the MSAL for Node.js docs for details about the scenarios, usage, and relevant concepts.
  • If your application is using the previous ADAL for Node.js library, you can follow this migration guide for Node.js apps.
  • Existing applications relying on ADAL for Node.js will continue to work.

Feedback

Update to MSAL for Node.js now!

MSAL for Node.js is the new authentication library to be used with the Microsoft identity platform

Building on top of ADAL, MSAL works with the new and Open ID Connect certified Azure AD V2 endpoint and the new social identity solution from Microsoft, Azure AD B2C.

ADAL for Node.js is in maintenance mode and no new features will be added to the ADAL library anymore. All our ongoing efforts will be focused on improving the new MSAL for Node.js. MSAL’s documentation also contains a migration guide which simplifies upgrading from ADAL for Node.js.

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.

Versions

You can find the changes for each version in the change log.

Samples and Documentation

We provide a full suite of sample applications and documentation on GitHub 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 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

Submit Feedback

We'd like your thoughts on this library. Please complete this short survey.

Security Reporting

If you find a security issue with our libraries or services please report it to secure@microsoft.com with as much detail as possible. Your submission may be eligible for a bounty through the Microsoft Bounty 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 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.

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 for a complete bare bones express based web site that makes use of the code below.

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.

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);
  }
});

Note on Proxy support for adal-node

We have moved to axios as the package of choice for HTTP requests for adal-node from request since the npm support for request is discontinued. However we noticed later that axios does not support proxies -filed here. Since we are making only security changes to adal-node, we are refraining from making any changes. However, we do recommend using https-proxy-agent as suggested in the issue linked in case you need proxies for your application.

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. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

changelog

Version 0.2.4

Release Date: 8 December 2022

  • Dependency update for @xmldom/xmldom to 0.8.3

Version 0.2.3

Release Date: 23 August 2021

  • Dependency update from xmldom to @xmldom/xmldom

Version 0.2.2

Release Date: 8 February 2020

  • Replace 'requestwithaxios` final version
  • Fix unit tests

Version 0.2.2-beta

Release Date: 1 February 2020

  • Replace 'requestwithaxios`

Version 0.2.0

Release Date: 23 August 2019

  • Add support for user provided api version

Version 0.1.28

Release Date: 26 Feburary 2018

  • Added GDPR support per Microsoft policy.

Version 0.1.27

Release Date: 08 January 2018

  • Fix Issue #185 Dependency "xpath.js" version does not have an OSI-approved license

Version 0.1.26

Release Date: 05 December 2017

  • Added .npmignore to avoid publishing unnecessary files

Version 0.1.25

Release Date: 06 November 2017

  • Fixed typing: acquireUserCode returns UserCodeInfo in the callback

Version 0.1.24

Release Date: 06 November 2017

  • Added type definitions to the project. This should help users developing their apps in TypeScript #142, DefinitelyTyped.
  • Replaced "node-uuid" with "uuid" #170.

Version 0.1.23

Release Date: 26 Oct 2017

  • Fix Issue #95 - Change npm licenses to license and use spdx syntax
  • Fix Issue #122 - Broken link on README file
  • Fix Issue #155 - remove need for dynamic access to package.json to obtain lib version (util.js)
  • Fix Issue #172 - Add login.microsoftonline.us endpoint
  • Added Windows Graph Sample

Version 0.1.22

Release Date: 1 Aug 2016

  • Fix Issue #132 - Trim developer provided authority to construct endpoints

Version 0.1.21

Release Date: 23 Jun 2016

  • Policheck and credscan fixes

Version 0.1.20

Release Date: 17 Jun 2016

  • Add support for resource owner grant flow for ADFS

Version 0.1.19

Release Date: 26 Apr 2016

  • Fixed CredScan issue for the checked in pem file
  • Policheck fixes
  • Updated node-uuid
  • Fixed Readme.md
  • Inline the adal version during compile time
  • Fix issue #71 - client-credential cert-bad-cert failing
  • Fix issue #78 - Express dependency in package.json doesn't match syntax used in website-sample.js
  • Fix issue #80 - Unreachable ADFS server results in misleading error message

Version 0.1.18

Release Date: 5 Feb 2016

  • Add oid in IDTokenMap to expose for returned user info.

Version 0.1.17

Release Date: 8 Oct 2015

  • Add support for cross tenant refresh token

Version 0.1.16

Release Date: 3 Sep 2015

  • Add support for device profile

Version 0.1.15

Release Date: 4 Aug 2015

  • Fix issue #68 - add support for wstrust 2005 endpoint.
  • Fix issue #62 - escape xml chars in password before creating a xml request for wstrust.

Version 0.1.14

Release Date: 5 May 2015

  • Add timestamp to the log entries.

Version 0.1.13

Release Date: 1 May 2015

  • Scrub security sensitive data in WS-Trust exchange from log messages.
  • Update the version of the jws dependency to the latest release.

Version 0.1.12

Release Date: 19 February 2015

  • Add login.microsoftonline.com to the static list of known authorities.
  • Bug fixes. (#36)

Version 0.1.11

Release Date: 16 December 2014

  • Added support for certificate authentication for confidential clients.

Version 0.1.10

Release Date: 24 November 2014

  • 0.1.9 published version was corrupt.

Version 0.1.9

Release Date: 24 November 2014

  • Fixed issue #22 - ADAL needs a method that allows confidential clients to acquire a new token via a refresh token.The acquireTokenWithRefreshToken function was updated to take an additional clientSecret argument. Passing this new argument is optional and the change should not breakapps using this function before the additional argument was added.

Version 0.1.8

Release Date: 29 October 2014

  • Update version of xpath.js dependency to 1.0.5
  • Fix a bug in the regular expression used to parse a 401 challenge.

Version 0.1.7

Release Date: 2 September 2014

  • Add caching support to AuthenticationContext.acquireTokenWithClientCredentials