Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ import AppLayout from '@keyringjs/ui-react/layouts/app-layout';
import SignInMethods from '@keyringjs/ui-react/components/keyring/sign-in-methods';
```

It has the primitives; the app's components (`app-shell`, `app-sidebar`, `nav-main`, `nav-footer`, `nav-user`, `user-menu-content`, `breadcrumbs`, `text-link`, `appearance-tabs`, `app-logo`, the inputs, the status note and the rest, under `components/`); the layouts (`layouts/app-layout`, `layouts/auth-layout`, the auth and settings layouts); Keyring's settings components (`components/keyring/`: sign-in methods, change password, recovery codes, devices, sessions, activity) and `keyring/actions` (`run()`, which shows Keyring's message or error and handles a `423`); the hooks (`use-appearance`, `use-clipboard`, `use-current-url`, `use-initials`, `use-mobile-navigation`); `keyring/factors` (the challenge's factor components, written against what Keyring's Inertia and headless hooks have in common) and `keyring/format` (`ago()`, places, factor names and the "was this you?" alerts' wording).
It has the primitives; the app's components (`app-shell`, `app-sidebar`, `nav-main`, `nav-footer`, `nav-user`, `user-menu-content`, `breadcrumbs`, `text-link`, `appearance-tabs`, `app-logo`, the inputs, the status note and the rest, under `components/`); the layouts (`layouts/app-layout`, `layouts/auth-layout`, the auth and settings layouts); Keyring's settings components (`components/keyring/`: sign-in methods, change password, recovery codes, devices, sessions, activity), `keyring/actions` (`run()`, which shows Keyring's message or error and handles a `423`) and `keyring/changes` (the password a first second factor takes, and the refusals of frozen sign-in methods, with the proof that makes one such change now); the hooks (`use-appearance`, `use-clipboard`, `use-current-url`, `use-initials`, `use-mobile-navigation`); `keyring/factors` (the challenge's factor components, written against what Keyring's Inertia and headless hooks have in common) and `keyring/format` (`ago()`, places, factor names and the "was this you?" alerts' wording).

The ones that link, post or read the page's props do it through `@keyringjs/ui-react/adapter`: `Link`, `Form`, `usePage()`, `router` and the `InertiaLinkProps` type, the part of Inertia's React API they use, which each app provides once with `<AdapterProvider>`. The Inertia app passes Inertia's own (`resources/js/lib/adapter.tsx`, from `resources/js/app.tsx`), and the TanStack kit its router's and Keyring's headless forms (see [its README](kits/tanstack-start/README.md#shared-components)). They also link with Wayfinder-shaped routes from `@keyringjs/ui-react/routes` (`dashboard()`, `edit()`...) and use the types in `@keyringjs/ui-react/types`, and import Keyring's helpers from `@keyringjs/keyring/react`, which the TanStack kit points at its headless entry.

Expand Down Expand Up @@ -192,8 +192,9 @@ What you can try:

- Register. The form answers the same whether or not the address has an account, and signs nobody in: you get "check your email", follow the link in `storage/logs/laravel.log`, and sign in. An address that already has an account is emailed instead of being told on the form.
- Sign in with a password, a passkey (including from the email field's autofill), an authenticator app or a recovery code.
- Add a second factor. Everyone needs one here, so an account with only a password is sent to Settings → Security after signing in, and every other page sends it back there until it has a passkey, a security key or an authenticator app. The first one comes with recovery codes.
- Add a second factor. Everyone needs one here, so an account with only a password is sent to Settings → Security after signing in, and every other page sends it back there until it has a passkey, a security key or an authenticator app. Adding one asks for the password first, until the account has had a second factor for a week, so someone in a copy of the session can't add their own. The first one comes with recovery codes.
- Add an email code, and see what it is: an extra step after your password, not a second factor. The session stays at AAL1 and the app still asks for a real one.
- See your sign-in methods held for a week after you add one: until then the ones you had before can't be removed, renamed or replaced (the new one can always be removed), and the page says until when. A passkey or security key you've had longer can still make one such change now.
- Step-up: Settings → Security and deleting your account need a verification from the last 15 minutes, with the strongest proof the account has. Someone with a security key has to use it to add or remove a sign-in method; a password and an authenticator app's code aren't enough for them.
- Change your email from Settings → Profile. The new address has to confirm, and the old one gets a link to undo the change. The old address has to confirm too, unless you signed in with a second factor the account has had for a week (the two-factor demo account's is dated a month back).
- See your devices and your sessions, with where they are when there's a [locator](#locations), end one or sign out everywhere else (also while adding or removing a sign-in method, with the checkbox), and read the security log: which factors each sign in used, from which browser, and what risk found.
Expand Down Expand Up @@ -389,13 +390,13 @@ The app started as Laravel's React starter kit, with Fortify taken out and Keyri
- `laravel/fortify` and `laravel/chisel` removed. `keyring/keyring` added, with its migrations published.
- `App\Models\User` uses `HasKeyring`. Email and password can only change through Keyring's flows.
- `App\Providers\KeyringServiceProvider` turns on every factor but emailed codes (there to uncomment, since they aren't a second factor), a two-factor policy once someone has a second factor, the `keyring.enrolled` middleware that makes everyone add one, every security email and every risk signal (the breached password one behind a setting).
- `config/keyring.php` has the features, the page people are sent to for a second factor or a new password (both `/settings/security`), the list of words a password mustn't be made up of, the breached password check and the locator, both picked from `.env`.
- `config/keyring.php` has the features, the page people are sent to for a second factor or a new password and the one the emails about a new sign-in method link to (all `/settings/security`), the list of words a password mustn't be made up of, the breached password check and the locator, both picked from `.env`.
- `app/Notifications/Keyring` holds every email Keyring sends, with its views in `resources/views/emails/keyring`.
- Laravel's page names already match Keyring's (`auth/login`, `auth/challenge`…), so only step-up confirmation is remapped, to the challenge page.
- New pages: `auth/challenge` (second step and step-up), and the pages emailed links open, each with one button that posts the link's token or signature in the body: `auth/email-change`, `auth/device-report` and `auth/email-verification`. Login gained passkey sign-in, and shows Keyring's message for whatever sent you there.
- A key for a logo, and a sidebar link to Keyring's docs.
- A welcome page about the demo instead of Laravel's, a dashboard of things to try, sidebar links to this repository, and emailed codes turned on, to show what they are. <!-- @monorepo-only -->
- Settings → Security lists sign-in methods (the password form opens from the password's row), where you're signed in (devices and sessions, with one "Sign out everywhere else"), and recent activity. It talks to Keyring's JSON routes through `@keyringjs/keyring`, with `run()` from the shared `keyring/actions.ts` (`resources/js/keyring/actions.ts` in the Inertia kit) handling the 423 step-up answer and showing Keyring's own messages.
- Settings → Security lists sign-in methods (the password form opens from the password's row; adding a first second factor asks for the password; while Keyring holds changes after a new method, the page says until when, and a refused change offers a key the account has had longer), where you're signed in (devices and sessions, with one "Sign out everywhere else"), and recent activity. It talks to Keyring's JSON routes through `@keyringjs/keyring`, with `run()` from the shared `keyring/actions.ts` (`resources/js/keyring/actions.ts` in the Inertia kit) handling the 423 step-up answer and showing Keyring's own messages.
- `HandleInertiaRequests` shares the flashed `status` and its message from `Keyring::statusMessage()`.
- Settings → Profile changes the name directly and the email through Keyring's confirmed flow.
- Laravel's Fortify tests were rewritten against Keyring (`php artisan test`), and `npm test` runs the frontend's own tests (the Inertia kit's render the welcome page and the dashboard).
Expand Down
28 changes: 24 additions & 4 deletions app/Http/Controllers/Settings/SecurityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
use App\Http\Responses\PageResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Keyring\Changes\AuthenticatorChanges;
use Keyring\Contracts\Enrollable;
use Keyring\Devices\DeviceManager;
use Keyring\Enums\Category;
use Keyring\Facades\Keyring;
use Keyring\Models\Authenticator;
use Keyring\Models\SecurityEvent;
Expand All @@ -27,22 +29,40 @@ class SecurityController extends Controller
*
* A headless frontend loads the same props as JSON (PageResponse).
*/
public function edit(Request $request, DeviceManager $devices, Sessions $sessions, SessionAssurance $assurance): PageResponse
public function edit(Request $request, DeviceManager $devices, Sessions $sessions, SessionAssurance $assurance, AuthenticatorChanges $changes): PageResponse
{
$user = $request->user();
$enrollable = Keyring::registered()->filter(fn ($factor) => $factor instanceof Enrollable);
// While a sign-in method is new, or one was just removed, Keyring
// refuses to remove, rename or replace the ones the account has had
// a while ("Frozen Sign-In Methods" in its docs).
$freeze = $changes->frozen($user);

return new PageResponse('settings/security', [
'authenticators' => $user->keyring->all()
->reject(fn (Authenticator $authenticator) => $authenticator->isVirtual())
->map(fn (Authenticator $authenticator) => [
...$authenticator->factor()->present($authenticator),
'isDefault' => $authenticator->is_default,
// Held by the freeze: one the account has had a while,
// and counts on (a factor, or recovery codes with some
// left), as Keyring decides it. A newer one can always
// be removed, and an emailed code isn't a factor.
'frozen' => $freeze !== null
&& $user->keyring->isEstablished($authenticator)
&& array_any($authenticator->factor()->categories($authenticator), fn (Category $category) => $category->authenticates()),
])
->values(),
'enrollable' => Keyring::registered()
->filter(fn ($factor) => $factor instanceof Enrollable)
->map(fn ($factor) => $factor->key())
'enrollable' => $enrollable->map(fn ($factor) => $factor->key())->values(),
// The factors that take the current password to add: second
// factors, while the account has a password and no second
// factor it has had a while. Asked for before beginning.
'passwordToAdd' => $enrollable
->filter(fn (Enrollable $factor) => $changes->needsPassword($user, $factor))
->map(fn (Enrollable $factor) => $factor->key())
->values(),
// Why, and until when (ISO 8601), or null when nothing's held.
'frozen' => $freeze === null ? null : [...$freeze->toArray(), 'message' => $freeze->message()],
'devices' => $devices->list($request, $user),
'sessions' => $sessions->list($request, $user),
'activity' => Keyring::activity($user, 20)->map(fn (SecurityEvent $event) => [
Expand Down
4 changes: 2 additions & 2 deletions app/Notifications/Keyring/NoticeMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ protected static function look(SecurityNotice $notice): array
'tone' => 'success',
'icon' => 'key-round',
'nameLabel' => __('Sign-in method'),
'advice' => __('If you didn\'t add it, remove it from your sign-in methods and change your password straight away.'),
'advice' => __('If you didn\'t add it, someone else may be signed in to your account. Remove it from your sign-in methods, sign out of your account everywhere, and change your password straight away.'),
],
$notice instanceof RecoveryCodesGenerated => [
'tone' => 'success',
'icon' => 'life-buoy',
'advice' => __('If you didn\'t make them, someone may be signed in to your account. Change your password and check your sign-in methods straight away.'),
'advice' => __('If you didn\'t make them, someone else may be signed in to your account. Sign out of your account everywhere, change your password, and remove any sign-in method you don\'t recognise straight away.'),
],
$notice instanceof EmailChanged => [
'tone' => 'success',
Expand Down
15 changes: 15 additions & 0 deletions config/keyring.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@
// the frontend's layout needs on every page.
'enrollment_except' => ['home', 'well-known.passkeys', ...($headless ? ['security.edit', 'api.me'] : [])],

/*
|--------------------------------------------------------------------------
| Sign-In Methods Page
|--------------------------------------------------------------------------
|
| Your page where users see and remove their sign-in methods. The emails
| about a new sign-in method and new recovery codes link to it, so the
| owner can remove one they didn't add. Here that's the security page.
| The path is put after APP_URL, or FRONTEND_URL in headless mode
| (KeyringServiceProvider's linkRootUsing()).
|
*/

'authenticators_url' => '/settings/security',

/*
|--------------------------------------------------------------------------
| Support Contact
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
* When each account last lost or replaced a sign-in method it had for
* "authenticator_changes.established_after", or its recovery codes: until
* that long after, it can't do so again. And when its first password was
* set from a session, which isn't established until that long after. One
* row per user.
*/
return new class extends Migration
{
public function up(): void
{
Schema::create('keyring_sign_in_changes', function (Blueprint $table) {
$table->ulid('id')->primary();
$table->morphs('authenticatable', 'keyring_sign_in_changes_owner_index');
$table->timestamp('changed_at')->nullable()->index();
$table->timestamp('password_set_at')->nullable()->index();
$table->timestamps();

$table->unique(['authenticatable_type', 'authenticatable_id'], 'keyring_sign_in_changes_owner_unique');
});
}

public function down(): void
{
Schema::dropIfExists('keyring_sign_in_changes');
}
};
10 changes: 7 additions & 3 deletions docs/headless-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ And the dashboard has `demoSettings`:
"name": "1Password",
"createdAt": "2026-...",
"lastUsedAt": null,
"isDefault": false
"isDefault": false,
"frozen": false
},
{
"factor": "recovery",
Expand All @@ -264,10 +265,13 @@ And the dashboard has `demoSettings`:
"lastUsedAt": null,
"remaining": 10,
"replace": false,
"isDefault": false
"isDefault": false,
"frozen": false
}
],
"enrollable": ["passkey", "security-key", "totp", "recovery"],
"passwordToAdd": [],
"frozen": null,
"devices": [
{
"id": "...",
Expand Down Expand Up @@ -313,7 +317,7 @@ And the dashboard has `demoSettings`:
}
```

Each authenticator is what its factor's `present()` gives, plus `isDefault`: passkeys and security keys add their own keys, and recovery codes add `remaining` and `replace`. `sessions[].id` is the handle for `DELETE /user/sessions/{id}`, never the session's own ID. `activity[].id` is a ULID string. `activity[].meta` only has `factors`, `name`, `count`, `remaining`, `reason`, `reasons` and `resource`, when the entry has them, and an entry with none of them has `"meta": []`, an empty array rather than `{}`. `enrollable` lists the factors the user can add. `assurance` is `AAL1`, `AAL2` or `AAL3`. Keyring has `GET /user/devices` and `GET /user/sessions` too, for refreshing those lists after a change without reloading the page, but no route lists sign-in methods or activity: reload the page for those.
Each authenticator is what its factor's `present()` gives, plus `isDefault` and `frozen`: passkeys and security keys add their own keys, and recovery codes add `remaining` and `replace`. `sessions[].id` is the handle for `DELETE /user/sessions/{id}`, never the session's own ID. `activity[].id` is a ULID string. `activity[].meta` only has `factors`, `name`, `count`, `remaining`, `reason`, `reasons` and `resource`, when the entry has them, and an entry with none of them has `"meta": []`, an empty array rather than `{}`. `enrollable` lists the factors the user can add, and `passwordToAdd` the ones of those that take the current password, sent as `password` when beginning: second factors, while the account has a password and no second factor it has had for 7 days (Keyring's [password for a first second factor](https://github.com/keyringphp/keyring/blob/main/docs/authenticators.md#the-password-for-a-first-second-factor)). Ask for it before beginning, and again if beginning answers a `422` on `password`. `frozen` is `null` unless the account's sign-in methods are [frozen](https://github.com/keyringphp/keyring/blob/main/docs/authenticators.md#frozen-sign-in-methods), after a new one was added or one it had a while was removed or replaced. Then it holds the `reason`, `new-authenticator` or `recent-change`, `until`, an ISO 8601 time, and Keyring's `message`, and each authenticator's `frozen` says whether it's one the freeze holds: one the account has had a while, which can't be removed, renamed or, for recovery codes, replaced until then. Keyring refuses those with a `422` on `authenticator` and a `frozen` of its own, whose `skip` lists the factors that can make that change now, sent as `proof` with it. `assurance` is `AAL1`, `AAL2` or `AAL3`. Keyring has `GET /user/devices` and `GET /user/sessions` too, for refreshing those lists after a change without reloading the page, but no route lists sign-in methods or activity: reload the page for those.

<!-- @monorepo-only:start -->

Expand Down
Loading
Loading