4 * Authentication adapter for Google OAuth2.
6 final class PhutilGoogleAuthAdapter
extends PhutilOAuthAuthAdapter
{
8 public function getAdapterType() {
12 public function getAdapterDomain() {
16 protected function newAccountIdentifiers() {
17 $identifiers = array();
19 $account_id = $this->getOAuthAccountData('id');
20 if ($account_id !== null) {
21 $account_id = sprintf(
24 $identifiers[] = $this->newAccountIdentifier($account_id);
27 $email = $this->getAccountEmail();
28 if ($email !== null) {
29 $identifiers[] = $this->newAccountIdentifier($email);
35 public function getAccountEmail() {
36 return $this->getOAuthAccountData('email');
39 public function getAccountName() {
40 // Guess account name from email address, this is just a hint anyway.
41 $email = $this->getAccountEmail();
42 $email = explode('@', $email);
43 $email = head($email);
47 public function getAccountImageURI() {
48 $uri = $this->getOAuthAccountData('picture');
50 // Change the "sz" parameter ("size") from the default to 100 to ask for
53 $uri = new PhutilURI($uri);
54 $uri->replaceQueryParam('sz', 100);
61 public function getAccountURI() {
62 return $this->getOAuthAccountData('link');
65 public function getAccountRealName() {
66 return $this->getOAuthAccountData('name');
69 protected function getAuthenticateBaseURI() {
70 return 'https://accounts.google.com/o/oauth2/auth';
73 protected function getTokenBaseURI() {
74 return 'https://accounts.google.com/o/oauth2/token';
77 public function getScope() {
83 return implode(' ', $scopes);
86 public function getExtraAuthenticateParameters() {
88 'response_type' => 'code',
92 public function getExtraTokenParameters() {
94 'grant_type' => 'authorization_code',
98 protected function loadOAuthAccountData() {
99 $uri = new PhutilURI('https://www.googleapis.com/userinfo/v2/me');
100 $uri->replaceQueryParam('access_token', $this->getAccessToken());
102 $future = new HTTPSFuture($uri);
103 list($status, $body) = $future->resolve();
105 if ($status->isError()) {
110 $result = phutil_json_decode($body);
111 } catch (PhutilJSONParserException
$ex) {
112 throw new PhutilProxyException(
113 pht('Expected valid JSON response from Google account data request.'),