-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.coffee
68 lines (60 loc) · 1.73 KB
/
app.coffee
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
ldap = require 'ldapjs'
Service = require './lib/service'
moment = require 'moment'
_ = require 'underscore'
api = new Service 'ldap', process.env.API_PASSWORD
# GLOBALS
basedn = "dc=ares"
pamdn = "cn=admin, #{basedn}"
usersBasedn = "ou=users, #{basedn}"
groupsBasedn = "ou=groups, #{basedn}"
server = ldap.createServer()
# pam_ldap bind
server.bind pamdn, (req, res, next) ->
console.log "#{moment().format()} BIND #{req.dn.toString()}"
if req.credentials is process.env.LDAP_TOKEN
res.end()
return next()
else
return next(new ldap.InsufficientAccessRightsError())
# user bind
server.bind usersBasedn, (req, res, next) ->
console.log "#{moment().format()} BIND #{req.dn.toString()}"
uid = req.dn.rdns[0].cn
api.bind
username: uid
password: req.credentials
success: ->
res.end()
next()
error: ->
next(new ldap.InsufficientAccessRightsError())
server.search usersBasedn, (req, res, next) ->
console.log "#{moment().format()} SEARCH #{req.dn.toString()} #{req.filter.toString()}"
api.users
success: (users) ->
_.each users, (user) ->
if req.filter.matches user.attributes
console.log user
res.send user
res.end()
next()
error: ->
res.end()
next()
server.search groupsBasedn, (req, res, next) ->
console.log "#{moment().format()} SEARCH #{req.dn.toString()} #{req.filter.toString()}"
api.groups
success: (groups) ->
_.each groups, (group) ->
if req.filter.matches group.attributes
console.log group
res.send group
res.end()
next()
error: ->
res.end()
next()
# Start server
server.listen parseInt(process.env.LDAP_PORT), ->
console.log 'LDAP server up at: %s', server.url