forked from teaDesign/VotingInfoApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
92 lines (80 loc) · 2.76 KB
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Created by bantonides on 11/22/13.
*/
//for developmental testing
//TODO: Remove this authentication code and use Crowd
var LocalStrategy = require('passport-local').Strategy;
var users = [
{ id: 1, username: 'testuser', password: 'test', email: '[email protected]', name: { givenName: 'Test', familyName: 'User'} },
{ id: 2, username: 'testuser2', password: 'test2', email: '[email protected]', name: { givenName: 'Test', familyName: 'User2'} }
];
//Strategy for production
var CrowdStrategy = require('passport-atlassian-crowd').Strategy;
var _ = require('underscore');
//logged in user profiles
var profiles = [];
function findByUsername(username, fn) {
for (var i = 0, len = users.length; i < len; i++) {
var user = users[i];
if (user.username === username) {
return fn(null, user);
}
}
return fn(null, null);
}
var setup = function (config, passport, isDevelopment) {
passport.serializeUser(function (user, done) {
done(null, user.username);
});
passport.deserializeUser(function (username, done) {
var user = _.find(profiles, function (user) {
return user.username == username;
});
if (user === undefined) {
done(new Error("No user with username '" + username + "' found."));
}
else {
done(null, user);
}
});
if (isDevelopment) {
passport.use(new LocalStrategy(
function(username, password, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
// Find the user by username. If there is no user with the given
// username, or the password is not correct, set the user to `false` to
// indicate failure and set a flash message. Otherwise, return the
// authenticated `user`.
findByUsername(username, function(err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false, { message: 'Unknown user ' + username }); }
if (user.password != password) { return done(null, false, { message: 'Invalid password' }); }
profiles.push(user);
return done(null, user);
})
});
}
));
} else {
passport.use(new CrowdStrategy({
crowdServer: config.crowd.server,
crowdApplication: config.crowd.application,
crowdApplicationPassword: config.crowd.apppass,
retrieveGroupMemberships: config.crowd.retrieveGroups
},
function (userprofile, done) {
process.nextTick(function () {
var exists = _.any(profiles, function (user) {
return user.id == userprofile.id;
});
if (!exists) {
profiles.push(userprofile);
}
return done(null, userprofile);
})
}
));
}
};
exports.authSetup = setup;