protected function loadOAuthAccountData() { $uri = new PhutilURI('https://api.amazon.com/user/profile'); $uri->setQueryParam('access_token', $this->getAccessToken()); $future = new HTTPSFuture($uri); list($body) = $future->resolvex(); try { return phutil_json_decode($body); } catch (PhutilJSONParserException $ex) { throw new PhutilProxyException(pht('Expected valid JSON response from Amazon account data request.'), $ex); } }
protected function loadOAuthAccountData() { $uri = new PhutilURI('https://api.amazon.com/user/profile'); $uri->setQueryParam('access_token', $this->getAccessToken()); $future = new HTTPSFuture($uri); list($body) = $future->resolvex(); $data = json_decode($body, true); if (!is_array($data)) { throw new Exception("Expected valid JSON response from Amazon account data request, " . "got: " . $body); } return $data; }
/** * Load contents of remote URI. Behaves pretty much like * `@file_get_contents($uri)` but doesn't require `allow_url_fopen`. * * @param string * @param float * @return string|false */ public static function loadContent($uri, $timeout = null) { $future = new HTTPSFuture($uri); if ($timeout !== null) { $future->setTimeout($timeout); } try { list($body) = $future->resolvex(); return $body; } catch (HTTPFutureResponseStatus $ex) { return false; } }
protected function loadOAuthAccountData() { $uri = new PhutilURI('https://api.github.com/user'); $uri->setQueryParam('access_token', $this->getAccessToken()); $future = new HTTPSFuture($uri); // NOTE: GitHub requires a User-Agent string. $future->addHeader('User-Agent', 'PhutilAuthAdapterOAuthGitHub'); list($body) = $future->resolvex(); $data = json_decode($body, true); if (!is_array($data)) { throw new Exception("Expected valid JSON response from GitHub account data request, " . "got: " . $body); } return $data; }
protected function loadOAuthAccountData() { $uri = new PhutilURI('https://api.github.com/user'); $uri->setQueryParam('access_token', $this->getAccessToken()); $future = new HTTPSFuture($uri); // NOTE: GitHub requires a User-Agent string. $future->addHeader('User-Agent', __CLASS__); list($body) = $future->resolvex(); try { return phutil_json_decode($body); } catch (PhutilJSONParserException $ex) { throw new PhutilProxyException(pht('Expected valid JSON response from GitHub account data request.'), $ex); } }
protected function loadOAuthAccountData() { $uri = new PhutilURI('https://disqus.com/api/3.0/users/details.json'); $uri->setQueryParam('api_key', $this->getClientID()); $uri->setQueryParam('access_token', $this->getAccessToken()); $uri = (string) $uri; $future = new HTTPSFuture($uri); $future->setMethod('GET'); list($body) = $future->resolvex(); $data = json_decode($body, true); if (!is_array($data)) { throw new Exception("Expected valid JSON response from Disqus account data request, " . "got: " . $body); } return $data['response']; }
protected function loadOAuthAccountData() { $uri = new PhutilURI('https://disqus.com/api/3.0/users/details.json'); $uri->setQueryParam('api_key', $this->getClientID()); $uri->setQueryParam('access_token', $this->getAccessToken()); $uri = (string) $uri; $future = new HTTPSFuture($uri); $future->setMethod('GET'); list($body) = $future->resolvex(); try { $data = phutil_json_decode($body); return $data['response']; } catch (PhutilJSONParserException $ex) { throw new PhutilProxyException(pht('Expected valid JSON response from Disqus account data request.'), $ex); } }
private function executeRequest($path, array $data, $is_write = false) { $uri = new PhutilURI($this->uri); $data = json_encode($data); $uri->setPath($path); $future = new HTTPSFuture($uri, $data); if ($is_write) { $future->setMethod('PUT'); } if ($this->getTimeout()) { $future->setTimeout($this->getTimeout()); } list($body) = $future->resolvex(); if ($is_write) { return null; } $body = json_decode($body, true); if (!is_array($body)) { throw new Exception("elasticsearch server returned invalid JSON!"); } return $body; }
public function send() { $user = PhabricatorEnv::getEnvConfig('sendgrid.api-user'); $key = PhabricatorEnv::getEnvConfig('sendgrid.api-key'); if (!$user || !$key) { throw new Exception("Configure 'sendgrid.api-user' and 'sendgrid.api-key' to use " . "SendGrid for mail delivery."); } $params = array(); $ii = 0; foreach (idx($this->params, 'tos', array()) as $to) { $params['to[' . $ii++ . ']'] = $to; } $params['subject'] = idx($this->params, 'subject'); $params['text'] = idx($this->params, 'body'); if (idx($this->params, 'html-body')) { $params['html'] = idx($this->params, 'html-body'); } $params['from'] = idx($this->params, 'from'); if (idx($this->params, 'from-name')) { $params['fromname'] = $this->params['from-name']; } if (idx($this->params, 'reply-to')) { $replyto = $this->params['reply-to']; // Pick off the email part, no support for the name part in this API. $params['replyto'] = $replyto[0]['email']; } foreach (idx($this->params, 'files', array()) as $name => $data) { $params['files[' . $name . ']'] = $data; } $headers = idx($this->params, 'headers', array()); // See SendGrid Support Ticket #29390; there's no explicit REST API support // for CC right now but it works if you add a generic "Cc" header. // // SendGrid said this is supported: // "You can use CC as you are trying to do there [by adding a generic // header]. It is supported despite our limited documentation to this // effect, I am glad you were able to figure it out regardless. ..." if (idx($this->params, 'ccs')) { $headers[] = array('Cc', implode(', ', $this->params['ccs'])); } if ($headers) { // Convert to dictionary. $headers = ipull($headers, 1, 0); $headers = json_encode($headers); $params['headers'] = $headers; } $params['api_user'] = $user; $params['api_key'] = $key; $future = new HTTPSFuture('https://sendgrid.com/api/mail.send.json', $params); $future->setMethod('POST'); list($body) = $future->resolvex(); $response = json_decode($body, true); if (!is_array($response)) { throw new Exception("Failed to JSON decode response: {$body}"); } if ($response['message'] !== 'success') { $errors = implode(';', $response['errors']); throw new Exception("Request failed with errors: {$errors}."); } return true; }
private function makeTokenRequest(array $params) { $uri = $this->getTokenBaseURI(); $query_data = array('client_id' => $this->getClientID(), 'client_secret' => $this->getClientSecret()->openEnvelope(), 'redirect_uri' => $this->getRedirectURI()) + $params; $future = new HTTPSFuture($uri, $query_data); $future->setMethod('POST'); list($body) = $future->resolvex(); $data = $this->readAccessTokenResponse($body); if (isset($data['expires_in'])) { $data['expires_epoch'] = $data['expires_in']; } else { if (isset($data['expires'])) { $data['expires_epoch'] = $data['expires']; } } // If we got some "expires" value back, interpret it as an epoch timestamp // if it's after the year 2010 and as a relative number of seconds // otherwise. if (isset($data['expires_epoch'])) { if ($data['expires_epoch'] < 60 * 60 * 24 * 365 * 40) { $data['expires_epoch'] += time(); } } if (isset($data['error'])) { throw new Exception('Access token error: ' . $data['error']); } return $data; }
private function executeRequest($path, array $data, $method = 'GET') { $uri = new PhutilURI($this->uri); $uri->setPath($this->index); $uri->appendPath($path); $data = json_encode($data); $future = new HTTPSFuture($uri, $data); if ($method != 'GET') { $future->setMethod($method); } if ($this->getTimeout()) { $future->setTimeout($this->getTimeout()); } list($body) = $future->resolvex(); if ($method != 'GET') { return null; } try { return phutil_json_decode($body); } catch (PhutilJSONParserException $ex) { throw new PhutilProxyException(pht('ElasticSearch server returned invalid JSON!'), $ex); } }
public function processRequest() { $provider = $this->provider; $auth_enabled = $provider->isProviderEnabled(); $client_id = $provider->getClientID(); $client_secret = $provider->getClientSecret(); $key = $provider->getProviderKey(); $name = phutil_escape_html($provider->getProviderName()); $res_ok = '<strong style="color: #00aa00;">OK</strong>'; $res_no = '<strong style="color: #aa0000;">NO</strong>'; $res_na = '<strong style="color: #999999;">N/A</strong>'; $results = array(); $auth_key = $key . '.auth-enabled'; if (!$auth_enabled) { $results[$auth_key] = array($res_no, 'false', $name . ' authentication is disabled in the configuration. Edit the ' . 'Phabricator configuration to enable "' . $auth_key . '".'); } else { $results[$auth_key] = array($res_ok, 'true', $name . ' authentication is enabled.'); } $client_id_key = $key . '.application-id'; if (!$client_id) { $results[$client_id_key] = array($res_no, null, 'No ' . $name . ' Application ID is configured. Edit the Phabricator ' . 'configuration to specify an application ID in ' . '"' . $client_id_key . '". ' . $provider->renderGetClientIDHelp()); } else { $results[$client_id_key] = array($res_ok, $client_id, 'Application ID is set.'); } $client_secret_key = $key . '.application-secret'; if (!$client_secret) { $results[$client_secret_key] = array($res_no, null, 'No ' . $name . ' Application secret is configured. Edit the ' . 'Phabricator configuration to specify an Application Secret, in ' . '"' . $client_secret_key . '". ' . $provider->renderGetClientSecretHelp()); } else { $results[$client_secret_key] = array($res_ok, "It's a secret!", 'Application secret is set.'); } $timeout = 5; $internet = HTTPSFuture::loadContent("http://google.com/", $timeout); if ($internet === false) { $results['internet'] = array($res_no, null, 'Unable to make an HTTP request to Google. Check your outbound ' . 'internet connection and firewall/filtering settings.'); } else { $results['internet'] = array($res_ok, null, 'Internet seems OK.'); } $test_uris = $provider->getTestURIs(); foreach ($test_uris as $uri) { $success = HTTPSFuture::loadContent($uri, $timeout); if ($success === false) { $results[$uri] = array($res_no, null, "Unable to make an HTTP request to {$uri}. {$name} may be " . 'down or inaccessible.'); } else { $results[$uri] = array($res_ok, null, 'Made a request to ' . $uri . '.'); } } if ($provider->shouldDiagnoseAppLogin()) { $test_uri = new PhutilURI($provider->getTokenURI()); $test_uri->setQueryParams(array('client_id' => $client_id, 'client_secret' => $client_secret, 'grant_type' => 'client_credentials')); $future = new HTTPSFuture($test_uri); $future->setTimeout($timeout); try { list($body) = $future->resolvex(); $results['App Login'] = array($res_ok, '(A Valid Token)', "Raw application login to {$name} works."); } catch (Exception $ex) { if ($ex instanceof HTTPFutureResponseStatusCURL) { $results['App Login'] = array($res_no, null, "Unable to perform an application login with your Application ID " . "and Application Secret. You may have mistyped or misconfigured " . "them; {$name} may have revoked your authorization; or {$name} " . "may be having technical problems."); } else { $data = json_decode($token_value, true); if (!is_array($data)) { $results['App Login'] = array($res_no, $token_value, "Application Login failed but the provider did not respond " . "with valid JSON error information. {$name} may be experiencing " . "technical problems."); } else { $results['App Login'] = array($res_no, null, "Application Login failed with error: " . $token_value); } } } } return $this->renderResults($results); }
public function send() { $key = PhabricatorEnv::getEnvConfig('mailgun.api-key'); $domain = PhabricatorEnv::getEnvConfig('mailgun.domain'); $params = array(); $params['to'] = implode(', ', idx($this->params, 'tos', array())); $params['subject'] = idx($this->params, 'subject'); $params['text'] = idx($this->params, 'body'); if (idx($this->params, 'html-body')) { $params['html'] = idx($this->params, 'html-body'); } $from = idx($this->params, 'from'); if (idx($this->params, 'from-name')) { $params['from'] = "{$this->params['from-name']} <{$from}>"; } else { $params['from'] = $from; } if (idx($this->params, 'reply-to')) { $replyto = $this->params['reply-to']; $params['h:reply-to'] = implode(', ', $replyto); } if (idx($this->params, 'ccs')) { $params['cc'] = implode(', ', $this->params['ccs']); } foreach (idx($this->params, 'headers', array()) as $header) { list($name, $value) = $header; $params['h:' . $name] = $value; } $future = new HTTPSFuture("https://*****:*****@api.mailgun.net/v2/{$domain}/messages", $params); $future->setMethod('POST'); foreach ($this->attachments as $attachment) { $future->attachFileData('attachment', $attachment['data'], $attachment['name'], $attachment['type']); } list($body) = $future->resolvex(); $response = json_decode($body, true); if (!is_array($response)) { throw new Exception("Failed to JSON decode response: {$body}"); } if (!idx($response, 'id')) { $message = $response['message']; throw new Exception("Request failed with errors: {$message}."); } return true; }
private function retrieveAccessToken(PhabricatorOAuthProvider $provider) { $request = $this->getRequest(); $token = $request->getStr('confirm_token'); if ($token) { $this->tokenExpires = $request->getInt('expires'); $this->accessToken = $token; $this->oauthState = $request->getStr('state'); return null; } $client_id = $provider->getClientID(); $client_secret = $provider->getClientSecret(); $redirect_uri = $provider->getRedirectURI(); $auth_uri = $provider->getTokenURI(); $code = $request->getStr('code'); $query_data = array('client_id' => $client_id, 'client_secret' => $client_secret, 'redirect_uri' => $redirect_uri, 'code' => $code) + $provider->getExtraTokenParameters(); $future = new HTTPSFuture($auth_uri, $query_data); $future->setMethod('POST'); try { list($response) = $future->resolvex(); } catch (Exception $ex) { return $this->buildErrorResponse(new PhabricatorOAuthFailureView()); } $data = $provider->decodeTokenResponse($response); $token = idx($data, 'access_token'); if (!$token) { return $this->buildErrorResponse(new PhabricatorOAuthFailureView()); } $this->tokenExpires = $provider->getTokenExpiryFromArray($data); $this->accessToken = $token; $this->oauthState = $request->getStr('state'); return null; }