Skip to content

Commit

Permalink
added ability to send appsecret_proof with request (#44)
Browse files Browse the repository at this point in the history
* added ability to use appsecret_proof to request

* applied changes from StyleCI
  • Loading branch information
pschocke committed Feb 26, 2020
1 parent 554576f commit c395767
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -43,6 +43,9 @@ Next we need to add this token to our Laravel configurations. Create a new Faceb

// Optional - Omit this if you want to use default version.
'version' => env('FACEBOOK_GRAPH_API_VERSION', '4.0')

// Optional - If set, the appsecret_proof will be send to verify your page-token
'app-secret' => env('FACEBOOK_APP_SECRET', '')
],
...
```
Expand Down
23 changes: 23 additions & 0 deletions src/Facebook.php
Expand Up @@ -20,6 +20,9 @@ class Facebook
/** @var string|null Page Token. */
protected $token;

/** @var string|null App Secret */
protected $secret;

/** @var string Default Graph API Version */
protected $graphApiVersion = '4.0';

Expand Down Expand Up @@ -48,6 +51,20 @@ public function setGraphApiVersion($graphApiVersion): self
return $this;
}

/**
* Set App Secret to generate appsecret_proof.
*
* @param string $secret
*
* @return Facebook
*/
public function setSecret($secret = null): self
{
$this->secret = $secret;

return $this;
}

/**
* Get HttpClient.
*
Expand Down Expand Up @@ -117,6 +134,12 @@ protected function api(string $endpoint, array $options, $method = 'GET')

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

if ($this->secret) {
$appsecret_proof = hash_hmac('sha256', $this->token, $this->secret);

$url .= "&appsecret_proof={$appsecret_proof}";
}

try {
return $this->httpClient()->request($method, $url, $options);
} catch (ClientException $exception) {
Expand Down
4 changes: 3 additions & 1 deletion src/FacebookServiceProvider.php
Expand Up @@ -19,7 +19,9 @@ public function boot()
->give(static function () {
$facebook = new Facebook(config('services.facebook.page-token'));

return $facebook->setGraphApiVersion(config('services.facebook.version', '4.0'));
return $facebook
->setGraphApiVersion(config('services.facebook.version', '4.0'))
->setSecret(config('services.facebook.app-secret'));
});
}
}

0 comments on commit c395767

Please sign in to comment.