Skip to content

Generate HTTP authentication headers in PHP (Basic, Bearer, Digest, JWT) with a fluent, zero-dependency API.

License

Notifications You must be signed in to change notification settings

chr15k/php-auth-generator

Repository files navigation

PHP Auth Generator

GitHub Workflow Status (main) Total Downloads Latest Version License

📦 Overview

php-auth-generator is a lightweight PHP library for generating HTTP authentication headers with a clean, fluent API.

Whether you're building API testing tools, CLI utilities, or custom HTTP clients, this package helps you programmatically generate authentication headers for:

  • 🔐 Basic Auth
  • 🪪 Bearer Tokens
  • 🔄 Digest Auth
  • 🔏 JWTs

Built with zero dependencies, it’s fast to install, easy to use, and doesn’t add unnecessary bloat to your project.

✨ Features

  • ✅ Fluent, chainable API for building authentication headers effortlessly
  • ✅ Lightweight and dependency-free PHP library
  • ✅ Supports Basic Auth, Bearer tokens, Digest Auth, and JWT generation
  • ✅ Easily integrates into CLI tools, HTTP clients, API testing, and automation workflows

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

About

Generate HTTP authentication headers in PHP (Basic, Bearer, Digest, JWT) with a fluent, zero-dependency API.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages