Search in sources :

Example 31 with Credential

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

the class X509RestHttpRequestCredentialFactoryTests method createX509Credential.

@Test
public void createX509Credential() throws IOException {
    final MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
    final Scanner scan = new Scanner(new ClassPathResource("ldap-crl.crt").getFile(), StandardCharsets.UTF_8.name());
    final String certStr = scan.useDelimiter("\\Z").next();
    scan.close();
    requestBody.add("cert", certStr);
    final Credential cred = factory.fromRequestBody(requestBody).iterator().next();
    assertTrue(cred instanceof X509CertificateCredential);
}
Also used : Scanner(java.util.Scanner) X509CertificateCredential(org.apereo.cas.adaptors.x509.authentication.principal.X509CertificateCredential) Credential(org.apereo.cas.authentication.Credential) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) X509CertificateCredential(org.apereo.cas.adaptors.x509.authentication.principal.X509CertificateCredential) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 32 with Credential

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

the class TicketGrantingTicketResource method createTicketGrantingTicketForRequest.

/**
 * Create ticket granting ticket for request ticket granting ticket.
 *
 * @param requestBody the request body
 * @param request     the request
 * @return the ticket granting ticket
 */
protected TicketGrantingTicket createTicketGrantingTicketForRequest(final MultiValueMap<String, String> requestBody, final HttpServletRequest request) {
    final Collection<Credential> credential = this.credentialFactory.fromRequestBody(requestBody);
    if (credential == null || credential.isEmpty()) {
        throw new BadRestRequestException("No credentials are provided or extracted to authenticate the REST request");
    }
    final Service service = this.serviceFactory.createService(request);
    final AuthenticationResult authenticationResult = authenticationSystemSupport.handleAndFinalizeSingleAuthenticationTransaction(service, credential);
    return centralAuthenticationService.createTicketGrantingTicket(authenticationResult);
}
Also used : Credential(org.apereo.cas.authentication.Credential) CentralAuthenticationService(org.apereo.cas.CentralAuthenticationService) Service(org.apereo.cas.authentication.principal.Service) BadRestRequestException(org.apereo.cas.rest.BadRestRequestException) AuthenticationResult(org.apereo.cas.authentication.AuthenticationResult)

Example 33 with Credential

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

the class UserAuthenticationResource method createTicketGrantingTicket.

/**
 * Create new ticket granting ticket.
 *
 * @param requestBody username and password application/x-www-form-urlencoded values
 * @param request     raw HttpServletRequest used to call this method
 * @return ResponseEntity representing RESTful response
 */
@PostMapping(value = "/v1/users", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<String> createTicketGrantingTicket(@RequestBody final MultiValueMap<String, String> requestBody, final HttpServletRequest request) {
    try {
        final Collection<Credential> credential = this.credentialFactory.fromRequestBody(requestBody);
        if (credential == null || credential.isEmpty()) {
            throw new BadRestRequestException("No credentials are provided or extracted to authenticate the REST request");
        }
        final Service service = this.serviceFactory.createService(request);
        final AuthenticationResult authenticationResult = authenticationSystemSupport.handleAndFinalizeSingleAuthenticationTransaction(service, credential);
        return this.userAuthenticationResourceEntityResponseFactory.build(authenticationResult, request);
    } catch (final AuthenticationException e) {
        return RestResourceUtils.createResponseEntityForAuthnFailure(e);
    } catch (final BadRestRequestException e) {
        LOGGER.error(e.getMessage(), e);
        return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
        return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
Also used : Credential(org.apereo.cas.authentication.Credential) AuthenticationException(org.apereo.cas.authentication.AuthenticationException) Service(org.apereo.cas.authentication.principal.Service) BadRestRequestException(org.apereo.cas.rest.BadRestRequestException) AuthenticationException(org.apereo.cas.authentication.AuthenticationException) BadRestRequestException(org.apereo.cas.rest.BadRestRequestException) AuthenticationResult(org.apereo.cas.authentication.AuthenticationResult) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 34 with Credential

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

the class ChainingPrincipalResolverTests method examineSupports.

@Test
public void examineSupports() {
    final Credential credential = mock(Credential.class);
    when(credential.getId()).thenReturn("a");
    final PrincipalResolver resolver1 = mock(PrincipalResolver.class);
    when(resolver1.supports(eq(credential))).thenReturn(true);
    final PrincipalResolver resolver2 = mock(PrincipalResolver.class);
    when(resolver2.supports(eq(credential))).thenReturn(false);
    final ChainingPrincipalResolver resolver = new ChainingPrincipalResolver();
    resolver.setChain(Arrays.asList(resolver1, resolver2));
    assertTrue(resolver.supports(credential));
}
Also used : Credential(org.apereo.cas.authentication.Credential) ChainingPrincipalResolver(org.apereo.cas.authentication.principal.resolvers.ChainingPrincipalResolver) ChainingPrincipalResolver(org.apereo.cas.authentication.principal.resolvers.ChainingPrincipalResolver) Test(org.junit.Test)

Example 35 with Credential

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

the class ChainingPrincipalResolverTests method examineResolve.

@Test
public void examineResolve() {
    final Principal principalOut = principalFactory.createPrincipal("output");
    final Credential credential = mock(Credential.class);
    when(credential.getId()).thenReturn("input");
    final PrincipalResolver resolver1 = mock(PrincipalResolver.class);
    when(resolver1.supports(eq(credential))).thenReturn(true);
    when(resolver1.resolve(eq(credential), any(Principal.class), any(AuthenticationHandler.class))).thenReturn(principalOut);
    final PrincipalResolver resolver2 = mock(PrincipalResolver.class);
    when(resolver2.supports(any(Credential.class))).thenReturn(true);
    when(resolver2.resolve(any(Credential.class), any(Principal.class), any(AuthenticationHandler.class))).thenReturn(principalFactory.createPrincipal("output", Collections.singletonMap("mail", "final@example.com")));
    final ChainingPrincipalResolver resolver = new ChainingPrincipalResolver();
    resolver.setChain(Arrays.asList(resolver1, resolver2));
    final Principal principal = resolver.resolve(credential, principalOut, new SimpleTestUsernamePasswordAuthenticationHandler());
    assertEquals("output", principal.getId());
    assertEquals("final@example.com", principal.getAttributes().get("mail"));
}
Also used : Credential(org.apereo.cas.authentication.Credential) ChainingPrincipalResolver(org.apereo.cas.authentication.principal.resolvers.ChainingPrincipalResolver) SimpleTestUsernamePasswordAuthenticationHandler(org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler) SimpleTestUsernamePasswordAuthenticationHandler(org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler) AuthenticationHandler(org.apereo.cas.authentication.AuthenticationHandler) ChainingPrincipalResolver(org.apereo.cas.authentication.principal.resolvers.ChainingPrincipalResolver) Test(org.junit.Test)

Aggregations

Credential (org.apereo.cas.authentication.Credential)67 Test (org.junit.Test)39 UsernamePasswordCredential (org.apereo.cas.authentication.UsernamePasswordCredential)29 TicketGrantingTicket (org.apereo.cas.ticket.TicketGrantingTicket)26 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)18 AuthenticationResult (org.apereo.cas.authentication.AuthenticationResult)13 Service (org.apereo.cas.authentication.principal.Service)13 HttpBasedServiceCredential (org.apereo.cas.authentication.HttpBasedServiceCredential)11 HashMap (java.util.HashMap)10 ServiceTicket (org.apereo.cas.ticket.ServiceTicket)10 CachedData (net.spy.memcached.CachedData)9 AuthenticationException (org.apereo.cas.authentication.AuthenticationException)9 LinkedHashMap (java.util.LinkedHashMap)8 RegisteredService (org.apereo.cas.services.RegisteredService)8 CentralAuthenticationService (org.apereo.cas.CentralAuthenticationService)7 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)7 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)7 MockServletContext (org.springframework.mock.web.MockServletContext)7 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)7 MockRequestContext (org.springframework.webflow.test.MockRequestContext)7