/** * Handles request in order to authenticate. * * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface $servletRequest The request instance * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance * * @return boolean TRUE if the authentication has been successful, else FALSE * * @throws \Exception */ public function handleRequest(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse) { // iterate over all servlets and return the matching one /** * @var string $urlPattern * @var \AppserverIo\Http\Authentication\AuthenticationInterface $authenticationAdapter */ foreach ($this->authenticationAdapters as $urlPattern => $authenticationAdapter) { // we'll match our URI against the URL pattern if (fnmatch($urlPattern, $servletRequest->getServletPath() . $servletRequest->getPathInfo())) { // the URI pattern matches, init the adapter and try to authenticate // check if auth header is not set in coming request headers if (!$servletRequest->hasHeader(Protocol::HEADER_AUTHORIZATION)) { // send header for challenge authentication against client $servletResponse->addHeader(HttpProtocol::HEADER_WWW_AUTHENTICATE, $authenticationAdapter->getAuthenticateHeader()); } // initialize the adapter with the current request $authenticationAdapter->init($servletRequest->getHeader(HttpProtocol::HEADER_AUTHORIZATION), $servletRequest->getMethod()); // try to authenticate the request $authenticated = $authenticationAdapter->authenticate(); if (!$authenticated) { // send header for challenge authentication against client $servletResponse->addHeader(HttpProtocol::HEADER_WWW_AUTHENTICATE, $authenticationAdapter->getAuthenticateHeader()); } return $authenticated; } } // we did not find an adapter for that URI pattern, no authentication required then return true; }
/** * 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; }