use of org.apereo.cas.authentication.support.password.PasswordPolicyContext in project cas by apereo.
the class OktaAuthenticationStateHandlerAdapterTests method handleUnauthenticated.
@Test
public void handleUnauthenticated() {
val adapter = new OktaAuthenticationStateHandlerAdapter(new DefaultPasswordPolicyHandlingStrategy<>(), new PasswordPolicyContext());
val response = mock(AuthenticationResponse.class);
when(response.getStatusString()).thenReturn("error");
adapter.handleUnauthenticated(response);
assertThrows(FailedLoginException.class, adapter::throwExceptionIfNecessary);
}
use of org.apereo.cas.authentication.support.password.PasswordPolicyContext in project cas by apereo.
the class OktaAuthenticationStateHandlerAdapterTests method handleUnknownPasswordPolicy.
@Test
public void handleUnknownPasswordPolicy() throws Exception {
val strategy = mock(AuthenticationPasswordPolicyHandlingStrategy.class);
when(strategy.supports(any())).thenReturn(Boolean.TRUE);
when(strategy.handle(any(), any())).thenThrow(new RuntimeException());
val adapter = new OktaAuthenticationStateHandlerAdapter(strategy, new PasswordPolicyContext());
val response = mock(AuthenticationResponse.class);
when(response.getSessionToken()).thenReturn("token");
adapter.handlePasswordWarning(response);
assertThrows(AccountNotFoundException.class, adapter::throwExceptionIfNecessary);
assertTrue(adapter.getWarnings().isEmpty());
}
use of org.apereo.cas.authentication.support.password.PasswordPolicyContext in project cas by apereo.
the class OktaAuthenticationStateHandlerAdapterTests method handlePasswordExpired.
@Test
public void handlePasswordExpired() {
val adapter = new OktaAuthenticationStateHandlerAdapter(new DefaultPasswordPolicyHandlingStrategy<>(), new PasswordPolicyContext());
val response = mock(AuthenticationResponse.class);
when(response.getStatusString()).thenReturn("error");
adapter.handlePasswordExpired(response);
assertThrows(AccountExpiredException.class, adapter::throwExceptionIfNecessary);
}
use of org.apereo.cas.authentication.support.password.PasswordPolicyContext in project cas by apereo.
the class OktaAuthenticationStateHandlerAdapterTests method handleSuccess.
@Test
public void handleSuccess() {
val adapter = new OktaAuthenticationStateHandlerAdapter(new DefaultPasswordPolicyHandlingStrategy<>(), new PasswordPolicyContext());
val response = mock(AuthenticationResponse.class);
when(response.getSessionToken()).thenReturn("token");
when(response.getStatusString()).thenReturn("error");
val user = mock(User.class);
when(user.getLogin()).thenReturn("cas");
when(user.getId()).thenReturn("cas-id");
when(user.getProfile()).thenReturn(Map.of("name", "something", "lastName", "something-else"));
when(response.getUser()).thenReturn(user);
adapter.handleSuccess(response);
assertDoesNotThrow(adapter::throwExceptionIfNecessary);
assertEquals("cas", adapter.getUsername());
assertFalse(adapter.getUserAttributes().isEmpty());
}
use of org.apereo.cas.authentication.support.password.PasswordPolicyContext in project cas by apereo.
the class LdapUtils method createLdapPasswordPolicyConfiguration.
/**
* Create ldap password policy configuration.
*
* @param passwordPolicy the password policy
* @param authenticator the authenticator
* @param attributes the attributes
* @return the password policy context
*/
public static PasswordPolicyContext createLdapPasswordPolicyConfiguration(final LdapPasswordPolicyProperties passwordPolicy, final Authenticator authenticator, final Multimap<String, Object> attributes) {
val cfg = new PasswordPolicyContext(passwordPolicy);
val requestHandlers = new HashSet<>();
val responseHandlers = new HashSet<>();
val customPolicyClass = passwordPolicy.getCustomPolicyClass();
if (StringUtils.isNotBlank(customPolicyClass)) {
try {
LOGGER.debug("Configuration indicates use of a custom password policy handler [{}]", customPolicyClass);
val clazz = (Class<AuthenticationResponseHandler>) Class.forName(customPolicyClass);
responseHandlers.add(clazz.getDeclaredConstructor().newInstance());
} catch (final Exception e) {
LoggingUtils.warn(LOGGER, "Unable to construct an instance of the password policy handler", e);
}
}
LOGGER.debug("Password policy authentication response handler is set to accommodate directory type: [{}]", passwordPolicy.getType());
switch(passwordPolicy.getType()) {
case AD:
responseHandlers.add(new ActiveDirectoryAuthenticationResponseHandler(Period.ofDays(cfg.getPasswordWarningNumberOfDays())));
Arrays.stream(ActiveDirectoryAuthenticationResponseHandler.ATTRIBUTES).forEach(a -> {
LOGGER.debug("Configuring authentication to retrieve password policy attribute [{}]", a);
attributes.put(a, a);
});
break;
case FreeIPA:
Arrays.stream(FreeIPAAuthenticationResponseHandler.ATTRIBUTES).forEach(a -> {
LOGGER.debug("Configuring authentication to retrieve password policy attribute [{}]", a);
attributes.put(a, a);
});
responseHandlers.add(new FreeIPAAuthenticationResponseHandler(Period.ofDays(cfg.getPasswordWarningNumberOfDays()), cfg.getLoginFailures()));
break;
case EDirectory:
Arrays.stream(EDirectoryAuthenticationResponseHandler.ATTRIBUTES).forEach(a -> {
LOGGER.debug("Configuring authentication to retrieve password policy attribute [{}]", a);
attributes.put(a, a);
});
responseHandlers.add(new EDirectoryAuthenticationResponseHandler(Period.ofDays(cfg.getPasswordWarningNumberOfDays())));
break;
default:
requestHandlers.add(new PasswordPolicyAuthenticationRequestHandler());
responseHandlers.add(new PasswordPolicyAuthenticationResponseHandler());
responseHandlers.add(new PasswordExpirationAuthenticationResponseHandler());
break;
}
if (!requestHandlers.isEmpty()) {
authenticator.setRequestHandlers(requestHandlers.toArray(AuthenticationRequestHandler[]::new));
}
authenticator.setResponseHandlers(responseHandlers.toArray(AuthenticationResponseHandler[]::new));
LOGGER.debug("LDAP authentication response handlers configured are: [{}]", responseHandlers);
if (!passwordPolicy.isAccountStateHandlingEnabled()) {
cfg.setAccountStateHandler((response, configuration) -> new ArrayList<>(0));
LOGGER.trace("Handling LDAP account states is disabled via CAS configuration");
} else if (StringUtils.isNotBlank(passwordPolicy.getWarningAttributeName()) && StringUtils.isNotBlank(passwordPolicy.getWarningAttributeValue())) {
val accountHandler = new OptionalWarningLdapAccountStateHandler();
accountHandler.setDisplayWarningOnMatch(passwordPolicy.isDisplayWarningOnMatch());
accountHandler.setWarnAttributeName(passwordPolicy.getWarningAttributeName());
accountHandler.setWarningAttributeValue(passwordPolicy.getWarningAttributeValue());
accountHandler.setAttributesToErrorMap(passwordPolicy.getPolicyAttributes());
cfg.setAccountStateHandler(accountHandler);
LOGGER.debug("Configuring an warning account state handler for LDAP authentication for warning attribute [{}] and value [{}]", passwordPolicy.getWarningAttributeName(), passwordPolicy.getWarningAttributeValue());
} else {
val accountHandler = new DefaultLdapAccountStateHandler();
accountHandler.setAttributesToErrorMap(passwordPolicy.getPolicyAttributes());
cfg.setAccountStateHandler(accountHandler);
LOGGER.debug("Configuring the default account state handler for LDAP authentication");
}
return cfg;
}
Aggregations