Installation
On this page
Introduction
This page covers installing Keyring in detail. The Quick Start has the short version.
Requirements
- PHP 8.5 or later
- Laravel 13.43 or later
- Inertia v3, optional. Without it, Keyring renders Blade views. See Pages.
- The
sodiumPHP extension, for passkeys and security keys that sign with Ed25519 (EdDSA). Without it, Keyring only offers ES256 keys by default.
Installing Keyring
Install the package, then run the installer and your migrations:
composer require keyring/keyring php artisan keyring:installphp artisan migrateThe installer asks three questions:
- Which sign-in methods besides the password? None by default. Recovery codes come with any of them. Email codes are an extra step at sign-in, not a second factor, so a password and an emailed code is still AAL1. See Email Codes.
- Require a second factor for everyone? Asked when you pick an authenticator app, passkeys or security keys, and yes by default. Until a user sets one up, every page sends them to
/settings/security. See Requiring a Second Factor. It isn't asked with only the password or email codes, since nobody could meet the requirement. - Which features? By default: registration, password resets, email verification, changing the password, and the security log. Picking a sign-in method also adds managing sign-in methods. Registration always brings email verification, because new users sign in once they've verified their address.
Without prompts (--no-interaction or in CI) you get the same defaults, which means the password alone. Answer with options instead:
php artisan keyring:install --factor=totp --factor=passkeys --feature=registration --feature=manage-authenticators --require-second-factor| Option | Values |
|---|---|
--factor |
totp, passkeys, security-keys or email-codes. none keeps the password alone. |
--feature |
registration, reset-passwords, email-verification, update-passwords, manage-authenticators, devices, email-changes, security-log or impersonation. |
--require-second-factor, --optional-second-factor |
Answer the second question. |
--feature replaces the defaults, so list registration, reset-passwords, email-verification, update-passwords and security-log too if you want them. An unknown value stops the command before it writes anything. So does requiring a second factor without a sign-in method that counts as one.
The command doesn't overwrite existing files unless you pass --force. If config/keyring.php exists and you don't pass --force, the command keeps its features and doesn't ask.
It's safe to run again. A migration you already have is never added twice, and ones a later version adds are published. Running it on an older install adds new actions, but leaves your provider alone, so register them there yourself. Until you do, the action's routes throw an exception naming the Keyring::*Using() call that's missing.
If you're moving from Fortify, read Upgrading From Fortify before you migrate.
What Gets Published
keyring:install publishes these files:
| File | What It Is |
|---|---|
config/keyring.php |
A short config with the guard, username, home, features, registration answers, your enrollment and sign-in methods pages, and support contact. Every other option keeps its default until you copy it in from Configuration. |
database/migrations/*_create_keyring_*_table.php |
Eight tables: keyring_authenticators, keyring_devices, keyring_sessions, keyring_pending_changes, keyring_events, keyring_failure_counts, keyring_password_holds and keyring_sign_in_changes. All are created now, so a feature needs no new migration. Tables for unused features stay empty. |
database/migrations/*_add_*_to_keyring_*_table.php |
Four migrations that add columns: origin to keyring_sessions (see handles), announced_at, end_reason, announce_attempts and announce_after to keyring_sessions (see sweeping), impersonator_id, impersonator_type and impersonator_session to keyring_sessions (see impersonation), and password and lifted_at to keyring_password_holds (see holds). |
app/Providers/KeyringServiceProvider.php |
Where you register sign-in methods, the policy, risk signals and your actions. It's added to bootstrap/providers.php. |
app/Actions/Keyring/*.php |
Your code for the features that need it: CreateNewUser, ResetUserPassword, UpdateUserPassword, ValidateEmailChange and PasswordValidationRules. Keyring does the security work around them. |
Keyring's emails aren't published. They're Keyring's own, and you replace one by giving Keyring::notifications() your own class.
To publish files yourself, use the keyring-config, keyring-migrations or keyring-lang tag. keyring-config publishes the full config file, with every option and its default:
php artisan vendor:publish --tag=keyring-configThe migrations use morphs(). If your users have UUID or ULID keys, set Laravel's morph key type first, for example with Schema::morphUsingUlids(), or edit the published migrations.
Preparing Your User Model
Add the HasKeyring trait, and implement MustVerifyEmail:
use Illuminate\Contracts\Auth\MustVerifyEmail;use Illuminate\Foundation\Auth\User as Authenticatable;use Keyring\Concerns\HasKeyring; class User extends Authenticatable implements MustVerifyEmail{ use HasKeyring; protected function casts(): array { return [ 'email_verified_at' => 'datetime', 'password' => 'hashed', ]; }}The trait does three things:
- It adds
$user->keyring,$user->authenticators()and$user->devices(). - Once you turn on
Features::updatePasswords()(forpassword) orFeatures::emailChanges()(foremail), it stops those attributes from changing outside Keyring's flows. See Protected Changes. - When you delete a user, it revokes their sessions, ends their reset links, and deletes their authenticators, devices and pending changes. With soft deletes, sessions, reset links, "remember me" cookies, device trust and pending email changes end at once, and the rest waits for a force delete. A restored user keeps their sign-in methods.
The published actions hash passwords themselves. With the hashed cast, a password that's already hashed isn't hashed again.
MustVerifyEmail and Features::emailVerification() go together:
- With registration on and hiding taken addresses, the app won't boot unless email verification is on.
registration.reveal_takenturns that rule off. - Registering throws before anything is saved if the model lacks
MustVerifyEmailwhile addresses are hidden, or has it while the feature is off. - With registration on, adding a sign-in method throws
EmailVerificationRequiredfor a user who can't prove their address, such as one whose model lacksMustVerifyEmail. Users with the interface must verify before they add one. See Managing Authenticators. - With hidden addresses, an unverified user can only sign in from the browser that followed their verification link.
Keyring looks users up by email by default. Change this with username (see Configuration). Lookups trim and lowercase the input, so store addresses the same way. Keyring::normalizeUsername() does that, and the published CreateNewUser action uses it. For other rules, see Finding the User.
Frontend
If you offer passkeys or security keys, install the browser package, @keyringjs/keyring. It also has helpers for the challenge page:
npm install @keyringjs/keyringSee Frontend.
Routes Keyring Registers
Keyring registers its routes under the web middleware group. Signed-in routes check Keyring's guard (auth:{guard}). Some routes use Laravel's names, such as login, logout, register, password.request, password.reset and verification.notice. If you have routes of your own with those names, remove them.
The full list is in The Sign In Flow, Step-Up, Managing Authenticators, Devices & Sessions and Protected Changes. To change the prefix, domain or middleware, or turn the routes off, see Configuration.
After Installing
- Set
APP_URLto the address users visit. Keyring builds emailed links from it, and passkeys and security keys only work on that origin unless you configure others. See Passkeys & Security Keys. - Build your pages. If you require a second factor, build the page at
enrollment_urltoo. - Run a queue worker if your queue isn't
sync. Security emails are queued. Sign-in codes are sent straight away. - Run Laravel's scheduler (
* * * * * php artisan schedule:run). Keyring scheduleskeyring:prunedaily, which deletes old devices, finished email changes, old security log entries, ended sessions, old sign-in changes, and password holds lifted a year ago. See Pruning. - Sessions end after two hours without activity, whatever
session.lifetimesays, unless the browser ticked "remember me". To change that, setsessions.idle. See Timeouts. - Signed-in routes outside the
webgroup need thekeyring.sessionmiddleware. Otherwise, a revoked or timed-out session keeps working on them. See The Session Middleware. - To hear about every session that ends, set
sessions.sweep_every. Keyring then scheduleskeyring:sweep. See Sweeping Ended Sessions. keyring:sign-outsigns one user out everywhere, and--allsigns everyone out. Use it for a stolen laptop or a compromised account. See For Administrators.
The installer ends by listing what to check before going live: the idle timeout, a bcrypt note when bcrypt is the default hasher, the scheduler, the keyring.session middleware and keyring:sign-out.