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

PHP example in docs has issues #49

Closed
aliask opened this issue Aug 23, 2020 · 2 comments
Closed

PHP example in docs has issues #49

aliask opened this issue Aug 23, 2020 · 2 comments

Comments

@aliask
Copy link

aliask commented Aug 23, 2020

Noticed a couple of issues while implementing in Laravel.

  1. Using php://input is problematic for tests, and generally fails in older versions of PHP.
  2. If header is missing, hash_equals will throw an exception - better to use ?? '' to ensure variable is non-null.
  3. Trailing , in header() call

Revised code to address these issues:

public function handleWebhookEvent(Request $request, $hook) {
    $received_signature = $request->header(
        'X-Up-Authenticity-Signature'
    ) ?? '';
    $raw_body = $request->getContent();
    $signature = hash_hmac('sha256', $raw_body, $hook->secret_key);
    if (!hash_equals($signature, $received_signature)) {
        abort(401);
    }
    // Process webhook event
}
@d11wtq
Copy link
Contributor

d11wtq commented Aug 23, 2020

❤️ Thanks, will fix up the example. Trailing commas are fine, but looks like that was only added with PHP 7.3 so will take that out too.

@d11wtq
Copy link
Contributor

d11wtq commented Aug 24, 2020

Fix incoming 🚢

@d11wtq d11wtq closed this as completed Aug 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants