Ejemplo n.º 1
0
 /**
  * Try to authenticate the user making this request, based on the specified login configuration.
  *
  * Return TRUE if any specified constraint has been satisfied, or FALSE if we have created a response
  * challenge already.
  *
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The servlet request instance
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The servlet response instance
  *
  * @return boolean TRUE if authentication has already been processed on a request before, else FALSE
  * @throws \AppserverIo\Http\Authentication\AuthenticationException Is thrown if the request can't be authenticated
  */
 public function authenticate(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
 {
     // check if auth header is not set in coming request headers
     if ($servletRequest->hasHeader(Protocol::HEADER_AUTHORIZATION) === false) {
         // stop processing immediately
         $servletRequest->setDispatched(true);
         $servletResponse->setStatusCode(401);
         $servletResponse->addHeader(Protocol::HEADER_WWW_AUTHENTICATE, $this->getAuthenticateHeader());
         return false;
     }
     // load the raw login credentials
     $rawAuthData = $servletRequest->getHeader(Protocol::HEADER_AUTHORIZATION);
     // set auth hash got from auth data request header and check if username and password has been passed
     if (strstr($credentials = base64_decode(trim(strstr($rawAuthData, " "))), ':') === false) {
         // stop processing immediately
         $servletRequest->setDispatched(true);
         $servletResponse->setStatusCode(401);
         $servletResponse->addHeader(Protocol::HEADER_WWW_AUTHENTICATE, $this->getAuthenticateHeader());
         return false;
     }
     // get out username and password
     list($username, $password) = explode(':', $credentials);
     // query whether or not a username and a password has been passed
     if ($password === null || $username === null) {
         // stop processing immediately
         $servletRequest->setDispatched(true);
         $servletResponse->setStatusCode(401);
         $servletResponse->addHeader(Protocol::HEADER_WWW_AUTHENTICATE, $this->getAuthenticateHeader());
         return false;
     }
     // set username and password
     $this->username = new String($username);
     $this->password = new String($password);
     // load the realm to authenticate this request for
     /** @var AppserverIo\Appserver\ServletEngine\Security\RealmInterface $realm */
     $realm = $this->getAuthenticationManager()->getRealm($this->getRealmName());
     // authenticate the request and initialize the user principal
     $userPrincipal = $realm->authenticate($this->getUsername(), $this->getPassword());
     // query whether or not the realm returned an authenticated user principal
     if ($userPrincipal == null) {
         // stop processing immediately
         $servletRequest->setDispatched(true);
         $servletResponse->setStatusCode(401);
         $servletResponse->setBodyStream('Unauthorized');
         $servletResponse->addHeader(Protocol::HEADER_WWW_AUTHENTICATE, $this->getAuthenticateHeader());
         return false;
     }
     // add the user principal and the authentication type to the request
     $servletRequest->setUserPrincipal($userPrincipal);
     $servletRequest->setAuthType($this->getAuthType());
     return true;
 }
Ejemplo n.º 2
0
 /**
  * Forward's the request to the configured error page.
  *
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The servlet request instance
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The servlet response instance
  *
  * @return void
  */
 protected function forwardToErrorPage(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
 {
     // query whether or not we've an error page configured
     if ($formLoginConfig = $this->getConfigData()->getFormLoginConfig()) {
         if ($formErrorPage = $formLoginConfig->getFormErrorPage()) {
             // initialize the location to redirect to
             $location = $formErrorPage->__toString();
             if ($baseModifier = $servletRequest->getBaseModifier()) {
                 $location = $baseModifier . $location;
             }
             // redirect to the configured error page
             $servletRequest->setDispatched(true);
             $servletResponse->setStatusCode(307);
             $servletResponse->addHeader(Protocol::HEADER_LOCATION, $location);
             return;
         }
     }
     // redirect to the default error page
     $servletRequest->setAttribute(RequestHandlerKeys::ERROR_MESSAGE, 'Please configure a form-error-page when using auth-method \'Form\' in the login-config of your application\'s web.xml');
     $servletRequest->setDispatched(true);
     $servletResponse->setStatusCode(500);
 }
Ejemplo n.º 3
0
 /**
  * This method finally handles all PHP and user errors as well as the exceptions that
  * have been thrown through the servlet processing.
  *
  * @param \AppserverIo\Appserver\ServletEngine\RequestHandler        $requestHandler  The request handler instance
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The actual request instance
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The actual request instance
  *
  * @return void
  */
 public function handleErrors(RequestHandler $requestHandler, HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
 {
     // return immediately if we don't have any errors
     if (sizeof($errors = $requestHandler->getErrors()) === 0) {
         return;
     }
     // iterate over the errors to process each of them
     foreach ($errors as $error) {
         // prepare the error message
         $message = $this->prepareMessage($error);
         // query whether or not we have to log the error
         if (Boolean::valueOf(new String(ini_get('log_errors')))->booleanValue()) {
             // create a local copy of the application
             if ($application = $servletRequest->getContext()) {
                 // try to load the system logger from the application
                 if ($systemLogger = $application->getLogger(LoggerUtils::SYSTEM)) {
                     $systemLogger->log($this->mapLogLevel($error), $message);
                 }
             }
         }
         // query whether or not, the error has an status code
         if ($statusCode = $error->getStatusCode()) {
             $servletResponse->setStatusCode($statusCode);
         }
     }
     // we add the error to the servlet request
     $servletRequest->setAttribute(RequestHandlerKeys::ERROR_MESSAGES, $errors);
     // we append the the errors to the body stream if display_errors is on
     if (Boolean::valueOf(new String(ini_get('display_errors')))->booleanValue()) {
         $servletResponse->appendBodyStream(implode('<br/>', $errors));
     }
     // query whether or not we've a client or an server error
     if ($servletResponse->getStatusCode() > 399) {
         try {
             // create a local copy of the application
             $application = $servletRequest->getContext();
             // inject the application and servlet response
             $servletRequest->injectResponse($servletResponse);
             $servletRequest->injectContext($application);
             // load the servlet context instance
             $servletManager = $application->search(ServletContextInterface::IDENTIFIER);
             // initialize the request URI for the error page to be rendered
             $requestUri = null;
             // iterate over the configured error pages to find a matching one
             foreach ($servletManager->getErrorPages() as $errorCodePattern => $errorPage) {
                 // query whether or not we found an error page configured for the actual status code
                 if (fnmatch($errorCodePattern, $servletResponse->getStatusCode())) {
                     $requestUri = $errorPage;
                     break;
                 }
             }
             // query whether or not we've an found a configured error page
             if ($requestUri == null) {
                 throw new ServletException(sprintf('Please configure an error page for status code %s', $servletResponse->getStatusCode()));
             }
             // initialize the request URI
             $servletRequest->setRequestUri($requestUri);
             // prepare the request with the new data
             $servletRequest->prepare();
             // reset the body stream to remove content, that has already been appended
             $servletResponse->resetBodyStream();
             // load the servlet path and session-ID
             $servletPath = $servletRequest->getServletPath();
             $sessionId = $servletRequest->getProposedSessionId();
             // load and process the servlet
             $servlet = $servletManager->lookup($servletPath, $sessionId);
             $servlet->service($servletRequest, $servletResponse);
         } catch (\Exception $e) {
             // finally log the exception
             $application->getInitialContext()->getSystemLogger()->critical($e->__toString());
             // append the exception message to the body stream
             $servletResponse->appendBodyStream($e->__toString());
         }
     }
 }
Ejemplo n.º 4
0
 /**
  * Tries to load the requested file and adds the content to the response.
  *
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The request instance
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance
  *
  * @return void
  */
 public function doGet(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
 {
     // load \Mage
     $this->load();
     // init globals
     $this->initGlobals($servletRequest);
     // run \Mage and set content
     $servletResponse->appendBodyStream($this->run($servletRequest));
     // add the status code we've caught from the legacy app
     $servletResponse->setStatusCode(appserver_get_http_response_code());
     // add this header to prevent .php request to be cached
     $servletResponse->addHeader(Protocol::HEADER_EXPIRES, '19 Nov 1981 08:52:00 GMT');
     $servletResponse->addHeader(Protocol::HEADER_CACHE_CONTROL, 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
     $servletResponse->addHeader(Protocol::HEADER_PRAGMA, 'no-cache');
     // set per default text/html mimetype
     $servletResponse->addHeader(Protocol::HEADER_CONTENT_TYPE, 'text/html');
     // grep headers and set to response object
     foreach (appserver_get_headers(true) as $i => $h) {
         // set headers defined in sapi headers
         $h = explode(':', $h, 2);
         if (isset($h[1])) {
             // load header key and value
             $key = trim($h[0]);
             $value = trim($h[1]);
             // if no status, add the header normally
             if ($key === Protocol::HEADER_STATUS) {
                 // set status by Status header value which is only used by fcgi sapi's normally
                 $servletResponse->setStatusCode($value);
             } elseif ($key === Protocol::HEADER_SET_COOKIE) {
                 $servletResponse->addHeader($key, $value, true);
             } else {
                 $servletResponse->addHeader($key, $value);
             }
         }
     }
 }
Ejemplo n.º 5
0
 /**
  * Tries to load the requested vhosts and adds them to the response.
  *
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The request instance
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance
  *
  * @return void
  * @see \AppserverIo\Psr\Servlet\Http\HttpServlet::doGet()
  *
  * @SWG\Get(
  *   path="/virtualHosts.do",
  *   tags={"virtualHosts"},
  *   summary="List's all virtual hosts",
  *   @SWG\Response(
  *     response=200,
  *     description="A list with the available virtual hosts",
  *     @SWG\Schema(
  *       type="array",
  *       @SWG\Items(ref="#/definitions/VirtualHostOverviewData")
  *     )
  *   ),
  *   @SWG\Response(
  *     response="500",
  *     description="Internal Server Error"
  *   )
  * )
  *
  * @SWG\Get(
  *   path="/virtualHosts.do/{id}",
  *   tags={"virtualHosts"},
  *   summary="Loads the virtual host with the passed ID",
  *   @SWG\Parameter(
  *      name="id",
  *      in="path",
  *      description="The UUID of the virtual host to load",
  *      required=true,
  *      type="string"
  *   ),
  *   @SWG\Response(
  *     response=200,
  *     description="The requested virtual host",
  *     @SWG\Schema(
  *       ref="#/definitions/VirtualHostViewData"
  *     )
  *   ),
  *   @SWG\Response(
  *     response="500",
  *     description="Internal Server Error"
  *   )
  * )
  */
 public function doGet(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
 {
     try {
         // load the requested path info, e. g. /api/applications.do/example/
         $pathInfo = trim($servletRequest->getPathInfo(), '/');
         // extract the entity and the ID, if available
         list($id, ) = explode('/', $pathInfo);
         // query whether we've found an ID or not
         if ($id == null) {
             $content = $this->getVirtualHostProcessor()->findAll();
         } else {
             $content = $this->getVirtualHostProcessor()->load($id);
         }
     } catch (\Exception $e) {
         // set error message and status code
         $content = $e->getMessage();
         $servletResponse->setStatusCode(500);
     }
     // add the result to the request
     $servletRequest->setAttribute(RequestKeys::RESULT, $content);
 }
 public function prepareResponse(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
 {
     error_log("Now in " . __METHOD__ . " handling request " . $servletRequest->getUri());
     // load the session and persist the data from $_SESSION
     $session = $servletRequest->getSession();
     if ($session != null && isset($_SESSION)) {
         foreach ($_SESSION as $namespace => $data) {
             if ($namespace !== 'identifier') {
                 error_log("Now add data for session {$session->getId()} and namespace {$namespace}: " . PHP_EOL . var_export($data, true));
                 $session->putData($namespace, $data);
             }
         }
     }
     // add the status code we've caught from the legacy app
     $servletResponse->setStatusCode(appserver_get_http_response_code());
     // add this header to prevent .php request to be cached
     $servletResponse->addHeader(Protocol::HEADER_EXPIRES, '19 Nov 1981 08:52:00 GMT');
     $servletResponse->addHeader(Protocol::HEADER_CACHE_CONTROL, 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
     $servletResponse->addHeader(Protocol::HEADER_PRAGMA, 'no-cache');
     // set per default text/html mimetype
     $servletResponse->addHeader(Protocol::HEADER_CONTENT_TYPE, 'text/html');
     error_log("============= RESPONSE before appserver_get_headers(true) =================");
     error_log(var_export($servletResponse, true));
     // grep headers and set to response object
     foreach (appserver_get_headers(true) as $i => $h) {
         // set headers defined in sapi headers
         $h = explode(':', $h, 2);
         if (isset($h[1])) {
             // load header key and value
             $key = trim($h[0]);
             $value = trim($h[1]);
             // if no status, add the header normally
             if ($key === Protocol::HEADER_STATUS) {
                 // set status by Status header value which is only used by fcgi sapi's normally
                 $servletResponse->setStatus($value);
             } elseif ($key === Protocol::HEADER_SET_COOKIE) {
                 $servletResponse->addHeader($key, $value, true);
             } else {
                 $servletResponse->addHeader($key, $value);
             }
         }
     }
     error_log("============= RESPONSE after appserver_get_headers(true) =================");
     error_log(var_export($servletResponse, true));
 }