Search in sources :

Example 11 with J2EContext

use of org.pac4j.core.context.J2EContext in project pac4j by pac4j.

the class RedirectSAML2ClientTests method testSetComparisonTypeWithRedirectBinding.

@Test
public void testSetComparisonTypeWithRedirectBinding() {
    final SAML2Client client = getClient();
    client.getConfiguration().setComparisonType(AuthnContextComparisonTypeEnumeration.EXACT.toString());
    final WebContext context = new J2EContext(new MockHttpServletRequest(), new MockHttpServletResponse());
    final RedirectAction action = client.getRedirectAction(context);
    assertTrue(getInflatedAuthnRequest(action.getLocation()).contains("Comparison=\"exact\""));
}
Also used : WebContext(org.pac4j.core.context.WebContext) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) J2EContext(org.pac4j.core.context.J2EContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) RedirectAction(org.pac4j.core.redirect.RedirectAction) Test(org.junit.Test)

Example 12 with J2EContext

use of org.pac4j.core.context.J2EContext in project cas by apereo.

the class SAML2ClientLogoutAction method doExecute.

@Override
protected Event doExecute(final RequestContext requestContext) throws Exception {
    try {
        final HttpServletRequest request = WebUtils.getHttpServletRequest(requestContext);
        final HttpServletResponse response = WebUtils.getHttpServletResponse(requestContext);
        final J2EContext context = WebUtils.getPac4jJ2EContext(request, response);
        final SAML2Client client = clients.findClient(SAML2Client.class);
        if (client != null) {
            LOGGER.debug("Located SAML2 client [{}]", client);
            final RedirectAction action = client.getLogoutAction(context, null, null);
            LOGGER.debug("Preparing logout message to send is [{}]", action.getLocation());
            action.perform(context);
        }
    } catch (final Exception e) {
        LOGGER.warn(e.getMessage(), e);
    }
    return null;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) SAML2Client(org.pac4j.saml.client.SAML2Client) J2EContext(org.pac4j.core.context.J2EContext) RedirectAction(org.pac4j.core.redirect.RedirectAction)

Example 13 with J2EContext

use of org.pac4j.core.context.J2EContext in project cas by apereo.

the class OAuth20CallbackAuthorizeEndpointController method handleRequestInternal.

/**
     * Handle request.
     *
     * @param request  the request
     * @param response the response
     * @return the model and view
     * @throws Exception the exception
     */
@GetMapping(path = OAuthConstants.BASE_OAUTH20_URL + '/' + OAuthConstants.CALLBACK_AUTHORIZE_URL)
public ModelAndView handleRequestInternal(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    this.callbackController.callback(request, response);
    final String url = StringUtils.remove(response.getHeader("Location"), "redirect:");
    final J2EContext ctx = WebUtils.getPac4jJ2EContext(request, response);
    final ProfileManager manager = WebUtils.getPac4jProfileManager(request, response);
    return oAuth20CallbackAuthorizeViewResolver.resolve(ctx, manager, url);
}
Also used : ProfileManager(org.pac4j.core.profile.ProfileManager) J2EContext(org.pac4j.core.context.J2EContext) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 14 with J2EContext

use of org.pac4j.core.context.J2EContext in project cas by apereo.

the class OAuth20AccessTokenEndpointController method verifyAccessTokenRequest.

/**
     * Verify the access token request.
     *
     * @param request  the HTTP request
     * @param response the HTTP response
     * @return true, if successful
     */
private boolean verifyAccessTokenRequest(final HttpServletRequest request, final HttpServletResponse response) {
    // must have the right grant type
    final String grantType = request.getParameter(OAuthConstants.GRANT_TYPE);
    if (!checkGrantTypes(grantType, OAuth20GrantTypes.AUTHORIZATION_CODE, OAuth20GrantTypes.PASSWORD, OAuth20GrantTypes.REFRESH_TOKEN)) {
        return false;
    }
    // must be authenticated (client or user)
    final J2EContext context = WebUtils.getPac4jJ2EContext(request, response);
    final ProfileManager manager = WebUtils.getPac4jProfileManager(request, response);
    final Optional<UserProfile> profile = manager.get(true);
    if (profile == null || !profile.isPresent()) {
        return false;
    }
    final UserProfile uProfile = profile.get();
    // authorization code grant type
    if (isGrantType(grantType, OAuth20GrantTypes.AUTHORIZATION_CODE)) {
        final String clientId = uProfile.getId();
        final String redirectUri = request.getParameter(OAuthConstants.REDIRECT_URI);
        final OAuthRegisteredService registeredService = OAuthUtils.getRegisteredOAuthService(getServicesManager(), clientId);
        return uProfile instanceof OAuthClientProfile && getValidator().checkParameterExist(request, OAuthConstants.REDIRECT_URI) && getValidator().checkParameterExist(request, OAuthConstants.CODE) && getValidator().checkCallbackValid(registeredService, redirectUri);
    } else if (isGrantType(grantType, OAuth20GrantTypes.REFRESH_TOKEN)) {
        // refresh token grant type
        return uProfile instanceof OAuthClientProfile && getValidator().checkParameterExist(request, OAuthConstants.REFRESH_TOKEN);
    } else {
        final String clientId = request.getParameter(OAuthConstants.CLIENT_ID);
        final OAuthRegisteredService registeredService = OAuthUtils.getRegisteredOAuthService(getServicesManager(), clientId);
        // resource owner password grant type
        return uProfile instanceof OAuthUserProfile && getValidator().checkParameterExist(request, OAuthConstants.CLIENT_ID) && getValidator().checkServiceValid(registeredService);
    }
}
Also used : ProfileManager(org.pac4j.core.profile.ProfileManager) OAuthUserProfile(org.apereo.cas.support.oauth.profile.OAuthUserProfile) UserProfile(org.pac4j.core.profile.UserProfile) OAuthRegisteredService(org.apereo.cas.support.oauth.services.OAuthRegisteredService) OAuthClientProfile(org.apereo.cas.support.oauth.profile.OAuthClientProfile) J2EContext(org.pac4j.core.context.J2EContext) OAuthUserProfile(org.apereo.cas.support.oauth.profile.OAuthUserProfile)

Example 15 with J2EContext

use of org.pac4j.core.context.J2EContext in project cas by apereo.

the class CasConsentReviewController method callback.

/**
 * Endpoint for Cas Client Callback.
 *
 * @param request the request
 * @param response the response
 */
@GetMapping("/callback")
public void callback(final HttpServletRequest request, final HttpServletResponse response) {
    LOGGER.debug("Callback endpoint hit...");
    final CallbackLogic logic = this.pac4jConfig.getCallbackLogic();
    final J2EContext context = Pac4jUtils.getPac4jJ2EContext(request, response);
    final String defaultUrl = this.casProperties.getServer().getPrefix().concat("/consentReview");
    logic.perform(context, this.pac4jConfig, J2ENopHttpActionAdapter.INSTANCE, defaultUrl, false, false, false, null);
}
Also used : CallbackLogic(org.pac4j.core.engine.CallbackLogic) J2EContext(org.pac4j.core.context.J2EContext) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

J2EContext (org.pac4j.core.context.J2EContext)32 RedirectAction (org.pac4j.core.redirect.RedirectAction)13 Test (org.junit.Test)11 WebContext (org.pac4j.core.context.WebContext)11 ProfileManager (org.pac4j.core.profile.ProfileManager)11 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)11 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 UnauthorizedServiceException (org.apereo.cas.services.UnauthorizedServiceException)6 OAuthRegisteredService (org.apereo.cas.support.oauth.services.OAuthRegisteredService)6 UserProfile (org.pac4j.core.profile.UserProfile)6 GetMapping (org.springframework.web.bind.annotation.GetMapping)6 Service (org.apereo.cas.authentication.principal.Service)5 Authentication (org.apereo.cas.authentication.Authentication)4 AccessToken (org.apereo.cas.ticket.accesstoken.AccessToken)4 TicketGrantingTicket (org.apereo.cas.ticket.TicketGrantingTicket)3 CentralAuthenticationService (org.apereo.cas.CentralAuthenticationService)2 AuthenticationResult (org.apereo.cas.authentication.AuthenticationResult)2 PrincipalException (org.apereo.cas.authentication.PrincipalException)2