Search in sources :

Example 26 with Credential

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

the class KryoTranscoderTests method verifyEncodeDecodeTGTWithUnmodifiableMap.

@Test
public void verifyEncodeDecodeTGTWithUnmodifiableMap() throws Exception {
    final Credential userPassCredential = new UsernamePasswordCredential(USERNAME, PASSWORD);
    final TicketGrantingTicket expectedTGT = new MockTicketGrantingTicket(TGT_ID, userPassCredential, new HashMap<>(this.principalAttributes));
    expectedTGT.grantServiceTicket(ST_ID, null, null, false, true);
    assertEquals(expectedTGT, transcoder.decode(transcoder.encode(expectedTGT)));
}
Also used : MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) UsernamePasswordCredential(org.apereo.cas.authentication.UsernamePasswordCredential) Credential(org.apereo.cas.authentication.Credential) HttpBasedServiceCredential(org.apereo.cas.authentication.HttpBasedServiceCredential) TicketGrantingTicket(org.apereo.cas.ticket.TicketGrantingTicket) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) UsernamePasswordCredential(org.apereo.cas.authentication.UsernamePasswordCredential) Test(org.junit.Test)

Example 27 with Credential

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

the class SpnegoCredentialsAction method setResponseHeader.

/**
     * Sets the response header based on the retrieved token.
     *
     * @param context    the context
     */
private void setResponseHeader(final RequestContext context) {
    final Credential credential = WebUtils.getCredential(context);
    if (credential == null) {
        LOGGER.debug("No credential was provided. No response header set.");
        return;
    }
    final HttpServletResponse response = WebUtils.getHttpServletResponse(context);
    final SpnegoCredential spnegoCredentials = (SpnegoCredential) credential;
    final byte[] nextToken = spnegoCredentials.getNextToken();
    if (nextToken != null) {
        LOGGER.debug("Obtained output token: [{}]", new String(nextToken, Charset.defaultCharset()));
        response.setHeader(SpnegoConstants.HEADER_AUTHENTICATE, (this.ntlm ? SpnegoConstants.NTLM : SpnegoConstants.NEGOTIATE) + ' ' + EncodingUtils.encodeBase64(nextToken));
    } else {
        LOGGER.debug("Unable to obtain the output token required.");
    }
    if (spnegoCredentials.getPrincipal() == null && this.send401OnAuthenticationFailure) {
        LOGGER.debug("Setting HTTP Status to 401");
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    }
}
Also used : SpnegoCredential(org.apereo.cas.support.spnego.authentication.principal.SpnegoCredential) Credential(org.apereo.cas.authentication.Credential) SpnegoCredential(org.apereo.cas.support.spnego.authentication.principal.SpnegoCredential) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 28 with Credential

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

the class ChainingPrincipalResolver method resolve.

/**
     * {@inheritDoc}
     * Resolves a credential by delegating to each of the configured resolvers in sequence. Note that the
     * final principal is taken from the first resolved principal in the chain, yet attributes are merged.
     *
     * @param credential Authenticated credential.
     * @param principal  Authenticated principal, if any.
     * @return The principal from the last configured resolver in the chain.
     */
@Override
public Principal resolve(final Credential credential, final Principal principal, final AuthenticationHandler handler) {
    final List<Principal> principals = new ArrayList<>();
    chain.stream().filter(resolver -> resolver.supports(credential)).forEach(resolver -> {
        LOGGER.debug("Invoking principal resolver [{}]", resolver);
        final Principal p = resolver.resolve(credential, principal, handler);
        if (p != null) {
            principals.add(p);
        }
    });
    if (principals.isEmpty()) {
        LOGGER.warn("None of the principal resolvers in the chain were able to produce a principal");
        return NullPrincipal.getInstance();
    }
    final Map<String, Object> attributes = new HashMap<>();
    principals.forEach(p -> {
        if (p != null) {
            LOGGER.debug("Resolved principal [{}]", p);
            if (p.getAttributes() != null && !p.getAttributes().isEmpty()) {
                LOGGER.debug("Adding attributes [{}] for the final principal", p.getAttributes());
                attributes.putAll(p.getAttributes());
            }
        }
    });
    final long count = principals.stream().map(p -> p.getId().trim().toLowerCase()).distinct().collect(Collectors.toSet()).size();
    if (count > 1) {
        throw new PrincipalException("Resolved principals by the chain are not unique because principal resolvers have produced CAS principals " + "with different identifiers which typically is the result of a configuration issue.", Collections.emptyMap(), Collections.emptyMap());
    }
    final String principalId = principal != null ? principal.getId() : principals.iterator().next().getId();
    final Principal finalPrincipal = this.principalFactory.createPrincipal(principalId, attributes);
    LOGGER.debug("Final principal constructed by the chain of resolvers is [{}]", finalPrincipal);
    return finalPrincipal;
}
Also used : PrincipalException(org.apereo.cas.authentication.PrincipalException) Logger(org.slf4j.Logger) NullPrincipal(org.apereo.cas.authentication.principal.NullPrincipal) PrincipalResolver(org.apereo.cas.authentication.principal.PrincipalResolver) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) IPersonAttributeDao(org.apereo.services.persondir.IPersonAttributeDao) ArrayList(java.util.ArrayList) List(java.util.List) AuthenticationHandler(org.apereo.cas.authentication.AuthenticationHandler) PrincipalFactory(org.apereo.cas.authentication.principal.PrincipalFactory) ToStringBuilder(org.apache.commons.lang3.builder.ToStringBuilder) Map(java.util.Map) Principal(org.apereo.cas.authentication.principal.Principal) Credential(org.apereo.cas.authentication.Credential) Collections(java.util.Collections) DefaultPrincipalFactory(org.apereo.cas.authentication.principal.DefaultPrincipalFactory) MergingPersonAttributeDaoImpl(org.apereo.services.persondir.support.MergingPersonAttributeDaoImpl) HashMap(java.util.HashMap) PrincipalException(org.apereo.cas.authentication.PrincipalException) ArrayList(java.util.ArrayList) NullPrincipal(org.apereo.cas.authentication.principal.NullPrincipal) Principal(org.apereo.cas.authentication.principal.Principal)

Example 29 with Credential

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

the class TicketOrCredentialPrincipalResolverTests method verifyResolverServiceTicket.

@Test
public void verifyResolverServiceTicket() throws Exception {
    final Credential c = CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword();
    final AuthenticationResult ctx = CoreAuthenticationTestUtils.getAuthenticationResult(getAuthenticationSystemSupport(), c);
    final TicketGrantingTicket ticketId = getCentralAuthenticationService().createTicketGrantingTicket(ctx);
    final ServiceTicket st = getCentralAuthenticationService().grantServiceTicket(ticketId.getId(), CoreAuthenticationTestUtils.getService(), ctx);
    final TicketOrCredentialPrincipalResolver res = new TicketOrCredentialPrincipalResolver(getCentralAuthenticationService());
    final JoinPoint jp = mock(JoinPoint.class);
    when(jp.getArgs()).thenReturn(new Object[] { st.getId() });
    final String result = res.resolveFrom(jp, null);
    assertNotNull(result);
    assertEquals(result, c.getId());
}
Also used : Credential(org.apereo.cas.authentication.Credential) TicketGrantingTicket(org.apereo.cas.ticket.TicketGrantingTicket) ServiceTicket(org.apereo.cas.ticket.ServiceTicket) AuthenticationResult(org.apereo.cas.authentication.AuthenticationResult) JoinPoint(org.aspectj.lang.JoinPoint) Test(org.junit.Test)

Example 30 with Credential

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

the class TicketOrCredentialPrincipalResolver method resolveArgument.

/**
     * Resolve the join point argument.
     *
     * @param arg1 the arg
     * @return the resolved string
     */
private String resolveArgument(final Object arg1) {
    LOGGER.debug("Resolving argument [{}] for audit", arg1.getClass().getSimpleName());
    if (arg1 instanceof AuthenticationTransaction) {
        final AuthenticationTransaction transaction = AuthenticationTransaction.class.cast(arg1);
        return resolveArguments(new StringBuilder(), transaction.getCredentials());
    }
    if (arg1 instanceof Credential) {
        return arg1.toString();
    }
    if (arg1 instanceof String) {
        try {
            final Ticket ticket = this.centralAuthenticationService.getTicket((String) arg1, Ticket.class);
            Authentication authentication = null;
            if (ticket instanceof ServiceTicket) {
                authentication = ServiceTicket.class.cast(ticket).getGrantingTicket().getAuthentication();
            } else if (ticket instanceof TicketGrantingTicket) {
                authentication = TicketGrantingTicket.class.cast(ticket).getAuthentication();
            }
            return this.principalIdProvider.getPrincipalIdFrom(authentication);
        } catch (final InvalidTicketException e) {
            LOGGER.trace(e.getMessage(), e);
        }
        LOGGER.debug("Could not locate ticket [{}] in the registry", arg1);
    }
    return WebUtils.getAuthenticatedUsername();
}
Also used : ServiceTicket(org.apereo.cas.ticket.ServiceTicket) TicketGrantingTicket(org.apereo.cas.ticket.TicketGrantingTicket) Ticket(org.apereo.cas.ticket.Ticket) Credential(org.apereo.cas.authentication.Credential) Authentication(org.apereo.cas.authentication.Authentication) TicketGrantingTicket(org.apereo.cas.ticket.TicketGrantingTicket) InvalidTicketException(org.apereo.cas.ticket.InvalidTicketException) ServiceTicket(org.apereo.cas.ticket.ServiceTicket) AuthenticationTransaction(org.apereo.cas.authentication.AuthenticationTransaction)

Aggregations

Credential (org.apereo.cas.authentication.Credential)43 Test (org.junit.Test)27 TicketGrantingTicket (org.apereo.cas.ticket.TicketGrantingTicket)19 UsernamePasswordCredential (org.apereo.cas.authentication.UsernamePasswordCredential)13 HttpBasedServiceCredential (org.apereo.cas.authentication.HttpBasedServiceCredential)11 AuthenticationResult (org.apereo.cas.authentication.AuthenticationResult)10 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)10 ServiceTicket (org.apereo.cas.ticket.ServiceTicket)9 Service (org.apereo.cas.authentication.principal.Service)8 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)8 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)8 AuthenticationException (org.apereo.cas.authentication.AuthenticationException)7 MockServletContext (org.springframework.mock.web.MockServletContext)7 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)7 MockRequestContext (org.springframework.webflow.test.MockRequestContext)7 HashMap (java.util.HashMap)6 CentralAuthenticationService (org.apereo.cas.CentralAuthenticationService)5 AuthenticationResultBuilder (org.apereo.cas.authentication.AuthenticationResultBuilder)5 LinkedHashMap (java.util.LinkedHashMap)4 RegisteredService (org.apereo.cas.services.RegisteredService)4