use of org.apereo.cas.authentication.credential.OneTimePasswordCredential in project cas by apereo.
the class PasswordlessTokenAuthenticationHandlerTests method verifyAction.
@Test
public void verifyAction() throws Exception {
val repository = new InMemoryPasswordlessTokenRepository(60);
repository.saveToken("casuser", "123456");
val h = new PasswordlessTokenAuthenticationHandler(null, mock(ServicesManager.class), PrincipalFactoryUtils.newPrincipalFactory(), 0, repository);
val c = new OneTimePasswordCredential("casuser", "123456");
assertNotNull(h.authenticate(c));
assertThrows(FailedLoginException.class, () -> h.authenticate(new OneTimePasswordCredential("1", "2")));
assertTrue(h.supports(c));
assertTrue(h.supports(c.getCredentialClass()));
assertFalse(h.supports(new UsernamePasswordCredential()));
}
use of org.apereo.cas.authentication.credential.OneTimePasswordCredential in project cas by apereo.
the class AcceptPasswordlessAuthenticationAction method doExecute.
@Override
protected Event doExecute(final RequestContext requestContext) {
val principal = WebUtils.getPasswordlessAuthenticationAccount(requestContext, PasswordlessUserAccount.class);
try {
val token = requestContext.getRequestParameters().getRequired("token");
val currentToken = passwordlessTokenRepository.findToken(principal.getUsername());
if (currentToken.isPresent() && token.equalsIgnoreCase(currentToken.get())) {
val credential = new OneTimePasswordCredential(principal.getUsername(), token);
val service = WebUtils.getService(requestContext);
val authenticationResult = authenticationSystemSupport.finalizeAuthenticationTransaction(service, credential);
WebUtils.putAuthenticationResult(authenticationResult, requestContext);
WebUtils.putAuthentication(authenticationResult.getAuthentication(), requestContext);
WebUtils.putCredential(requestContext, credential);
val finalEvent = super.doExecute(requestContext);
passwordlessTokenRepository.deleteToken(principal.getUsername(), currentToken.get());
return finalEvent;
}
} catch (final Exception e) {
LoggingUtils.error(LOGGER, e);
val attributes = new LocalAttributeMap<>();
attributes.put("error", e);
var account = principal != null ? passwordlessUserAccountStore.findUser(principal.getUsername()) : Optional.empty();
if (account.isPresent()) {
attributes.put("passwordlessAccount", account.get());
return new EventFactorySupport().event(this, CasWebflowConstants.TRANSITION_ID_AUTHENTICATION_FAILURE, attributes);
}
}
LOGGER.error("Unable to locate token for user [{}]", principal.getUsername());
val attributes = new LocalAttributeMap<>();
attributes.put("error", new AuthenticationException("Invalid token"));
return new EventFactorySupport().event(this, CasWebflowConstants.TRANSITION_ID_AUTHENTICATION_FAILURE, attributes);
}
use of org.apereo.cas.authentication.credential.OneTimePasswordCredential in project cas by apereo.
the class OneTimePasswordCredentialTests method verifySerializeAnOneTimePasswordCredentialToJson.
@Test
public void verifySerializeAnOneTimePasswordCredentialToJson() throws Exception {
val credentialWritten = new OneTimePasswordCredential("id", "password");
MAPPER.writeValue(JSON_FILE, credentialWritten);
val credentialRead = MAPPER.readValue(JSON_FILE, OneTimePasswordCredential.class);
assertEquals(credentialWritten, credentialRead);
}
use of org.apereo.cas.authentication.credential.OneTimePasswordCredential in project cas by apereo.
the class MultifactorAuthenticationTests method verifyAllowsAccessToNormalSecurityServiceWithOTP.
@Test
public void verifyAllowsAccessToNormalSecurityServiceWithOTP() {
val ctx = processAuthenticationAttempt(NORMAL_SERVICE, new OneTimePasswordCredential(ALICE, PASSWORD_31415));
val tgt = cas.createTicketGrantingTicket(ctx);
assertNotNull(tgt);
val st = cas.grantServiceTicket(tgt.getId(), NORMAL_SERVICE, ctx);
assertNotNull(st);
}
use of org.apereo.cas.authentication.credential.OneTimePasswordCredential in project cas by apereo.
the class MultifactorAuthenticationTests method verifyDeniesAccessToHighSecurityServiceWithOTP.
@Test
public void verifyDeniesAccessToHighSecurityServiceWithOTP() {
val ctx = processAuthenticationAttempt(HIGH_SERVICE, new OneTimePasswordCredential(ALICE, PASSWORD_31415));
val tgt = cas.createTicketGrantingTicket(ctx);
assertNotNull(tgt);
assertThrows(UnsatisfiedAuthenticationPolicyException.class, () -> cas.grantServiceTicket(tgt.getId(), HIGH_SERVICE, ctx));
}
Aggregations