use of org.apache.shiro.authc.SimpleAccount in project ART-TIME by Artezio.
the class AdminRealmTest method testdoGetAuthenticationInfo.
@Test
public void testdoGetAuthenticationInfo() {
Settings settings = new Settings(new EnumMap<>(Setting.Name.class));
settings.setAdminUsername("admin");
AuthenticationToken token = new UsernamePasswordToken("admin", new char[] {});
PowerMock.mockStatic(CDIUtils.class);
expect(CDIUtils.getBean(SettingsService.class)).andReturn(settingsService);
expect(settingsService.getSettings()).andReturn(settings);
PowerMock.replayAll(CDIUtils.class, settingsService);
AuthenticationInfo actual = adminRealm.doGetAuthenticationInfo(token);
PowerMock.verifyAll();
assertNotNull(actual);
assertTrue(actual instanceof SimpleAccount);
}
use of org.apache.shiro.authc.SimpleAccount in project ART-TIME by Artezio.
the class AdminRealmTest method testDoGetAuthorizationInfo.
@Test
public void testDoGetAuthorizationInfo() {
Settings settings = new Settings(new EnumMap<>(Setting.Name.class));
settings.setAdminUsername("admin");
PrincipalCollection principalCollection = new SimplePrincipalCollection("admin", "realm");
PowerMock.mockStatic(CDIUtils.class);
expect(CDIUtils.getBean(SettingsService.class)).andReturn(settingsService);
expect(settingsService.getSettings()).andReturn(settings);
PowerMock.replayAll(CDIUtils.class, settingsService);
AuthorizationInfo actual = adminRealm.doGetAuthorizationInfo(principalCollection);
PowerMock.verifyAll();
assertNotNull(actual);
assertTrue(actual instanceof SimpleAccount);
}
use of org.apache.shiro.authc.SimpleAccount in project ART-TIME by Artezio.
the class AdminRealmTest method testGetAccount_ifLoggedUserIsAdmin.
@Test
public void testGetAccount_ifLoggedUserIsAdmin() {
Settings settings = new Settings(new EnumMap<>(Setting.Name.class));
settings.setAdminUsername("admin");
SimpleAccount actual = adminRealm.getAccount("admin", settings);
assertNotNull(actual);
}
use of org.apache.shiro.authc.SimpleAccount in project graylog2-server by Graylog2.
the class BearerTokenRealm method toAuthenticationInfo.
private AuthenticationInfo toAuthenticationInfo(AuthServiceResult result) {
String realmName = NAME + "/" + result.backendType();
@SuppressWarnings("ConstantConditions") final SimplePrincipalCollection principals = new SimplePrincipalCollection(ImmutableList.of(result.userProfileId(), result.sessionAttributes()), realmName);
return new SimpleAccount(principals, null, realmName);
}
use of org.apache.shiro.authc.SimpleAccount in project graylog2-server by Graylog2.
the class LdapUserAuthenticator method doGetAuthenticationInfo.
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authtoken) throws AuthenticationException {
// safe, we only handle this type
final UsernamePasswordToken token = (UsernamePasswordToken) authtoken;
final LdapSettings ldapSettings = ldapSettingsService.load();
if (ldapSettings == null || !ldapSettings.isEnabled()) {
LOG.trace("LDAP is disabled, skipping");
return null;
}
final LdapConnectionConfig config = new LdapConnectionConfig();
config.setLdapHost(ldapSettings.getUri().getHost());
config.setLdapPort(ldapSettings.getUri().getPort());
config.setUseSsl(ldapSettings.getUri().getScheme().startsWith("ldaps"));
config.setUseTls(ldapSettings.isUseStartTls());
if (ldapSettings.isTrustAllCertificates()) {
config.setTrustManagers(new TrustAllX509TrustManager());
}
config.setName(ldapSettings.getSystemUserName());
config.setCredentials(ldapSettings.getSystemPassword());
final String principal = (String) token.getPrincipal();
final char[] tokenPassword = firstNonNull(token.getPassword(), new char[0]);
final String password = String.valueOf(tokenPassword);
// do not try to look a token up in LDAP if there is no principal or password
if (isNullOrEmpty(principal) || isNullOrEmpty(password)) {
LOG.debug("Principal or password were empty. Not trying to look up a token in LDAP.");
return null;
}
try (final LdapNetworkConnection connection = ldapConnector.connect(config)) {
if (null == connection) {
LOG.error("Couldn't connect to LDAP directory");
return null;
}
final LdapEntry userEntry = ldapConnector.search(connection, ldapSettings.getSearchBase(), ldapSettings.getSearchPattern(), ldapSettings.getDisplayNameAttribute(), principal, ldapSettings.isActiveDirectory(), ldapSettings.getGroupSearchBase(), ldapSettings.getGroupIdAttribute(), ldapSettings.getGroupSearchPattern());
if (userEntry == null) {
LOG.debug("User {} not found in LDAP", principal);
return null;
}
// needs to use the DN of the entry, not the parameter for the lookup filter we used to find the entry!
final boolean authenticated = ldapConnector.authenticate(connection, userEntry.getDn(), password);
if (!authenticated) {
LOG.info("Invalid credentials for user {} (DN {})", principal, userEntry.getDn());
return null;
}
// user found and authenticated, sync the user entry with mongodb
final User user = syncFromLdapEntry(userEntry, ldapSettings, principal);
if (user == null) {
// in case there was an error reading, creating or modifying the user in mongodb, we do not authenticate the user.
LOG.error("Unable to sync LDAP user {} (DN {})", userEntry.getBindPrincipal(), userEntry.getDn());
return null;
}
return new SimpleAccount(principal, null, "ldap realm");
} catch (LdapException e) {
LOG.error("LDAP error", e);
} catch (CursorException e) {
LOG.error("Unable to read LDAP entry", e);
} catch (Exception e) {
LOG.error("Error during LDAP user account sync. Cannot log in user {}", principal, e);
}
// Return null by default to ensure a login failure if anything goes wrong.
return null;
}
Aggregations