Sorry, no results were found for this search.

Authenticator

Using the HTTP Authenticator

283k Downloads / Month

Open Source MIT License

Secure PHP resources with HTTP Basic and Digest authentication. Configure vaults, custom failure messages, and integrate effortlessly with code examples.

Creating the Authenticator

I strongly recommend using a library like PHP dotenv to store the credentials and keep usernames and passwords out of version control.

Create Authenticator by Passing the Vault

Create by Class Constructor

public Authenticator::__construct(VaultInterface $vault): Authenticator

Create a new authenticator instance by passing the desired type of authorization vault in the class constructor.

Parameters

Name Type Description
vault VaultInterface Instance of VaultInterface. Usually instance BasicVault::class or DigestVault::class

Example

use Intervention\HttpAuth\Authenticator;
use Intervention\HttpAuth\Vaults\BasicVault

// create vault first
$vault = new BasicVault(
    'myUsername',
    'myPassword',
    'Secured Area',
);

// create authenticator
$auth = new Authenticator($vault);

Create by Static Helper

public Authenticator::withVault(VaultInterface $vault): Authenticator

Create a new authenticator instance by calling the static factory method and passing the vault instance directly.

Parameters

Name Type Description
vault VaultInterface Instance of VaultInterface. Usually instance BasicVault::class or DigestVault::class

Example

use Intervention\HttpAuth\Authenticator;
use Intervention\HttpAuth\Vaults\DigestVault

// create vault first
$vault = new DigestVault('myUsername', 'myPassword');

// create authenticator with vault
$auth = Authenticator::withVault($vault);

Create Authenticator with Static Factory Methods

Basic Auth Factory Method

public Authenticator::basic(string $username, string $password, string $realm = 'Secured Area'): Authenticator

Create a new basic auth authenticator instance by calling the static factory method and passing the credentials as well as the name of the resource.

Parameters

Name Type Description
username string Username for securing the resource
password string Password for securing the resource
realm string Name of the secured resource

Example

use Intervention\HttpAuth\Authenticator;

// create authenticator
$auth = Authenticator::basic(
    'myUsername',
    'myPassword',
    'Secured Area',
);

Digest Auth Factory Method

public Authenticator::digest(string $username, string $password, string $realm = 'Secured Area'): Authenticator

Create a new digest auth authenticator instance by calling the static factory method and passing the credentials as well as the name of the resource.

Parameters

Name Type Description
username string Username for securing the resource
password string Password for securing the resource
realm string Name of the secured resource

Example

use Intervention\HttpAuth\Authenticator;

// create authenticator
$auth = Authenticator::digest(
    'myUsername',
    'myPassword',
    'Secured Area',
);

Securing the Resource

public Authenticator::secure(?string $message = null): void

After you create an HTTP authenticator instance, you need to call secure() to secure the resource by checking for credentials. Otherwise nothing will happen.

By calling Authenticator::secure() the server asks the user for a username and a password. If the credentials are entered incorrectly, an HTTP status code 401 is sent and the user will not be able to access the resource.

The method optionally accepts a string as content that is displayed to the user if verification fails. HTML content can also be passed here or output from template engines can be used.

Parameters

Name Type Description
message string or null Content that is displayed to the user if authentication fails.

Example

use Intervention\HttpAuth\Authenticator;

// creating the authenticator and checking credentials can be a one liner
Authenticator::basic('myUsername', 'myPassword')->secure();
use Intervention\HttpAuth\Authenticator;

// create auth
$auth = Authenticator::digest('myUsername', 'myPassword', 'Secure Area');

// secure resource with custom message
$auth->secure('Sorry, you can not access this resource!');
Edit