chr15k / php-auth-generator
Generate authentication tokens
Requires
- php: ^8.2
Requires (Dev)
- laravel/pint: ^1.20.0
- pestphp/pest: ^2.36|^3.7.4
- pestphp/pest-plugin-type-coverage: ^2.8.7|^3.2.3
- phpstan/phpstan: ^1.12.16
- rector/rector: ^1.2.10
- symfony/var-dumper: ^7.2.0
Suggests
- ext-sodium: Support EdDSA (Ed25519) signatures
- paragonie/sodium_compat: Support EdDSA (Ed25519) signatures when libsodium is not present
README
A PHP library that focuses exclusively on generating HTTP authentication tokens, including Basic Auth, Bearer tokens, Digest Auth, and JWTs with a fluent API. Built with zero dependencies, it's lightweight and adds token creation capabilities without bloating your project.
Important
This package is designed solely for generating authentication tokens. It does not include any token decoding, validation, or verification functionality.
Tip
For complete JWT encoding/decoding solutions, consider using a dedicated library such as firebase/php-jwt
Installation
Note
- Requires PHP 8.2+
composer require chr15k/php-auth-generator
Usage
Basic Auth
use Chr15k\AuthGenerator\AuthGenerator; // Generate a Basic Auth token $token = AuthGenerator::basicAuth() ->username('user') ->password('pass') ->toString(); // Output: dXNlcjpwYXNz
Bearer Token
use Chr15k\AuthGenerator\AuthGenerator; // Generate a Bearer token with custom length and prefix $token = AuthGenerator::bearerToken() ->length(64) ->prefix('api_') ->toString(); // Output: api_8f7d49b3c70e4...
Digest Auth
use Chr15k\AuthGenerator\AuthGenerator; use Chr15k\AuthGenerator\Enums\DigestAlgorithm; // Generate a Digest Auth token $token = AuthGenerator::digestAuth() ->username('user') ->password('pass') ->realm('example.com') ->uri('/protected-resource') ->method('GET') ->algorithm(DigestAlgorithm::MD5) ->toString(); // Output: username="user", realm="example.com", nonce="1234abcd...", uri="/protected-resource", algorithm="MD5" response="a2fc57d9..."
JWT
use Chr15k\AuthGenerator\AuthGenerator; use Chr15k\AuthGenerator\Enums\Algorithm; // Generate a JWT with claims and custom algorithm $token = AuthGenerator::jwt() ->key('secret-key') ->algorithm(Algorithm::HS256) ->claim('user_id', 123) ->claim('role', 'admin') ->expiresIn(3600) // 1 hour ->issuedBy('https://myapp.com') ->toString(); // Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxMjMsInJvbGUiOiJhZG1pbiIsImlhdCI6MTY...
Headers and Formatting
The library provides fluent methods to format tokens for use in HTTP headers:
use Chr15k\AuthGenerator\AuthGenerator; // Generate a Basic Auth token and get headers in one go $headers = AuthGenerator::basicAuth() ->username('user') ->password('pass') ->toArray([ 'Content-Type' => 'application/json', 'Accept' => 'application/json', ]); /* [ 'Authorization' => 'Basic dXNlcjpwYXNz', 'Content-Type' => 'application/json', 'Accept' => 'application/json', ] */ // For Bearer tokens $headers = AuthGenerator::bearerToken() ->length(64) ->prefix('api_') ->toArray(); // Output: ['Authorization' => 'Bearer api_8f7d49b3...'] // For JWT tokens $headers = AuthGenerator::jwt() ->key('secret-key') ->claim('user_id', 123) ->toArray([ 'Content-Type' => 'application/json', ]); // Get the raw token string $token = AuthGenerator::basicAuth() ->username('user') ->password('pass') ->toString(); // Output: 'dXNlcjpwYXNz' // Format tokens directly to header strings with the fluent toHeader() method $headerString = AuthGenerator::basicAuth() ->username('user') ->password('pass') ->toHeader(); // Output: 'Basic dXNlcjpwYXNz'
HTTP Client Integration
You can easily integrate with popular HTTP clients:
Guzzle
use Chr15k\AuthGenerator\AuthGenerator; use GuzzleHttp\Client; // Create Guzzle client with authentication headers $client = new Client([ 'base_uri' => 'https://api.example.com', 'headers' => AuthGenerator::jwt() ->key('secret-key') ->claim('user_id', 123) ->expiresIn(3600) ->toArray([ 'Content-Type' => 'application/json', ]), ]); $response = $client->get('/resources');
Advanced Usage
You can access the underlying generator instance if needed:
$generator = AuthGenerator::basicAuth() ->username('user') ->password('pass') ->build(); // Now you can work directly with the generator $token = $generator->generate();
Documentation
- User Guide - Comprehensive guide with examples
- API Cheat Sheet - Quick reference of all available methods