Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / oauthserver / PhabricatorOAuthResponse.php
blobe0fca827b408496ad9f65b46a3f2d4630cf66156
1 <?php
3 final class PhabricatorOAuthResponse extends AphrontResponse {
5 private $state;
6 private $content;
7 private $clientURI;
8 private $error;
9 private $errorDescription;
11 private function getState() {
12 return $this->state;
14 public function setState($state) {
15 $this->state = $state;
16 return $this;
19 private function getContent() {
20 return $this->content;
22 public function setContent($content) {
23 $this->content = $content;
24 return $this;
27 private function getClientURI() {
28 return $this->clientURI;
30 public function setClientURI(PhutilURI $uri) {
31 $this->setHTTPResponseCode(302);
32 $this->clientURI = $uri;
33 return $this;
35 private function getFullURI() {
36 $base_uri = $this->getClientURI();
37 $query_params = $this->buildResponseDict();
38 foreach ($query_params as $key => $value) {
39 $base_uri->replaceQueryParam($key, $value);
41 return $base_uri;
44 private function getError() {
45 return $this->error;
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);
54 $this->error = $error;
55 return $this;
58 private function getErrorDescription() {
59 return $this->errorDescription;
62 public function setErrorDescription($error_description) {
63 $this->errorDescription = $error_description;
64 return $this;
67 public function __construct() {
68 $this->setHTTPResponseCode(200); // assume the best
71 public function getHeaders() {
72 $headers = array(
73 array('Content-Type', 'application/json'),
75 if ($this->getClientURI()) {
76 $headers[] = array('Location', $this->getFullURI());
78 // TODO -- T844 set headers with X-Auth-Scopes, etc
79 $headers = array_merge(parent::getHeaders(), $headers);
80 return $headers;
83 private function buildResponseDict() {
84 if ($this->getError()) {
85 $content = array(
86 'error' => $this->getError(),
87 'error_description' => $this->getErrorDescription(),
89 $this->setContent($content);
92 $content = $this->getContent();
93 if (!$content) {
94 return '';
96 if ($this->getState()) {
97 $content['state'] = $this->getState();
99 return $content;
102 public function buildResponseString() {
103 return $this->encodeJSONForHTTPResponse($this->buildResponseDict());