Download the PHP package popphp/pop-auth without Composer
On this page you can find all versions of the php package popphp/pop-auth. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download popphp/pop-auth
More information about popphp/pop-auth
Files in popphp/pop-auth
Package pop-auth
Short Description Pop Auth Component for Pop PHP Framework
License BSD-3-Clause
Homepage https://github.com/popphp/pop-auth
Informations about the package pop-auth
pop-auth
- Overview
- Install
- Quickstart
- Handling Exceptions
- Using a File
- Using a JWT
- Getting the User
- Rehashing Passwords
Overview
pop-auth provides adapters to authenticate users via different authentication sources.
The adapters share the same interface and are interchangeable. The available
adapters are:
- File
- JWT
pop-auth is a component of the Pop PHP Framework.
Database-backed authentication (matching a username/password against a
Pop\Db\Record-backed table) has moved to thepop-dbcomponent, in favor of a tighter coupling with the DB layer it depends on. Seepop-db's documentation for the replacement.Remote/delegated authentication (forwarding credentials to a remote HTTP endpoint and checking the response) is no longer a dedicated adapter here — use
pop-http'sClientandAuthclasses directly.
Install
Install pop-auth using Composer.
composer require popphp/pop-auth
Or, require it in your composer.json file
"require": {
"popphp/pop-auth" : "^5.0.0"
}
Top
Quickstart
To verify an authentication attempt, create a new auth object pointed at its authentication source.
From there, you can attempt to call the authenticate() with a username and password.
If you need to reference the same authentication attempt result at a later time in the application,
you can call isAuthenticated():
Top
Handling Exceptions
authenticate() returning 0 means the credentials were checked and didn't match — that's a normal, expected
outcome, not an error. It can also throw Pop\Auth\Exception, but only when the adapter can't perform the check
at all: a missing or unreadable file, or malformed/unusable key material for the JWT adapter. Wrap
authenticate() in a try/catch to handle both cases:
Top
Using a File
Using the file adapter, you need to create a file containing a colon-delimited list of usernames and passwords or, preferably, password hashes:
The file adapter also supports realm-scoped entries and a custom field delimiter. When a
realm is set, matching lines must have 3 fields (username:realm:hash) with both the
username and the realm matching:
A different delimiter can be set as the third constructor argument, e.g.
new Auth\File('/path/to/.htmyauth', 'example.com', '|') for pipe-delimited entries.
Top
Using a JWT
Using the JWT adapter, you authenticate a bearer token by verifying its signature and claims — there's no network call and no stored username/password, just the token itself and the key material to check it against.
The algorithm is fixed at construction and is never read from the token — pass the shared secret for HS256,
or a PEM-encoded public key for RS256/ES256:
exp and nbf claims, when present on the token, are always checked (with an optional leeway, in seconds, to
absorb clock skew between issuer and verifier). aud and iss are opt-in — only checked if you configure them:
Top
Getting the User
The JWT adapter has a method that allows you to get the decoded claims from a successfully verified token.
That method is getUser():
This allows you access to the token's claims without decoding it yourself.
Top
Rehashing Passwords
After a successful authentication, you can check whether the stored hash should be upgraded (e.g. it was stored as plaintext, or was hashed at a now-outdated cost):
This is only meaningful for the File adapter, since it's the one that compares a submitted
password against a stored hash. The JWT adapter verifies a signature instead of a hash —
needsRehash() is always false on it.
Top