Skip to content

Commit

Permalink
feat: Support securing requests
Browse files Browse the repository at this point in the history
  • Loading branch information
CasperLaiTW committed Apr 18, 2017
1 parent 759ae05 commit 713ad87
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 3 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ php artisan vendor:publish --provider="Casperlaitw\LaravelFbMessenger\LaravelFbM

## Configuration

### Security

Almost every API request with `access_token`, if you want to improved security in your app,
you can use `appsecret_proof`. Please add `MESSENGER_APP_SECRET` to `.env` file and enable proof on all calls.
*If you don't know how to get secret token and enabled proof, please checkout [Graph Api](https://developers.facebook.com/docs/graph-api/securing-requests)*

`.env`
```
MESSENGER_APP_SECRET="APP SECRET TOKEN"
```

### Token
Add you token to `.env` file or modify `fb-messenger.php` config.

Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,10 @@
},
"config": {
"preferred-install": "dist"
},
"extra": {
"branch-alias": {
"dev-master": "1.4.x-dev"
}
}
}
1 change: 1 addition & 0 deletions config/fb-messenger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'debug' => env('APP_DEBUG', false),
'verify_token' => env('MESSENGER_VERIFY_TOKEN'),
'app_token' => env('MESSENGER_APP_TOKEN'),
'app_secret' => env('MESSENGER_APP_SECRET', null),
'auto_typing' => true,
'handlers' => [
Casperlaitw\LaravelFbMessenger\Contracts\DefaultHandler::class
Expand Down
5 changes: 4 additions & 1 deletion src/Commands/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ abstract class BaseCommand extends Command
public function __construct(CommandHandler $handler, Repository $config)
{
parent::__construct();
$this->handler = $handler->createBot($config->get('fb-messenger.app_token'));
$this->handler = $handler->createBot(
$config->get('fb-messenger.app_token'),
$config->get('fb-messenger.app_secret')
);
}
}
4 changes: 3 additions & 1 deletion src/Contracts/BaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ abstract class BaseHandler implements HandlerInterface
* Create bot to send API
*
* @param $token
* @param $secret
*
* @return $this
*/
public function createBot($token)
public function createBot($token, $secret = null)
{
$this->bot = new Bot($token);
$this->bot->setSecret($secret);

return $this;
}
Expand Down
18 changes: 18 additions & 0 deletions src/Contracts/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class Bot
*/
private $debug = null;

/**
* @var null|string
*/
private $secret;

/**
* FbBotApp constructor.
* @param string $token
Expand All @@ -70,6 +75,15 @@ public function setDebug($debug)
$this->debug = $debug;
}

/**
* @param $secret
* @return $this
*/
public function setSecret($secret)
{
$this->secret = hash_hmac('sha256', $this->token, $secret);
}

/**
* Request to API
*
Expand All @@ -89,6 +103,10 @@ protected function call($url, $data, $type = self::TYPE_POST)
],
];

if ($this->secret) {
$options['query']['appsecret_proof'] = $this->secret;
}

switch ($type) {
case self::TYPE_DELETE:
case self::TYPE_POST:
Expand Down
3 changes: 3 additions & 0 deletions tests/CommandTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ private function getArtisan()
->shouldReceive('get')
->with('fb-messenger.app_token')
->andReturn(getenv('MESSENGER_APP_TOKEN'))
->shouldReceive('get')
->with('fb-messenger.app_secret')
->andReturn(getenv('MESSENGER_APP_SECRET'))
->getMock();

$response = m::mock(HandleMessageResponse::class)->makePartial();
Expand Down
13 changes: 12 additions & 1 deletion tests/Contracts/BotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class BotTest extends TestCase
{
protected function setUp()
{
parent::setUp(); // TODO: Change the autogenerated stub
parent::setUp();
$this->bot = new Bot(getenv('MESSENGER_APP_TOKEN'));
}

Expand Down Expand Up @@ -76,4 +76,15 @@ public function test_pusher_connect_fail()
->andReturn([]);
$this->bot->send($message);
}

public function test_securing_request()
{
$appSecret = 'test_app_secret';
$expected = hash_hmac('sha256', getenv('MESSENGER_APP_TOKEN'), $appSecret);
$this->bot->setSecret($appSecret);
$actual = $this->getPrivateProperty(Bot::class, 'secret')->getValue($this->bot);

$this->assertEquals($expected, $actual);

}
}

0 comments on commit 713ad87

Please sign in to comment.