Proof of concept for a webhook\callback based authentication
9.1K
Python3 based proof of concept for a webhook\callback based authentication which allows any API to know that any other API is in fact who it claim it is without having to configure any preshared secrets between them (it ensures that the URL of the requester is really who he claim it is).
The basic idea is that one can use webhooks as the authentication method provided that the API call uses HTTPS (to guard against MITM attacks) & that the DNS is secure (but if someone manages to hijack your DNS you have bigger problems).
This allows one to connect API's to each other in a secure fashion without having to worry about keeping secrets as every request has a random one use token generated for it in the requester API (the one which sends the request), the requester then hashes the one use token and saves (be it in memory or in a backend DB) the following pieces of information:
The hashed token is then sent to the receiver API along with a webhook address which is part of the requester API as part of the Authorization header in the format of Webhook requester_webhook_url hashed_token, upon receiving the hashed token the receiver API then confirms with the requester_webhook_url that it is in fact the one that sent the request by opening a new connection to the requester_webhook_url address provided in the Authorization header and sending it the hashed_token & the url of itself (the receiver url where the request was received at) in the body of a POST request in the following JSON format {"hash": "hashed_token", "url": "receiver_webhook_url"}.
The requester API then receives the request to the webhook address and does the following:
hashed_token to - this guards against passing the tokens between multiple API'sThe receiver API then checks that the unhashed version does in fact matches the hashed version by running it through the hashing algorithm & if both are true he knows that the original request does in fact came from the API that's in control of the webhook (being the original creator of the random token and using modern hashing functions no one else can make that link in a reasonable time frame).
Attached below is a diagram of the workflow between all components:

If you prefer reading about Alice & Bob and how they use this to authenticate each other you can read about it at https://github.com/naorlivne/webhook_auth_poc/blob/master/docs/webhook_callback_based_auth_process.md but take into account that this authentication is designed for the web so some steps sounds a bit silly when using snail mail as the example.
The following envvars are used to control the POC:
there are 3 api endpoints available in this POC:
RECEIVER_WEBHOOK_URL with the REQUESTER_WEBHOOK_URL being used as the callback URLI used bcrypt in this POC but any secure hashing algorithm is possible to use provided that both sides of the authentication agreed on beforehand (bcrypt being the default algorithm used otherwise).
I also used Redis as the backend DB but it's possible to use any DB or even save the data in memory for monolithic API's.
For simplicity sake this POC uses HTTP rather the HTTPS, it is highly recommended that you use HTTPS on all steps in the process as it guards against MITM attacks.
This is a very basic POC designed to just show that the underlying concept works so be aware that the code may very well be ugly.
The docker compose example includes 5 containers:
It's worth mentioning that while Alice, Bob & Eve containers are all created from the same Docker image they don't share any data in any way outside of the HTTP requests they send to each other thus providing a realistic example of the authentication process between different API's.
start it by running first cloning the repo then running from the repo root folder docker-compose up and then you can:
http://127.0.0.1:5000/example which will reply to you with {"allowed": true}http://127.0.0.1:5001/example which will reply to you with {"allowed": false}Content type
Image
Digest
sha256:aefaf928a…
Size
110.5 MB
Last updated
about 1 year ago
docker pull naorlivne/webhook_auth_poc