Signing InPolicies

Policies

On this page

Introduction

The policy decides what a user must prove to sign in and to confirm sensitive actions. Set it once, in your KeyringServiceProvider:

PHP
use Keyring\Facades\Keyring;use Keyring\Login\Requirement; Keyring::require(Requirement::aal2()->ifEnrolled()->noDowngrade());

keyring:install writes this line whichever sign-in methods you pick. Without Keyring::require, the policy is Requirement::aal2()->ifEnrolled(). Users sign in with their password until they add a second factor, and then they need it too.

Requirements

A Keyring\Login\Requirement counts distinct categories, not verifications. The authentication categories are knowledge, possession and inherence, so a password plus a PIN is still one factor. An email code is Category::Address, which never counts as a factor.

PHP
use Keyring\Enums\Category;use Keyring\Login\Requirement; Requirement::aal2();                                             // two categories, one of them possessionRequirement::factors(3);                                         // three categories of any kindRequirement::aal2()->including(Category::Knowledge)->phishingResistant();

Requirements are immutable. Each modifier returns a new one, and PHP warns if you discard the result. The requirement reference lists them all.

Assurance Levels

Keyring works out a level from what the user proved in their session, following NIST SP 800-63B-4 as far as a library can check. The levels are the cases of Keyring\Enums\Assurance: None, AAL1, AAL2 and AAL3.

Proved Level
A password, or a password and an email code (EmailCode::asExtraStep()) AAL1
A password and a TOTP or recovery code AAL2
A passkey that syncs (iCloud Keychain, Google Password Manager...) AAL2
A passkey or security key that stays on its device, added without attestation, alone or with a password AAL2
A synced passkey that verified the user, and an attested security key that was only touched AAL2
A password and an attested security key, without user verification AAL3
An attested security key or passkey that stays on its device and verified the user (PIN or biometric) AAL3

A user-verified passkey proves possession and inherence in one step, so it reaches AAL2 alone. Email codes never reach AAL2, because NIST doesn't accept email as an authenticator.

AAL2 needs two categories, one of them possession. NIST accepts a password or a biometric only with a physical authenticator (Sec. 2.2.1), and never a biometric alone (Sec. 3.2.3). A factor of your own that reports Inherence alone, or Knowledge and Inherence, stays at AAL1. See categories.

AAL3 needs more than a hardware key. NIST asks for a key that can't be exported, with a second factor that's the key's own user verification or a password (Sec. 2.3.1 and 2.3.2). So Keyring requires all of the following:

  • The key is phishing resistant and reports that it can't sync. A key that hasn't reported this counts as synced.
  • The key belongs to a factor set up with requireAttestation(), and was added with an attestation that chained to a trusted root. A key's flags alone are only what it says about itself.
  • The second factor is the key's own user verification or a password. Another authenticator's proof doesn't count.

Without attestation, no session reaches AAL3, and keyring:aal3 sends users to your enrollment page. A phishing-resistant key that stays on its device still counts at AAL2, through Requirement::aal2()->deviceBound() or keyring:aal2,device-bound.

Warning

A session Keyring calls AAL3 has met NIST's requirements for the authentication. NIST's AAL3 also needs validated cryptography (FIPS 140), which no library supplies. Before you say your application is AAL3, buy validated keys, limit the factor to those models with allowOnly(), and run PHP on a validated cryptographic module.

The step-up middleware asks for these levels by name, as in keyring:aal2.

Only When Enrolled

ifEnrolled() applies a requirement as far as the user's authenticators can meet it. A user who can't meet all of it proves the strongest version they can, and never gets away with one factor while they have a second.

Only established authenticators decide what an ifEnrolled() policy asks. An authenticator is established once the account has had it for authenticator_changes.established_after, 7 days by default. With Requirement::factors(3)->ifEnrolled(), a user with a password and an established app signs in with those two, even after adding a passkey. The new passkey is asked for only once it's established. The exception is a user with a password and no established second factor. They're asked for a new one straight away, because adding it took their password. Accounts without a password go by their established authenticators alone.

For phishingResistant(), deviceBound() and aal3(), only an established key counts. A newer key doesn't make the user need a key, so one added from a stolen session can't lock out the owner. Recovery codes don't meet a key requirement.

A user whose only other method is an email code is asked for the password and the emailed code, at AAL1, until they add a real second factor, after which the emailed code is no longer asked for.

Without ifEnrolled(), users who can't meet the requirement can't sign in, and they see keyring::keyring.unavailable. Leave ifEnrolled() out only when every account has the factors it needs before it signs in. For a password and an emailed code to be enough, use Requirement::steps(2).

Requiring a Second Factor

ifEnrolled() lets users without a second factor carry on with a password. To require one from everyone, which OWASP ASVS 5.0.0 asks for at Level 2 (requirement 6.3.3), add the keyring.enrolled middleware. Usually that's the whole app:

PHP
// bootstrap/app.php->withMiddleware(function (Middleware $middleware) {    $middleware->web(append: ['keyring.enrolled']);})

It lets guests through, so it's safe in the web group. It also works on a single route or group, placed after auth.

When you pick an authenticator app, passkeys or security keys, keyring:install asks "Require a second factor for everyone?", and yes is the default. It adds the same middleware to the web group in your KeyringServiceProvider. It doesn't ask when the password is the only method, or the only other one is an emailed code, since nobody could meet the requirement.

Users go to enrollment_url, which the installer sets in config/keyring.php:

PHP
'enrollment_url' => '/settings/security',

Every request under the middleware sends a user without enough factors there, with the enroll-first status. JSON requests get a 403 with that status and the URL, or a plain 403 without an enrollment_url. Recovery codes and email codes don't count. For an attested key that stays on its device, use keyring.enrolled:aal3. For a device-bound key, attested or not, use keyring.enrolled:aal2,device-bound.

The enrollment page and the routes that get users there and back are always let through: Keyring's adding and confirming routes, logout, email verification and changing the password. So is your password_change_url while the user has to choose a new password, since the hold sends every other page, the enrollment page included, there. Keep anything else the enrollment page calls outside the middleware, or name it in enrollment_except.

Carrying On Afterwards

When Keyring sends a user to enroll, it remembers the page they were going to, and the requirement that page held them for. Once they add a sign-in method that meets the requirement, the authenticator-added status response carries that page as continue. An email code doesn't meet a second-factor requirement, so it doesn't release the page, and neither do recovery codes.

Keyring doesn't redirect there itself, because the security page may be showing new recovery codes. Show a Continue link, and wait for the codes to be saved when there are some:

Vue
<a v-if="keyringResult?.continue && (!keyringResult.recoveryCodes || codesSaved)" :href="keyringResult.continue">Continue</a>

keyringResult is Keyring::result(), shared as in Flash Messages and Alerts.

Only a page the browser navigated to is remembered: a GET with Sec-Fetch-Mode: navigate. Prefetches, fetch() calls, HEAD requests, form posts and Keyring's own pages never replace it. The page is only used on your own origin, for the user it was saved for, within an hour.

Note

Browsers only send Sec-Fetch-* headers over HTTPS or to localhost. On a plain-HTTP development host, a Blade app gets no continue. Inertia visits still do.

No Downgrade

noDowngrade() stops users with a phishing-resistant authenticator from being talked into a weaker one. Once a user has an established passkey or security key, the requirement gains phishingResistant(), and "Try another way" offers only their keys plus recovery codes.

A newer key signs in like any other but doesn't raise the bar until it's established, after authenticator_changes.established_after (7 days). Otherwise a session holder could add a key and lock out the owner. While an unestablished key is on the account, its other sign-in methods are frozen, and the owner can remove the new key.

Recovery codes stay on offer, since they're the way back in when the only key is lost. To accept only keys, pass an empty except list:

PHP
Keyring::require(Requirement::aal2()->ifEnrolled()->noDowngrade(except: []));

The except list names the factor keys allowed to stand in for a key. An explicit phishingResistant() never makes exceptions.

Deciding Per User and Attempt

A closure can decide the requirement for each user and attempt. It receives the user and the Keyring\Login\Attempt, and returns a Requirement:

PHP
use App\Models\User;use Keyring\Login\Attempt;use Keyring\Login\Requirement; Keyring::require(fn (User $user, Attempt $attempt) => match (true) {    $user->is_admin => Requirement::aal2()->phishingResistant(),    $attempt->device()?->isTrusted() === true => Requirement::aal1(),    $attempt->risk?->isElevated() === true => Requirement::aal2(),    // An unknown device and risk land here, so keep this the strictest    // answer you want for an attempt Keyring knows nothing about.    default => Requirement::aal2()->ifEnrolled(),});

ifEnrolled() and noDowngrade() work the same when a closure returns them. The attempt gives you user(), device() (the known Keyring\Models\Device, or null for a new browser; isTrusted() is true when the user ticked "don't ask again on this device", see Trusted Devices), isNewDevice(), risk (the Keyring\Risk\Risk, or null), verifications, remember and keyring().

Your closure must cope with a null device and risk. Keyring also calls it when a user removes an authenticator, to check what's left. On keyring.fresh routes it's asked twice, once with the request's device and once with none, and the stricter answer stands, so a trusted device gets no discount. Risk is always null there.

Checking Requirements in Code

PHP
use Keyring\Enums\Category;use Keyring\Login\Requirement; $user->keyring->canMeet(Requirement::aal2());   // could their authenticators meet it?$user->keyring->has(Category::Possession);       // do they have a possession factor?

Keyring::policy($user, $attempt) returns what the policy says. Keyring::requirementFor() returns it with ifEnrolled() and noDowngrade() applied.

Requirement Reference

Method Means
Requirement::aal1() Any one category.
Requirement::aal2() Two categories, one of them possession.
Requirement::aal3() aal2() with an attested, phishing-resistant key that can't leave its device, by itself or with a password.
Requirement::factors(3) That many categories of any kind, and at least one. factors(2) asks for less than aal2(), since it needs no possession.
Requirement::steps(2) That many categories, at least one a factor. A password and an email code will do.
Requirement::for(Assurance::AAL2) The requirement for an assurance level.
->including(Category::Possession) That category must be among those verified.
->phishingResistant() At least one verification must be a passkey or security key.
->deviceBound() At least one verification must be a phishing-resistant key that can't leave its device.
->attestedKey() That key must have a verified attestation.
->ifEnrolled() Apply the requirement as far as the user's authenticators can meet it.
->noDowngrade() Users with a passkey or security key must use one.

How It Works

The policy's effects beyond a single sign-in:

  • The policy is asked again after every factor verified in a sign-in, so a risk assessment can raise it partway through.
  • When a risk response asks for a step-up and the user has enough authenticators, the requirement becomes at least aal2().
  • Fresh verification asks for as much as a sign-in would, and never less than the account's strongest combination. A step-up to a level sets its own requirement.
  • Registration doesn't consult the policy. With registration.reveal_taken, a new user is signed in with their password alone.
  • Users can't remove an authenticator if what's left can't sign in or meet the policy, unless the policy uses ifEnrolled(). See Removing Authenticators. Nobody can remove an established one while sign-in methods are frozen.