Esempio n. 1
0
 public function handleRequest(HttpRequest $request)
 {
     $response = new HttpResponse(200, "application/json");
     try {
         if ("POST" !== $request->getRequestMethod()) {
             // method not allowed
             $response->setStatusCode(405);
             $response->setHeader("Allow", "POST");
         } else {
             $response->setHeader('Content-Type', 'application/json');
             $response->setHeader('Cache-Control', 'no-store');
             $response->setHeader('Pragma', 'no-cache');
             $response->setContent(Json::enc($this->_handleToken($request->getPostParameters(), $request->getBasicAuthUser(), $request->getBasicAuthPass())));
         }
     } catch (TokenException $e) {
         if ($e->getResponseCode() === 401) {
             $response->setHeader("WWW-Authenticate", 'Basic realm="OAuth Server"');
         }
         $response->setStatusCode($e->getResponseCode());
         $response->setHeader('Cache-Control', 'no-store');
         $response->setHeader('Pragma', 'no-cache');
         $response->setContent(Json::enc(array("error" => $e->getMessage(), "error_description" => $e->getDescription())));
         if (NULL !== $this->_logger) {
             $this->_logger->logFatal($e->getLogMessage(TRUE) . PHP_EOL . $request . PHP_EOL . $response);
         }
     }
     return $response;
 }
 public function testAuthentication()
 {
     $h = new HttpRequest("http://www.example.org", "GET");
     $h->setBasicAuthUser("foo");
     $h->setBasicAuthPass("bar");
     $this->assertEquals("foo", $h->getBasicAuthUser());
     $this->assertEquals("bar", $h->getBasicAuthPass());
 }