3 final class PhabricatorDuoFuture
8 private $integrationKey;
12 private $httpMethod = 'POST';
17 public function __construct() {
18 parent
::__construct(null);
21 public function setIntegrationKey($integration_key) {
22 $this->integrationKey
= $integration_key;
26 public function setSecretKey(PhutilOpaqueEnvelope
$key) {
27 $this->secretKey
= $key;
31 public function setAPIHostname($hostname) {
32 $this->apiHostname
= $hostname;
36 public function setMethod($method, array $parameters) {
37 $this->method
= $method;
38 $this->parameters
= $parameters;
42 public function setTimeout($timeout) {
43 $this->timeout
= $timeout;
47 public function getTimeout() {
48 return $this->timeout
;
51 public function setHTTPMethod($method) {
52 $this->httpMethod
= $method;
56 public function getHTTPMethod() {
57 return $this->httpMethod
;
60 protected function getProxiedFuture() {
62 if ($this->integrationKey
=== null) {
63 throw new PhutilInvalidStateException('setIntegrationKey');
66 if ($this->secretKey
=== null) {
67 throw new PhutilInvalidStateException('setSecretKey');
70 if ($this->apiHostname
=== null) {
71 throw new PhutilInvalidStateException('setAPIHostname');
74 if ($this->method
=== null ||
$this->parameters
=== null) {
75 throw new PhutilInvalidStateException('setMethod');
78 $path = (string)urisprintf('/auth/v2/%s', $this->method
);
80 $host = $this->apiHostname
;
81 $host = phutil_utf8_strtolower($host);
83 $data = $this->parameters
;
86 $http_method = $this->getHTTPMethod();
89 $data_parts = phutil_build_http_querystring($data);
98 $corpus = implode("\n", $corpus);
100 $signature = hash_hmac(
103 $this->secretKey
->openEnvelope());
104 $signature = new PhutilOpaqueEnvelope($signature);
106 if ($http_method === 'GET') {
108 $body_data = array();
114 $uri = id(new PhutilURI('', $uri_data))
115 ->setProtocol('https')
119 $future = id(new HTTPSFuture($uri, $body_data))
120 ->setHTTPBasicAuthCredentials($this->integrationKey
, $signature)
121 ->setMethod($http_method)
122 ->addHeader('Accept', 'application/json')
123 ->addHeader('Date', $date);
125 $timeout = $this->getTimeout();
127 $future->setTimeout($timeout);
130 $this->future
= $future;
133 return $this->future
;
136 protected function didReceiveResult($result) {
137 list($status, $body, $headers) = $result;
139 if ($status->isError()) {
144 $data = phutil_json_decode($body);
145 } catch (PhutilJSONParserException
$ex) {
146 throw new PhutilProxyException(
147 pht('Expected JSON response from Duo.'),