Search in sources :

Example 41 with RedirectView

use of org.springframework.web.servlet.view.RedirectView in project cas by apereo.

the class OidcCallbackAuthorizeViewResolver method resolve.

@Override
public ModelAndView resolve(final J2EContext ctx, final ProfileManager manager, final String url) {
    final Set<String> prompt = authorizationRequestSupport.getOidcPromptFromAuthorizationRequest(url);
    if (prompt.contains(OidcConstants.PROMPT_NONE)) {
        if (manager.get(true) != null) {
            return new ModelAndView(url);
        }
        final Map<String, String> model = new HashMap<>();
        model.put(OAuth20Constants.ERROR, OidcConstants.LOGIN_REQUIRED);
        return new ModelAndView(new MappingJackson2JsonView(), model);
    }
    return new ModelAndView(new RedirectView(url));
}
Also used : HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) RedirectView(org.springframework.web.servlet.view.RedirectView) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView)

Example 42 with RedirectView

use of org.springframework.web.servlet.view.RedirectView in project cxf by apache.

the class ApplicationController method revokeAccess.

@RequestMapping("/revokeAccess")
public ModelAndView revokeAccess(HttpServletRequest request) {
    String consumerKey = request.getParameter("consumerKey");
    clientManager.removeAllTokens(consumerKey);
    ModelAndView modelAndView = new ModelAndView(new RedirectView("/app/listAuthorizedClients"));
    return modelAndView;
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) RedirectView(org.springframework.web.servlet.view.RedirectView) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 43 with RedirectView

use of org.springframework.web.servlet.view.RedirectView in project cas by apereo.

the class CasApplicationContextConfiguration method rootController.

@Bean
protected Controller rootController() {
    return new ParameterizableViewController() {

        @Override
        protected ModelAndView handleRequestInternal(final HttpServletRequest request, final HttpServletResponse response) {
            final String queryString = request.getQueryString();
            final String url = request.getContextPath() + "/login" + (queryString != null ? '?' + queryString : StringUtils.EMPTY);
            return new ModelAndView(new RedirectView(response.encodeURL(url)));
        }
    };
}
Also used : ParameterizableViewController(org.springframework.web.servlet.mvc.ParameterizableViewController) HttpServletRequest(javax.servlet.http.HttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) RedirectView(org.springframework.web.servlet.view.RedirectView) HttpServletResponse(javax.servlet.http.HttpServletResponse) Bean(org.springframework.context.annotation.Bean)

Example 44 with RedirectView

use of org.springframework.web.servlet.view.RedirectView in project cas by apereo.

the class CasWebAppConfiguration method rootController.

@Bean
protected Controller rootController() {
    return new ParameterizableViewController() {

        @Override
        protected ModelAndView handleRequestInternal(final HttpServletRequest request, final HttpServletResponse response) {
            final String queryString = request.getQueryString();
            final String url = request.getContextPath() + "/login" + (queryString != null ? '?' + queryString : StringUtils.EMPTY);
            return new ModelAndView(new RedirectView(response.encodeURL(url)));
        }
    };
}
Also used : ParameterizableViewController(org.springframework.web.servlet.mvc.ParameterizableViewController) HttpServletRequest(javax.servlet.http.HttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) RedirectView(org.springframework.web.servlet.view.RedirectView) HttpServletResponse(javax.servlet.http.HttpServletResponse) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ServletListenerRegistrationBean(org.springframework.boot.web.servlet.ServletListenerRegistrationBean) Bean(org.springframework.context.annotation.Bean)

Example 45 with RedirectView

use of org.springframework.web.servlet.view.RedirectView in project ORCID-Source by ORCID.

the class OauthRegistrationController method registerAndAuthorize.

@RequestMapping(value = "/oauth/custom/registerConfirm.json", method = RequestMethod.POST)
@ResponseBody
public RequestInfoForm registerAndAuthorize(HttpServletRequest request, HttpServletResponse response, @RequestBody OauthRegistrationForm form) {
    RequestInfoForm requestInfoForm = (RequestInfoForm) request.getSession().getAttribute(REQUEST_INFO_FORM);
    if (form.getApproved()) {
        boolean usedCaptcha = false;
        // block google.
        if (form.getGrecaptchaWidgetId().getValue() != null) {
            // to the login page
            if (request.getSession().getAttribute(RegistrationController.GRECAPTCHA_SESSION_ATTRIBUTE_NAME) == null || PojoUtil.isEmpty(form.getGrecaptcha()) || !form.getGrecaptcha().getValue().equals(request.getSession().getAttribute(RegistrationController.GRECAPTCHA_SESSION_ATTRIBUTE_NAME))) {
                String redirectUri = this.getBaseUri() + REDIRECT_URI_ERROR;
                // Set the client id
                redirectUri = redirectUri.replace("{0}", requestInfoForm.getClientId());
                // Set the response type if needed
                if (!PojoUtil.isEmpty(requestInfoForm.getResponseType()))
                    redirectUri += "&response_type=" + requestInfoForm.getResponseType();
                // Set the redirect uri
                if (!PojoUtil.isEmpty(requestInfoForm.getRedirectUrl()))
                    redirectUri += "&redirect_uri=" + requestInfoForm.getRedirectUrl();
                // remove email access scope if present but not granted
                if (requestInfoForm.containsEmailReadPrivateScope() && !form.isEmailAccessAllowed()) {
                    requestInfoForm.removeEmailReadPrivateScope();
                }
                // Set the scope param
                if (!PojoUtil.isEmpty(requestInfoForm.getScopesAsString()))
                    redirectUri += "&scope=" + requestInfoForm.getScopesAsString();
                // Copy the state param if present
                if (!PojoUtil.isEmpty(requestInfoForm.getStateParam()))
                    redirectUri += "&state=" + requestInfoForm.getStateParam();
                requestInfoForm.setRedirectUrl(redirectUri);
                SavedRequest savedRequest = new HttpSessionRequestCache().getRequest(request, response);
                if (savedRequest != null)
                    LOGGER.info("OauthConfirmAccessController original request: " + savedRequest.getRedirectUrl());
                LOGGER.info("OauthConfirmAccessController form.getRedirectUri being sent to client browser: " + requestInfoForm.getRedirectUrl());
                return requestInfoForm;
            }
            usedCaptcha = true;
        }
        // Remove the session hash if needed
        if (request.getSession().getAttribute(RegistrationController.GRECAPTCHA_SESSION_ATTRIBUTE_NAME) != null) {
            request.getSession().removeAttribute(RegistrationController.GRECAPTCHA_SESSION_ATTRIBUTE_NAME);
        }
        //Strip any html code from names before validating them
        if (!PojoUtil.isEmpty(form.getFamilyNames())) {
            form.getFamilyNames().setValue(OrcidStringUtils.stripHtml(form.getFamilyNames().getValue()));
        }
        if (!PojoUtil.isEmpty(form.getGivenNames())) {
            form.getGivenNames().setValue(OrcidStringUtils.stripHtml(form.getGivenNames().getValue()));
        }
        // Check there are no errors
        registrationController.validateRegistrationFields(request, form);
        if (form.getErrors().isEmpty()) {
            // Register user
            try {
                // Locale
                Locale locale = RequestContextUtils.getLocale(request);
                // Ip
                String ip = OrcidRequestUtil.getIpAddress(request);
                registrationController.createMinimalRegistration(request, form, usedCaptcha, locale, ip);
            } catch (Exception e) {
                requestInfoForm.getErrors().add(getMessage("register.error.generalError"));
                return requestInfoForm;
            }
            // Authenticate user
            String email = form.getEmail().getValue();
            String password = form.getPassword().getValue();
            Authentication auth = authenticateUser(request, email, password);
            // Create authorization params
            SimpleSessionStatus status = new SimpleSessionStatus();
            Map<String, Object> model = new HashMap<String, Object>();
            Map<String, String> params = new HashMap<String, String>();
            Map<String, String> approvalParams = new HashMap<String, String>();
            fillOauthParams(requestInfoForm, params, approvalParams, form.getPersistentTokenEnabled(), form.isEmailAccessAllowed());
            // Authorize
            try {
                authorizationEndpoint.authorize(model, params, status, auth);
            } catch (RedirectMismatchException rUriError) {
                String redirectUri = this.getBaseUri() + REDIRECT_URI_ERROR;
                // Set the client id
                redirectUri = redirectUri.replace("{0}", requestInfoForm.getClientId());
                // Set the response type if needed
                if (!PojoUtil.isEmpty(requestInfoForm.getResponseType()))
                    redirectUri += "&response_type=" + requestInfoForm.getResponseType();
                // Set the redirect uri
                if (!PojoUtil.isEmpty(requestInfoForm.getRedirectUrl()))
                    redirectUri += "&redirect_uri=" + requestInfoForm.getRedirectUrl();
                // Set the scope param
                if (!PojoUtil.isEmpty(requestInfoForm.getScopesAsString()))
                    redirectUri += "&scope=" + requestInfoForm.getScopesAsString();
                // Copy the state param if present
                if (!PojoUtil.isEmpty(requestInfoForm.getStateParam()))
                    redirectUri += "&state=" + requestInfoForm.getStateParam();
                requestInfoForm.setRedirectUrl(redirectUri);
                LOGGER.info("OauthRegisterController being sent to client browser: " + requestInfoForm.getRedirectUrl());
                return requestInfoForm;
            }
            // Approve
            RedirectView view = (RedirectView) authorizationEndpoint.approveOrDeny(approvalParams, model, status, auth);
            requestInfoForm.setRedirectUrl(view.getUrl());
        }
    } else {
        requestInfoForm.setRedirectUrl(buildDenyRedirectUri(requestInfoForm.getRedirectUrl(), requestInfoForm.getStateParam()));
    }
    if (new HttpSessionRequestCache().getRequest(request, response) != null)
        new HttpSessionRequestCache().removeRequest(request, response);
    LOGGER.info("OauthConfirmAccessController form.getRedirectUri being sent to client browser: " + requestInfoForm.getRedirectUrl());
    return requestInfoForm;
}
Also used : Locale(java.util.Locale) HashMap(java.util.HashMap) HttpSessionRequestCache(org.springframework.security.web.savedrequest.HttpSessionRequestCache) RedirectMismatchException(org.springframework.security.oauth2.common.exceptions.RedirectMismatchException) Authentication(org.springframework.security.core.Authentication) RedirectMismatchException(org.springframework.security.oauth2.common.exceptions.RedirectMismatchException) RedirectView(org.springframework.web.servlet.view.RedirectView) RequestInfoForm(org.orcid.pojo.ajaxForm.RequestInfoForm) SavedRequest(org.springframework.security.web.savedrequest.SavedRequest) SimpleSessionStatus(org.springframework.web.bind.support.SimpleSessionStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

RedirectView (org.springframework.web.servlet.view.RedirectView)86 ModelAndView (org.springframework.web.servlet.ModelAndView)70 Test (org.junit.Test)34 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)19 HashMap (java.util.HashMap)18 View (org.springframework.web.servlet.View)17 Authentication (org.springframework.security.core.Authentication)14 AuthorizationRequest (org.springframework.security.oauth2.provider.AuthorizationRequest)13 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)9 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)8 TokenRequest (org.springframework.security.oauth2.provider.TokenRequest)8 DefaultUserApprovalHandler (org.springframework.security.oauth2.provider.approval.DefaultUserApprovalHandler)8 ServletException (javax.servlet.ServletException)7 RequestInfoForm (org.orcid.pojo.ajaxForm.RequestInfoForm)7 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)7 TokenGranter (org.springframework.security.oauth2.provider.TokenGranter)7 Principal (org.apereo.cas.authentication.principal.Principal)6 OAuthRegisteredService (org.apereo.cas.support.oauth.services.OAuthRegisteredService)6 CasProfile (org.pac4j.cas.profile.CasProfile)6 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)6