Authenticator
Using the HTTP Authenticator
283k Downloads / Month
Open Source MIT License
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.
I strongly recommend using a library like PHP dotenv to store the credentials and keep usernames and passwords out of version control.
public Authenticator::__construct(VaultInterface $vault): Authenticator
Create a new authenticator instance by passing the desired type of authorization vault in the class constructor.
| Name | Type | Description |
|---|---|---|
| vault | VaultInterface | Instance of VaultInterface. Usually instance BasicVault::class or DigestVault::class |
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);
public Authenticator::withVault(VaultInterface $vault): Authenticator
Create a new authenticator instance by calling the static factory method and passing the vault instance directly.
| Name | Type | Description |
|---|---|---|
| vault | VaultInterface | Instance of VaultInterface. Usually instance BasicVault::class or DigestVault::class |
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);
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.
| Name | Type | Description |
|---|---|---|
| username | string | Username for securing the resource |
| password | string | Password for securing the resource |
| realm | string | Name of the secured resource |
use Intervention\HttpAuth\Authenticator; // create authenticator $auth = Authenticator::basic( 'myUsername', 'myPassword', 'Secured Area', );
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.
| Name | Type | Description |
|---|---|---|
| username | string | Username for securing the resource |
| password | string | Password for securing the resource |
| realm | string | Name of the secured resource |
use Intervention\HttpAuth\Authenticator; // create authenticator $auth = Authenticator::digest( 'myUsername', 'myPassword', 'Secured Area', );
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.
| Name | Type | Description |
|---|---|---|
| message | string or null | Content that is displayed to the user if authentication fails. |
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!');