public Authentication validateRequest()

in plugins/jetty9/src/main/java/org/apache/cxf/fediz/jetty9/FederationAuthenticator.java [151:249]


    public Authentication validateRequest(ServletRequest req, ServletResponse res, boolean mandatory)
        throws ServerAuthException {

        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)res;

        HttpSession session = request.getSession(true);

        String contextName = request.getSession().getServletContext().getContextPath();
        if (contextName == null || contextName.isEmpty()) {
            contextName = "/";
        }
        FedizContext fedConfig = getContextConfiguration(contextName);

        // Check to see if it is a metadata request
        MetadataDocumentHandler mdHandler = new MetadataDocumentHandler(fedConfig);
        if (mdHandler.canHandleRequest(request)) {
            Authentication authentication = Authentication.SEND_FAILURE;
            if (mdHandler.handleRequest(request, response)) {
                authentication = Authentication.SEND_CONTINUE;
            }
            return authentication;
        }

        if (!mandatory) {
            return new DeferredAuthentication(this);
        }

        try {
            req.setCharacterEncoding(this.encoding);
        } catch (UnsupportedEncodingException ex) {
            LOG.warn("Unsupported encoding '" + this.encoding + "'", ex);
        }

        try {
            String action = request.getParameter(FederationConstants.PARAM_ACTION);
            Authentication authentication = null;

            // Handle a request for authentication.
            if (isSignInRequest(request, fedConfig)) {
                authentication = handleSignInRequest(request, response, session, fedConfig);
            } else if (FederationConstants.ACTION_SIGNOUT_CLEANUP.equals(action)) {
                authentication = handleSignOutCleanup(response, session);
            } else if (!FederationConstants.ACTION_SIGNOUT.equals(action) && action != null) {
                LOG.warn("Not supported action found in parameter wa: " + action);
                response.sendError(HttpServletResponse.SC_BAD_REQUEST);
                authentication = Authentication.UNAUTHENTICATED;
            }

            if (authentication != null) {
                return authentication;
            }

            // Look for cached authentication
            authentication = handleCachedAuthentication(request, response, session, fedConfig);
            if (authentication != null) {
                return authentication;
            }

            // if we can't send challenge
            if (DeferredAuthentication.isDeferred(response)) {
                LOG.debug("auth deferred {}", session.getId());
                return Authentication.UNAUTHENTICATED;
            }

            // remember the current URI
            synchronized (session) {
                // But only if it is not set already, or we save every uri that leads to a login form redirect
                if (session.getAttribute(J_URI) == null) { // || alwaysSaveUri)
                    StringBuffer buf = request.getRequestURL();
                    if (request.getQueryString() != null) {
                        buf.append('?').append(request.getQueryString());
                    }
                    session.setAttribute(J_URI, buf.toString());

                    if (MimeTypes.Type.FORM_ENCODED.asString().equals(req.getContentType())
                        && HttpMethod.POST.asString().equals(request.getMethod())) {
                        Request baseRequest = (Request)req;
                            //(req instanceof Request)?(Request)req:HttpConnection.getCurrentConnection().getRequest();
                        // Load the parameters (previously extractParameters)
                        baseRequest.getParameterMap();
                        session.setAttribute(J_POST, new MultiMap<String>(baseRequest.getQueryParameters()));
                    }
                }
            }

            FedizProcessor wfProc =
                FedizProcessorFactory.newFedizProcessor(fedConfig.getProtocol());
            signInRedirectToIssuer(request, response, wfProc, session);

            return Authentication.SEND_CONTINUE;

        } catch (IOException e) {
            throw new ServerAuthException(e);
        }
        /*
         * catch (ServletException e) { throw new ServerAuthException(e); }
         */
    }