Path: blob/master/src/applications/auth/adapter/PhutilPhabricatorAuthAdapter.php
12256 views
<?php12/**3* Authentication adapter for Phabricator OAuth2.4*/5final class PhutilPhabricatorAuthAdapter extends PhutilOAuthAuthAdapter {67private $phabricatorBaseURI;8private $adapterDomain;910public function setPhabricatorBaseURI($uri) {11$this->phabricatorBaseURI = $uri;12return $this;13}1415public function getPhabricatorBaseURI() {16return $this->phabricatorBaseURI;17}1819public function getAdapterDomain() {20return $this->adapterDomain;21}2223public function setAdapterDomain($domain) {24$this->adapterDomain = $domain;25return $this;26}2728public function getAdapterType() {29return 'phabricator';30}3132public function getAccountID() {33return $this->getOAuthAccountData('phid');34}3536public function getAccountEmail() {37return $this->getOAuthAccountData('primaryEmail');38}3940public function getAccountName() {41return $this->getOAuthAccountData('userName');42}4344public function getAccountImageURI() {45return $this->getOAuthAccountData('image');46}4748public function getAccountURI() {49return $this->getOAuthAccountData('uri');50}5152public function getAccountRealName() {53return $this->getOAuthAccountData('realName');54}5556protected function getAuthenticateBaseURI() {57return $this->getPhabricatorURI('oauthserver/auth/');58}5960protected function getTokenBaseURI() {61return $this->getPhabricatorURI('oauthserver/token/');62}6364public function getScope() {65return '';66}6768public function getExtraAuthenticateParameters() {69return array(70'response_type' => 'code',71);72}7374public function getExtraTokenParameters() {75return array(76'grant_type' => 'authorization_code',77);78}7980protected function loadOAuthAccountData() {81$uri = id(new PhutilURI($this->getPhabricatorURI('api/user.whoami')))82->replaceQueryParam('access_token', $this->getAccessToken());83list($body) = id(new HTTPSFuture($uri))->resolvex();8485try {86$data = phutil_json_decode($body);87return $data['result'];88} catch (PhutilJSONParserException $ex) {89throw new Exception(90pht(91'Expected valid JSON response from "user.whoami" request.'),92$ex);93}94}9596private function getPhabricatorURI($path) {97return rtrim($this->phabricatorBaseURI, '/').'/'.ltrim($path, '/');98}99100}101102103