Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webhook authentication #81

Merged
merged 2 commits into from
Oct 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ $connection->setBaseUrl('https://start.exactonline.de');

Check [src/Picqer/Financials/Exact](src/Picqer/Financials/Exact) for all available entities.

## Webhooks
Managaging webhook subscriptions is possible through the [WebhookSubscription](src/Picqer/Financials/Exact/WebhookSubscription.php) entitiy.

For authenticating incoming webhook calls you can use the [Authenticatable](src/Picqer/Financials/Exact/Webhook/Authenticatable.php) trait.
Supply the authenticate method with the full JSON request and your Webhook secret supplied by Exact, it will return true or false.

## Troubleshooting
> 'Picqer\Financials\Exact\ApiException' with message 'Error 400: Please add a $select or a $top=1 statement to the query string.'

Expand Down
15 changes: 15 additions & 0 deletions src/Picqer/Financials/Exact/Webhook/Authenticatable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php namespace Picqer\Financials\Exact\Webhook;

trait Authenticatable
{
public function authenticate($requestContent, $webhookSecret)
{
$matches = [];
$matched = preg_match('/^{"Content":(.*),"HashCode":"(.*)"}$/', $requestContent, $matches);

if ($matched === 1 && isset($matches[1]) && isset($matches[2])) {
return $matches[2] === strtoupper(hash_hmac('sha256', $matches[1], $webhookSecret));
}
return false;
}
}