|
| 1 | +/** |
| 2 | + * Copyright (c) Microsoft Corporation |
| 3 | + * All Rights Reserved |
| 4 | + * Apache License 2.0 |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + * @flow |
| 18 | + */ |
| 19 | + |
| 20 | +'use strict'; |
| 21 | + |
| 22 | +/** |
| 23 | + * Module dependencies. |
| 24 | + */ |
| 25 | + |
| 26 | +var express = require('express'); |
| 27 | +var cookieParser = require('cookie-parser'); |
| 28 | +var expressSession = require('express-session'); |
| 29 | +var bodyParser = require('body-parser'); |
| 30 | +var passport = require('passport'); |
| 31 | +var util = require('util'); |
| 32 | +var bunyan = require('bunyan'); |
| 33 | +var config = require('./config'); |
| 34 | +var OIDCStrategy = require('./lib/passport-azure-ad/index').OIDCStrategy; |
| 35 | + |
| 36 | +var log = bunyan.createLogger({ |
| 37 | + name: 'Microsoft OIDC Example Web Application' |
| 38 | +}); |
| 39 | + |
| 40 | + |
| 41 | +// Passport session setup. |
| 42 | +// To support persistent login sessions, Passport needs to be able to |
| 43 | +// serialize users into and deserialize users out of the session. Typically, |
| 44 | +// this will be as simple as storing the user ID when serializing, and finding |
| 45 | +// the user by ID when deserializing. |
| 46 | +passport.serializeUser(function(user, done) { |
| 47 | + done(null, user.preferred_username); |
| 48 | +}); |
| 49 | + |
| 50 | +passport.deserializeUser(function(id, done) { |
| 51 | + findByEmail(id, function (err, user) { |
| 52 | + done(err, user); |
| 53 | + }); |
| 54 | +}); |
| 55 | + |
| 56 | +// array to hold logged in users |
| 57 | +var users = []; |
| 58 | + |
| 59 | +var findByEmail = function(email, fn) { |
| 60 | + for (var i = 0, len = users.length; i < len; i++) { |
| 61 | + var user = users[i]; |
| 62 | + if (user.preferred_username === email) { |
| 63 | + return fn(null, user); |
| 64 | + } |
| 65 | + } |
| 66 | + return fn(null, null); |
| 67 | +}; |
| 68 | + |
| 69 | +// Use the OIDCStrategy within Passport. |
| 70 | +// Strategies in passport require a `validate` function, which accept |
| 71 | +// credentials (in this case, an OpenID identifier), and invoke a callback |
| 72 | +// with a user object. |
| 73 | +passport.use(new OIDCStrategy({ |
| 74 | + callbackURL: config.creds.returnURL, |
| 75 | + realm: config.creds.realm, |
| 76 | + clientID: config.creds.clientID, |
| 77 | + clientSecret: config.creds.clientSecret, |
| 78 | + oidcIssuer: config.creds.issuer, |
| 79 | + identityMetadata: config.creds.identityMetadata, |
| 80 | + skipUserProfile: true // doesn't fetch user profile |
| 81 | + }, |
| 82 | + function(iss, sub, email, claims, profile, accessToken, refreshToken, done) { |
| 83 | + log.info('We received claims of: ', claims); |
| 84 | + log.info('Example: Email address we received was: ', claims.preferred_username); |
| 85 | + // asynchronous verification, for effect... |
| 86 | + process.nextTick(function () { |
| 87 | + findByEmail(claims.preferred_username, function(err, user) { |
| 88 | + if (err) { |
| 89 | + return done(err); |
| 90 | + } |
| 91 | + if (!user) { |
| 92 | + // "Auto-registration" |
| 93 | + users.push(claims); |
| 94 | + return done(null, claims); |
| 95 | + } |
| 96 | + return done(null, user); |
| 97 | + }); |
| 98 | + }); |
| 99 | + } |
| 100 | +)); |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | +var app = express(); |
| 106 | + |
| 107 | +// configure Express |
| 108 | +app.configure(function() { |
| 109 | + app.set('views', __dirname + '/views'); |
| 110 | + app.set('view engine', 'ejs'); |
| 111 | + app.use(express.logger()); |
| 112 | + app.use(express.methodOverride()); |
| 113 | + app.use(cookieParser()); |
| 114 | + app.use(expressSession({ secret: 'keyboard cat', resave: true, saveUninitialized: false })); |
| 115 | + app.use(bodyParser.urlencoded({ extended : true })); |
| 116 | + // Initialize Passport! Also use passport.session() middleware, to support |
| 117 | + // persistent login sessions (recommended). |
| 118 | + app.use(passport.initialize()); |
| 119 | + app.use(passport.session()); |
| 120 | + app.use(app.router); |
| 121 | + app.use(express.static(__dirname + '/../../public')); |
| 122 | +}); |
| 123 | + |
| 124 | + |
| 125 | +app.get('/', function(req, res){ |
| 126 | + res.render('index', { user: req.user }); |
| 127 | +}); |
| 128 | + |
| 129 | +app.get('/account', ensureAuthenticated, function(req, res){ |
| 130 | + res.render('account', { user: req.user }); |
| 131 | +}); |
| 132 | + |
| 133 | +app.get('/login', |
| 134 | + passport.authenticate('azuread-openidconnect', { failureRedirect: '/login' }), |
| 135 | + function(req, res) { |
| 136 | + log.info('Login was called in the Sample'); |
| 137 | + res.redirect('/'); |
| 138 | +}); |
| 139 | + |
| 140 | +// POST /auth/openid |
| 141 | +// Use passport.authenticate() as route middleware to authenticate the |
| 142 | +// request. The first step in OpenID authentication will involve redirecting |
| 143 | +// the user to their OpenID provider. After authenticating, the OpenID |
| 144 | +// provider will redirect the user back to this application at |
| 145 | +// /auth/openid/return |
| 146 | +app.post('/auth/openid', |
| 147 | + passport.authenticate('azuread-openidconnect', { failureRedirect: '/login' }), |
| 148 | + function(req, res) { |
| 149 | + log.info('Authenitcation was called in the Sample'); |
| 150 | + res.redirect('/'); |
| 151 | + }); |
| 152 | + |
| 153 | +// GET /auth/openid/return |
| 154 | +// Use passport.authenticate() as route middleware to authenticate the |
| 155 | +// request. If authentication fails, the user will be redirected back to the |
| 156 | +// login page. Otherwise, the primary route function function will be called, |
| 157 | +// which, in this example, will redirect the user to the home page. |
| 158 | +app.get('/auth/openid/return', |
| 159 | + passport.authenticate('azuread-openidconnect', { failureRedirect: '/login' }), |
| 160 | + function(req, res) { |
| 161 | + log.info('We received a return from AzureAD.'); |
| 162 | + res.redirect('/'); |
| 163 | + }); |
| 164 | + |
| 165 | +app.get('/logout', function(req, res){ |
| 166 | + req.logout(); |
| 167 | + res.redirect('/'); |
| 168 | +}); |
| 169 | + |
| 170 | +app.listen(3000); |
| 171 | + |
| 172 | + |
| 173 | +// Simple route middleware to ensure user is authenticated. |
| 174 | +// Use this route middleware on any resource that needs to be protected. If |
| 175 | +// the request is authenticated (typically via a persistent login session), |
| 176 | +// the request will proceed. Otherwise, the user will be redirected to the |
| 177 | +// login page. |
| 178 | +function ensureAuthenticated(req, res, next) { |
| 179 | + if (req.isAuthenticated()) { return next(); } |
| 180 | + res.redirect('/login') |
| 181 | +} |
0 commit comments