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);
}
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);
}
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);
}
}
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));
}
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"));
}
Aggregations