Skip to content

Commit c395767

Browse files
authored
added ability to send appsecret_proof with request (#44)
* added ability to use appsecret_proof to request * applied changes from StyleCI
1 parent 554576f commit c395767

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ Next we need to add this token to our Laravel configurations. Create a new Faceb
4343

4444
// Optional - Omit this if you want to use default version.
4545
'version' => env('FACEBOOK_GRAPH_API_VERSION', '4.0')
46+
47+
// Optional - If set, the appsecret_proof will be send to verify your page-token
48+
'app-secret' => env('FACEBOOK_APP_SECRET', '')
4649
],
4750
...
4851
```

src/Facebook.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class Facebook
2020
/** @var string|null Page Token. */
2121
protected $token;
2222

23+
/** @var string|null App Secret */
24+
protected $secret;
25+
2326
/** @var string Default Graph API Version */
2427
protected $graphApiVersion = '4.0';
2528

@@ -48,6 +51,20 @@ public function setGraphApiVersion($graphApiVersion): self
4851
return $this;
4952
}
5053

54+
/**
55+
* Set App Secret to generate appsecret_proof.
56+
*
57+
* @param string $secret
58+
*
59+
* @return Facebook
60+
*/
61+
public function setSecret($secret = null): self
62+
{
63+
$this->secret = $secret;
64+
65+
return $this;
66+
}
67+
5168
/**
5269
* Get HttpClient.
5370
*
@@ -117,6 +134,12 @@ protected function api(string $endpoint, array $options, $method = 'GET')
117134

118135
$url = "https://graph.facebook.com/v{$this->graphApiVersion}/{$endpoint}?access_token={$this->token}";
119136

137+
if ($this->secret) {
138+
$appsecret_proof = hash_hmac('sha256', $this->token, $this->secret);
139+
140+
$url .= "&appsecret_proof={$appsecret_proof}";
141+
}
142+
120143
try {
121144
return $this->httpClient()->request($method, $url, $options);
122145
} catch (ClientException $exception) {

src/FacebookServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public function boot()
1919
->give(static function () {
2020
$facebook = new Facebook(config('services.facebook.page-token'));
2121

22-
return $facebook->setGraphApiVersion(config('services.facebook.version', '4.0'));
22+
return $facebook
23+
->setGraphApiVersion(config('services.facebook.version', '4.0'))
24+
->setSecret(config('services.facebook.app-secret'));
2325
});
2426
}
2527
}

0 commit comments

Comments
 (0)