# Installation

Source: https://keyringauth.com/docs/0.x/installation

## Introduction

This page covers installing Keyring in detail. The [Quick Start](https://keyringauth.com/docs/0.x/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](https://keyringauth.com/docs/0.x/sign-in-flow#pages).
- The `sodium` PHP 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:

```bash
composer require keyring/keyring

php artisan keyring:install
php artisan migrate
```

The installer asks three questions:

1. **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](https://keyringauth.com/docs/0.x/factors#what-turning-email-codes-on-means).
2. **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](https://keyringauth.com/docs/0.x/policies#requiring-a-second-factor). It isn't asked with only the password or email codes, since nobody could meet the requirement.
3. **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](https://keyringauth.com/docs/0.x/sign-in-flow#without-revealing-who-has-an-account).

Without prompts (`--no-interaction` or in CI) you get the same defaults, which means the password alone. Answer with options instead:

```bash
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](https://keyringauth.com/docs/0.x/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](https://keyringauth.com/docs/0.x/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](https://keyringauth.com/docs/0.x/devices-and-sessions#listing-and-ending-sessions)), `announced_at`, `end_reason`, `announce_attempts` and `announce_after` to `keyring_sessions` (see [sweeping](https://keyringauth.com/docs/0.x/devices-and-sessions#sweeping-ended-sessions)), `impersonator_id`, `impersonator_type` and `impersonator_session` to `keyring_sessions` (see [impersonation](https://keyringauth.com/docs/0.x/impersonation)), and `password` and `lifted_at` to `keyring_password_holds` (see [holds](https://keyringauth.com/docs/0.x/protected-changes#requiring-a-password-change)). |
| `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](https://keyringauth.com/docs/0.x/security-log-and-emails#security-emails), 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:

```bash
php artisan vendor:publish --tag=keyring-config
```

The 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`:

```php
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()` (for `password`) or `Features::emailChanges()` (for `email`), it stops those attributes from changing outside Keyring's flows. See [Protected Changes](https://keyringauth.com/docs/0.x/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_taken`](https://keyringauth.com/docs/0.x/configuration#registration) turns that rule off.
- Registering throws before anything is saved if the model lacks `MustVerifyEmail` while addresses are hidden, or has it while the feature is off.
- With registration on, adding a sign-in method throws `EmailVerificationRequired` for a user who can't prove their address, such as one whose model lacks `MustVerifyEmail`. Users with the interface must verify before they add one. See [Managing Authenticators](https://keyringauth.com/docs/0.x/authenticators#email-verification).
- 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](https://keyringauth.com/docs/0.x/configuration#guard-and-users)). 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](https://keyringauth.com/docs/0.x/sign-in-flow#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:

```bash
npm install @keyringjs/keyring
```

See [Frontend](https://keyringauth.com/docs/0.x/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](https://keyringauth.com/docs/0.x/sign-in-flow#routes), [Step-Up](https://keyringauth.com/docs/0.x/step-up), [Managing Authenticators](https://keyringauth.com/docs/0.x/authenticators#routes), [Devices & Sessions](https://keyringauth.com/docs/0.x/devices-and-sessions#routes) and [Protected Changes](https://keyringauth.com/docs/0.x/protected-changes#routes). To change the prefix, domain or middleware, or turn the routes off, see [Configuration](https://keyringauth.com/docs/0.x/configuration#routes).

## After Installing

- Set `APP_URL` to 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](https://keyringauth.com/docs/0.x/passkeys#relying-party-and-origins).
- Build your [pages](https://keyringauth.com/docs/0.x/quick-start#your-pages). If you require a second factor, build the page at `enrollment_url` too.
- Run a queue worker if your queue isn't `sync`. [Security emails](https://keyringauth.com/docs/0.x/security-log-and-emails#security-emails) are queued. Sign-in codes are sent straight away.
- Run Laravel's scheduler (`* * * * * php artisan schedule:run`). Keyring schedules `keyring:prune` daily, 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](https://keyringauth.com/docs/0.x/devices-and-sessions#pruning).
- Sessions end after two hours without activity, whatever `session.lifetime` says, unless the browser ticked "remember me". To change that, set `sessions.idle`. See [Timeouts](https://keyringauth.com/docs/0.x/devices-and-sessions#timeouts).
- Signed-in routes outside the `web` group need the `keyring.session` middleware. Otherwise, a revoked or timed-out session keeps working on them. See [The Session Middleware](https://keyringauth.com/docs/0.x/devices-and-sessions#the-session-middleware).
- To hear about every session that ends, set `sessions.sweep_every`. Keyring then schedules `keyring:sweep`. See [Sweeping Ended Sessions](https://keyringauth.com/docs/0.x/devices-and-sessions#sweeping-ended-sessions).
- `keyring:sign-out` signs one user out everywhere, and `--all` signs everyone out. Use it for a stolen laptop or a compromised account. See [For Administrators](https://keyringauth.com/docs/0.x/devices-and-sessions#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`.
