]>
Commit | Line | Data |
---|---|---|
1 | 'use strict'; | |
2 | ||
3 | var passport = require('passport'), | |
4 | path = require('path'), | |
5 | safe = require('safetydance'), | |
6 | bcrypt = require('bcryptjs'), | |
7 | uuid = require('uuid/v4'), | |
8 | redis = require('redis'), | |
9 | BearerStrategy = require('passport-http-bearer').Strategy, | |
10 | LdapStrategy = require('passport-ldapjs').Strategy, | |
11 | HttpError = require('connect-lastmile').HttpError, | |
12 | HttpSuccess = require('connect-lastmile').HttpSuccess; | |
13 | ||
14 | var LOCAL_AUTH_FILE = path.resolve(process.env.LOCAL_AUTH_FILE || './.users.json'); | |
15 | ||
16 | var tokenStore = { | |
17 | data: {}, | |
18 | get: function (token, callback) { | |
19 | callback(tokenStore.data[token] ? null : 'not found', tokenStore.data[token]); | |
20 | }, | |
21 | set: function (token, data, callback) { | |
22 | tokenStore.data[token] = data; | |
23 | callback(null); | |
24 | }, | |
25 | del: function (token, callback) { | |
26 | delete tokenStore.data[token]; | |
27 | callback(null); | |
28 | } | |
29 | }; | |
30 | ||
31 | if (process.env.REDIS_URL) { | |
32 | console.log('Enable redis token store'); | |
33 | ||
34 | var redisClient = redis.createClient(process.env.REDIS_URL); | |
35 | ||
36 | if (process.env.REDIS_PASSWORD) { | |
37 | console.log('Using redis auth'); | |
38 | redisClient.auth(process.env.REDIS_PASSWORD); | |
39 | } | |
40 | ||
41 | // overwrite the tokenStore api | |
42 | tokenStore.get = function (token, callback) { | |
43 | redisClient.get(token, function (error, result) { | |
44 | callback(error || null, safe.JSON.parse(result)); | |
45 | }); | |
46 | }; | |
47 | tokenStore.set = function (token, data, callback) { | |
48 | redisClient.set(token, JSON.stringify(data), callback); | |
49 | }; | |
50 | tokenStore.del = redisClient.del.bind(redisClient); | |
51 | } else { | |
52 | console.log('Use in-memory token store'); | |
53 | } | |
54 | ||
55 | function issueAccessToken() { | |
56 | return function (req, res, next) { | |
57 | var accessToken = uuid(); | |
58 | ||
59 | tokenStore.set(accessToken, req.user, function (error) { | |
60 | if (error) return next(new HttpError(500, error)); | |
61 | next(new HttpSuccess(201, { accessToken: accessToken, user: req.user })); | |
62 | }); | |
63 | }; | |
64 | } | |
65 | ||
66 | passport.serializeUser(function (user, done) { | |
67 | console.log('serializeUser', user); | |
68 | done(null, user.uid); | |
69 | }); | |
70 | ||
71 | passport.deserializeUser(function (id, done) { | |
72 | console.log('deserializeUser', id); | |
73 | done(null, { uid: id }); | |
74 | }); | |
75 | ||
76 | var LDAP_URL = process.env.LDAP_URL; | |
77 | var LDAP_USERS_BASE_DN = process.env.LDAP_USERS_BASE_DN; | |
78 | ||
79 | if (LDAP_URL && LDAP_USERS_BASE_DN) { | |
80 | console.log('Enable ldap auth'); | |
81 | ||
82 | exports.login = [ passport.authenticate('ldap'), issueAccessToken() ]; | |
83 | } else { | |
84 | console.log('Use local user file:', LOCAL_AUTH_FILE); | |
85 | ||
86 | exports.login = [ | |
87 | function (req, res, next) { | |
88 | var users = safe.JSON.parse(safe.fs.readFileSync(LOCAL_AUTH_FILE)); | |
89 | if (!users) return res.send(401); | |
90 | if (!users[req.body.username]) return res.send(401); | |
91 | ||
92 | bcrypt.compare(req.body.password, users[req.body.username].passwordHash, function (error, valid) { | |
93 | if (error || !valid) return res.send(401); | |
94 | ||
95 | req.user = { | |
96 | username: req.body.username | |
97 | }; | |
98 | ||
99 | next(); | |
100 | }); | |
101 | }, | |
102 | issueAccessToken() | |
103 | ]; | |
104 | } | |
105 | ||
106 | var opts = { | |
107 | server: { | |
108 | url: LDAP_URL, | |
109 | }, | |
110 | base: LDAP_USERS_BASE_DN, | |
111 | search: { | |
112 | filter: '(|(username={{username}})(mail={{username}}))', | |
113 | attributes: ['displayname', 'username', 'mail', 'uid'], | |
114 | scope: 'sub' | |
115 | }, | |
116 | uidTag: 'cn', | |
117 | usernameField: 'username', | |
118 | passwordField: 'password', | |
119 | }; | |
120 | ||
121 | passport.use(new LdapStrategy(opts, function (profile, done) { | |
122 | done(null, profile); | |
123 | })); | |
124 | ||
125 | exports.verify = passport.authenticate('bearer', { session: false }); | |
126 | ||
127 | passport.use(new BearerStrategy(function (token, done) { | |
128 | tokenStore.get(token, function (error, result) { | |
129 | if (error) { | |
130 | console.error(error); | |
131 | return done(null, false); | |
132 | } | |
133 | ||
134 | done(null, result, { accessToken: token }); | |
135 | }); | |
136 | })); | |
137 | ||
138 | exports.logout = function (req, res, next) { | |
139 | tokenStore.del(req.authInfo.accessToken, function (error) { | |
140 | if (error) console.error(error); | |
141 | ||
142 | next(new HttpSuccess(200, {})); | |
143 | }); | |
144 | }; | |
145 | ||
146 | exports.getProfile = function (req, res, next) { | |
147 | next(new HttpSuccess(200, { username: req.user.username })); | |
148 | }; |