Search in sources :

Example 1 with AccessTokenRequestContext

use of org.apereo.cas.support.oauth.web.response.accesstoken.ext.AccessTokenRequestContext in project cas by apereo.

the class OAuth20AccessTokenEndpointController method examineAndExtractAccessTokenGrantRequest.

private AccessTokenRequestContext examineAndExtractAccessTokenGrantRequest(final HttpServletRequest request, final HttpServletResponse response) {
    val audit = AuditableContext.builder().httpRequest(request).httpResponse(response).build();
    val accessResult = accessTokenGrantAuditableRequestExtractor.execute(audit);
    val execResult = accessResult.getExecutionResult();
    return (AccessTokenRequestContext) execResult.orElseThrow(() -> new UnsupportedOperationException("Access token request is not supported"));
}
Also used : lombok.val(lombok.val) AccessTokenRequestContext(org.apereo.cas.support.oauth.web.response.accesstoken.ext.AccessTokenRequestContext)

Example 2 with AccessTokenRequestContext

use of org.apereo.cas.support.oauth.web.response.accesstoken.ext.AccessTokenRequestContext in project cas by apereo.

the class OAuth20AuthorizeEndpointController method prepareAccessTokenRequestContext.

/**
 * Build access token request context.
 *
 * @param authzRequest      the authz request
 * @param registeredService the registered service
 * @param context           the context
 * @param service           the service
 * @param authentication    the authentication
 * @return the access token request context
 * @throws Exception the exception
 */
protected AccessTokenRequestContext prepareAccessTokenRequestContext(final OAuth20AuthorizationRequest authzRequest, final OAuthRegisteredService registeredService, final JEEContext context, final Service service, final Authentication authentication) throws Exception {
    var payloadBuilder = AccessTokenRequestContext.builder();
    if (authzRequest.isSingleSignOnSessionRequired()) {
        val tgt = getConfigurationContext().fetchTicketGrantingTicketFrom(context);
        payloadBuilder = payloadBuilder.ticketGrantingTicket(tgt);
    }
    val redirectUri = OAuth20Utils.getRequestParameter(context, OAuth20Constants.REDIRECT_URI).map(String::valueOf).orElse(StringUtils.EMPTY);
    val grantType = context.getRequestParameter(OAuth20Constants.GRANT_TYPE).map(String::valueOf).orElseGet(OAuth20GrantTypes.AUTHORIZATION_CODE::getType).toUpperCase();
    val scopes = OAuth20Utils.parseRequestScopes(context);
    val codeChallenge = context.getRequestParameter(OAuth20Constants.CODE_CHALLENGE).map(String::valueOf).orElse(StringUtils.EMPTY);
    val codeChallengeMethod = context.getRequestParameter(OAuth20Constants.CODE_CHALLENGE_METHOD).map(String::valueOf).orElse(StringUtils.EMPTY).toUpperCase();
    val userProfile = OAuth20Utils.getAuthenticatedUserProfile(context, getConfigurationContext().getSessionStore());
    val claims = OAuth20Utils.parseRequestClaims(context);
    val holder = payloadBuilder.service(service).authentication(authentication).registeredService(registeredService).grantType(OAuth20Utils.getGrantType(context)).responseType(OAuth20Utils.getResponseType(context)).codeChallenge(codeChallenge).codeChallengeMethod(codeChallengeMethod).scopes(scopes).clientId(authzRequest.getClientId()).redirectUri(redirectUri).userProfile(userProfile).claims(claims).responseMode(OAuth20Utils.getResponseModeType(context)).build();
    context.getRequestParameters().keySet().forEach(key -> context.getRequestParameter(key).ifPresent(value -> holder.getParameters().put(key, value)));
    LOGGER.debug("Building authorization response for grant type [{}] with scopes [{}] for client id [{}]", grantType, scopes, authzRequest.getClientId());
    return holder;
}
Also used : lombok.val(lombok.val) RegisteredServiceAccessStrategyUtils(org.apereo.cas.services.RegisteredServiceAccessStrategyUtils) StringUtils(org.apache.commons.lang3.StringUtils) WebContext(org.pac4j.core.context.WebContext) AuthenticationCredentialsThreadLocalBinder(org.apereo.cas.authentication.AuthenticationCredentialsThreadLocalBinder) AccessTokenRequestContext(org.apereo.cas.support.oauth.web.response.accesstoken.ext.AccessTokenRequestContext) LoggingUtils(org.apereo.cas.util.LoggingUtils) HttpServletRequest(javax.servlet.http.HttpServletRequest) Authentication(org.apereo.cas.authentication.Authentication) OAuth20AuthorizationRequest(org.apereo.cas.support.oauth.web.response.OAuth20AuthorizationRequest) GetMapping(org.springframework.web.bind.annotation.GetMapping) OAuth20AuthorizationResponseBuilder(org.apereo.cas.support.oauth.web.response.callback.OAuth20AuthorizationResponseBuilder) JEEContext(org.pac4j.core.context.JEEContext) PostMapping(org.springframework.web.bind.annotation.PostMapping) OAuth20Constants(org.apereo.cas.support.oauth.OAuth20Constants) Unchecked(org.jooq.lambda.Unchecked) OAuth20Utils(org.apereo.cas.support.oauth.util.OAuth20Utils) AuditableContext(org.apereo.cas.audit.AuditableContext) OAuth20GrantTypes(org.apereo.cas.support.oauth.OAuth20GrantTypes) lombok.val(lombok.val) HttpServletResponse(javax.servlet.http.HttpServletResponse) OAuthRegisteredService(org.apereo.cas.support.oauth.services.OAuthRegisteredService) ProfileManager(org.pac4j.core.profile.ProfileManager) OrderComparator(org.springframework.core.OrderComparator) Objects(java.util.Objects) HttpStatus(org.springframework.http.HttpStatus) ModelAndView(org.springframework.web.servlet.ModelAndView) Slf4j(lombok.extern.slf4j.Slf4j) Service(org.apereo.cas.authentication.principal.Service) Optional(java.util.Optional) PreventedException(org.apereo.cas.authentication.PreventedException) OAuth20GrantTypes(org.apereo.cas.support.oauth.OAuth20GrantTypes)

Example 3 with AccessTokenRequestContext

use of org.apereo.cas.support.oauth.web.response.accesstoken.ext.AccessTokenRequestContext in project cas by apereo.

the class OAuth20AccessTokenGrantRequestAuditResourceResolver method resolveFrom.

@Override
public String[] resolveFrom(final JoinPoint auditableTarget, final Object retval) {
    val auditResult = (AuditableExecutionResult) retval;
    val executionResult = auditResult.getExecutionResult();
    if (executionResult.isPresent()) {
        val accessTokenRequest = (AccessTokenRequestContext) executionResult.get();
        val tokenId = accessTokenRequest.getToken() == null ? "N/A" : accessTokenRequest.getToken().getId();
        val values = new HashMap<>();
        values.put("token", tokenId);
        values.put("client_id", accessTokenRequest.getRegisteredService().getClientId());
        values.put("service", accessTokenRequest.getService().getId());
        values.put("grant_type", accessTokenRequest.getGrantType().getType());
        values.put("response_type", accessTokenRequest.getResponseType().getType());
        values.put("scopes", accessTokenRequest.getScopes());
        return new String[] { auditFormat.serialize(values) };
    }
    return ArrayUtils.EMPTY_STRING_ARRAY;
}
Also used : lombok.val(lombok.val) AccessTokenRequestContext(org.apereo.cas.support.oauth.web.response.accesstoken.ext.AccessTokenRequestContext) HashMap(java.util.HashMap) AuditableExecutionResult(org.apereo.cas.audit.AuditableExecutionResult)

Example 4 with AccessTokenRequestContext

use of org.apereo.cas.support.oauth.web.response.accesstoken.ext.AccessTokenRequestContext in project cas by apereo.

the class OAuth20AuthorizationCodeAuthorizationResponseBuilder method build.

@Audit(action = AuditableActions.OAUTH2_CODE_RESPONSE, actionResolverName = AuditActionResolvers.OAUTH2_CODE_RESPONSE_ACTION_RESOLVER, resourceResolverName = AuditResourceResolvers.OAUTH2_CODE_RESPONSE_RESOURCE_RESOLVER)
@Override
public ModelAndView build(final AccessTokenRequestContext holder) throws Exception {
    val authentication = holder.getAuthentication();
    val factory = (OAuth20CodeFactory) configurationContext.getTicketFactory().get(OAuth20Code.class);
    val code = factory.create(holder.getService(), authentication, holder.getTicketGrantingTicket(), holder.getScopes(), holder.getCodeChallenge(), holder.getCodeChallengeMethod(), holder.getClientId(), holder.getClaims(), holder.getResponseType(), holder.getGrantType());
    LOGGER.debug("Generated OAuth code: [{}]", code);
    configurationContext.getCentralAuthenticationService().addTicket(code);
    val ticketGrantingTicket = holder.getTicketGrantingTicket();
    Optional.ofNullable(ticketGrantingTicket).ifPresent(tgt -> {
        FunctionUtils.doAndHandle(ticket -> {
            configurationContext.getCentralAuthenticationService().updateTicket(ticket);
        }, (CheckedFunction<Throwable, TicketGrantingTicket>) throwable -> {
            LOGGER.error("Unable to update ticket-granting-ticket [{}]", ticketGrantingTicket, throwable);
            return null;
        }).accept(tgt);
    });
    return buildCallbackViewViaRedirectUri(holder, code);
}
Also used : lombok.val(lombok.val) OAuth20Constants(org.apereo.cas.support.oauth.OAuth20Constants) OAuth20ResponseTypes(org.apereo.cas.support.oauth.OAuth20ResponseTypes) OAuth20Utils(org.apereo.cas.support.oauth.util.OAuth20Utils) OAuth20ConfigurationContext(org.apereo.cas.support.oauth.web.endpoints.OAuth20ConfigurationContext) CheckedFunction(org.jooq.lambda.fi.util.function.CheckedFunction) Audit(org.apereo.inspektr.audit.annotation.Audit) OAuth20CodeFactory(org.apereo.cas.ticket.code.OAuth20CodeFactory) lombok.val(lombok.val) StringUtils(org.apache.commons.lang3.StringUtils) AuditActionResolvers(org.apereo.cas.audit.AuditActionResolvers) LinkedHashMap(java.util.LinkedHashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) AccessTokenRequestContext(org.apereo.cas.support.oauth.web.response.accesstoken.ext.AccessTokenRequestContext) FunctionUtils(org.apereo.cas.util.function.FunctionUtils) Slf4j(lombok.extern.slf4j.Slf4j) OAuth20AuthorizationRequest(org.apereo.cas.support.oauth.web.response.OAuth20AuthorizationRequest) AuditResourceResolvers(org.apereo.cas.audit.AuditResourceResolvers) AuditableActions(org.apereo.cas.audit.AuditableActions) Optional(java.util.Optional) OAuth20Code(org.apereo.cas.ticket.code.OAuth20Code) TicketGrantingTicket(org.apereo.cas.ticket.TicketGrantingTicket) OAuth20Code(org.apereo.cas.ticket.code.OAuth20Code) OAuth20CodeFactory(org.apereo.cas.ticket.code.OAuth20CodeFactory) CheckedFunction(org.jooq.lambda.fi.util.function.CheckedFunction) Audit(org.apereo.inspektr.audit.annotation.Audit)

Aggregations

lombok.val (lombok.val)4 AccessTokenRequestContext (org.apereo.cas.support.oauth.web.response.accesstoken.ext.AccessTokenRequestContext)4 Optional (java.util.Optional)2 Slf4j (lombok.extern.slf4j.Slf4j)2 StringUtils (org.apache.commons.lang3.StringUtils)2 OAuth20Constants (org.apereo.cas.support.oauth.OAuth20Constants)2 OAuth20Utils (org.apereo.cas.support.oauth.util.OAuth20Utils)2 OAuth20AuthorizationRequest (org.apereo.cas.support.oauth.web.response.OAuth20AuthorizationRequest)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Objects (java.util.Objects)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 AuditActionResolvers (org.apereo.cas.audit.AuditActionResolvers)1 AuditResourceResolvers (org.apereo.cas.audit.AuditResourceResolvers)1 AuditableActions (org.apereo.cas.audit.AuditableActions)1 AuditableContext (org.apereo.cas.audit.AuditableContext)1 AuditableExecutionResult (org.apereo.cas.audit.AuditableExecutionResult)1 Authentication (org.apereo.cas.authentication.Authentication)1