Skip to content

Commit b13635a

Browse files
authored
Merge pull request #279 from alecdwm/ldap-auth
Support for LDAP server authentication
2 parents 23a12dd + 94abfab commit b13635a

11 files changed

Lines changed: 191 additions & 6 deletions

File tree

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ Environment variables (will overwrite other server configs)
140140
| HMD_DROPBOX_CLIENTSECRET | no example | Dropbox API client secret |
141141
| HMD_GOOGLE_CLIENTID | no example | Google API client id |
142142
| HMD_GOOGLE_CLIENTSECRET | no example | Google API client secret |
143+
| HMD_LDAP_URL | ldap://example.com | url of LDAP server |
144+
| HMD_LDAP_BINDDN | no example | bindDn for LDAP access |
145+
| HMD_LDAP_BINDCREDENTIALS | no example | bindCredentials for LDAP access |
146+
| HMD_LDAP_TOKENSECRET | supersecretkey | secret used for generating access/refresh tokens |
147+
| HMD_LDAP_SEARCHBASE | o=users,dc=example,dc=com | LDAP directory to begin search from |
148+
| HMD_LDAP_SEARCHFILTER | (uid={{username}}) | LDAP filter to search with |
149+
| HMD_LDAP_SEARCHATTRIBUTES | no example | LDAP attributes to search with |
150+
| HMD_LDAP_TLS_CA | no example | Root CA for LDAP TLS in PEM format |
143151
| HMD_IMGUR_CLIENTID | no example | Imgur API client id |
144152
| HMD_EMAIL | `true` or `false` | set to allow email register and signin |
145153
| HMD_IMAGE_UPLOAD_TYPE | `imgur`, `s3` or `filesystem` | Where to upload image. For S3, see our [S3 Image Upload Guide](docs/guides/s3-image-upload.md) |
@@ -194,7 +202,7 @@ Third-party integration api key settings
194202

195203
| service | settings location | description |
196204
| ------- | --------- | ----------- |
197-
| facebook, twitter, github, gitlab, dropbox, google | environment variables or `config.json` | for signin |
205+
| facebook, twitter, github, gitlab, dropbox, google, ldap | environment variables or `config.json` | for signin |
198206
| imgur | environment variables or `config.json` | for image upload |
199207
| google drive, dropbox | `public/js/config.js` | for export and import |
200208

app.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,18 @@ if (config.google) {
381381
failureRedirect: config.serverurl + '/'
382382
}));
383383
}
384+
// ldap auth
385+
if (config.ldap) {
386+
app.post('/auth/ldap', urlencodedParser, function (req, res, next) {
387+
if (!req.body.username || !req.body.password) return response.errorBadRequest(res);
388+
setReturnToFromReferer(req);
389+
passport.authenticate('ldapauth', {
390+
successReturnToOrRedirect: config.serverurl + '/',
391+
failureRedirect: config.serverurl + '/',
392+
failureFlash: true
393+
})(req, res, next);
394+
});
395+
}
384396
// email auth
385397
if (config.email) {
386398
app.post('/register', urlencodedParser, function (req, res, next) {

config.json.example

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@
5151
"clientID": "change this",
5252
"clientSecret": "change this"
5353
},
54+
"ldap": {
55+
"url": "ldap://change_this",
56+
"bindDn": null,
57+
"bindCredentials": null,
58+
"tokenSecret": "change this",
59+
"searchBase": "change this",
60+
"searchFilter": "change this",
61+
"searchAttributes": "change this",
62+
"tlsOptions": {
63+
"changeme": "See https://nodejs.org/api/tls.html#tls_tls_connect_options_callback"
64+
}
65+
},
5466
"imgur": {
5567
"clientID": "change this"
5668
}

lib/auth.js

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var GithubStrategy = require('passport-github').Strategy;
77
var GitlabStrategy = require('passport-gitlab2').Strategy;
88
var DropboxStrategy = require('passport-dropbox-oauth2').Strategy;
99
var GoogleStrategy = require('passport-google-oauth20').Strategy;
10+
var LdapStrategy = require('passport-ldapauth');
1011
var LocalStrategy = require('passport-local').Strategy;
1112
var validator = require('validator');
1213

@@ -110,6 +111,62 @@ if (config.google) {
110111
callbackURL: config.serverurl + '/auth/google/callback'
111112
}, callback));
112113
}
114+
// ldap
115+
if (config.ldap) {
116+
passport.use(new LdapStrategy({
117+
server: {
118+
url: config.ldap.url || null,
119+
bindDn: config.ldap.bindDn || null,
120+
bindCredentials: config.ldap.bindCredentials || null,
121+
searchBase: config.ldap.searchBase || null,
122+
searchFilter: config.ldap.searchFilter || null,
123+
searchAttributes: config.ldap.searchAttributes || null,
124+
tlsOptions: config.ldap.tlsOptions || null
125+
},
126+
},
127+
function(user, done) {
128+
var profile = {
129+
id: 'LDAP-' + user.uidNumber,
130+
username: user.uid,
131+
displayName: user.displayName,
132+
emails: user.mail ? [user.mail] : [],
133+
avatarUrl: null,
134+
profileUrl: null,
135+
provider: 'ldap',
136+
}
137+
var stringifiedProfile = JSON.stringify(profile);
138+
models.User.findOrCreate({
139+
where: {
140+
profileid: profile.id.toString()
141+
},
142+
defaults: {
143+
profile: stringifiedProfile,
144+
}
145+
}).spread(function (user, created) {
146+
if (user) {
147+
var needSave = false;
148+
if (user.profile != stringifiedProfile) {
149+
user.profile = stringifiedProfile;
150+
needSave = true;
151+
}
152+
if (needSave) {
153+
user.save().then(function () {
154+
if (config.debug)
155+
logger.info('user login: ' + user.id);
156+
return done(null, user);
157+
});
158+
} else {
159+
if (config.debug)
160+
logger.info('user login: ' + user.id);
161+
return done(null, user);
162+
}
163+
}
164+
}).catch(function (err) {
165+
logger.error('ldap auth failed: ' + err);
166+
return done(err, null);
167+
});
168+
}));
169+
}
113170
// email
114171
if (config.email) {
115172
passport.use(new LocalStrategy({
@@ -130,4 +187,4 @@ if (config.email) {
130187
return done(err);
131188
});
132189
}));
133-
}
190+
}

lib/config.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,37 @@ var google = (process.env.HMD_GOOGLE_CLIENTID && process.env.HMD_GOOGLE_CLIENTSE
9595
clientID: process.env.HMD_GOOGLE_CLIENTID,
9696
clientSecret: process.env.HMD_GOOGLE_CLIENTSECRET
9797
} : config.google || false;
98+
var ldap = config.ldap || (
99+
process.env.HMD_LDAP_URL ||
100+
process.env.HMD_LDAP_BINDDN ||
101+
process.env.HMD_LDAP_BINDCREDENTIALS ||
102+
process.env.HMD_LDAP_TOKENSECRET ||
103+
process.env.HMD_LDAP_SEARCHBASE ||
104+
process.env.HMD_LDAP_SEARCHFILTER ||
105+
process.env.HMD_LDAP_SEARCHATTRIBUTES
106+
) || false;
107+
if (ldap == true)
108+
ldap = {};
109+
if (process.env.HMD_LDAP_URL)
110+
ldap.url = process.env.HMD_LDAP_URL;
111+
if (process.env.HMD_LDAP_BINDDN)
112+
ldap.bindDn = process.env.HMD_LDAP_BINDDN;
113+
if (process.env.HMD_LDAP_BINDCREDENTIALS)
114+
ldap.bindCredentials = process.env.HMD_LDAP_BINDCREDENTIALS;
115+
if (process.env.HMD_LDAP_TOKENSECRET)
116+
ldap.tokenSecret = process.env.HMD_LDAP_TOKENSECRET;
117+
if (process.env.HMD_LDAP_SEARCHBASE)
118+
ldap.searchBase = process.env.HMD_LDAP_SEARCHBASE;
119+
if (process.env.HMD_LDAP_SEARCHFILTER)
120+
ldap.searchFilter = process.env.HMD_LDAP_SEARCHFILTER;
121+
if (process.env.HMD_LDAP_SEARCHATTRIBUTES)
122+
ldap.searchAttributes = process.env.HMD_LDAP_SEARCHATTRIBUTES;
123+
if (process.env.HMD_LDAP_TLS_CA) {
124+
var ca = {
125+
ca: process.env.HMD_LDAP_TLS_CA
126+
}
127+
ldap.tlsOptions = ldap.tlsOptions ? Object.assign(ldap.tlsOptions, ca) : ca
128+
}
98129
var imgur = process.env.HMD_IMGUR_CLIENTID || config.imgur || false;
99130
var email = process.env.HMD_EMAIL ? (process.env.HMD_EMAIL === 'true') : !!config.email;
100131

@@ -156,6 +187,7 @@ module.exports = {
156187
gitlab: gitlab,
157188
dropbox: dropbox,
158189
google: google,
190+
ldap: ldap,
159191
imgur: imgur,
160192
email: email,
161193
imageUploadType: imageUploadType,

lib/letter-avatars.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use strict";
2+
3+
// external modules
4+
var randomcolor = require('randomcolor');
5+
6+
// core
7+
module.exports = function(name) {
8+
var color = randomcolor({
9+
seed: name,
10+
luminosity: 'dark'
11+
});
12+
var letter = name.substring(0, 1).toUpperCase();
13+
14+
var svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>';
15+
svg += '<svg xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://www.w3.org/2000/svg" height="96" width="96" version="1.1" viewBox="0 0 96 96">';
16+
svg += '<g>';
17+
svg += '<rect width="96" height="96" fill="' + color + '" />';
18+
svg += '<text font-size="64px" font-family="sans-serif" text-anchor="middle" fill="#ffffff">';
19+
svg += '<tspan x="48" y="72" stroke-width=".26458px" fill="#ffffff">' + letter + '</tspan>';
20+
svg += '</text>';
21+
svg += '</g>';
22+
svg += '</svg>';
23+
24+
return 'data:image/svg+xml;base64,' + new Buffer(svg).toString('base64');
25+
};

lib/models/user.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var scrypt = require('scrypt');
77

88
// core
99
var logger = require("../logger.js");
10+
var letterAvatars = require('../letter-avatars.js');
1011

1112
module.exports = function (sequelize, DataTypes) {
1213
var User = sequelize.define("User", {
@@ -105,6 +106,16 @@ module.exports = function (sequelize, DataTypes) {
105106
case "google":
106107
photo = profile.photos[0].value.replace(/(\?sz=)\d*$/i, '$196');
107108
break;
109+
case "ldap":
110+
//no image api provided,
111+
//use gravatar if email exists,
112+
//otherwise generate a letter avatar
113+
if (profile.emails[0]) {
114+
photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0]) + '?s=96';
115+
} else {
116+
photo = letterAvatars(profile.username);
117+
}
118+
break;
108119
}
109120
return photo;
110121
},

lib/response.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ function showIndex(req, res, next) {
6666
gitlab: config.gitlab,
6767
dropbox: config.dropbox,
6868
google: config.google,
69+
ldap: config.ldap,
6970
email: config.email,
7071
signin: req.isAuthenticated(),
7172
infoMessage: req.flash('info'),
@@ -94,6 +95,7 @@ function responseHackMD(res, note) {
9495
gitlab: config.gitlab,
9596
dropbox: config.dropbox,
9697
google: config.google,
98+
ldap: config.ldap,
9799
email: config.email
98100
});
99101
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"passport-github": "^1.1.0",
8686
"passport-gitlab2": "^2.2.0",
8787
"passport-google-oauth20": "^1.0.0",
88+
"passport-ldapauth": "^0.6.0",
8889
"passport-local": "^1.0.0",
8990
"passport-twitter": "^1.0.4",
9091
"passport.socketio": "^3.7.0",

public/views/index.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
<% if (errorMessage && errorMessage.length > 0) { %>
5858
<div class="alert alert-danger" style="max-width: 400px; margin: 0 auto;"><%= errorMessage %></div>
5959
<% } %>
60-
<% if(facebook || twitter || github || gitlab || dropbox || google || email) { %>
60+
<% if(facebook || twitter || github || gitlab || dropbox || google || ldap || email) { %>
6161
<span class="ui-signin">
6262
<br>
6363
<a type="button" class="btn btn-lg btn-success ui-signin" data-toggle="modal" data-target=".signin-modal" style="min-width: 170px;"><%= __('Sign In') %></a>
@@ -97,7 +97,7 @@
9797
</div>
9898

9999
<div id="history" class="section"<% if(!signin) { %> style="display:none;"<% } %>>
100-
<% if(facebook || twitter || github || gitlab || dropbox || google || email) { %>
100+
<% if(facebook || twitter || github || gitlab || dropbox || google || ldap || email) { %>
101101
<div class="ui-signin">
102102
<p><%= __('Below is the history from browser') %></p>
103103
</div>

0 commit comments

Comments
 (0)