Skip to content

Commit

Permalink
Mongo support
Browse files Browse the repository at this point in the history
  • Loading branch information
leleuj committed Jul 28, 2015
1 parent 2005585 commit fe5e972
Show file tree
Hide file tree
Showing 14 changed files with 641 additions and 52 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -36,7 +36,8 @@ They depend on the `pac4j-core` module (groupId: `org.pac4j`):
7. **OpenID Connect** 1.0 using the `pac4j-oidc` module 7. **OpenID Connect** 1.0 using the `pac4j-oidc` module
8. **JWT** using the `pac4j-jwt` module 8. **JWT** using the `pac4j-jwt` module
9. **LDAP** using the `pac4j-ldap` module 9. **LDAP** using the `pac4j-ldap` module
10. **relational DB** using the `pac4j-sql` module. 10. **relational DB** using the `pac4j-sql` module
11. **MongoDB** using the `pac4j-mongo` module.


See [all authentication mechanisms](https://github.com/pac4j/pac4j/wiki/Clients). See [all authentication mechanisms](https://github.com/pac4j/pac4j/wiki/Clients).


Expand Down
Expand Up @@ -75,6 +75,8 @@ public static UserProfile buildProfile(final String typedId, final Map<String, O
completeName = "org.pac4j.ldap.profile.LdapProfile"; completeName = "org.pac4j.ldap.profile.LdapProfile";
} else if ("DbProfile".equals(className)) { } else if ("DbProfile".equals(className)) {
completeName = "org.pac4j.sql.profile.DbProfile"; completeName = "org.pac4j.sql.profile.DbProfile";
} else if ("MongoProfile".equals(className)) {
completeName = "org.pac4j.mongo.profile.MongoProfile";
} else if ("YahooOpenIdProfile".equals(className)) { } else if ("YahooOpenIdProfile".equals(className)) {
completeName = "org.pac4j.openid.profile.yahoo.YahooOpenIdProfile"; completeName = "org.pac4j.openid.profile.yahoo.YahooOpenIdProfile";
} else if ("GaeUserServiceProfile".equals(className)) { } else if ("GaeUserServiceProfile".equals(className)) {
Expand Down
Expand Up @@ -50,7 +50,15 @@ public interface TestsConstants {
String TITLE = "title"; String TITLE = "title";
String NAMESPACE = "namespace"; String NAMESPACE = "namespace";
int INT_ID = 1234; int INT_ID = 1234;

String SALT = "sel";
String GOOD_USERNAME = "jle";
String GOOD_USERNAME2 = "jleleu";
String BAD_USERNAME = "michael";
String MULTIPLE_USERNAME = "misagh";
String FIRSTNAME = "firstname";
String FIRSTNAME_VALUE = "Jerome";
String CLIENT_NAME = "clientname";

// urls // urls
String CALLBACK_URL = "http://myserver/callback"; String CALLBACK_URL = "http://myserver/callback";
String GOOGLE_URL = "http://www.google.com"; String GOOGLE_URL = "http://www.google.com";
Expand Down
Expand Up @@ -20,6 +20,7 @@
import org.pac4j.core.exception.BadCredentialsException; import org.pac4j.core.exception.BadCredentialsException;
import org.pac4j.core.exception.TechnicalException; import org.pac4j.core.exception.TechnicalException;
import org.pac4j.core.profile.UserProfile; import org.pac4j.core.profile.UserProfile;
import org.pac4j.core.util.TestsConstants;
import org.pac4j.http.credentials.UsernamePasswordCredentials; import org.pac4j.http.credentials.UsernamePasswordCredentials;
import org.pac4j.ldap.profile.LdapProfile; import org.pac4j.ldap.profile.LdapProfile;
import org.pac4j.ldap.test.tools.AuthenticatorGenerator; import org.pac4j.ldap.test.tools.AuthenticatorGenerator;
Expand All @@ -35,10 +36,7 @@
* @author Jerome Leleu * @author Jerome Leleu
* @since 1.8.0 * @since 1.8.0
*/ */
public class LdapAuthenticatorTests { public class LdapAuthenticatorTests implements TestsConstants {

private final static String CLIENT_NAME = "clientName";
private final static String BAD_USERNAME = "michael";


private LdapServer ldapServer; private LdapServer ldapServer;


Expand Down Expand Up @@ -74,56 +72,56 @@ public void testNullAttributes() {
public void authentFailed() { public void authentFailed() {
final LdapAuthenticator ldapAuthenticator = new LdapAuthenticator(authenticator); final LdapAuthenticator ldapAuthenticator = new LdapAuthenticator(authenticator);


final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(BAD_USERNAME, LdapServer.PASSWORD, CLIENT_NAME); final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(BAD_USERNAME, PASSWORD, CLIENT_NAME);
ldapAuthenticator.validate(credentials); ldapAuthenticator.validate(credentials);
} }


@Test @Test
public void authentSuccessNoAttribute() { public void authentSuccessNoAttribute() {
final LdapAuthenticator ldapAuthenticator = new LdapAuthenticator(authenticator); final LdapAuthenticator ldapAuthenticator = new LdapAuthenticator(authenticator);


final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(LdapServer.USERNAME, LdapServer.PASSWORD, CLIENT_NAME); final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(GOOD_USERNAME, PASSWORD, CLIENT_NAME);
ldapAuthenticator.validate(credentials); ldapAuthenticator.validate(credentials);


final UserProfile profile = credentials.getUserProfile(); final UserProfile profile = credentials.getUserProfile();
assertNotNull(profile); assertNotNull(profile);
assertTrue(profile instanceof LdapProfile); assertTrue(profile instanceof LdapProfile);
final LdapProfile ldapProfile = (LdapProfile) profile; final LdapProfile ldapProfile = (LdapProfile) profile;
assertEquals(LdapServer.USERNAME, ldapProfile.getId()); assertEquals(GOOD_USERNAME, ldapProfile.getId());
assertEquals(0, ldapProfile.getAttributes().size()); assertEquals(0, ldapProfile.getAttributes().size());
} }


@Test @Test
public void authentSuccessSingleAttribute() { public void authentSuccessSingleAttribute() {
final LdapAuthenticator ldapAuthenticator = new LdapAuthenticator(authenticator, LdapServer.CN + "," + LdapServer.SN); final LdapAuthenticator ldapAuthenticator = new LdapAuthenticator(authenticator, LdapServer.CN + "," + LdapServer.SN);


final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(LdapServer.USERNAME, LdapServer.PASSWORD, CLIENT_NAME); final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(GOOD_USERNAME, PASSWORD, CLIENT_NAME);
ldapAuthenticator.validate(credentials); ldapAuthenticator.validate(credentials);


final UserProfile profile = credentials.getUserProfile(); final UserProfile profile = credentials.getUserProfile();
assertNotNull(profile); assertNotNull(profile);
assertTrue(profile instanceof LdapProfile); assertTrue(profile instanceof LdapProfile);
final LdapProfile ldapProfile = (LdapProfile) profile; final LdapProfile ldapProfile = (LdapProfile) profile;
assertEquals(LdapServer.USERNAME, ldapProfile.getId()); assertEquals(GOOD_USERNAME, ldapProfile.getId());
assertEquals(2, ldapProfile.getAttributes().size()); assertEquals(2, ldapProfile.getAttributes().size());
assertEquals(LdapServer.USERNAME, ldapProfile.getAttribute(LdapServer.CN)); assertEquals(GOOD_USERNAME, ldapProfile.getAttribute(LdapServer.CN));
assertEquals(LdapServer.FIRSTNAME, ldapProfile.getAttribute(LdapServer.SN)); assertEquals(FIRSTNAME_VALUE, ldapProfile.getAttribute(LdapServer.SN));
} }


@Test @Test
public void authentSuccessMultiAttribute() { public void authentSuccessMultiAttribute() {
final LdapAuthenticator ldapAuthenticator = new LdapAuthenticator(authenticator, LdapServer.CN + "," + LdapServer.SN + "," + LdapServer.ROLE); final LdapAuthenticator ldapAuthenticator = new LdapAuthenticator(authenticator, LdapServer.CN + "," + LdapServer.SN + "," + LdapServer.ROLE);


final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(LdapServer.USERNAME2, LdapServer.PASSWORD, CLIENT_NAME); final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(GOOD_USERNAME2, PASSWORD, CLIENT_NAME);
ldapAuthenticator.validate(credentials); ldapAuthenticator.validate(credentials);


final UserProfile profile = credentials.getUserProfile(); final UserProfile profile = credentials.getUserProfile();
assertNotNull(profile); assertNotNull(profile);
assertTrue(profile instanceof LdapProfile); assertTrue(profile instanceof LdapProfile);
final LdapProfile ldapProfile = (LdapProfile) profile; final LdapProfile ldapProfile = (LdapProfile) profile;
assertEquals(LdapServer.USERNAME2, ldapProfile.getId()); assertEquals(GOOD_USERNAME2, ldapProfile.getId());
assertEquals(2, ldapProfile.getAttributes().size()); assertEquals(2, ldapProfile.getAttributes().size());
assertEquals(LdapServer.USERNAME2, ldapProfile.getAttribute(LdapServer.CN)); assertEquals(GOOD_USERNAME2, ldapProfile.getAttribute(LdapServer.CN));
assertNull(ldapProfile.getAttribute(LdapServer.SN)); assertNull(ldapProfile.getAttribute(LdapServer.SN));
final Collection<String> attributes = (Collection<String>) ldapProfile.getAttribute(LdapServer.ROLE); final Collection<String> attributes = (Collection<String>) ldapProfile.getAttribute(LdapServer.ROLE);
assertEquals(2, attributes.size()); assertEquals(2, attributes.size());
Expand Down
17 changes: 7 additions & 10 deletions pac4j-ldap/src/test/java/org/pac4j/ldap/test/tools/LdapServer.java
Expand Up @@ -18,24 +18,21 @@
import com.unboundid.ldap.listener.InMemoryDirectoryServer; import com.unboundid.ldap.listener.InMemoryDirectoryServer;
import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig; import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
import com.unboundid.ldap.listener.InMemoryListenerConfig; import com.unboundid.ldap.listener.InMemoryListenerConfig;
import org.pac4j.core.util.TestsConstants;


/** /**
* Simulates a basic LDAP server. * Simulates a basic LDAP server.
* *
* @author Jerome Leleu * @author Jerome Leleu
* @since 1.8.0 * @since 1.8.0
*/ */
public class LdapServer { public class LdapServer implements TestsConstants {


public final static String BASE_DN = "dc=example,dc=com"; public final static String BASE_DN = "dc=example,dc=com";
public final static String BASE_PEOPLE_DN = "ou=people,dc=example,dc=com"; public final static String BASE_PEOPLE_DN = "ou=people,dc=example,dc=com";
public final static int PORT = 33389; public final static int PORT = 33389;
public final static String CN = "cn"; public final static String CN = "cn";
public final static String SN = "sn"; public final static String SN = "sn";
public final static String USERNAME = "jle";
public final static String USERNAME2 = "jleleu";
public final static String PASSWORD = "password";
public final static String FIRSTNAME = "Jerome";
public final static String ROLE = "role"; public final static String ROLE = "role";
public final static String ROLE1 = "role1"; public final static String ROLE1 = "role1";
public final static String ROLE2 = "role2"; public final static String ROLE2 = "role2";
Expand All @@ -49,14 +46,14 @@ public void start() {
dsConfig.setEnforceAttributeSyntaxCompliance(false); dsConfig.setEnforceAttributeSyntaxCompliance(false);
dsConfig.setEnforceSingleStructuralObjectClass(false); dsConfig.setEnforceSingleStructuralObjectClass(false);
dsConfig.setListenerConfigs(new InMemoryListenerConfig("myListener", null, PORT, null, null, null)); dsConfig.setListenerConfigs(new InMemoryListenerConfig("myListener", null, PORT, null, null, null));
dsConfig.addAdditionalBindCredentials(CN + "=" + USERNAME + "," + BASE_PEOPLE_DN, PASSWORD); dsConfig.addAdditionalBindCredentials(CN + "=" + GOOD_USERNAME + "," + BASE_PEOPLE_DN, PASSWORD);
dsConfig.addAdditionalBindCredentials(CN + "=" + USERNAME2 + "," + BASE_PEOPLE_DN, PASSWORD); dsConfig.addAdditionalBindCredentials(CN + "=" + GOOD_USERNAME2 + "," + BASE_PEOPLE_DN, PASSWORD);
this.ds = new InMemoryDirectoryServer(dsConfig); this.ds = new InMemoryDirectoryServer(dsConfig);
this.ds.add("dn: " + BASE_DN, "objectClass: organizationalUnit", "objectClass: top"); this.ds.add("dn: " + BASE_DN, "objectClass: organizationalUnit", "objectClass: top");
this.ds.add("dn: " + BASE_PEOPLE_DN, "objectClass: organizationalUnit"); this.ds.add("dn: " + BASE_PEOPLE_DN, "objectClass: organizationalUnit");
this.ds.add("dn: " + CN + "=" + USERNAME + "," + BASE_PEOPLE_DN, CN + ": " + USERNAME, SN + ": " this.ds.add("dn: " + CN + "=" + GOOD_USERNAME + "," + BASE_PEOPLE_DN, CN + ": " + GOOD_USERNAME, SN + ": "
+ FIRSTNAME, "objectClass: person"); + FIRSTNAME_VALUE, "objectClass: person");
this.ds.add("dn: " + CN + "=" + USERNAME2 + "," + BASE_PEOPLE_DN, ROLE + ": " + ROLE1, ROLE + ": " + ROLE2, this.ds.add("dn: " + CN + "=" + GOOD_USERNAME2 + "," + BASE_PEOPLE_DN, ROLE + ": " + ROLE1, ROLE + ": " + ROLE2,
"objectClass: person"); "objectClass: person");


//Debug.setEnabled(true); //Debug.setEnabled(true);
Expand Down
78 changes: 78 additions & 0 deletions pac4j-mongo/pom.xml
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2012 - 2015 pac4j organization
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.pac4j</groupId>
<artifactId>pac4j</artifactId>
<version>1.8.0-SNAPSHOT</version>
</parent>

<artifactId>pac4j-mongo</artifactId>
<packaging>jar</packaging>
<name>pac4j for MongoDB</name>

<dependencies>
<dependency>
<groupId>org.pac4j</groupId>
<artifactId>pac4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.pac4j</groupId>
<artifactId>pac4j-http</artifactId>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
</dependency>
<!-- for testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.pac4j</groupId>
<artifactId>pac4j-core</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
</dependency>
<!-- for testing -->
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
<Bundle-SymbolicName>org.pac4j.mongo</Bundle-SymbolicName>
<Export-Package>org.pac4j.mongo.*;version=${project.version}</Export-Package>
<Import-Package>*</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>

</project>

0 comments on commit fe5e972

Please sign in to comment.