Skip to content

Commit 916cff8

Browse files
committed
Merge pull request #294 from d-ph/more-state-csrf-hashes
Add support for CSRF state hashes for Facebook, Instagram and Google
2 parents 824e138 + 7d99126 commit 916cff8

6 files changed

Lines changed: 32 additions & 6 deletions

File tree

examples/facebook.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@
3636
$facebookService = $serviceFactory->createService('facebook', $credentials, $storage, array());
3737

3838
if (!empty($_GET['code'])) {
39+
// retrieve the CSRF state parameter
40+
$state = isset($_GET['state']) ? $_GET['state'] : null;
41+
3942
// This was a callback request from facebook, get the token
40-
$token = $facebookService->requestAccessToken($_GET['code']);
43+
$token = $facebookService->requestAccessToken($_GET['code'], $state);
4144

4245
// Send a request with it
4346
$result = json_decode($facebookService->request('/me'), true);

examples/google.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@
3535
$googleService = $serviceFactory->createService('google', $credentials, $storage, array('userinfo_email', 'userinfo_profile'));
3636

3737
if (!empty($_GET['code'])) {
38+
// retrieve the CSRF state parameter
39+
$state = isset($_GET['state']) ? $_GET['state'] : null;
40+
3841
// This was a callback request from google, get the token
39-
$googleService->requestAccessToken($_GET['code']);
42+
$googleService->requestAccessToken($_GET['code'], $state);
4043

4144
// Send a request with it
42-
$result = json_decode($googleService->request('https://www.googleapis.com/oauth2/v1/userinfo'), true);
45+
$result = json_decode($googleService->request('userinfo'), true);
4346

4447
// Show some of the resultant data
4548
echo 'Your unique google user id is: ' . $result['id'] . ' and your name is ' . $result['name'];

examples/instagram.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@
3838
$instagramService = $serviceFactory->createService('instagram', $credentials, $storage, $scopes);
3939

4040
if (!empty($_GET['code'])) {
41+
// retrieve the CSRF state parameter
42+
$state = isset($_GET['state']) ? $_GET['state'] : null;
43+
4144
// This was a callback request from Instagram, get the token
42-
$instagramService->requestAccessToken($_GET['code']);
45+
$instagramService->requestAccessToken($_GET['code'], $state);
4346

4447
// Send a request with it
4548
$result = json_decode($instagramService->request('users/self'), true);

src/OAuth/OAuth2/Service/Facebook.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function __construct(
130130
UriInterface $baseApiUri = null,
131131
$apiVersion = ""
132132
) {
133-
parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, false, $apiVersion);
133+
parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true, $apiVersion);
134134

135135
if (null === $baseApiUri) {
136136
$this->baseApiUri = new Uri('https://graph.facebook.com'.$this->getApiVersionString().'/');

src/OAuth/OAuth2/Service/Google.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace OAuth\OAuth2\Service;
44

5+
use OAuth\Common\Consumer\CredentialsInterface;
6+
use OAuth\Common\Http\Client\ClientInterface;
7+
use OAuth\Common\Http\Uri\UriInterface;
8+
use OAuth\Common\Storage\TokenStorageInterface;
59
use OAuth\OAuth2\Token\StdOAuth2Token;
610
use OAuth\Common\Http\Exception\TokenResponseException;
711
use OAuth\OAuth2\Service\Exception\InvalidAccessTypeException;
@@ -110,6 +114,19 @@ class Google extends AbstractService
110114

111115
protected $accessType = 'online';
112116

117+
public function __construct(
118+
CredentialsInterface $credentials,
119+
ClientInterface $httpClient,
120+
TokenStorageInterface $storage,
121+
$scopes = array(),
122+
UriInterface $baseApiUri = null
123+
) {
124+
parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true);
125+
126+
if (null === $baseApiUri) {
127+
$this->baseApiUri = new Uri('https://www.googleapis.com/oauth2/v1/');
128+
}
129+
}
113130

114131
public function setAccessType($accessType)
115132
{

src/OAuth/OAuth2/Service/Instagram.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(
2828
$scopes = array(),
2929
UriInterface $baseApiUri = null
3030
) {
31-
parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
31+
parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true);
3232

3333
if (null === $baseApiUri) {
3434
$this->baseApiUri = new Uri('https://api.instagram.com/v1/');

0 commit comments

Comments
 (0)