Download the PHP package rafalmasiarek/authkit without Composer
On this page you can find all versions of the php package rafalmasiarek/authkit. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rafalmasiarek/authkit
More information about rafalmasiarek/authkit
Files in rafalmasiarek/authkit
Package authkit
Short Description Lightweight and extensible PHP authentication library
License MIT
Informations about the package authkit
AuthKit
Lightweight, extensible PHP 8.1+ authentication library.
- Registration & login with secure password hashing
- Server-side session tokens stored in DB with optional TTL
- Pluggable login extension pipeline — deny or challenge after credentials pass
- Multi-step challenge flows (MFA, email activation, device check, etc.)
- Pluggable credential provider — local PDO default, Cognito/Keycloak as external packages
- Pluggable password hasher —
NativePasswordHasherdefault - Pluggable token transport — PHP session (web) or Bearer header (API)
- Pluggable user ID policy — auto-increment default, UUID in your app
- Lean core schema —
userstable contains onlyid,email,password_hash; extra columns (e.g.active,suspended_at) declared by the extensions that need them - Optional hooks for audit and policy (rate-limit, IP checks, logging)
- Admin features: force logout by user / token / email
Works with MySQL/MariaDB and SQLite. Designed for Slim, Laminas, Symfony or plain PHP.
Installation
Quick start
Constructor
Clock and timezone
By default Auth uses UTC for all session and challenge expiry calculations.
purgeExpiredTokens() and purgeExpired() are maintenance operations called outside the login flow.
They require an explicit DateTimeImmutable $now from the caller:
Login result
login() returns a Login object — never throws by default.
With throwExceptions: true — failure throws AuthException instead.
Login extension pipeline
Extensions run after credentials are verified. Register them with addLoginExtension().
The first deny or challenge decision terminates the pipeline.
LoginContext carries $user, $ip, $userAgent, and $credentialMeta from the credential provider.
Use $context->credential('key') to read individual metadata values.
Challenge flows (MFA, email activation, etc.)
An extension can require a second step instead of immediately allowing or denying.
Flow:
Challenge storage must be provided:
Schema: (new PdoChallengeStorage($pdo))->createSchema();
Token transport
Controls how the active token is stored and read on the client side.
Web (default — PHP session)
API (Bearer header)
External credential providers
AuthKit core authenticates into a local User object. External providers such as Cognito, Keycloak, Authentik or Zitadel should map their external identity to a local user inside their own CredentialProviderInterface implementation.
A credential provider may pass non-persistent login metadata through CredentialResult::success($user, $meta). This metadata is available in login extensions through LoginContext::credential().
Users authenticated via an external provider have no local password — password_hash is NULL in the database. PdoCredentialProvider explicitly rejects login attempts for such accounts.
This keeps AuthKit core storage-agnostic and avoids forcing external identity tables into the base package.
Storage schema
PdoUserStorage::createSchema() creates the users and sessions tables.
PdoChallengeStorage::createSchema() creates auth_challenges (only needed for challenge flows).
Both support MySQL/MariaDB and SQLite.
Auth::createSchema() — preferred bootstrap entry point
Auth::createSchema() is the single call to initialise the full schema. It:
- Calls
PdoUserStorage::createSchema()with the correct column types for the configured ID policy. - Runs
additionalSchema()for every registered login extension that implementsSchemaProviderInterface. - Accepts extra
SchemaProviderInterfaceinstances for non-extension components (e.g. a mailer).
$storage->createSchema() still works for simple bootstraps without extensions.
User ID policy
UserIdPolicyInterface controls both ID generation and the schema column types used by createSchema().
| Method | Purpose |
|---|---|
generate() |
Returns the ID to insert, or null to let the database auto-increment. |
idColumnDefinition(string $driver) |
Full inline column definition for users.id. Drives PRIMARY KEY placement. |
userIdForeignType(string $driver) |
SQL type for sessions.user_id and any other FK referencing users.id. |
NullUserIdPolicy (default) — delegates to the database:
| Driver | users.id |
sessions.user_id |
|---|---|---|
| MySQL | INT NOT NULL AUTO_INCREMENT |
INT NOT NULL |
| SQLite | INTEGER PRIMARY KEY AUTOINCREMENT |
INTEGER NOT NULL |
Custom policy (e.g. UUID):
Extension schema — SchemaProviderInterface
Any class can declare the tables or columns it needs by implementing SchemaProviderInterface.
Auth::createSchema() automatically picks it up from registered login extensions and any explicitly
passed providers.
Rules for additionalSchema():
- Every statement must be idempotent —
CREATE TABLE IF NOT EXISTS,ALTER TABLE … ADD COLUMN IF NOT EXISTS, etc. - The method receives the PDO driver name (
'mysql','sqlite', …) so you can emit driver-specific SQL. - Statements run after the core
usersandsessionstables are created.
External users and nullable password_hash
password_hash is NULL in the default schema. PdoCredentialProvider treats a NULL or empty hash
as a disabled local login and returns a failure — this prevents accidental password bypass.
PdoUserStorage::createUser() is intended for local password accounts and requires a non-empty password hash.
External credential providers (Cognito, Keycloak, etc.) should manage user creation with their own logic,
or insert directly with password_hash = NULL for users that only authenticate externally.
Hooks
All hook methods are optional — implement only what you need.
Force logout
v2.1.0 breaking changes
createSchema()no longer creates anactivecolumn on theuserstable.
v2 breaking changes
Auth::login()now returnsAuthKit\Login, not?string. Check$login->isSuccess(),$login->isFailure(), or$login->requiresChallenge().- Login-time
$additionalChecksparameter removed fromlogin(). UseLoginExtensionInterfaceinstead. UserStorageInterface::findByToken()requires a second argument:findByToken(string $token, \DateTimeImmutable $now).ChallengeStorageInterface::complete()requires a second argument:complete(ChallengeRecord $record, \DateTimeImmutable $now).ChallengeStorageInterface::purgeExpired()requires\DateTimeImmutable $now.PdoUserStorage::purgeExpiredTokens()requires\DateTimeImmutable $now.UserStorageInterface::storeToken()accepts?\DateTimeInterfaceinstead of?\DateTime.Authconstructor:$sessionKeyparameter removed — usetransport: new PhpSessionTransport('key')instead.Auth::setSessionKey()removed.- PHP
>=8.1required (was>=8.0). ramsey/uuiddependency removed — tokens are generated withbin2hex(random_bytes(32)).
License
MIT
All versions of authkit with dependencies
ext-pdo Version *