Path: blob/master/src/applications/auth/adapter/PhutilGitHubAuthAdapter.php
12256 views
<?php12/**3* Authentication adapter for Github OAuth2.4*/5final class PhutilGitHubAuthAdapter extends PhutilOAuthAuthAdapter {67public function getAdapterType() {8return 'github';9}1011public function getAdapterDomain() {12return 'github.com';13}1415public function getAccountID() {16return $this->getOAuthAccountData('id');17}1819public function getAccountEmail() {20return $this->getOAuthAccountData('email');21}2223public function getAccountName() {24return $this->getOAuthAccountData('login');25}2627public function getAccountImageURI() {28return $this->getOAuthAccountData('avatar_url');29}3031public function getAccountURI() {32$name = $this->getAccountName();33if (strlen($name)) {34return 'https://github.com/'.$name;35}36return null;37}3839public function getAccountRealName() {40return $this->getOAuthAccountData('name');41}4243protected function getAuthenticateBaseURI() {44return 'https://github.com/login/oauth/authorize';45}4647protected function getTokenBaseURI() {48return 'https://github.com/login/oauth/access_token';49}5051protected function loadOAuthAccountData() {52$uri = new PhutilURI('https://api.github.com/user');5354$future = new HTTPSFuture($uri);5556// NOTE: GitHub requires a User-Agent string.57$future->addHeader('User-Agent', __CLASS__);5859// See T13485. Circa early 2020, GitHub has deprecated use of the60// "access_token" URI parameter.61$token_header = sprintf('token %s', $this->getAccessToken());62$future->addHeader('Authorization', $token_header);6364list($body) = $future->resolvex();6566try {67return phutil_json_decode($body);68} catch (PhutilJSONParserException $ex) {69throw new PhutilProxyException(70pht('Expected valid JSON response from GitHub account data request.'),71$ex);72}73}7475}767778