use of org.apereo.cas.authentication.Authentication in project cas by apereo.
the class OAuth20AccessTokenControllerTests method verifyRefreshTokenExpiredToken.
@Test
public void verifyRefreshTokenExpiredToken() throws Exception {
final Principal principal = createPrincipal();
final RegisteredService registeredService = addRegisteredService();
final Authentication authentication = getAuthentication(principal);
final WebApplicationServiceFactory factory = new WebApplicationServiceFactory();
final Service service = factory.createService(registeredService.getServiceId());
final DefaultRefreshTokenFactory expiringRefreshTokenFactory = new DefaultRefreshTokenFactory(new AlwaysExpiresExpirationPolicy());
final RefreshToken refreshToken = expiringRefreshTokenFactory.create(service, authentication);
oAuth20AccessTokenController.getTicketRegistry().addTicket(refreshToken);
final MockHttpServletRequest mockRequest = new MockHttpServletRequest(GET, CONTEXT + OAuthConstants.ACCESS_TOKEN_URL);
mockRequest.setParameter(OAuthConstants.GRANT_TYPE, OAuth20GrantTypes.REFRESH_TOKEN.name().toLowerCase());
mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
mockRequest.setParameter(OAuthConstants.CLIENT_SECRET, CLIENT_SECRET);
mockRequest.setParameter(OAuthConstants.REFRESH_TOKEN, refreshToken.getId());
final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
requiresAuthenticationInterceptor.preHandle(mockRequest, mockResponse, null);
oAuth20AccessTokenController.handleRequestInternal(mockRequest, mockResponse);
assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus());
assertEquals(ERROR_EQUALS + OAuthConstants.INVALID_GRANT, mockResponse.getContentAsString());
}
use of org.apereo.cas.authentication.Authentication in project cas by apereo.
the class MultifactorAuthenticationSetTrustAction method doExecute.
@Override
public Event doExecute(final RequestContext requestContext) throws Exception {
final Authentication c = WebUtils.getAuthentication(requestContext);
if (c == null) {
LOGGER.error("Could not determine authentication from the request context");
return error();
}
AuthenticationCredentialsLocalBinder.bindCurrent(c);
final String principal = c.getPrincipal().getId();
if (!MultifactorAuthenticationTrustUtils.isMultifactorAuthenticationTrustedInScope(requestContext)) {
LOGGER.debug("Attempt to store trusted authentication record for [{}]", principal);
final MultifactorAuthenticationTrustRecord record = MultifactorAuthenticationTrustRecord.newInstance(principal, MultifactorAuthenticationTrustUtils.generateGeography());
if (requestContext.getRequestParameters().contains(PARAM_NAME_DEVICE_NAME)) {
final String deviceName = requestContext.getRequestParameters().get(PARAM_NAME_DEVICE_NAME);
if (StringUtils.isNotBlank(deviceName)) {
record.setName(deviceName);
}
}
storage.set(record);
LOGGER.debug("Saved trusted authentication record for [{}] under [{}]", principal, record.getName());
}
LOGGER.debug("Trusted authentication session exists for [{}]", principal);
MultifactorAuthenticationTrustUtils.trackTrustedMultifactorAuthenticationAttribute(c, trustedProperties.getAuthenticationContextAttribute());
return success();
}
use of org.apereo.cas.authentication.Authentication in project cas by apereo.
the class MultifactorAuthenticationVerifyTrustAction method doExecute.
@Override
protected Event doExecute(final RequestContext requestContext) throws Exception {
final Authentication c = WebUtils.getAuthentication(requestContext);
if (c == null) {
LOGGER.warn("Could not determine authentication from the request context");
return no();
}
final String principal = c.getPrincipal().getId();
final LocalDate onOrAfter = LocalDate.now().minus(trustedProperties.getExpiration(), DateTimeUtils.toChronoUnit(trustedProperties.getTimeUnit()));
LOGGER.warn("Retrieving trusted authentication records for [{}] that are on/after [{}]", principal, onOrAfter);
final Set<MultifactorAuthenticationTrustRecord> results = storage.get(principal, onOrAfter);
if (results.isEmpty()) {
LOGGER.debug("No valid trusted authentication records could be found for [{}]", principal);
return no();
}
final String geography = MultifactorAuthenticationTrustUtils.generateGeography();
LOGGER.debug("Retrieving authentication records for [{}] that match [{}]", principal, geography);
if (results.stream().noneMatch(entry -> entry.getGeography().equals(geography))) {
LOGGER.debug("No trusted authentication records could be found for [{}] to match the current geography", principal);
return no();
}
LOGGER.debug("Trusted authentication records found for [{}] that matches the current geography", principal);
MultifactorAuthenticationTrustUtils.setMultifactorAuthenticationTrustedInScope(requestContext);
MultifactorAuthenticationTrustUtils.trackTrustedMultifactorAuthenticationAttribute(c, trustedProperties.getAuthenticationContextAttribute());
return yes();
}
use of org.apereo.cas.authentication.Authentication in project cas by apereo.
the class U2FAuthenticationHandler method doAuthentication.
@Override
protected HandlerResult doAuthentication(final Credential credential) throws GeneralSecurityException, PreventedException {
final U2FTokenCredential tokenCredential = (U2FTokenCredential) credential;
final RequestContext context = RequestContextHolder.getRequestContext();
if (context == null) {
new IllegalArgumentException("No request context could be found to locate an authentication event");
}
final Authentication authentication = WebUtils.getAuthentication(context);
if (authentication == null) {
new IllegalArgumentException("Request context has no reference to an authentication event to locate a principal");
}
final Principal p = authentication.getPrincipal();
final AuthenticateResponse authenticateResponse = AuthenticateResponse.fromJson(tokenCredential.getToken());
final String authJson = u2FDeviceRepository.getDeviceAuthenticationRequest(authenticateResponse.getRequestId(), p.getId());
final AuthenticateRequestData authenticateRequest = AuthenticateRequestData.fromJson(authJson);
DeviceRegistration registration = null;
try {
registration = u2f.finishAuthentication(authenticateRequest, authenticateResponse, u2FDeviceRepository.getRegisteredDevices(p.getId()));
return createHandlerResult(tokenCredential, p, null);
} catch (final DeviceCompromisedException e) {
registration = e.getDeviceRegistration();
throw new PreventedException("Device possibly compromised and therefore blocked: " + e.getMessage(), e);
} finally {
u2FDeviceRepository.authenticateDevice(p.getId(), registration);
}
}
Aggregations