Download the PHP package aporat/laravel-auth-signature without Composer
On this page you can find all versions of the php package aporat/laravel-auth-signature. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download aporat/laravel-auth-signature
More information about aporat/laravel-auth-signature
Files in aporat/laravel-auth-signature
Package laravel-auth-signature
Short Description A Laravel package providing a middleware for validating API requests with HMAC-SHA256 signatures
License MIT
Homepage https://github.com/aporat/laravel-auth-signature
Informations about the package laravel-auth-signature
Laravel Auth Signature
A robust Laravel package providing a middleware for validating API requests with HMAC-SHA256 signatures. It features configurable signature templates, version-specific authentication settings, and a secure, time-based validation to protect your endpoints.
โจ Features
- HMAC-SHA256 Validation: Securely validates incoming API requests.
- Configurable Signature Templates: Easily define the exact order and components of the string-to-be-signed.
- Version-Specific Rules: Apply different secrets, states, and signature templates based on an
X-Auth-Versionheader. - Timestamp Validation: Protects against replay attacks by ensuring requests are recent.
- Simple Middleware Integration: Secure your routes with a single middleware alias:
auth.signature. - Clean and Modern Codebase: Fully typed, tested, and built on modern PHP and Laravel features.
๐ Requirements
- PHP: ^8.4
- Laravel: 12.x or 13.x
๐ Installation
-
Install the package via Composer:
-
Publish the configuration file. The service provider is auto-discovered.
This will create a new configuration file at
config/auth-signature.php.
๐ง Configuration
Edit config/auth-signature.php to define your clients, authentication versions, and settings.
Remember to add the corresponding keys to your .env file for security.
๐ ๏ธ Usage
Applying the Middleware
Apply the auth.signature middleware to any route or route group that requires signature validation.
The middleware will automatically validate incoming requests and throw a SignatureException (resulting in a 4xx HTTP response) if validation fails.
What the signature covers
The signature is an HMAC-SHA256 over the concatenation (no separator) of the
components named by the auth version's signature_template. The signature
component is the canonical parameter string, built from the request as
follows:
- Parameter keys are lowercased.
- Keys are sorted as plain strings (so
"10"sorts before"9"). - Each key/value pair is encoded as
rawurlencode(key)=value, and the pairs are joined with&. - Values encode as:
nullโ empty,true/falseโ1/0, integers verbatim, floats keeping their zero fraction (1.0, not1), objects as compact JSON with recursively sorted keys, and everything else percent-encoded withrawurlencode. - List values expand to
key[0],key[1], โฆ after the sort, in list order โtags[10]therefore followstags[9], anda[0]precedesa2. Expansion is recursive, and an empty list encodes askey=.
The parameter set is read off the wire: the raw request body (JSON decoded
on a JSON request, parse_str on a form-urlencoded one) unioned with the raw
query string, with the body winning a key collision.
It is deliberately not $request->input(). Laravel's TrimStrings and
ConvertEmptyStringsToNull are global middleware, so they rewrite the parsed
bags before any route middleware โ including this one โ runs. Verifying against
those rewritten values means a client that legitimately signs name=Rabi%20
gets checked against name=Rabi, and every request carrying leading or
trailing whitespace in a string field is rejected with a mismatch the client
cannot see or fix. Reading the raw request leaves both transforms in place for
the application behind the middleware.
The query string is included because query parameters are readable through
$request->input() regardless of content type, so leaving them out would let
anyone append parameters to a captured request without invalidating it.
Uploaded files are not signed โ their temporary paths differ on every
request โ so a multipart upload signs only its text fields, which are read from
the parsed bag because PHP consumes a multipart body before php://input can
be read.
The path component is the percent-decoded path (rawurldecode, which leaves a
literal + alone), and bundle_id is taken from the client's configuration
verbatim.
Generating a Signature
You can use the SignatureGenerator class to create a valid signature, which is useful for testing or for client-side implementations.
Example Client Request
A client would then make a request including the generated signature and other required headers.
๐งช Testing
The package is fully tested. To run the test suite:
๐ค Contributing
Contributions are welcome! Please feel free to fork the repository, create a feature branch, and open a pull request.
๐ License
This package is open-source software licensed under the MIT License.
๐ฌ Support
If you encounter any issues or have questions, please open an issue on the GitHub repository.
All versions of laravel-auth-signature with dependencies
aporat/laravel-filter-var Version ^5.0
illuminate/support Version ^12.0 || ^13.0