Last active
July 2, 2016 16:20
-
-
Save phindmarsh/da1bf9e970f42ca6d984 to your computer and use it in GitHub Desktop.
Facebook Grant Type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyApp\OAuth2\GrantType; | |
use Facebook\FacebookRequest; | |
use Facebook\FacebookSession; | |
use Facebook\GraphUser; | |
use OAuth2\RequestInterface; | |
use OAuth2\ResponseInterface; | |
use OAuth2\Storage\ClientCredentialsInterface; | |
use OAuth2\Storage\AccessTokenInterface; | |
class FacebookGrant implements GrantTypeInterface { | |
public function getQuerystringIdentifier() { | |
return 'facebook'; | |
} | |
public function validateRequest(RequestInterface $request, ResponseInterface $response) { | |
if(!$this->clientAssertion->validateRequest($request, $response)) { | |
return false; | |
} | |
if (!$request->request("fb_access_token")) { | |
$response->setError(400, 'invalid_request', 'A fb_access_token token is required'); | |
return null; | |
} | |
$fb_access_token = $request->request("fb_access_token"); | |
$session = new FacebookSession($fb_access_token); | |
$request = new FacebookRequest($session, 'GET', '/me'); | |
$response = $request->execute(); | |
$fb_user = $response->getGraphObject(GraphUser::className()); | |
if(!($email = $fb_user->getProperty('email')) || empty($email)){ | |
$response->setError(400, 'invalid_request', 'Email address permission was not granted for user'); | |
return null; | |
} | |
$user = User::loadByEmail($email); | |
if($user === null) { | |
$user = new User(); | |
$user->email = $email; | |
$user->save(); | |
} | |
$this->userInfo = $user; | |
return true; | |
} | |
public function getClientId() | |
{ | |
return null; | |
} | |
public function getUserId() | |
{ | |
return $this->userInfo['user_id']; | |
} | |
public function getScope() | |
{ | |
return isset($this->userInfo['scope']) ? $this->userInfo['scope'] : null; | |
} | |
public function createAccessToken(AccessTokenInterface $accessToken, $client_id, $user_id, $scope) | |
{ | |
return $accessToken->createAccessToken($client_id, $user_id, $scope); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use MyApp\OAuth2\GrantType\FacebookGrant; | |
$storage = new OAuth2\Server\Pdo(); | |
$server = new OAuth2\Server($storage); | |
$server->addGrantType(new FacebookGrant(), 'facebook'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @phindmarsh , Did u implemented this kind of grant in apigility? cfr bshaffer/oauth2-server-php#627
I need to do implement fb connect on an apigility app. This gist is the closest to what I need.
Would greatly appreciate some help on this.
Regards.