Created
June 28, 2012 22:49
-
-
Save tgpfeiffer/3014487 to your computer and use it in GitHub Desktop.
Creating an LDAP user using Scala/Lift and LDAPProtoUser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This code shows in a prototypical manner how to create a user | |
* in an LDAP directory using the code from the lift-ldap package, | |
* including salted SHA1 (SSHA) passwords. | |
* | |
* The Java code from | |
* http://stackoverflow.com/questions/8176001/trouble-creating-active-directory-users-with-java | |
* was used as a template, kudos! | |
*/ | |
import javax.naming.directory._ | |
import net.liftweb.mapper._ | |
import net.liftweb.util._ | |
import net.liftweb.common._ | |
import Helpers._ | |
import net.liftweb.ldap._ | |
object User extends User with MetaLDAPProtoUser[User] { | |
// ... | |
// LDAP vendor for read/login operations | |
object myLdap extends LDAPVendor | |
myLdap.configure(Map( | |
"ldap.url" -> Props.get("ldap.url", "ldap://localhost:389"), | |
"ldap.base" -> Props.get("ldap.base", "") | |
)) | |
override def ldapVendor = myLdap | |
// LDAP vendor to create new users | |
object myAdminLdap extends LDAPVendor | |
myAdminLdap.configure(Map( | |
"ldap.url" -> Props.get("ldap.url", "ldap://localhost:389"), | |
"ldap.base" -> Props.get("ldap.base", ""), | |
"ldap.userName" -> Props.get("ldap.userName", ""), | |
"ldap.password" -> Props.get("ldap.password", "") | |
)) | |
} | |
class User extends LDAPProtoUser[User] { | |
// ... | |
def getSingleton = User | |
def mkLDAPEntry = { | |
val ctx = User.myAdminLdap.initialContext | |
// create container | |
val container = new BasicAttributes | |
// add object class | |
val objClasses = new BasicAttribute("objectClass") | |
objClasses add "top" | |
objClasses add "person" | |
objClasses add "inetOrgPerson" | |
// other attributes | |
val sn = new BasicAttribute("sn", "User") | |
val firstName = new BasicAttribute("givenName", "Test") | |
val cn = new BasicAttribute("cn", "Test User") | |
val uid = new BasicAttribute("uid", "me@privacy.net") | |
val mail = new BasicAttribute("mail", "me@privacy.net") | |
// compute password | |
val md = java.security.MessageDigest.getInstance("SHA-1") | |
val salt = randomString(4) | |
val hash = md.digest(("myPassword"+salt).getBytes) | |
val pwValue = "{SSHA}" + base64Encode(hash ++ salt.getBytes) | |
val pw = new BasicAttribute("userPassword", pwValue) | |
// add attributes to container | |
container put objClasses | |
container put sn | |
container put firstName | |
container put cn | |
container put uid | |
container put mail | |
container put pw | |
// create in directory | |
ctx.createSubcontext("uid=me,ou=users,dc=myorg", container) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment