Skip to content

Instantly share code, notes, and snippets.

@velveteer
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save velveteer/f0d9190cf1950d690759 to your computer and use it in GitHub Desktop.
Save velveteer/f0d9190cf1950d690759 to your computer and use it in GitHub Desktop.
Meteor LDAPS Server
assert = Meteor.require 'assert'
ldap = Meteor.require 'ldapjs'
Future = Meteor.require 'fibers/future'
LDAP = {}
LDAP.url = 'XXXXXXX'
LDAP.cert = Assets.getText 'edir-wildcard.pem'
LDAP.searchOu = 'ou=XXX,o=XXXX'
LDAP.searchQuery = (user) -> filter: "(cn=#{user})", scope: 'sub'
LDAP.checkAccount = (options) ->
LDAP.client = ldap.createClient do
url: LDAP.url
tlsOptions: cert: LDAP.cert
options = options or {}
dn = []
future = new Future!
if options.password.length is 0 or
options.username.length is 0
future['return'] void
return void
LDAP.client.search do
LDAP.searchOu
LDAP.searchQuery options.username
(err, search) ->
assert.ifError err
do
entry <- search.on 'searchEntry'
dn.push entry.objectName
LDAP.displayName = entry.object.displayName
do
err <- search.on 'error'
throw new Meteor.Error 500, "LDAP server error"
do
search.on 'end', ->
if dn.length == 0
future['return'] false
return false
err <- LDAP.client.bind dn[0], options.password
if err
future['return'] false
return false
err <- LDAP.client.unbind!
assert.ifError err
future['return'] !err
future.wait!
Accounts.registerLoginHandler 'ldap', (loginRequest) ->
if LDAP.checkAccount loginRequest
user = Meteor.users.findOne username: loginRequest.username
if user
userId = user._id
else
userId = Meteor.users.insert do
username: loginRequest.username
profile: name: LDAP.displayName
userId: userId
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment