PHPackages                             pflorek/php-basic-auth - 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. pflorek/php-basic-auth

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

pflorek/php-basic-auth
======================

Provides a simple way to get or set credentials (username, password) on a PSR-7 `RequestInterface`. Also it helps challenging an unauthorized client by adding the 'WWW-authenticate' header line with status code 401 to a PSR-7 `ResponseInterface`.

1.0.1(7y ago)3761MITPHPPHP &gt;=5.4

Since Nov 16Pushed 6y ago1 watchersCompare

[ Source](https://github.com/pflorek/php-basic-auth)[ Packagist](https://packagist.org/packages/pflorek/php-basic-auth)[ RSS](/packages/pflorek-php-basic-auth/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (1)Dependencies (2)Versions (3)Used By (0)

PHP Basic Auth
==============

[](#php-basic-auth)

[![Build Status](https://camo.githubusercontent.com/e7477a2acf07e42b52bd472f054c24929d7f8362b8e632ff876c140cb880a7b0/68747470733a2f2f7472617669732d63692e6f72672f70666c6f72656b2f7068702d62617369632d617574682e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/pflorek/php-basic-auth)[![Coverage Status](https://camo.githubusercontent.com/06e71fcbf84dc70b51f1f2277b4ce19e1749795ffe0b7335b541c7bffc22eaff/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f70666c6f72656b2f7068702d62617369632d617574682f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/pflorek/php-basic-auth?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/14b619fae25c78a8b96ecf8cba225eed1a77d3c3015c9c796d7bacb3c9b16d82/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f70666c6f72656b2f7068702d62617369632d617574682f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/pflorek/php-basic-auth/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/79f6b0ccfe7cb24f52de192342df039db5b50ad9062a6bf62750c24fe61edeca/68747470733a2f2f706f7365722e707567782e6f72672f70666c6f72656b2f7068702d62617369632d617574682f762f737461626c65)](https://packagist.org/packages/pflorek/php-basic-auth)[![Total Downloads](https://camo.githubusercontent.com/6a693be9c9c2ab518bb844180753907347778fe3ad448ece98a6331ae4ff7a95/68747470733a2f2f706f7365722e707567782e6f72672f70666c6f72656b2f7068702d62617369632d617574682f646f776e6c6f616473)](https://packagist.org/packages/pflorek/php-basic-auth)[![Latest Unstable Version](https://camo.githubusercontent.com/0467dbf24df5b3fe95cd1165deedfe311a796b5438338fa903513112a6a7e1be/68747470733a2f2f706f7365722e707567782e6f72672f70666c6f72656b2f7068702d62617369632d617574682f762f756e737461626c65)](https://packagist.org/packages/pflorek/php-basic-auth)[![License](https://camo.githubusercontent.com/3411de10847c5b2a637404d1ba009753ed73c997f2075f80f1d6bb8a4be505e0/68747470733a2f2f706f7365722e707567782e6f72672f70666c6f72656b2f7068702d62617369632d617574682f6c6963656e7365)](https://packagist.org/packages/pflorek/php-basic-auth)[![Monthly Downloads](https://camo.githubusercontent.com/534c92db79f4ded3b205d65c22e665fbcae36a040804897239ea88ca522a9407/68747470733a2f2f706f7365722e707567782e6f72672f70666c6f72656b2f7068702d62617369632d617574682f642f6d6f6e74686c79)](https://packagist.org/packages/pflorek/php-basic-auth)[![Daily Downloads](https://camo.githubusercontent.com/30594eecfb4e234a266679319bc444835004065cf38b915987aa347a26568517/68747470733a2f2f706f7365722e707567782e6f72672f70666c6f72656b2f7068702d62617369632d617574682f642f6461696c79)](https://packagist.org/packages/pflorek/php-basic-auth)[![composer.lock](https://camo.githubusercontent.com/bc688fb4fb9831d6951d75d34f4f66b807e153133634e455c8eff4169d8115cf/68747470733a2f2f706f7365722e707567782e6f72672f70666c6f72656b2f7068702d62617369632d617574682f636f6d706f7365726c6f636b)](https://packagist.org/packages/pflorek/php-basic-auth)

This library provides a simple way to get or set credentials (username, password) on a PSR-7 `RequestInterface`. Also it helps challenging an unauthorized client by adding the 'WWW-authenticate' header line with status code 401 to a PSR-7 `ResponseInterface`. It should be helpful if a PSR-15 `Middleware` is not applicable.

- There is no validation if username is correct (should not contain `:`).
- Also there is no validation if basic credentials are properly base64 encoded.
- Omitted `Authorization` header line or missing basic credentials will return `null` credentials.
- Can only challenge for `Basic Auth`. `Digest` is currently not supported.
- For backward compatibility for PHP &gt;= 5.4 PSR-17 HTTP factories currently not supported.
- Should comply with [RFC 7617](https://tools.ietf.org/html/rfc7617).

Usage
-----

[](#usage)

### Obtain credentials

[](#obtain-credentials)

Obtain credentials (username, password) from PSR-7 request interface.

```
use Psr\Http\Message\RequestInterface;
use \PFlorek\BasicAuth\BasicAuth;

//Given request with header line 'Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='
$credentials = $this->basicAuth->obtainCredentials($request);

var_dump($credentials);

//object(Credentials)#1 (2) {
//  ["username":"Credentials":private]=>
//  string(7) "Aladdin"
//  ["password":"Credentials":private]=>
//  string(11) "open sesame"
//}
```

### Add credentials

[](#add-credentials)

Add credentials (username, password) to PSR-7 request interface for basic authentication.

```
use Psr\Http\Message\RequestInterface;
use \PFlorek\BasicAuth\BasicAuth;

$credentials = new Credentials('Alladin, 'open sesame');
$request = $this->basicAuth->addCredentials($request, $credentials);

var_dump($request->getHeaderLine('WWW-Authenticate'));

//string(34) "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
```

### Add challenge

[](#add-challenge)

Add challenge with realm to PSR-7 response interface for basic authentication.

```
use Psr\Http\Message\ResponseInterface;
use \PFlorek\BasicAuth\BasicAuth;

$response = $this->basicAuth->addChallenge($response, 'WallyWorld);

var_dump($response->getHeaderLine('WWW-Authenticate'));

//string(24) "Basic realm=\"WallyWorld\""

var_dump($response->getStatusCode());

//int(401)
```

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

[](#installation)

Use [Composer](https://getcomposer.org) to install the package:

```
composer require pflorek/php-basic-auth
```

Authors
-------

[](#authors)

- [Patrick Florek](https://github.com/pflorek)

Contribute
----------

[](#contribute)

Contributions are always welcome!

- Report any bugs or issues on the [issue tracker](https://github.com/pflorek/php-basic-auth/issues).
- You can download the sources at the package's [Git repository](https://github.com/pflorek/php-basic-auth).

License
-------

[](#license)

All contents of this package are licensed under the [MIT license](LICENSE).

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity58

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 ~0 days

Total

2

Last Release

2785d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/13916107?v=4)[Patrick Florek](/maintainers/pflorek)[@pflorek](https://github.com/pflorek)

---

Top Contributors

[![pflorek](https://avatars.githubusercontent.com/u/13916107?v=4)](https://github.com/pflorek "pflorek (6 commits)")

---

Tags

httpresponserequestpsr-7authorizationbasicauthrealmWWW-AuthentocateRFC 7617

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/pflorek-php-basic-auth/health.svg)

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

###  Alternatives

[guzzlehttp/psr7

PSR-7 message implementation that also provides common utility methods

8.0k1.1B3.9k](/packages/guzzlehttp-psr7)[psr/http-factory

PSR-17: Common interfaces for PSR-7 HTTP message factories

1.9k747.1M2.6k](/packages/psr-http-factory)[psr/http-server-handler

Common interface for HTTP server-side request handler

180114.7M1.2k](/packages/psr-http-server-handler)[art4/requests-psr18-adapter

Use WordPress/Requests as a PSR-18 HTTP client

157.1k](/packages/art4-requests-psr18-adapter)[chillerlan/php-httpinterface

A PSR-7/17/18 http message/client implementation

1417.8k6](/packages/chillerlan-php-httpinterface)

PHPackages © 2026

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