Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
WebhookVerifier
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 IsValidWebhookSignature
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
 checkSignature
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Fingerprint\ServerSdk\Webhook;
4
5/**
6 * Verifies Fingerprint webhook signature.
7 */
8final class WebhookVerifier
9{
10    /**
11     * Checks whether the webhook signature header is valid for the given data and secret.
12     *
13     * @param string $header comma-separated list of versioned signatures
14     * @param string $data   raw webhook request body
15     * @param string $secret webhook signing secret
16     */
17    public static function IsValidWebhookSignature(string $header, string $data, string $secret): bool
18    {
19        $signatures = explode(',', $header);
20        foreach ($signatures as $signature) {
21            $parts = explode('=', $signature);
22            if (2 === count($parts) && 'v1' === $parts[0]) {
23                $hash = $parts[1];
24                if (self::checkSignature($hash, $data, $secret)) {
25                    return true;
26                }
27            }
28        }
29
30        return false;
31    }
32
33    /**
34     * Compares the given signature against an HMAC-SHA256 hash of the data.
35     */
36    private static function checkSignature(string $signature, string $data, string $secret): bool
37    {
38        $hash = hash_hmac('sha256', $data, $secret);
39
40        return hash_equals($hash, $signature);
41    }
42}