PHP code example of kipchak / middleware-auth-hmac

1. Go to this page and download the library: Download kipchak/middleware-auth-hmac library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kipchak / middleware-auth-hmac example snippets


use Kipchak\Middleware\Auth\HMAC\HMAC;

HMAC::initialise($app);

return [
    'enabled' => true,            // register globally; false to attach per route instead (see below)
    'ignore_options' => true,     // let CORS preflight through
    'ignore_paths' => ['/status'],
    'default_profile' => 'api',   // for paths no profile claims; null leaves them unchecked

    'profiles' => [
        'api' => [
            'scheme' => 'kipchak',
            'keys' => ['billing-service' => env('HMAC_BILLING_SERVICE_SECRET', '')],
            'signed_headers' => ['content-type'],
        ],
        'stripe' => [
            'scheme' => 'stripe',
            'secrets' => [env('STRIPE_WEBHOOK_SECRET', '')],
            'paths' => ['/webhooks/stripe'],
        ],
    ],

    'replay' => ['store' => 'valkey', 'pool' => 'cache'],
];

use Kipchak\Middleware\Auth\HMAC\Handlers\VerifySignature;

$app->post('/webhooks/stripe', [Controllers\Webhooks::class, 'stripe'])
    ->add(new VerifySignature($app->getContainer(), 'stripe'));

$app->group('/v1/internal', function ($group) { /* ... */ })
    ->add(new VerifySignature($app->getContainer(), 'api'));

$client = $request->getAttribute(VerifySignature::ATTRIBUTE_KEY_ID); // e.g. "billing-service"

use Kipchak\Middleware\Auth\HMAC\Signer;

$request = Signer::kipchak($request, 'billing-service', $secret, ['content-type']);

'stripe' => [
    'scheme' => 'stripe',
    'secrets' => [env('STRIPE_WEBHOOK_SECRET', '')],
    'paths' => ['/webhooks/stripe'],
],

'inbound' => [
    'scheme' => 'standard-webhooks',
    'secrets' => [env('WEBHOOK_SECRET', '')],  // "whsec_..."
    'paths' => ['/webhooks/inbound'],
    // 'header_prefix' => 'svix',              // for Svix's svix-id / svix-timestamp / svix-signature
],

use Kipchak\Middleware\Auth\HMAC\Signer;

$header = Signer::stripe($body, $secret);                          // Stripe-Signature value
$headers = Signer::standardWebhooks($messageId, $body, $secret);   // webhook-id, webhook-timestamp, webhook-signature