Changeset View
Changeset View
Standalone View
Standalone View
src/applications/auth/adapter/PhutilFacebookAuthAdapter.php
<?php | <?php | ||||
/** | /** | ||||
* Authentication adapter for Facebook OAuth2. | * Authentication adapter for Facebook OAuth2. | ||||
*/ | */ | ||||
final class PhutilFacebookAuthAdapter extends PhutilOAuthAuthAdapter { | final class PhutilFacebookAuthAdapter extends PhutilOAuthAuthAdapter { | ||||
private $requireSecureBrowsing; | |||||
public function setRequireSecureBrowsing($require_secure_browsing) { | |||||
$this->requireSecureBrowsing = $require_secure_browsing; | |||||
return $this; | |||||
} | |||||
public function getAdapterType() { | public function getAdapterType() { | ||||
return 'facebook'; | return 'facebook'; | ||||
} | } | ||||
public function getAdapterDomain() { | public function getAdapterDomain() { | ||||
return 'facebook.com'; | return 'facebook.com'; | ||||
} | } | ||||
Show All 33 Lines | final class PhutilFacebookAuthAdapter extends PhutilOAuthAuthAdapter { | ||||
public function getAccountURI() { | public function getAccountURI() { | ||||
return $this->getOAuthAccountData('link'); | return $this->getOAuthAccountData('link'); | ||||
} | } | ||||
public function getAccountRealName() { | public function getAccountRealName() { | ||||
return $this->getOAuthAccountData('name'); | return $this->getOAuthAccountData('name'); | ||||
} | } | ||||
public function getAccountSecuritySettings() { | |||||
return $this->getOAuthAccountData('security_settings'); | |||||
} | |||||
protected function getAuthenticateBaseURI() { | protected function getAuthenticateBaseURI() { | ||||
return 'https://www.facebook.com/dialog/oauth'; | return 'https://www.facebook.com/dialog/oauth'; | ||||
} | } | ||||
protected function getTokenBaseURI() { | protected function getTokenBaseURI() { | ||||
return 'https://graph.facebook.com/oauth/access_token'; | return 'https://graph.facebook.com/oauth/access_token'; | ||||
} | } | ||||
protected function loadOAuthAccountData() { | protected function loadOAuthAccountData() { | ||||
$fields = array( | $fields = array( | ||||
'id', | 'id', | ||||
'name', | 'name', | ||||
'email', | 'email', | ||||
'link', | 'link', | ||||
'security_settings', | |||||
'picture', | 'picture', | ||||
); | ); | ||||
$uri = new PhutilURI('https://graph.facebook.com/me'); | $uri = new PhutilURI('https://graph.facebook.com/me'); | ||||
$uri->replaceQueryParam('access_token', $this->getAccessToken()); | $uri->replaceQueryParam('access_token', $this->getAccessToken()); | ||||
$uri->replaceQueryParam('fields', implode(',', $fields)); | $uri->replaceQueryParam('fields', implode(',', $fields)); | ||||
list($body) = id(new HTTPSFuture($uri))->resolvex(); | list($body) = id(new HTTPSFuture($uri))->resolvex(); | ||||
$data = null; | $data = null; | ||||
try { | try { | ||||
$data = phutil_json_decode($body); | $data = phutil_json_decode($body); | ||||
} catch (PhutilJSONParserException $ex) { | } catch (PhutilJSONParserException $ex) { | ||||
throw new PhutilProxyException( | throw new PhutilProxyException( | ||||
pht('Expected valid JSON response from Facebook account data request.'), | pht('Expected valid JSON response from Facebook account data request.'), | ||||
$ex); | $ex); | ||||
} | } | ||||
if ($this->requireSecureBrowsing) { | |||||
if (empty($data['security_settings']['secure_browsing']['enabled'])) { | |||||
throw new Exception( | |||||
pht( | |||||
'This Phabricator install requires you to enable Secure Browsing '. | |||||
'on your Facebook account in order to use it to log in to '. | |||||
'Phabricator. For more information, see %s', | |||||
'https://www.facebook.com/help/156201551113407/')); | |||||
} | |||||
} | |||||
return $data; | return $data; | ||||
} | } | ||||
} | } |