Page MenuHomePhabricator

D9004.id21387.diff
No OneTemporary

D9004.id21387.diff

diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php
--- a/src/__phutil_library_map__.php
+++ b/src/__phutil_library_map__.php
@@ -106,6 +106,7 @@
'PhutilAuthAdapterOAuthJIRA' => 'auth/PhutilAuthAdapterOAuthJIRA.php',
'PhutilAuthAdapterOAuthTwitch' => 'auth/PhutilAuthAdapterOAuthTwitch.php',
'PhutilAuthAdapterOAuthTwitter' => 'auth/PhutilAuthAdapterOAuthTwitter.php',
+ 'PhutilAuthAdapterOAuthWordPress' => 'auth/PhutilAuthAdapterOAuthWordPress.php',
'PhutilAuthAdapterPersona' => 'auth/PhutilAuthAdapterPersona.php',
'PhutilAuthConfigurationException' => 'auth/exception/PhutilAuthConfigurationException.php',
'PhutilAuthCredentialException' => 'auth/exception/PhutilAuthCredentialException.php',
@@ -308,6 +309,7 @@
'PhutilURITestCase' => 'parser/__tests__/PhutilURITestCase.php',
'PhutilUTF8TestCase' => 'utils/__tests__/PhutilUTF8TestCase.php',
'PhutilUtilsTestCase' => 'utils/__tests__/PhutilUtilsTestCase.php',
+ 'PhutilWordPressFuture' => 'future/wordpress/PhutilWordPressFuture.php',
'PhutilXHPASTSyntaxHighlighter' => 'markup/syntax/highlighter/PhutilXHPASTSyntaxHighlighter.php',
'PhutilXHPASTSyntaxHighlighterFuture' => 'markup/syntax/highlighter/xhpast/PhutilXHPASTSyntaxHighlighterFuture.php',
'PhutilXHPASTSyntaxHighlighterTestCase' => 'markup/syntax/highlighter/__tests__/PhutilXHPASTSyntaxHighlighterTestCase.php',
@@ -525,6 +527,7 @@
'PhutilAuthAdapterOAuthJIRA' => 'PhutilAuthAdapterOAuth1',
'PhutilAuthAdapterOAuthTwitch' => 'PhutilAuthAdapterOAuth',
'PhutilAuthAdapterOAuthTwitter' => 'PhutilAuthAdapterOAuth1',
+ 'PhutilAuthAdapterOAuthWordPress' => 'PhutilAuthAdapterOAuth',
'PhutilAuthAdapterPersona' => 'PhutilAuthAdapter',
'PhutilAuthConfigurationException' => 'PhutilAuthException',
'PhutilAuthCredentialException' => 'PhutilAuthException',
diff --git a/src/auth/PhutilAuthAdapterOAuthWordPress.php b/src/auth/PhutilAuthAdapterOAuthWordPress.php
new file mode 100644
--- /dev/null
+++ b/src/auth/PhutilAuthAdapterOAuthWordPress.php
@@ -0,0 +1,73 @@
+<?php
+
+/**
+ * Authentication adapter for WordPress.com OAuth2.
+ */
+final class PhutilAuthAdapterOAuthWordPress extends PhutilAuthAdapterOAuth {
+
+ public function getAdapterType() {
+ return 'wordpress';
+ }
+
+ public function getAdapterDomain() {
+ return 'wordpress.com';
+ }
+
+ public function getAccountID() {
+ return $this->getOAuthAccountData('ID');
+ }
+
+ public function getAccountEmail() {
+ return $this->getOAuthAccountData('email');
+ }
+
+ public function getAccountName() {
+ return $this->getOAuthAccountData('username');
+ }
+
+ public function getAccountImageURI() {
+ return $this->getOAuthAccountData('avatar_URL');
+ }
+
+ public function getAccountURI() {
+ return $this->getOAuthAccountData('profile_URL');
+ }
+
+ public function getAccountRealName() {
+ return $this->getOAuthAccountData('display_name');
+ }
+
+ protected function getAuthenticateBaseURI() {
+ return 'https://public-api.wordpress.com/oauth2/authorize';
+ }
+
+ protected function getTokenBaseURI() {
+ return 'https://public-api.wordpress.com/oauth2/token';
+ }
+
+ public function getScope() {
+ return 'user_read';
+ }
+
+ public function getExtraAuthenticateParameters() {
+ return array(
+ 'response_type' => 'code',
+ 'blog_id' => 0,
+ );
+ }
+
+ public function getExtraTokenParameters() {
+ return array(
+ 'grant_type' => 'authorization_code',
+ );
+ }
+
+ protected function loadOAuthAccountData() {
+ return id(new PhutilWordPressFuture())
+ ->setClientID($this->getClientID())
+ ->setAccessToken($this->getAccessToken())
+ ->setRawWordPressQuery('/me/')
+ ->resolve();
+ }
+
+}
diff --git a/src/future/wordpress/PhutilWordPressFuture.php b/src/future/wordpress/PhutilWordPressFuture.php
new file mode 100644
--- /dev/null
+++ b/src/future/wordpress/PhutilWordPressFuture.php
@@ -0,0 +1,89 @@
+<?php
+
+/**
+ * @group wordpress
+ */
+final class PhutilWordPressFuture extends FutureProxy {
+
+ private $future;
+ private $clientID;
+ private $accessToken;
+ private $action;
+ private $params;
+ private $method = 'GET';
+
+ public function __construct() {
+ parent::__construct(null);
+ }
+
+ public function setAccessToken($token) {
+ $this->accessToken = $token;
+ return $this;
+ }
+
+ public function setClientID($client_id) {
+ $this->clientID = $client_id;
+ return $this;
+ }
+
+ public function setRawWordPressQuery($action, array $params = array()) {
+ $this->action = $action;
+ $this->params = $params;
+ return $this;
+ }
+
+ public function setMethod($method) {
+ $this->method = $method;
+ return $this;
+ }
+
+ protected function getProxiedFuture() {
+ if (!$this->future) {
+ $params = $this->params;
+
+ if (!$this->action) {
+ throw new Exception("You must setRawWordPressQuery()!");
+ }
+
+ if (!$this->accessToken) {
+ throw new Exception("You must setAccessToken()!");
+ }
+
+ $uri = new PhutilURI('https://public-api.wordpress.com/');
+ $uri->setPath('/rest/v1/'.ltrim($this->action, '/'));
+
+ $future = new HTTPSFuture($uri);
+ $future->setData($this->params);
+ $future->setMethod($this->method);
+
+ // NOTE: This is how WordPress.com REST API authenticates
+ $future->addHeader('Authorization', 'Bearer ' . $this->accessToken);
+
+ $this->future = $future;
+ }
+
+ return $this->future;
+ }
+
+ protected function didReceiveResult($result) {
+ list($status, $body, $headers) = $result;
+
+ if ($status->isError()) {
+ throw $status;
+ }
+
+ $data = json_decode($body, true);
+ if (!is_array($data)) {
+ throw new Exception("Expected JSON response from WordPress.com, ".
+ "got: {$body}");
+ }
+
+ if (idx($data, 'error')) {
+ $error = $data['error'];
+ throw new Exception("Received error from WordPress.com: {$error}");
+ }
+
+ return $data;
+ }
+
+}

File Metadata

Mime Type
text/plain
Expires
Thu, Mar 20, 7:34 AM (1 d, 3 h ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7707839
Default Alt Text
D9004.id21387.diff (5 KB)

Event Timeline