Path: blob/master/src/applications/auth/adapter/PhutilGoogleAuthAdapter.php
12256 views
<?php12/**3* Authentication adapter for Google OAuth2.4*/5final class PhutilGoogleAuthAdapter extends PhutilOAuthAuthAdapter {67public function getAdapterType() {8return 'google';9}1011public function getAdapterDomain() {12return 'google.com';13}1415protected function newAccountIdentifiers() {16$identifiers = array();1718$account_id = $this->getOAuthAccountData('id');19if ($account_id !== null) {20$account_id = sprintf(21'id(%s)',22$account_id);23$identifiers[] = $this->newAccountIdentifier($account_id);24}2526$email = $this->getAccountEmail();27if ($email !== null) {28$identifiers[] = $this->newAccountIdentifier($email);29}3031return $identifiers;32}3334public function getAccountEmail() {35return $this->getOAuthAccountData('email');36}3738public function getAccountName() {39// Guess account name from email address, this is just a hint anyway.40$email = $this->getAccountEmail();41$email = explode('@', $email);42$email = head($email);43return $email;44}4546public function getAccountImageURI() {47$uri = $this->getOAuthAccountData('picture');4849// Change the "sz" parameter ("size") from the default to 100 to ask for50// a 100x100px image.51if ($uri !== null) {52$uri = new PhutilURI($uri);53$uri->replaceQueryParam('sz', 100);54$uri = (string)$uri;55}5657return $uri;58}5960public function getAccountURI() {61return $this->getOAuthAccountData('link');62}6364public function getAccountRealName() {65return $this->getOAuthAccountData('name');66}6768protected function getAuthenticateBaseURI() {69return 'https://accounts.google.com/o/oauth2/auth';70}7172protected function getTokenBaseURI() {73return 'https://accounts.google.com/o/oauth2/token';74}7576public function getScope() {77$scopes = array(78'email',79'profile',80);8182return implode(' ', $scopes);83}8485public function getExtraAuthenticateParameters() {86return array(87'response_type' => 'code',88);89}9091public function getExtraTokenParameters() {92return array(93'grant_type' => 'authorization_code',94);95}9697protected function loadOAuthAccountData() {98$uri = new PhutilURI('https://www.googleapis.com/userinfo/v2/me');99$uri->replaceQueryParam('access_token', $this->getAccessToken());100101$future = new HTTPSFuture($uri);102list($status, $body) = $future->resolve();103104if ($status->isError()) {105throw $status;106}107108try {109$result = phutil_json_decode($body);110} catch (PhutilJSONParserException $ex) {111throw new PhutilProxyException(112pht('Expected valid JSON response from Google account data request.'),113$ex);114}115116return $result;117}118119}120121122