diff --git a/account/pom.xml b/account/pom.xml index 48d4501..26b7c09 100644 --- a/account/pom.xml +++ b/account/pom.xml @@ -22,10 +22,10 @@ - - org.apache.commons - commons-lang3 - + + org.apache.commons + commons-lang3 + org.freemarker freemarker @@ -55,6 +55,16 @@ mockito-core test + + org.slf4j + slf4j-log4j12 + test + + + org.slf4j + jcl-over-slf4j + test + javax javaee-api diff --git a/account/src/main/java/de/muehlencord/shared/account/business/ConfigService.java b/account/src/main/java/de/muehlencord/shared/account/business/ConfigService.java index 017bf04..7ccbb81 100644 --- a/account/src/main/java/de/muehlencord/shared/account/business/ConfigService.java +++ b/account/src/main/java/de/muehlencord/shared/account/business/ConfigService.java @@ -1,6 +1,7 @@ package de.muehlencord.shared.account.business; import de.muehlencord.shared.account.entity.ConfigEntity; +import java.io.Serializable; import javax.annotation.PostConstruct; import javax.ejb.Singleton; import javax.ejb.Startup; @@ -13,7 +14,9 @@ import javax.persistence.PersistenceContext; */ @Singleton @Startup -public class ConfigService { +public class ConfigService implements Serializable { + + private static final long serialVersionUID = -3195224653632853003L; @PersistenceContext EntityManager em; @@ -32,10 +35,10 @@ public class ConfigService { this.maxFailedLogins = Integer.parseInt(configEntity.getConfigValue()); } } - - public String getConfigValue (String configKey) { - ConfigEntity configEntity = em.find(ConfigEntity.class, configKey); - return (configEntity == null ? null : configEntity.getConfigValue()); + + public String getConfigValue(String configKey) { + ConfigEntity configEntity = em.find(ConfigEntity.class, configKey); + return (configEntity == null ? null : configEntity.getConfigValue()); } /* *** getter *** */ diff --git a/account/src/main/java/de/muehlencord/shared/account/util/UserNameActiveDirectoryRealm.java b/account/src/main/java/de/muehlencord/shared/account/util/UserNameActiveDirectoryRealm.java index feeeb55..8a28932 100644 --- a/account/src/main/java/de/muehlencord/shared/account/util/UserNameActiveDirectoryRealm.java +++ b/account/src/main/java/de/muehlencord/shared/account/util/UserNameActiveDirectoryRealm.java @@ -14,21 +14,23 @@ import org.apache.shiro.realm.ldap.LdapUtils; * @author joern.muehlencord */ public class UserNameActiveDirectoryRealm extends ActiveDirectoryRealm { - + @Override protected AuthenticationInfo queryForAuthenticationInfo(AuthenticationToken token, LdapContextFactory ldapContextFactory) throws NamingException { - UsernamePasswordToken upToken = (UsernamePasswordToken) token; // Binds using the username and password provided by the user. LdapContext ctx = null; String userName = upToken.getUsername(); - try { + try { if (principalSuffix != null) { - if (!userName.contains(principalSuffix)) + if (!userName.contains(principalSuffix)) { userName += principalSuffix; + } } - - ctx = ldapContextFactory.getLdapContext(userName, String.valueOf(upToken.getPassword())); + + // use system context (system user / password) to connect to server + // ctx = ldapContextFactory.getSystemLdapContext(); + ctx = ldapContextFactory.getLdapContext(userName, upToken.getCredentials()); } finally { LdapUtils.closeContext(ctx); } diff --git a/account/src/test/java/de/muehlencord/shared/account/util/UserNameActiveDirectoryRealmTest.java b/account/src/test/java/de/muehlencord/shared/account/util/UserNameActiveDirectoryRealmTest.java new file mode 100644 index 0000000..8f84201 --- /dev/null +++ b/account/src/test/java/de/muehlencord/shared/account/util/UserNameActiveDirectoryRealmTest.java @@ -0,0 +1,53 @@ +package de.muehlencord.shared.account.util; + +import org.apache.shiro.SecurityUtils; +import org.apache.shiro.authc.AuthenticationException; +import org.apache.shiro.authc.UsernamePasswordToken; +import org.apache.shiro.config.IniSecurityManagerFactory; +import org.apache.shiro.subject.Subject; +import org.junit.Test; +import org.apache.shiro.mgt.SecurityManager; +import static org.junit.Assume.assumeNotNull; + +/** + * + * @author Joern Muehlencord + */ +public class UserNameActiveDirectoryRealmTest { + + @Test + public void testUsernameLogin() { + String userName = "user.name"; + String password = "secret"; + testLogin(userName, password); + } + + @Test + public void testEmailaddressLogin() { + String userName = "user.name@domain.com"; + String password = "secret"; + testLogin(userName, password); + } + + @Test(expected=AuthenticationException.class) + public void testWrongUserNamePassword() { + String userName = "test123"; + String password = "secret"; + testLogin(userName, password); + } + + private void testLogin(String userName, String password) throws AuthenticationException { + assumeNotNull(UserNameActiveDirectoryRealmTest.class.getResource("/shiro.ini")); + + IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro.ini"); + SecurityManager securityManager = factory.getInstance(); + SecurityUtils.setSecurityManager(securityManager); + + UsernamePasswordToken token = new UsernamePasswordToken(userName, password); + Subject currentUser = SecurityUtils.getSubject(); + + currentUser.login(token); + System.out.println("Logged in"); + } + +} diff --git a/account/src/test/resources/log4j.xml b/account/src/test/resources/log4j.xml new file mode 100644 index 0000000..9386761 --- /dev/null +++ b/account/src/test/resources/log4j.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/account/src/test/resources/shiro.ini.dist b/account/src/test/resources/shiro.ini.dist new file mode 100644 index 0000000..f67381e --- /dev/null +++ b/account/src/test/resources/shiro.ini.dist @@ -0,0 +1,30 @@ +[main] +contextFactory = org.apache.shiro.realm.ldap.JndiLdapContextFactory +contextFactory.url = ldaps://ldap.domain.com:636 +contextFactory.systemUsername = user.name@domain.com +contextFactory.systemPassword = secret +contextFactory.environment[java.naming.security.protocol] = ssl + +cacheManager = org.apache.shiro.cache.MemoryConstrainedCacheManager +securityManager.cacheManager = $cacheManager + +# HashService +hashService = org.apache.shiro.crypto.hash.DefaultHashService +hashService.hashIterations = 500000 +hashService.hashAlgorithmName = SHA-512 +hashService.generatePublicSalt = true + +# Password service +passwordService = org.apache.shiro.authc.credential.DefaultPasswordService +passwordService.hashService = $hashService + +# LDAP Realm setup +ldapRealm = de.muehlencord.shared.account.util.UserNameActiveDirectoryRealm +ldapRealm.principalSuffix = @domain.com +ldapRealm.ldapContextFactory = $contextFactory +ldapRealm.searchBase = dc=domain,dc=com + +# LDAP (authentication) activation +authcStrategy = org.apache.shiro.authc.pam.AllSuccessfulStrategy +securityManager.realms = $ldapRealm +securityManager.authenticator.authenticationStrategy = $authcStrategy