use of org.apereo.cas.ticket.TransientSessionTicketFactory in project cas by apereo.
the class SendPasswordResetInstructionsAction method buildPasswordResetUrl.
/**
* Utility method to generate a password reset URL.
*
* @param username username
* @param passwordManagementService passwordManagementService
* @param casProperties casProperties
* @param service service from the flow scope
* @return URL a user can use to start the password reset process
* @throws Exception the exception
*/
protected String buildPasswordResetUrl(final String username, final PasswordManagementService passwordManagementService, final CasConfigurationProperties casProperties, final WebApplicationService service) throws Exception {
val query = PasswordManagementQuery.builder().username(username).build();
val token = passwordManagementService.createToken(query);
if (StringUtils.isNotBlank(token)) {
val transientFactory = (TransientSessionTicketFactory) this.ticketFactory.get(TransientSessionTicket.class);
val pm = casProperties.getAuthn().getPm();
val seconds = Beans.newDuration(pm.getReset().getExpiration()).toSeconds();
val properties = CollectionUtils.<String, Serializable>wrap(PasswordManagementWebflowUtils.FLOWSCOPE_PARAMETER_NAME_TOKEN, token, ExpirationPolicy.class.getName(), HardTimeoutExpirationPolicy.builder().timeToKillInSeconds(seconds).build());
val ticket = transientFactory.create(service, properties);
ticketRegistry.addTicket(ticket);
val resetUrl = new StringBuilder(casProperties.getServer().getPrefix()).append('/').append(CasWebflowConfigurer.FLOW_ID_LOGIN).append('?').append(PasswordManagementWebflowUtils.REQUEST_PARAMETER_NAME_PASSWORD_RESET_TOKEN).append('=').append(ticket.getId());
if (service != null) {
val encodeServiceUrl = UriUtils.encode(service.getOriginalUrl(), StandardCharsets.UTF_8);
resetUrl.append('&').append(CasProtocolConstants.PARAMETER_SERVICE).append('=').append(encodeServiceUrl);
}
val url = resetUrl.toString();
LOGGER.debug("Final password reset URL designed for [{}] is [{}]", username, url);
return url;
}
LOGGER.error("Could not create password reset url since no reset token could be generated");
return null;
}
use of org.apereo.cas.ticket.TransientSessionTicketFactory in project cas by apereo.
the class DuoSecurityUniversalPromptPrepareLoginAction method doExecute.
@Override
protected Event doExecute(final RequestContext requestContext) throws Exception {
val authentication = WebUtils.getInProgressAuthentication();
val duoSecurityIdentifier = WebUtils.getMultifactorAuthenticationProviderById(requestContext);
val provider = duoProviderBean.getProvider(duoSecurityIdentifier);
val client = provider.getDuoAuthenticationService().getDuoClient().map(c -> (Client) c).orElseThrow(() -> new RuntimeException("Unable to locate Duo Security client"));
val state = client.generateState();
val factory = (TransientSessionTicketFactory) ticketFactory.get(TransientSessionTicket.class);
val properties = new LinkedHashMap<String, Object>();
properties.put("duoProviderId", duoSecurityIdentifier);
properties.put(Authentication.class.getSimpleName(), authentication);
properties.put(AuthenticationResultBuilder.class.getSimpleName(), WebUtils.getAuthenticationResultBuilder(requestContext));
properties.put(AuthenticationResult.class.getSimpleName(), WebUtils.getAuthenticationResult(requestContext));
properties.put(Credential.class.getSimpleName(), WebUtils.getMultifactorAuthenticationParentCredential(requestContext));
val flowScope = requestContext.getFlowScope().asMap();
properties.put(MutableAttributeMap.class.getSimpleName(), flowScope);
Optional.ofNullable(WebUtils.getRegisteredService(requestContext)).ifPresent(registeredService -> properties.put(RegisteredService.class.getSimpleName(), registeredService));
val service = WebUtils.getService(requestContext);
val ticket = factory.create(state, service, properties);
ticketRegistry.addTicket(ticket);
LOGGER.debug("Stored Duo Security session via [{}]", ticket);
val principal = resolvePrincipal(authentication.getPrincipal());
val authUrl = client.createAuthUrl(principal.getId(), ticket.getId());
requestContext.getFlowScope().put("duoUniversalPromptLoginUrl", authUrl);
LOGGER.debug("Redirecting to Duo Security url at [{}]", authUrl);
return success(ticket);
}
Aggregations