Search in sources :

Example 21 with DefaultAuthenticationHandlerExecutionResult

use of org.apereo.cas.authentication.DefaultAuthenticationHandlerExecutionResult in project cas by apereo.

the class CasKryoTranscoderTests method verifyEncodeDecodeTGTImpl.

@Test
public void verifyEncodeDecodeTGTImpl() {
    val userPassCredential = new UsernamePasswordCredential(USERNAME, PASSWORD);
    val bldr = new DefaultAuthenticationBuilder(PrincipalFactoryUtils.newPrincipalFactory().createPrincipal("user", new HashMap<>(this.principalAttributes)));
    bldr.setAttributes(new HashMap<>(this.principalAttributes));
    bldr.setAuthenticationDate(ZonedDateTime.now(ZoneId.systemDefault()));
    bldr.addCredential(new BasicCredentialMetaData(userPassCredential));
    bldr.addFailure("error", new AccountNotFoundException());
    bldr.addSuccess("authn", new DefaultAuthenticationHandlerExecutionResult(new AcceptUsersAuthenticationHandler(StringUtils.EMPTY), new BasicCredentialMetaData(userPassCredential)));
    val authentication = bldr.build();
    val expectedTGT = new TicketGrantingTicketImpl(TGT_ID, RegisteredServiceTestUtils.getService(), null, authentication, NeverExpiresExpirationPolicy.INSTANCE);
    val serviceTicket = (ProxyGrantingTicketIssuerTicket) expectedTGT.grantServiceTicket(ST_ID, RegisteredServiceTestUtils.getService(), NeverExpiresExpirationPolicy.INSTANCE, false, true);
    var encoded = transcoder.encode(expectedTGT);
    var decoded = transcoder.decode(encoded);
    assertEquals(expectedTGT, decoded);
    encoded = transcoder.encode(serviceTicket);
    decoded = transcoder.decode(encoded);
    assertEquals(serviceTicket, decoded);
    decoded = transcoder.decode(encoded);
    assertEquals(serviceTicket, decoded);
    val pgt = serviceTicket.grantProxyGrantingTicket(PGT_ID, authentication, new HardTimeoutExpirationPolicy(100));
    encoded = transcoder.encode(pgt);
    decoded = transcoder.decode(encoded);
    assertEquals(pgt, decoded);
    val pt = pgt.grantProxyTicket(PT_ID, RegisteredServiceTestUtils.getService(), new HardTimeoutExpirationPolicy(100), true);
    encoded = transcoder.encode(pt);
    decoded = transcoder.decode(encoded);
    assertEquals(pt, decoded);
}
Also used : lombok.val(lombok.val) DefaultAuthenticationBuilder(org.apereo.cas.authentication.DefaultAuthenticationBuilder) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HardTimeoutExpirationPolicy(org.apereo.cas.ticket.expiration.HardTimeoutExpirationPolicy) DefaultAuthenticationHandlerExecutionResult(org.apereo.cas.authentication.DefaultAuthenticationHandlerExecutionResult) ProxyGrantingTicketIssuerTicket(org.apereo.cas.ticket.ProxyGrantingTicketIssuerTicket) TicketGrantingTicketImpl(org.apereo.cas.ticket.TicketGrantingTicketImpl) AcceptUsersAuthenticationHandler(org.apereo.cas.authentication.AcceptUsersAuthenticationHandler) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) AccountNotFoundException(javax.security.auth.login.AccountNotFoundException) BasicCredentialMetaData(org.apereo.cas.authentication.metadata.BasicCredentialMetaData) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 22 with DefaultAuthenticationHandlerExecutionResult

use of org.apereo.cas.authentication.DefaultAuthenticationHandlerExecutionResult in project cas by apereo.

the class SimpleTestUsernamePasswordAuthenticationHandler method authenticateUsernamePasswordInternal.

@Override
protected AuthenticationHandlerExecutionResult authenticateUsernamePasswordInternal(final UsernamePasswordCredential credential, final String originalPassword) throws GeneralSecurityException, PreventedException {
    val username = credential.getUsername();
    val password = credential.getPassword();
    val exception = this.usernameErrorMap.get(username);
    if (exception instanceof GeneralSecurityException) {
        throw (GeneralSecurityException) exception;
    }
    if (exception instanceof PreventedException) {
        throw (PreventedException) exception;
    }
    if (exception instanceof RuntimeException) {
        throw (RuntimeException) exception;
    }
    if (exception != null) {
        LOGGER.debug("Cannot throw checked exception [{}] since it is not declared by method signature.", exception.getClass().getName(), exception);
    }
    if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password) && (username.equals(password) || password.equals(StringUtils.reverse(username)))) {
        LOGGER.debug("User [{}] was successfully authenticated.", username);
        return new DefaultAuthenticationHandlerExecutionResult(this, new BasicCredentialMetaData(credential), this.principalFactory.createPrincipal(username), this.warnings);
    }
    LOGGER.debug("User [{}] failed authentication", username);
    throw new FailedLoginException();
}
Also used : lombok.val(lombok.val) FailedLoginException(javax.security.auth.login.FailedLoginException) GeneralSecurityException(java.security.GeneralSecurityException) PreventedException(org.apereo.cas.authentication.PreventedException) DefaultAuthenticationHandlerExecutionResult(org.apereo.cas.authentication.DefaultAuthenticationHandlerExecutionResult) BasicCredentialMetaData(org.apereo.cas.authentication.metadata.BasicCredentialMetaData)

Example 23 with DefaultAuthenticationHandlerExecutionResult

use of org.apereo.cas.authentication.DefaultAuthenticationHandlerExecutionResult in project cas by apereo.

the class TestOneTimePasswordAuthenticationHandler method authenticate.

@Override
public AuthenticationHandlerExecutionResult authenticate(final Credential credential) throws GeneralSecurityException {
    val otp = (OneTimePasswordCredential) credential;
    val valueOnRecord = credentialMap.get(otp.getId());
    if (otp.getPassword().equals(valueOnRecord)) {
        return new DefaultAuthenticationHandlerExecutionResult(this, new BasicCredentialMetaData(otp), getPrincipalFactory().createPrincipal(otp.getId()));
    }
    throw new FailedLoginException();
}
Also used : lombok.val(lombok.val) FailedLoginException(javax.security.auth.login.FailedLoginException) OneTimePasswordCredential(org.apereo.cas.authentication.credential.OneTimePasswordCredential) DefaultAuthenticationHandlerExecutionResult(org.apereo.cas.authentication.DefaultAuthenticationHandlerExecutionResult) BasicCredentialMetaData(org.apereo.cas.authentication.metadata.BasicCredentialMetaData)

Aggregations

DefaultAuthenticationHandlerExecutionResult (org.apereo.cas.authentication.DefaultAuthenticationHandlerExecutionResult)23 lombok.val (lombok.val)17 BasicCredentialMetaData (org.apereo.cas.authentication.metadata.BasicCredentialMetaData)12 FailedLoginException (javax.security.auth.login.FailedLoginException)8 BasicCredentialMetaData (org.apereo.cas.authentication.BasicCredentialMetaData)6 CredentialMetaData (org.apereo.cas.authentication.CredentialMetaData)6 DefaultAuthenticationBuilder (org.apereo.cas.authentication.DefaultAuthenticationBuilder)6 HashMap (java.util.HashMap)4 AuthenticationHandlerExecutionResult (org.apereo.cas.authentication.AuthenticationHandlerExecutionResult)4 UsernamePasswordCredential (org.apereo.cas.authentication.credential.UsernamePasswordCredential)4 SimpleTestUsernamePasswordAuthenticationHandler (org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler)4 BasicIdentifiableCredential (org.apereo.cas.authentication.BasicIdentifiableCredential)3 BasicIdentifiableCredential (org.apereo.cas.authentication.credential.BasicIdentifiableCredential)3 GeneralSecurityException (java.security.GeneralSecurityException)2 X509CertificateCredential (org.apereo.cas.adaptors.x509.authentication.principal.X509CertificateCredential)2 AuditableExecutionResult (org.apereo.cas.audit.AuditableExecutionResult)2 Authentication (org.apereo.cas.authentication.Authentication)2 AuthenticationBuilder (org.apereo.cas.authentication.AuthenticationBuilder)2 AuthenticationHandler (org.apereo.cas.authentication.AuthenticationHandler)2 DefaultAuthenticationServiceSelectionPlan (org.apereo.cas.authentication.DefaultAuthenticationServiceSelectionPlan)2