PHPackages                             chr15k/php-auth-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. chr15k/php-auth-generator

ActiveLibrary[HTTP &amp; Networking](/categories/http)

chr15k/php-auth-generator
=========================

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

0.2.1(10mo ago)2291MITPHPPHP ^8.2CI passing

Since Jun 15Pushed 10mo agoCompare

[ Source](https://github.com/chr15k/php-auth-generator)[ Packagist](https://packagist.org/packages/chr15k/php-auth-generator)[ RSS](/packages/chr15k-php-auth-generator/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (6)Dependencies (6)Versions (7)Used By (1)

PHP Auth Generator
==================

[](#php-auth-generator)

 [![GitHub Workflow Status (main)](https://camo.githubusercontent.com/88ef37aae69f3b7c8676254e7b1efe4a5246d6b3fa90840f474d4016add3346d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f63687231356b2f7068702d617574682d67656e657261746f722f6d61696e2e796d6c)](https://github.com/chr15k/php-auth-generator/actions) [![Total Downloads](https://camo.githubusercontent.com/ac01597c871353f5fb374b58926322e96327f2dd8c70d157d25492d81d29048b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f63687231356b2f7068702d617574682d67656e657261746f72)](https://packagist.org/packages/chr15k/php-auth-generator) [![Latest Version](https://camo.githubusercontent.com/aa2d6eceac8216cfe9bb1bf6b85c40bc5d92fdb5bf8c5670e5344826935b882a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f63687231356b2f7068702d617574682d67656e657261746f72)](https://packagist.org/packages/chr15k/php-auth-generator) [![License](https://camo.githubusercontent.com/70c489cbdc3920604c8a16ec3564457d7d55460ed4c81585f1c46cbf11bc5f3b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f63687231356b2f7068702d617574682d67656e657261746f72)](https://packagist.org/packages/chr15k/php-auth-generator)

### 📦 Overview

[](#-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

[](#-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](https://github.com/firebase/php-jwt)

Installation
------------

[](#installation)

Note

- Requires [PHP 8.2+](https://www.php.net/releases/)

```
composer require chr15k/php-auth-generator
```

Usage
-----

[](#usage)

### Basic Auth

[](#basic-auth)

```
use Chr15k\AuthGenerator\AuthGenerator;

// Generate a Basic Auth token
$token = AuthGenerator::basicAuth()
    ->username('user')
    ->password('pass')
    ->toString();

// Output: dXNlcjpwYXNz
```

### Bearer Token

[](#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

[](#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

[](#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
----------------------

[](#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
-----------------------

[](#http-client-integration)

You can easily integrate with popular HTTP clients:

### Guzzle

[](#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
--------------

[](#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
-------------

[](#documentation)

- [User Guide](docs/USER_GUIDE.md) - Comprehensive guide with examples
- [API Cheat Sheet](docs/API_CHEATSHEET.md) - Quick reference of all available methods

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance53

Moderate activity, may be stable

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

6

Last Release

325d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c817e814df42c78ac76505c9394a786946a0d9517b64984eca34b6f04010df95?d=identicon)[chr15k](/maintainers/chr15k)

---

Top Contributors

[![chr15k](https://avatars.githubusercontent.com/u/67823070?v=4)](https://github.com/chr15k "chr15k (30 commits)")

---

Tags

httpphpjwtapicliauthgenerator

###  Code Quality

TestsPest

Static AnalysisPHPStan, Rector

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/chr15k-php-auth-generator/health.svg)

```
[![Health](https://phpackages.com/badges/chr15k-php-auth-generator/health.svg)](https://phpackages.com/packages/chr15k-php-auth-generator)
```

###  Alternatives

[ciareis/bypass

Bypass for PHP provides a quick way to create a custom instead of an actual HTTP server to return prebaked responses to client requests. This is most useful in tests, when you want to create a mock HTTP server and test how your HTTP client handles different types of responses from the server.

11813.5k6](/packages/ciareis-bypass)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
