chr15k/php-auth-generator

Generate authentication tokens

0.1.2 2025-06-16 13:10 UTC

This package is auto-updated.

Last update: 2025-06-16 13:17:06 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

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

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