Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active Directory Integration issue Nodejs

I am new with Node js and I want to make Authentication with Azure Active Directory. I Downloaded Node and Installed NPM as mentioned Here

npm install express
npm install ejs
npm install ejs-locals
npm install restify
npm install mongoose
npm install bunyan
npm install assert-plus
npm install passport
npm install passport-azure-ad

after that as per document, I followed Step 3: Set up your app to use the passport-node-js strategy

Here is config.js

exports.creds = {
    returnURL: 'https://hpe.onmicrosoft.com/8d332647-xxxx-4xxc-8xx-11776XXXXX',
    identityMetadata: 'https://login.microsoftonline.com', // For using Microsoft you should never need to change this.
    clientID: '22XXXX9-b5fa-XXXXb-bc7a-XXXXXXXa92a',
    clientSecret: 'Srekv8dM1NqP4Sqnxxxxxxxxxxxx', // if you are doing code or id_token code
    skipUserProfile: true, // for AzureAD should be set to true.
    responseType: 'id_token code', // for login only flows use id_token. For accessing resources use `id_token code`
    responseMode: 'query', // For login only flows we should have token passed back to us in a POST
    //scope: ['email', 'profile'] // additional scopes you may wish to pass
 };

and rest of code i have written as mentioned in document.

But when I run app.js am getting issues.

C:\Nikunj Data\Project\Active Directory\WebApp-OpenIDConnect-NodeJS-skeleton\node_modules\passport-azure-ad\lib\validator.js:51
      throw new TypeError(`Invalid value for ${item}.${checker.error}`);
      ^

TypeError: Invalid value for redirectUrl.The URL must be valid and be https://
    at Object.keys.forEach (C:\Nikunj Data\Project\Active Directory\WebApp-OpenIDConnect-NodeJS-skeleton\node_modules\passport-azure-ad\lib\validator.js:51:13)
    at Array.forEach (native)
    at Validator.validate (C:\Nikunj Data\Project\Active Directory\WebApp-OpenIDConnect-NodeJS-skeleton\node_modules\passport-azure-ad\lib\validator.js:37:28)
    at new Strategy (C:\Nikunj Data\Project\Active Directory\WebApp-OpenIDConnect-NodeJS-skeleton\node_modules\passport-azure-ad\lib\oidcstrategy.js:495:13)
    at Object.<anonymous> (C:\Nikunj Data\Project\Active Directory\WebApp-OpenIDConnect-NodeJS-skeleton\app.js:42:14)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)

Please help me..

Regards Nikunj

Try 1 : I tried Nan yu 's suggested code and I got error :

(node:2628) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/connections.html#use-mongo-client

events.js:182
      throw er; // Unhandled 'error' event
      ^
MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
    at Pool.<anonymous> (C:\Nikunj Data\Project\Active Directory\WebApp-OpenIDConnect-NodeJS-master\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:329:35)
    at emitOne (events.js:115:13)
    at Pool.emit (events.js:210:7)
    at Connection.<anonymous> (C:\Nikunj Data\Project\Active Directory\WebApp-OpenIDConnect-NodeJS-master\node_modules\mongodb\node_modules\mongodb-core\lib\connection\pool.js:280:12)
    at Object.onceWrapper (events.js:318:30)
    at emitTwo (events.js:125:13)
    at Connection.emit (events.js:213:7)
    at Socket.<anonymous> (C:\Nikunj Data\Project\Active Directory\WebApp-OpenIDConnect-NodeJS-master\node_modules\mongodb\node_modules\mongodb-core\lib\connection\connection.js:187:49)
    at Object.onceWrapper (events.js:316:30)
    at emitOne (events.js:115:13)
    at Socket.emit (events.js:210:7)
    at emitErrorNT (internal/streams/destroy.js:62:8)
    at _combinedTickCallback (internal/process/next_tick.js:102:11)
    at process._tickCallback (internal/process/next_tick.js:161:9)
like image 895
Nikunj Patel Avatar asked Aug 03 '17 07:08

Nikunj Patel


1 Answers

Please refer to code sample : Azure Active Directory OIDC Web Sample

The redirect url should match the reply URL registered in AAD for your app . In that document , it is http://localhost:3000/auth/openid/return

Update

To make code sample https://github.com/AzureADQuickStarts/WebApp-OpenIDConnect-NodeJS work ,please refer to below steps:

  1. Sign in to the Azure portal.

  2. On the top bar, click on your account and under the Directory list, choose the Active Directory tenant where you wish to register your application.

  3. Click on More Services in the left hand nav, and choose Azure Active Directory.

  4. Click on App registrations and choose Add.

  5. Enter a friendly name for the application, for example 'WebApp-OpenIDConnect-nodejs' and select 'Web Application and/or Web API' as the Application Type. For the sign-on URL, enter the base URL for the sample, which is by default http://localhost:3000/. Click on Create to create the application.

  6. While still in the Azure portal, choose your application, click on Settings and choose Reply URLs. Add reply url :http://localhost:3000/auth/openid/return ,click Save button .

  7. Find the Application ID value and copy it to the clipboard. Open config.js file in code sample , replace clientID with the Application ID value.

  8. Setting identityMetadata with domain name or guid of tenant .

  9. In the Azure portal, choose your application, click on Settings and choose Keys. Add a app key and copy to clientSecret in config.js .

  10. Setting useMongoDBSessionStore to true if you want to use the mongoDB session store for session middleware , update correct database url value to exports.databaseUri . Otherwise set useMongoDBSessionStore to false to use default session store .

  11. Run the app. Use the following command in terminal: node app.js .

  12. Open browser ,type http://localhost:3000 and try your app .

like image 73
Nan Yu Avatar answered Oct 20 '22 14:10

Nan Yu