Skip to content
This repository was archived by the owner on Feb 26, 2020. It is now read-only.

Commit e742263

Browse files
committed
Big update to the sample to prepare for the new OIDC passport-strategy
* Allows for issuer and audience validation * Allows for delete and task list based on owner (token.sub right now)
1 parent 5f58c39 commit e742263

20 files changed

Lines changed: 1470 additions & 336 deletions
Lines changed: 53 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,28 @@
2727
var assert = require('assert-plus');
2828
var mongoose = require('mongoose/');
2929
var bunyan = require('bunyan');
30-
var getopt = require('posix-getopt');
3130
var restify = require('restify');
32-
var getopt = require('posix-getopt');
3331
var config = require('./config');
32+
var passport = require('passport');
33+
var OIDCBearerStrategy = require('./lib/passport-azure-ad/index').OIDCStrategy;
3434

3535

3636
// We pass these options in to the ODICBearerStrategy.
3737

3838
var options = {
3939
// The URL of the metadata document for your app. We will put the keys for token validation from the URL found in the jwks_uri tag of the in the metadata.
40-
metadataurl: config.creds.openid_configuration
40+
identityMetadata: config.creds.identityMetadata,
41+
// issuer: config.creds.issuer,
42+
audience: config.creds.audience
4143

4244
};
4345

44-
// array to hold logged in users
46+
// array to hold logged in users and the current logged in user (owner)
4547
var users = [];
4648
var owner = null;
4749

4850
// Our logger
49-
var log = bunyan.createLogger({name: 'Windows Azure Active Directory Tutorial'});
50-
51-
/**
52-
* Load Passport for OAuth2 flows
53-
*/
54-
55-
var passport = require('passport')
56-
, OIDCBearerStrategy = require('./lib/oidc_strategy');
57-
58-
51+
var log = bunyan.createLogger({name: 'Windows Azure Active Directory Sample'});
5952

6053
// MongoDB setup
6154
// Setup some configuration
@@ -156,7 +149,7 @@ function removeAll(req, res, next) {
156149
function getTask(req, res, next) {
157150

158151
log.info('getTask was called for: ', owner);
159-
Task.find(owner, function (err, data) {
152+
Task.find({ owner: owner }, function (err, data) {
160153
if (err) {
161154
req.log.warn(err, 'get: unable to read %s', owner);
162155
next(err);
@@ -180,7 +173,7 @@ function listTasks(req, res, next) {
180173

181174
log.info("listTasks was called for: ", owner);
182175

183-
Task.find(owner).limit(20).sort('date').exec(function (err,data) {
176+
Task.find({ owner: owner }).limit(20).sort('date').exec(function (err,data) {
184177

185178
if (err)
186179
return next(err);
@@ -193,6 +186,10 @@ function listTasks(req, res, next) {
193186
log.warn(err, "There is no tasks in the database. Did you initalize the database as stated in the README?");
194187
}
195188

189+
if (!owner) {
190+
log.warn(err, "You did not pass an owner when listing tasks.");
191+
}
192+
196193
else {
197194

198195
res.json(data);
@@ -290,47 +287,46 @@ var server = restify.createServer({
290287
server.use(passport.initialize()); // Starts passport
291288
server.use(passport.session()); // Provides session support
292289

293-
/**
294-
/*
295-
/* Calling the OIDCBearerStrategy and managing users
296-
/*
297-
/* Passport pattern provides the need to manage users and info tokens
298-
/* with a FindorCreate() method that must be provided by the implementor.
299-
/* Here we just autoregister any user and implement a FindById().
300-
/* You'll want to do something smarter.
301-
**/
302-
303-
var findById = function (id, fn) {
304-
for (var i = 0, len = users.length; i < len; i++) {
305-
var user = users[i];
306-
log.info('Got user: ',user);
307-
if (user.sub === id) {
308-
return fn(null, user);
309-
}
310-
}
311-
return fn(null, null);
312-
};
313-
314-
315-
var oidcStrategy = new OIDCBearerStrategy(options,
316-
function(token, done) {
317-
log.info('verifying the user');
318-
log.info(token, 'was the token retreived');
319-
findById(token.sub, function (err, user) {
320-
if (err) { return done(err); }
321-
if (!user) {
322-
// "Auto-registration"
323-
log.info('User was added automatically as they were new. Their sub is: ', token.sub)
324-
users.push(token);
325-
log.info(users);
326-
owner = token.sub;
327-
return done(null, token);
328-
}
329-
owner = token.sub;
330-
return done(null, user, token);
331-
});
332-
}
333-
);
290+
/**
291+
/*
292+
/* Calling the OIDCBearerStrategy and managing users
293+
/*
294+
/* Passport pattern provides the need to manage users and info tokens
295+
/* with a FindorCreate() method that must be provided by the implementor.
296+
/* Here we just autoregister any user and implement a FindById().
297+
/* You'll want to do something smarter.
298+
**/
299+
300+
var findById = function (id, fn) {
301+
for (var i = 0, len = users.length; i < len; i++) {
302+
var user = users[i];
303+
if (user.sub === id) {
304+
log.info('Found user: ',user);
305+
return fn(null, user);
306+
}
307+
}
308+
return fn(null, null);
309+
};
310+
311+
312+
var oidcStrategy = new OIDCBearerStrategy(options,
313+
function(token, done) {
314+
log.info('verifying the user');
315+
log.info(token, 'was the token retreived');
316+
findById(token.sub, function (err, user) {
317+
if (err) { return done(err); }
318+
if (!user) {
319+
// "Auto-registration"
320+
log.info('User was added automatically as they were new. Their sub is: ', token.sub)
321+
users.push(token);
322+
owner = token.sub;
323+
return done(null, token);
324+
}
325+
owner = token.sub;
326+
return done(null, user, token);
327+
});
328+
}
329+
);
334330

335331
passport.use(oidcStrategy);
336332

node-server/config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Don't commit this file to your public repos. This config is for first-run
22
exports.creds = {
33
mongoose_auth_local: 'mongodb://localhost/tasklist', // Your mongo auth uri goes here
4-
openid_configuration: 'https://login.microsoftonline.com/hypercubeb2c.onmicrosoft.com/.well-known/openid-configuration?p=b2c_1_B2CSI', // For using Microsoft you should never need to change this.
5-
openid_keys: 'https://login.microsoftonline.com/common/discovery/keys' // For using Microsoft you should never need to change this. If asbsent will attempt to get from openid_configuration
6-
}
4+
audience: 'https://com.microsoft.windowsazure.activedirectory.samples',
5+
identityMetadata: 'https://login.microsoftonline.com/hypercubeb2c.onmicrosoft.com/.well-known/openid-configuration?p=b2c_1_B2CSI' // For using Microsoft you should never need to change this.
6+
}

node-server/lib/index.js

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Copyright (c) Microsoft Open Technologies, Inc.
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+
18+
'use strict';
19+
20+
21+
22+
exports.getElement = function (parentElement, elementName) {
23+
if (parentElement['saml:' + elementName]) {
24+
return parentElement['saml:' + elementName];
25+
} else if (parentElement['samlp:'+elementName]) {
26+
return parentElement['samlp:'+elementName];
27+
}
28+
return parentElement[elementName];
29+
};
30+
31+
32+
exports.getFirstElement = function (parentElement, elementName) {
33+
var element = null;
34+
35+
if (parentElement['saml:' + elementName]) {
36+
element = parentElement['saml:' + elementName];
37+
} else if (parentElement['samlp:'+elementName]) {
38+
element = parentElement['samlp:'+elementName];
39+
} else {
40+
element = parentElement[elementName];
41+
}
42+
return Array.isArray(element) ? element[0] : element;
43+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Copyright (c) Microsoft Open Technologies, Inc.
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+
18+
"use strict";
19+
20+
exports.SamlStrategy = require('./samlstrategy');
21+
exports.WsfedStrategy = require('./wsfedstrategy');
22+
exports.OIDCStrategy = require('./oidcstrategy');

0 commit comments

Comments
 (0)