Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.

Commit 14bdf33

Browse files
author
Fosco Marotto
committed
Merge pull request #88 from facebook/gFosco.appsecret
Added appsecret_proof handling to FacebookRequest.
2 parents 7c4fa51 + 8f2fb25 commit 14bdf33

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

src/Facebook/FacebookRequest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ public function __construct(
184184
&& !isset($params["access_token"])) {
185185
$params["access_token"] = $session->getToken();
186186
}
187+
if (FacebookSession::useAppSecretProof()
188+
&& !isset($params["appsecret_proof"])) {
189+
$params["appsecret_proof"] = $this->getAppSecretProof(
190+
$params["access_token"]
191+
);
192+
}
187193
$this->params = $params;
188194
}
189195

@@ -251,6 +257,19 @@ public function execute() {
251257
return new FacebookResponse($this, $decodedResult, $result, $etagHit, $etagReceived);
252258
}
253259

260+
261+
/**
262+
* Generate and return the appsecret_proof value for an access_token
263+
*
264+
* @param string $token
265+
*
266+
* @return string
267+
*/
268+
public function getAppSecretProof($token)
269+
{
270+
return hash_hmac('sha256', $token, FacebookSession::_getTargetAppSecret());
271+
}
272+
254273
/**
255274
* appendParamsToUrl - Gracefully appends params to the URL.
256275
*

src/Facebook/FacebookSession.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ class FacebookSession
5252
*/
5353
private $signedRequestData;
5454

55+
/**
56+
* @var bool
57+
*/
58+
private static $useAppSecretProof = false;
59+
5560
/**
5661
* When creating a Session from an access_token, use:
5762
* var $session = new FacebookSession($accessToken);
@@ -443,4 +448,24 @@ public static function _base64UrlDecode($input) {
443448
return base64_decode(strtr($input, '-_', '+/'));
444449
}
445450

451+
/**
452+
* Enable or disable sending the appsecret_proof with requests.
453+
*
454+
* @param bool $on
455+
*/
456+
public static function enableAppSecretProof($on = true)
457+
{
458+
static::$useAppSecretProof = ($on ? true : false);
459+
}
460+
461+
/**
462+
* Get whether or not appsecret_proof should be sent with requests.
463+
*
464+
* @return bool
465+
*/
466+
public static function useAppSecretProof()
467+
{
468+
return static::$useAppSecretProof;
469+
}
470+
446471
}

tests/FacebookRequestTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,24 @@ public function testGracefullyHandlesUrlAppending()
118118
$this->assertEquals('https://www.foo.com/?access_token=bar&foo=bar', $processed_url);
119119
}
120120

121+
public function testAppSecretProof()
122+
{
123+
FacebookSession::enableAppSecretProof(true);
124+
$request = new FacebookRequest(
125+
FacebookTestHelper::$testSession,
126+
'GET',
127+
'/me'
128+
);
129+
$this->assertTrue(isset($request->getParameters()['appsecret_proof']));
130+
131+
132+
FacebookSession::enableAppSecretProof(false);
133+
$request = new FacebookRequest(
134+
FacebookTestHelper::$testSession,
135+
'GET',
136+
'/me'
137+
);
138+
$this->assertTrue(!isset($request->getParameters()['appsecret_proof']));
139+
}
140+
121141
}

0 commit comments

Comments
 (0)