Skip to content

Commit 217e793

Browse files
authored
Merge pull request AzureADQuickStarts#41 from AzureADQuickStarts/derisen
patch sample
2 parents fc8d925 + 276fcbc commit 217e793

File tree

9 files changed

+1598
-62
lines changed

9 files changed

+1598
-62
lines changed

README.md

+22-19
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
---
22
page_type: sample
33
languages:
4-
- nodejs
4+
- javascript
55
products:
6-
- azure
6+
- nodejs
7+
- passport-azure-ad
78
- azure-active-directory
89
description: "This sample demonstrates how to set up OpenId Connect authentication in a web application built using Node.js with Express."
910
---
1011

11-
# Azure Active Directory OIDC Node.js Web Sample
12+
# Azure Active Directory OIDC Node.js web app sample
1213

13-
| [Library](https://github.com/AzureAD/passport-azure-ad) | [Docs](https://aka.ms/aadv2) | [Support](README.md#community-help-and-support) | [Protocol](https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-v2-protocols-oidc)
14+
| [Library](https://github.com/AzureAD/passport-azure-ad) | [Docs](https://aka.ms/aadv2) | [Support](README.md#community-help-and-support) | [Protocol](https://docs.microsoft.com/azure/active-directory/develop/active-directory-v2-protocols-oidc)
1415
| --- | --- | --- | --- |
1516

1617
This sample demonstrates how to set up OpenId Connect authentication in a web application built using Node.js with Express. The sample is designed to run on any platform.
@@ -21,7 +22,7 @@ To run this sample you will need the following:
2122

2223
* Install Node.js from http://nodejs.org/
2324

24-
* Either a [Microsoft account](https://www.outlook.com) or [Office 365 for business account](https://msdn.microsoft.com/en-us/office/office365/howto/setup-development-environment#bk_Office365Account)
25+
* Either a [Microsoft account](https://www.outlook.com) or [Office 365 for business account](https://msdn.microsoft.com/office/office365/howto/setup-development-environment#bk_Office365Account)
2526

2627
## Register the sample
2728

@@ -47,9 +48,14 @@ To run this sample you will need the following:
4748

4849
1. In the list of pages for the app, select **Authentication**.
4950
- In the **Redirect URIs** section, select **Web** in the combo-box and enter the following redirect URI:
50-
`http://localhost:3000/auth/openid/return`
51-
- In the **Advanced settings** section, set **Logout URL** to `http://localhost:3000`.
52-
- In the **Advanced settings > Implicit grant** section, check **ID tokens** as this sample requires the [Implicit grant flow](https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-implicit-grant-flow) to be enabled to sign-in the user.
51+
`http://localhost:3000/auth/openid/return` (:warning:)
52+
- In the **Advanced settings > Implicit grant and hybrid flows** section, check **ID tokens** as this sample requires the [hybrid flow](https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-implicit-grant-flow) to be enabled to sign-in the user.
53+
- Select **Save**.
54+
1. In the list of pages for the app, select **Manifest**.
55+
- Find the key `logoutUrl` and replace its value with `http://localhost:3000/logout` (:warning:)
56+
- Select **Save**
57+
58+
> :warning: Except on localhost, HTTP schemes are considered insecure and should not be used. In production, you should use HTTPS instead. See for more: [Microsoft identity platform best practices and recommendations](https://docs.microsoft.com/azure/active-directory/develop/identity-platform-integration-checklist#security)
5359
5460
1. Select **Save**.
5561

@@ -60,7 +66,6 @@ To run this sample you will need the following:
6066

6167
You'll need this key later to configure the application. This key value will not be displayed again, nor retrievable by any other means, so record it as soon as it is visible from the Azure portal.
6268

63-
6469
## Download the sample application and modules
6570

6671
Next, clone the sample repo and install the NPM modules.
@@ -73,10 +78,9 @@ or
7378

7479
* `$ git clone https://github.com/AzureADQuickStarts/AppModelv2-WebApp-OpenIDConnect-nodejs.git`
7580

76-
7781
From the project root directory, run the command:
7882

79-
* `$ npm install`
83+
* `$ npm install`
8084

8185

8286
## Configure the application
@@ -105,16 +109,15 @@ The default session store in this sample is `express-session`. Note that the def
105109

106110
* Run the app using the following command from your command line.
107111

108-
```
109-
$ node app.js
112+
```console
113+
node app.js
110114
```
111115

112116
**Is the server output hard to understand?:** We use `bunyan` for logging in this sample. The console won't make much sense to you unless you also install bunyan and run the server like above but pipe it through the bunyan binary:
113117

114-
```
115-
$ npm install -g bunyan
116-
117-
$ node app.js | bunyan
118+
```console
119+
npm install -g bunyan
120+
node app.js | bunyan
118121
```
119122

120123
### You're done!
@@ -133,15 +136,15 @@ For issues with the passport-azure-ad library, please raise the issue on the lib
133136

134137
If you'd like to contribute to this sample, please follow the [GitHub Fork and Pull request model](https://help.github.com/articles/fork-a-repo/).
135138

136-
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.
139+
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.
137140

138141
## Security Library
139142

140143
This library controls how users sign-in and access services. We recommend you always take the latest version of our library in your app when possible.
141144

142145
## Security Reporting
143146

144-
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.
147+
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/security/dd252948) and subscribing to Security Advisory Alerts.
145148

146149
Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License (the "License");
147150

app.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030
var express = require('express');
3131
var cookieParser = require('cookie-parser');
3232
var expressSession = require('express-session');
33-
var bodyParser = require('body-parser');
3433
var methodOverride = require('method-override');
3534
var passport = require('passport');
36-
var util = require('util');
3735
var bunyan = require('bunyan');
36+
var morgan = require('morgan');
37+
3838
var config = require('./config');
3939

4040
// set up database for express session
@@ -149,7 +149,7 @@ var app = express();
149149

150150
app.set('views', __dirname + '/views');
151151
app.set('view engine', 'ejs');
152-
app.use(express.logger());
152+
app.use(morgan('dev'));
153153
app.use(methodOverride());
154154
app.use(cookieParser());
155155

@@ -168,13 +168,12 @@ if (config.useMongoDBSessionStore) {
168168
app.use(expressSession({ secret: 'keyboard cat', resave: true, saveUninitialized: false }));
169169
}
170170

171-
app.use(bodyParser.urlencoded({ extended : true }));
171+
app.use(express.urlencoded({ extended : true }));
172172

173173
// Initialize Passport! Also use passport.session() middleware, to support
174174
// persistent login sessions (recommended).
175175
app.use(passport.initialize());
176176
app.use(passport.session());
177-
app.use(app.router);
178177
app.use(express.static(__dirname + '/../../public'));
179178

180179
//-----------------------------------------------------------------------------
@@ -199,6 +198,7 @@ app.get('/', function(req, res) {
199198

200199
// '/account' is only available to logged in user
201200
app.get('/account', ensureAuthenticated, function(req, res) {
201+
console.log(req.user);
202202
res.render('account', { user: req.user });
203203
});
204204

@@ -226,7 +226,7 @@ app.get('/auth/openid/return',
226226
function(req, res, next) {
227227
passport.authenticate('azuread-openidconnect',
228228
{
229-
response: res, // required
229+
response: res, // required
230230
failureRedirect: '/'
231231
}
232232
)(req, res, next);
@@ -244,7 +244,7 @@ app.post('/auth/openid/return',
244244
function(req, res, next) {
245245
passport.authenticate('azuread-openidconnect',
246246
{
247-
response: res, // required
247+
response: res, // required
248248
failureRedirect: '/'
249249
}
250250
)(req, res, next);

config.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ exports.creds = {
1010
// Required, the client ID of your app in AAD
1111
clientID: '<your_client_id>',
1212

13+
// Required if `responseType` is 'code', 'id_token code' or 'code id_token'.
14+
// If app key contains '\', replace it with '\\'.
15+
clientSecret: '<your_client_secret>',
16+
1317
// Required, must be 'code', 'code id_token', 'id_token code' or 'id_token'
1418
// If you want to get access_token, you must use 'code', 'code id_token' or 'id_token code'
1519
responseType: 'code id_token',
@@ -23,10 +27,6 @@ exports.creds = {
2327
// Required if we use http for redirectUrl
2428
allowHttpForRedirectUrl: true,
2529

26-
// Required if `responseType` is 'code', 'id_token code' or 'code id_token'.
27-
// If app key contains '\', replace it with '\\'.
28-
clientSecret: '<your_client_secret>',
29-
3030
// Required to set to false if you don't want to validate issuer
3131
validateIssuer: false,
3232

0 commit comments

Comments
 (0)