Download the PHP package kipchak/middleware-auth-hmac without Composer

On this page you can find all versions of the php package kipchak/middleware-auth-hmac. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package middleware-auth-hmac

HMAC Request Signing Middleware for Kipchak

Verifies signed API requests and webhooks, with replay protection, for Kipchak APIs.

Composer Package

kipchak/middleware-auth-hmac

Installation

For replay protection, install one of the cache drivers too (and initialise it in drivers/drivers.php):

Store Package Notes
valkey kipchak/driver-valkey (Enterprise) Atomic SET NX. Recommended.
memcached kipchak/driver-memcached Atomic add.
filecache kipchak/driver-filecache One host only, and check-then-set. For development.

Then add the middleware in middlewares/middlewares.php:

and copy sample.config.php to config/kipchak.auth.hmac.php.

Configuration

Profiles

A profile is one way of verifying requests. Globally, each request uses the first profile whose paths cover it (a prefix matches itself and everything below it: /webhooks covers /webhooks/stripe), otherwise default_profile. If neither applies the request passes through unchecked.

Key Applies to Default Meaning
scheme all kipchak kipchak, stripe or standard-webhooks
keys kipchak required key ID => secret, or key ID => list of secrets while rotating. Secrets must be at least 16 bytes; use 32+ random bytes.
secrets stripe, standard-webhooks required Accepted secrets. Stripe's whsec_... is used as-is; Standard Webhooks' whsec_<base64> is decoded.
header kipchak, stripe Kipchak-Signature / Stripe-Signature Header carrying the signature
header_prefix standard-webhooks webhook Reads <prefix>-id, <prefix>-timestamp, <prefix>-signature. Use svix for Svix.
algorithm kipchak sha256 sha256, sha384 or sha512. Fixed by the server, so clients cannot downgrade it.
signed_headers kipchak [] Headers the signature covers, in order. content-type is a good start; add host only if no proxy rewrites it.
require_nonce kipchak true Require a nonce, so two identical requests in the same second are both accepted
tolerance all 300 Seconds either side of the server clock a signature stays valid
paths all [] Path prefixes this profile protects when the middleware is global
max_body_bytes all 1048576 Larger bodies are refused with 413 before being read in full
replay all true Set false to skip replay protection for this profile

Configuration is validated when the API boots. A missing or short secret, an unknown scheme, or a default_profile that does not exist stops the API from starting rather than accepting requests it cannot verify.

Replay protection

Key Default Meaning
enabled true Off switch for every profile
store valkey valkey, memcached, filecache or container
pool cache Pool name from the Valkey or Memcached driver config
service For container: a service that is a NonceStore, a \Redis/\RedisCluster/Predis/Relay client, a \Memcached client, or a PSR-6 pool
prefix kipchak.hmac Key prefix; give each API its own if they share a store
fail_open false When the store is unreachable: false answers 503, true accepts the request and logs an error

Each accepted signature is stored for exactly as long as it could still pass the timestamp check, then expires. Only signatures that verify are stored, so forged requests cannot fill the store or burn a client's nonces.

The Valkey and Memcached stores are atomic: when 24 processes raced to claim the same signature in the test suite, exactly one won every time. The filecache store and plain PSR-6 pools are check-then-set; in the same race through a PSR-6 adapter, between 4 and 21 copies got through. Sequential replays are always caught.

If the Valkey connection fails (for example while Valkey restarts), the store reconnects and retries once, so long-lived FrankenPHP workers recover by themselves once Valkey is back. Keep the pool's timeout option short (the Valkey sample uses 2.5s), since each request during an outage waits for one reconnect attempt.

Per route instead of globally

Set 'enabled' => false and attach the handler where you need it, naming the profile:

A route-level profile is used regardless of its paths.

What the route receives

Verified requests reach your controller with these request attributes:

Attribute (constant on VerifySignature) Value
ATTRIBUTE_PROFILE (kipchak.hmac.profile) Profile name
ATTRIBUTE_KEY_ID (kipchak.hmac.key_id) Kipchak scheme: the client's key ID
ATTRIBUTE_TIMESTAMP (kipchak.hmac.timestamp) The signed Unix timestamp
ATTRIBUTE_MESSAGE_ID (kipchak.hmac.message_id) Standard Webhooks: the sender's message ID

The body is still readable, and Slim's body parsing still works.

Rejected requests get a JSON error with a stable error code:

error Status When
signature_missing 401 No signature header(s)
signature_malformed 401 Header present but unparseable, a required part missing, or a nonce that is too short
signature_invalid 401 Wrong signature, or an unknown key ID (deliberately indistinguishable)
timestamp_out_of_tolerance 401 Authentic but outside tolerance (check the client's clock)
signature_replayed 401 This exact signed message was already accepted
body_too_large 413 Body over max_body_bytes
replay_store_unavailable 503 Replay store down and fail_open is false. Safe to retry with a fresh signature.

The detail behind each rejection (which key ID, how far the clock was off) is logged at warning through the Kipchak logger. Secrets and signatures are never logged.

The kipchak scheme

The client sends one header:

The canonical request is these lines joined with \n, with no trailing newline:

The scheme name, host and port are not signed, so requests keep verifying behind load balancers and proxies. Proxies must not rewrite the path, query or body.

Signing from PHP

Sign last, once the body and every signed header are final. The signer generates the timestamp and nonce.

Signing from Python

Signing from a shell

For a request with no query string and content-type as the only signed header:

Webhooks

Stripe

Use the endpoint's signing secret from the Stripe dashboard. While rolling it, list both secrets.

Any other provider that signs "{timestamp}.{body}" with HMAC-SHA256 in a t=...,v1=... header can use this scheme with its own header.

Standard Webhooks and Svix

Senders retry failed deliveries with the same message ID and a new timestamp. Replay protection blocks exact resends but lets retries through, so if your handler must run once per message, deduplicate on VerifySignature::ATTRIBUTE_MESSAGE_ID.

Sending webhooks from your API

Testing

The expected signatures in the tests were computed with the openssl CLI, and the Standard Webhooks test uses the spec's published example, so the tests pin the wire format rather than echo the implementation.

What is a Kipchak Middleware?

Kipchak Middleware is a standard PHP PSR-15 middleware.

Middleware is a component that sits between the request and response of a web application, allowing for interception and modification of the request and response data. In the context of Kipchak, middleware can be used to handle errors, authentication, and other cross-cutting concerns that are common across one or more API endpoints.


All versions of middleware-auth-hmac with dependencies

PHP Build Version
Package Version
Requires php Version >=8.4
kipchak/core Version ^3.18
kipchak/driver-config Version ^1
kipchak/driver-logger Version ^1
psr/cache Version ^2 || ^3
psr/clock Version ^1
psr/http-server-middleware Version ^1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package kipchak/middleware-auth-hmac contains the following files

Loading the files please wait ...