[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/oauthserver/ -> PhabricatorOAuthResponse.php (source)

   1  <?php
   2  
   3  final class PhabricatorOAuthResponse extends AphrontResponse {
   4  
   5    private $state;
   6    private $content;
   7    private $clientURI;
   8    private $error;
   9    private $errorDescription;
  10  
  11    private function getState() {
  12      return $this->state;
  13    }
  14    public function setState($state) {
  15      $this->state = $state;
  16      return $this;
  17    }
  18  
  19    private function getContent() {
  20      return $this->content;
  21    }
  22    public function setContent($content) {
  23      $this->content = $content;
  24      return $this;
  25    }
  26  
  27    private function getClientURI() {
  28      return $this->clientURI;
  29    }
  30    public function setClientURI(PhutilURI $uri) {
  31      $this->setHTTPResponseCode(302);
  32      $this->clientURI = $uri;
  33      return $this;
  34    }
  35    private function getFullURI() {
  36      $base_uri     = $this->getClientURI();
  37      $query_params = $this->buildResponseDict();
  38      foreach ($query_params as $key => $value) {
  39        $base_uri->setQueryParam($key, $value);
  40      }
  41      return $base_uri;
  42    }
  43  
  44    private function getError() {
  45      return $this->error;
  46    }
  47  
  48    public function setError($error) {
  49      // errors sometimes redirect to the client (302) but otherwise
  50      // the spec says all code 400
  51      if (!$this->getClientURI()) {
  52        $this->setHTTPResponseCode(400);
  53      }
  54      $this->error = $error;
  55      return $this;
  56    }
  57  
  58    private function getErrorDescription() {
  59      return $this->errorDescription;
  60    }
  61  
  62    public function setErrorDescription($error_description) {
  63      $this->errorDescription = $error_description;
  64      return $this;
  65    }
  66  
  67    public function __construct() {
  68      $this->setHTTPResponseCode(200);      // assume the best
  69      return $this;
  70    }
  71  
  72    public function getHeaders() {
  73      $headers = array(
  74        array('Content-Type', 'application/json'),
  75      );
  76      if ($this->getClientURI()) {
  77        $headers[] = array('Location', $this->getFullURI());
  78      }
  79      // TODO -- T844 set headers with X-Auth-Scopes, etc
  80      $headers = array_merge(parent::getHeaders(), $headers);
  81      return $headers;
  82    }
  83  
  84    private function buildResponseDict() {
  85      if ($this->getError()) {
  86        $content = array(
  87          'error'             => $this->getError(),
  88          'error_description' => $this->getErrorDescription(),
  89        );
  90        $this->setContent($content);
  91      }
  92  
  93      $content = $this->getContent();
  94      if (!$content) {
  95        return '';
  96      }
  97      if ($this->getState()) {
  98        $content['state'] = $this->getState();
  99      }
 100      return $content;
 101    }
 102  
 103    public function buildResponseString() {
 104      return $this->encodeJSONForHTTPResponse($this->buildResponseDict());
 105    }
 106  
 107  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1