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

Showcase webhook signature validator #18

Open
Simply007 opened this issue Sep 20, 2022 · 0 comments
Open

Showcase webhook signature validator #18

Simply007 opened this issue Sep 20, 2022 · 0 comments
Labels
enhancement New feature or request hacktoberfest https://hacktoberfest.digitalocean.com

Comments

@Simply007
Copy link

Simply007 commented Sep 20, 2022

Motivation

Currently, we have a guide about webhook validation in PHP. It would be nice to have it directly in a sample and README.

Proposed solution

Register a webhook controller which takes webhook secret from environment variables and validates the signature of the request and respond OK (or other dummy result/maybe with a webhhok body) if it is fine, or invalid if the signature does not match.

Additional context

web.php extension

use App\Http\Controllers\WebhookController;

// ... 

Route::post('/webhook', [WebhookController::class, 'index'])->name('webhook');

Controller sample

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class WebhookController extends Controller
{

    public function index(Request  $request)
    {
        // load from environment variables
        $secret = 'XYZ';
        $content = $request->getContent();

        $sig_calculated = base64_encode(hash_hmac('sha256', $content, $secret, true));

        $sig_sent = $request->headers->get('x-kc-signature');

        // return some more readable response
        return dd(hash_equals($sig_calculated, $sig_sent));
    }
}

There is a branch to check: https://github.com/Kentico/kontent-sample-app-php/tree/showcase-webhook

@Simply007 Simply007 added hacktoberfest https://hacktoberfest.digitalocean.com enhancement New feature or request labels Sep 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request hacktoberfest https://hacktoberfest.digitalocean.com
Projects
None yet
Development

No branches or pull requests

1 participant