Adding LDAP Authentication to a Play! 2 Application
Hacking . Java . Play! Framework . ToolingAs of Play! 1 is not really supported anymore, i will describe the steps for accessing Data from your LDAP Directory with your Play! 2 with this Post.
Prerequisites
As also mentioned in my last Post, for this example we are using the Vagrant vagrant-rundeck-ldap VM, I already mentioned here.
Setup
After you setup a basic Play! 2 Application with
play new ldap-test
We need to update our Applications Dependencies. To achieve this, we need to change project/Build.scala:
import sbt._
import Keys._
import play.Project._
object ApplicationBuild extends Build {
val appName = "play2-ldap-example"
val appVersion = "1.0-SNAPSHOT"
val appDependencies = Seq(
// Add your project dependencies here,
javaCore,
javaJdbc,
javaEbean,
"com.innoq.liqid" % "ldap-connector" % "1.3"
)
val main = play.Project(appName, appVersion, appDependencies).settings(
// Add your own project settings here
)
}
The Command:
play compile
Should download all necessary Project Depedencies and will do an initial Compilation of all your Project Files (currently not really many).
You also need to add the LDAP Settings to your Applications configuration. For this example we will use an external Properties File conf/ldap.properties:
# Settings for LiQID
# ~~~~~
ldap.user.objectClasses=person
ldap.group.objectClasses=groupOfUniqueNames
default.ldap=ldap1
# LDAP Listsing, divided by ","
ldap.listing=ldap1
ldap1.ou_people=ou=users
ldap1.ou_group=ou=roles
ldap1.url=ldap://localhost:3890
ldap1.principal=dc=Manager,dc=example,dc=com
ldap1.credentials=password
ldap1.base_dn=dc=example,dc=com
ldap1.admin.group.id=admin
ldap1.user.id.attribute=cn
ldap1.user.object.class=person
ldap1.group.id.attribute=cn
ldap1.group.object.class=groupOfUniqueNames
ldap1.group.member.attribute=uniqueMember
Implementation
Since Play1 2 does not really support the use of Before Filters, we will use Custom Actions for making our Login Authentication work.
We will create a new Package app/actions with two new files: an annotation interface BasicAuth and the implementation itself BasicAuthAction.
Annotations will be used to Set a specific Controller to use Basic Auth.
So lets start with BasicAuth:
package actions;
import play.mvc.With;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@With(BasicAuthAction.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE })
@Inherited
@Documented
public @interface BasicAuth {
}
After that you can annotate Controllers with @BasicAuth (but this won’t work, since the Implementation is still missing).
Here then the BasicAuthAction:
package actions;
import com.ning.http.util.Base64;
import models.User;
import play.mvc.Action;
import play.mvc.Http.Context;
import play.mvc.Result;
public class BasicAuthAction extends Action {
private static final String AUTHORIZATION = "authorization";
private static final String WWW_AUTHENTICATE = "WWW-Authenticate";
private static final String REALM = "Basic realm=\"play2-ldap-example\"";
@Override
public Result call(Context context) throws Throwable {
String authHeader = context.request().getHeader(AUTHORIZATION);
if (authHeader == null) {
return sendAuthRequest(context);
}
String auth = authHeader.substring(6);
byte[] decodedAuth = Base64.decode(auth);
String[] credString = new String(decodedAuth, "UTF-8").split(":");
if (credString == null || credString.length != 2) {
return sendAuthRequest(context);
}
String username = credString[0];
String password = credString[1];
User authUser = User.authenticate(username, password);
if (authUser == null) {
return sendAuthRequest(context);
}
context.request().setUsername(username);
return delegate.call(context);
}
private Result sendAuthRequest(Context context) {
context.response().setHeader(WWW_AUTHENTICATE, REALM);
return unauthorized();
}
}
As you can see, there are no LDAP specific Dependencies at all, since all necessary Logic is the User Model, in User.authenticate(username, password).So let’s have a look into that Model:
package models;
import play.Play;
import com.innoq.ldap.connector.LdapHelper;
import com.innoq.ldap.connector.LdapUser;
import com.innoq.liqid.utils.Configuration;
public class User {
private static LdapHelper HELPER = getHelper();
public String sn;
public String cn;
public String dn;
public User(String cn) {
this.cn = cn;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("cn: ").append(cn).append("\n");
sb.append("sn: ").append(sn).append("\n");
sb.append("dn: ").append(dn).append("\n");
return sb.toString();
}
public static User authenticate(String username, String password) {
if (HELPER.checkCredentials(username, password)) {
return new User(username);
}
return null;
}
public static User getUser(String username) {
LdapUser ldapUser = (LdapUser) LdapHelper.getInstance().getUser(
username);
User user = new User(username);
user.cn = ldapUser.get("cn");
user.sn = ldapUser.get("sn");
user.dn = ldapUser.getDn();
return user;
}
private static LdapHelper getHelper() {
Configuration.setPropertiesLocation(Play.application().path()
.getAbsolutePath()
+ "/conf/ldap.properties");
return LdapHelper.getInstance();
}
}
You also have a static Instace of that LDAP Helper, but authenticate User Credentials and Login are in two different Methods.Last thing is to Load a User from the LDAP Directory:Here the Admin Controller:
package controllers;
import models.User;
import actions.BasicAuth;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.Admin.index;
@BasicAuth
public class Admin extends Controller {
public static Result index() {
User u = User.getUser(request().username());
return ok(index.render("Hello Admin!", u));
}
}
And here the used Template File:
@(message: String, user: User)
@main("Admin Index") {
@{message}
<p>
<pre>
@{user}
</pre>
</p>
}
Links
You can find the Sources of that Library here: https://github.com/innoq/LiQIDYou can find an example Project here: https://github.com/phaus/play-ldap/tree/master/play2-ldap-example
Related
Archives
- August 2025
- November 2023
- February 2023
- January 2023
- April 2020
- January 2018
- December 2017
- May 2017
- February 2016
- September 2015
- December 2014
- August 2014
- June 2014
- March 2014
- February 2014
- September 2013
- August 2013
- July 2013
- November 2012
- October 2012
- September 2012
- June 2012
- May 2012
- April 2012
- March 2012
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- August 2011
- July 2011
- June 2011
- May 2011
- January 2011
- August 2010
- July 2010
- June 2010
- May 2010
- January 2010
- November 2009
- October 2009
- September 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- September 2006
- June 2006
- May 2006
- April 2006
- March 2006
- February 2006
- January 2006
Leave a Reply