Page MenuHomePhabricator

D11286.id27119.diff
No OneTemporary

D11286.id27119.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
@@ -245,6 +245,7 @@
'PhutilPerson' => 'internationalization/PhutilPerson.php',
'PhutilPersonTest' => 'internationalization/__tests__/PhutilPersonTest.php',
'PhutilPersonaAuthAdapter' => 'auth/PhutilPersonaAuthAdapter.php',
+ 'PhutilPhabricatorAuthAdapter' => 'auth/PhutilPhabricatorAuthAdapter.php',
'PhutilPhobjectTestCase' => 'object/__tests__/PhutilPhobjectTestCase.php',
'PhutilPhtTestCase' => 'internationalization/__tests__/PhutilPhtTestCase.php',
'PhutilProcessGroupDaemon' => 'daemon/torture/PhutilProcessGroupDaemon.php',
@@ -652,6 +653,7 @@
'PhutilPayPalAPIFuture' => 'FutureProxy',
'PhutilPersonTest' => 'PhutilPerson',
'PhutilPersonaAuthAdapter' => 'PhutilAuthAdapter',
+ 'PhutilPhabricatorAuthAdapter' => 'PhutilOAuthAuthAdapter',
'PhutilPhobjectTestCase' => 'PhutilTestCase',
'PhutilPhtTestCase' => 'PhutilTestCase',
'PhutilProcessGroupDaemon' => 'PhutilTortureTestDaemon',
diff --git a/src/auth/PhutilPhabricatorAuthAdapter.php b/src/auth/PhutilPhabricatorAuthAdapter.php
new file mode 100644
--- /dev/null
+++ b/src/auth/PhutilPhabricatorAuthAdapter.php
@@ -0,0 +1,100 @@
+<?php
+
+/**
+ * Authentication adapter for Phabricator OAuth2.
+ */
+final class PhutilPhabricatorAuthAdapter extends PhutilOAuthAuthAdapter {
+
+ private $phabricatorBaseURI;
+ private $adapterDomain;
+
+ public function setPhabricatorBaseURI($uri) {
+ $this->phabricatorBaseURI = $uri;
+ return $this;
+ }
+
+ public function getPhabricatorBaseURI() {
+ return $this->phabricatorBaseURI;
+ }
+
+ public function getAdapterDomain() {
+ return $this->adapterDomain;
+ }
+
+ public function setAdapterDomain($domain) {
+ $this->adapterDomain = $domain;
+ return $this;
+ }
+
+ public function getAdapterType() {
+ return 'phabricator';
+ }
+
+ public function getAccountID() {
+ return $this->getOAuthAccountData('phid');
+ }
+
+ public function getAccountEmail() {
+ return null;
+ }
+
+ public function getAccountName() {
+ return $this->getOAuthAccountData('userName');
+ }
+
+ public function getAccountImageURI() {
+ return $this->getOAuthAccountData('image');
+ }
+
+ public function getAccountURI() {
+ return $this->getOAuthAccountData('uri');
+ }
+
+ public function getAccountRealName() {
+ return $this->getOAuthAccountData('userName');
+ }
+
+ protected function getAuthenticateBaseURI() {
+ return $this->getPhabricatorURI('oauthserver/auth/');
+ }
+
+ protected function getTokenBaseURI() {
+ return $this->getPhabricatorURI('oauthserver/token/');
+ }
+
+ public function getScope() {
+ return 'whoami';
+ }
+
+ public function getExtraAuthenticateParameters() {
+ return array(
+ 'response_type' => 'code',
+ );
+ }
+
+ public function getExtraTokenParameters() {
+ return array(
+ 'grant_type' => 'authorization_code',
+ );
+ }
+
+ protected function loadOAuthAccountData() {
+ $uri = id(new PhutilURI($this->getPhabricatorURI('api/user.whoami')))
+ ->setQueryParam('access_token', $this->getAccessToken());
+ list($body) = id(new HTTPSFuture($uri))->resolvex();
+
+ $data = json_decode($body, true);
+ if (!is_array($data)) {
+ throw new Exception(
+ 'Expected valid JSON response from Phabricator user.whoami request, '.
+ 'got: '.$body);
+ }
+
+ return $data['result'];
+ }
+
+ private function getPhabricatorURI($path) {
+ return rtrim($this->phabricatorBaseURI, '/').'/'.ltrim($path, '/');
+ }
+
+}

File Metadata

Mime Type
text/plain
Expires
May 18 2024, 1:10 AM (4 w, 2 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6277293
Default Alt Text
D11286.id27119.diff (3 KB)

Event Timeline