LdapRealm extends CachingRealm { @Override public Account authenticate(StandardCredentials requestCredentials) { final String username = getSimpleUsername(requestCredentials.getUsername()); final String password = requestCredentials.getPassword(); if (hasAccount(username)) { return super.authenticate(new StandardCredentials(username, password)); } return authenticate(username, password); }  static final String escapeLDAPSearchFilter(String filter); @Override void setup(Config config); @Override void start(); @Override void stop(); @Override Account authenticate(StandardCredentials requestCredentials); @Override Account authenticate(final String username, final String password);  }
@Test public void testSimpleName() { Account userOne = ldap.authenticate("UserOne", "userOnePassword"); assertNotNull(userOne); assertEquals("User One", userOne.getName()); }
@Test public void testComplexName() { Map<String, Object> settings = getSettings(); settings.put("nameMapping", "${personalTitle}. ${givenName} ${surname}"); ldap = newRealm(settings); Account userOne = ldap.authenticate("UserOne", "userOnePassword"); assertNotNull(userOne); assertEquals("Mr. User One", userOne.getName()); }
@Test public void testSimpleEmail() { Account userOne = ldap.authenticate("UserOne", "userOnePassword"); assertNotNull(userOne); assertTrue(userOne.getEmailAddresses().contains("userone@gitblit.com")); }
@Test public void testComplexEmail() { Map<String, Object> settings = getSettings(); settings.put("emailMapping", "${givenName}.${surname}@gitblit.com"); ldap = newRealm(settings); Account userOne = ldap.authenticate("UserOne", "userOnePassword"); assertNotNull(userOne); assertTrue(userOne.getEmailAddresses().contains("User.One@gitblit.com")); }
@Test public void testLdapInjection() { Account userOneModel = ldap.authenticate("*)(userPassword=userOnePassword", "userOnePassword"); assertNull(userOneModel); }
@Test public void testBindWithUser() { Map<String, Object> settings = getSettings(); settings.put("bindPattern", "CN=${username},OU=US,OU=Users,OU=UserControl,OU=MyOrganization,DC=MyDomain"); settings.put("username", ""); settings.put("password", ""); ldap = newRealm(settings); Account userOne = ldap.authenticate("UserOne", "userOnePassword"); assertNotNull(userOne); Account userOneFailedAuth = ldap.authenticate("UserOne", "userTwoPassword"); assertNull(userOneFailedAuth); }
@Test public void testAuthenticate() { Account userOne = ldap.authenticate("UserOne", "userOnePassword"); assertNotNull(userOne); assertTrue("UserOne is missing the 'Git_Admins' role!", userOne.hasRole("Git_Admins")); assertTrue("UserOne is missing the 'Git_Users' role!", userOne.hasRole("Git_Users")); assertTrue("UserOne should be an administrator!", userOne.isAdministrator()); Account userOneAuthenticationFailed = ldap.authenticate("UserOne", "userTwoPassword"); assertNull(userOneAuthenticationFailed); Account userTwo = ldap.authenticate("UserTwo", "userTwoPassword"); assertNotNull(userTwo); assertTrue("UserTwo is missing the 'Git_Users' role!", userTwo.hasRole("Git_Users")); assertFalse("UserTwo has the 'Git_Admins' role!", userTwo.hasRole("Git_Admins")); assertTrue("UserTwo is missing the 'Git Admins' role!", userTwo.hasRole("Git Admins")); assertTrue("UserTwo should be an administrator!", userTwo.isAdministrator()); Account userThree = ldap.authenticate("UserThree", "userThreePassword"); assertNotNull(userThree); assertTrue("UserThree is missing the 'Git_Users' role!", userThree.hasRole("Git_Users")); assertFalse("UserThree has the 'Git_Admins role!", userThree.hasRole("Git_Admins")); assertTrue("UserThree should be an administrator!", userThree.isAdministrator()); }