use of org.apereo.cas.authentication.AuthenticationResult in project cas by apereo.
the class OpenIdServiceTests method verifyGetResponse.
@Test
public void verifyGetResponse() {
try {
request.removeParameter(OpenIdProtocolConstants.OPENID_ASSOCHANDLE);
request.addParameter(OpenIdProtocolConstants.OPENID_ASSOCHANDLE, association.getHandle());
openIdService = openIdServiceFactory.createService(request);
final AuthenticationResult ctx = CoreAuthenticationTestUtils.getAuthenticationResult(getAuthenticationSystemSupport(), openIdService);
final String tgt = centralAuthenticationService.createTicketGrantingTicket(ctx).getId();
final String st = centralAuthenticationService.grantServiceTicket(tgt, openIdService, ctx).getId();
centralAuthenticationService.validateServiceTicket(st, openIdService);
final Response response = new OpenIdServiceResponseBuilder(OPEN_ID_PREFIX_URL, serverManager, centralAuthenticationService, new DefaultServicesManager(mock(ServiceRegistry.class), mock(ApplicationEventPublisher.class))).build(openIdService, "something", CoreAuthenticationTestUtils.getAuthentication());
assertNotNull(response);
assertEquals(association.getHandle(), response.getAttributes().get(OpenIdProtocolConstants.OPENID_ASSOCHANDLE));
assertEquals(RETURN_TO_URL, response.getAttributes().get(OpenIdProtocolConstants.OPENID_RETURNTO));
assertEquals(OPEN_ID_PREFIX_URL, response.getAttributes().get(OpenIdProtocolConstants.OPENID_IDENTITY));
final Response response2 = new OpenIdServiceResponseBuilder(OPEN_ID_PREFIX_URL, serverManager, centralAuthenticationService, new DefaultServicesManager(mock(ServiceRegistry.class), mock(ApplicationEventPublisher.class))).build(openIdService, null, CoreAuthenticationTestUtils.getAuthentication());
assertEquals("cancel", response2.getAttributes().get(OpenIdProtocolConstants.OPENID_MODE));
} catch (final Exception e) {
LOGGER.debug("Exception during verification of service ticket", e);
}
}
use of org.apereo.cas.authentication.AuthenticationResult in project cas by apereo.
the class RegisteredServiceResource method authenticateRequest.
private Authentication authenticateRequest(final HttpServletRequest request, final HttpServletResponse response) {
final BasicAuthExtractor extractor = new BasicAuthExtractor();
final WebContext webContext = new J2EContext(request, response);
final UsernamePasswordCredentials credentials = extractor.extract(webContext);
if (credentials != null) {
LOGGER.debug("Received basic authentication request from credentials [{}]", credentials);
final Credential c = new UsernamePasswordCredential(credentials.getUsername(), credentials.getPassword());
final Service serviceRequest = this.serviceFactory.createService(request);
final AuthenticationResult result = authenticationSystemSupport.handleAndFinalizeSingleAuthenticationTransaction(serviceRequest, c);
return result.getAuthentication();
}
throw new BadRestRequestException("Could not authenticate request");
}
use of org.apereo.cas.authentication.AuthenticationResult in project cas by apereo.
the class JWTTicketGrantingTicketResourceEntityResponseFactoryTests method verifyTicketGrantingTicketAsJwt.
@Test
public void verifyTicketGrantingTicketAsJwt() throws Exception {
final AuthenticationResult result = CoreAuthenticationTestUtils.getAuthenticationResult(authenticationSystemSupport, CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword("casuser"));
final TicketGrantingTicket tgt = centralAuthenticationService.createTicketGrantingTicket(result);
final MockHttpServletRequest request = new MockHttpServletRequest();
request.addParameter(TokenConstants.PARAMETER_NAME_TOKEN, Boolean.TRUE.toString());
final ResponseEntity<String> response = ticketGrantingTicketResourceEntityResponseFactory.build(tgt, request);
assertNotNull(response);
assertEquals(HttpStatus.CREATED, response.getStatusCode());
final Object jwt = this.tokenCipherExecutor.decode(response.getBody());
final JWTClaimsSet claims = JWTClaimsSet.parse(jwt.toString());
assertEquals(claims.getSubject(), tgt.getAuthentication().getPrincipal().getId());
}
use of org.apereo.cas.authentication.AuthenticationResult in project cas by apereo.
the class ServiceTicketResource method createServiceTicket.
/**
* Create new service ticket.
*
* @param httpServletRequest http request
* @param tgtId ticket granting ticket id URI path param
* @return {@link ResponseEntity} representing RESTful response
*/
@PostMapping(value = "/v1/tickets/{tgtId:.+}", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<String> createServiceTicket(final HttpServletRequest httpServletRequest, @PathVariable("tgtId") final String tgtId) {
try {
final Authentication authn = this.ticketRegistrySupport.getAuthenticationFrom(tgtId);
AuthenticationCredentialsThreadLocalBinder.bindCurrent(authn);
if (authn == null) {
throw new InvalidTicketException(tgtId);
}
final AuthenticationResultBuilder builder = new DefaultAuthenticationResultBuilder(this.authenticationSystemSupport.getPrincipalElectionStrategy());
final Service service = this.argumentExtractor.extractService(httpServletRequest);
if (service == null) {
throw new IllegalArgumentException("Target service/application is unspecified or unrecognized in the request");
}
final AuthenticationResult authenticationResult = builder.collect(authn).build(service);
return this.serviceTicketResourceEntityResponseFactory.build(tgtId, service, authenticationResult);
} catch (final InvalidTicketException e) {
return new ResponseEntity<>(tgtId + " could not be found or is considered invalid", HttpStatus.NOT_FOUND);
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
} finally {
AuthenticationCredentialsThreadLocalBinder.clear();
}
}
use of org.apereo.cas.authentication.AuthenticationResult in project cas by apereo.
the class ECPProfileHandlerController method authenticateEcpRequest.
/**
* Authenticate ecp request.
*
* @param credential the credential
* @param authnRequest the authn request
* @return the authentication
*/
protected Authentication authenticateEcpRequest(final Credential credential, final Pair<AuthnRequest, MessageContext> authnRequest) {
final String issuer = SamlIdPUtils.getIssuerFromSamlRequest(authnRequest.getKey());
LOGGER.debug("Located issuer [{}] from request prior to authenticating [{}]", issuer, credential.getId());
final Service service = webApplicationServiceFactory.createService(issuer);
LOGGER.debug("Executing authentication request for service [{}] on behalf of credential id [{}]", service, credential.getId());
final AuthenticationResult authenticationResult = authenticationSystemSupport.handleAndFinalizeSingleAuthenticationTransaction(service, credential);
return authenticationResult.getAuthentication();
}
Aggregations