Skip to content

feat(email-code): add an opt-in sign-in link and make the code easier for mail apps to find - #160

Merged
entity merged 10 commits into
mainfrom
feat/email-sign-in-link
Dec 3, 2026
Merged

entity merged 10 commits into
mainfrom
feat/email-sign-in-link

Conversation

@RobertBoes

@RobertBoes RobertBoes commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Two changes to the email code factor:

  1. The code where mail apps look for it. The verification_code email's first line is now Your verification code is 48291374: one run of digits beside the word "code", in the HTML and plain-text parts, in place of the code on a line of its own in groups of four. Mail apps that offer to copy a code or fill it in guess from this. None of them promise to, so the docs call it a heuristic and pair it with autocomplete="one-time-code" and inputmode="numeric" on the code field. The subject stays Your verification code: mail providers and apps log subjects, and those logs outlive the code.

  2. An opt-in "Sign in" button beside the code, for users who would rather click than type:

    Keyring::factors([Password::class, Totp::class, EmailCode::asExtraStep()->withLink()]);

    It's off by default, and only sign-in emails carry it. Step-up and enrollment emails carry the code alone.

How the link stays safe

Emailed sign-in links have three well-known problems: mail scanners open them, people open them on another device, and a button that signs in wherever it's pressed lets someone with the password get the owner to approve the attacker's sign-in. The design handles each one:

  • Opening the link changes nothing. GET /login/challenge/email-code/link only renders the sign-in-link page. A scanner that follows it signs nobody in and uses nothing up.
  • Only the browser that started the sign-in can finish it. The page offers its button only when this browser's session holds the pending attempt the link was sent for. The button posts the code to POST /login/challenge/email-code/link, which is ChallengeController::verify for the email code factor. So it has the code's own pipeline, throttling, guess limit, single use, events and log entries, and the CSRF check and session lock of any challenge.
  • Anywhere else, the page neither finishes the sign-in nor shows the code. It asks for the code from the email to be typed where the sign-in started. Showing the code there would hand it to scanners that keep or publish the pages they open, and whoever opened the link already has the email the code is in. Nobody can approve a sign-in from their inbox.
  • Starting over ends the link. A browser that has since started a newer sign-in sees the link as ended (expiresAt is null), so the page asks for a new code instead of offering a button that would fail and use up a guess.
  • The link is no stronger than the code. It carries the code, encrypted with APP_KEY together with the attempt's ID and expiry, plus a marker so no other value Keyring encrypts can pass for one. Nothing about the link is stored: the code's own record decides whether it still works. It works once, for as long as the code and the attempt do, and using the link or typing the code uses up both. It stays Category::Address, never a factor.
  • Both routes answer 404 until withLink() is on. The page is sent with the headers of Keyring's other emailed-link pages (Referrer-Policy: no-referrer, no caching, a full Inertia page load after posting). A token Keyring didn't make is logged as link_refused with link sign_in_link.

API

  • EmailCode::withLink(bool $link = true): static and EmailCode::sendsLinks(): bool.
  • A new sign-in-link page with the props code, sameBrowser, url and expiresAt. code and url are only set when sameBrowser is true, and expiresAt is null once the link has ended.
  • New routes keyring.login.link and keyring.login.link.store, and the RequireSignInLinks middleware that guards both.
  • VerificationCode takes an optional url. It's only passed when there is a link, so an app's own notification isn't handed null.
  • CodeFactor::deliverFor($attempt, $authenticator, $code) is a new hook for sending something that needs the attempt. By default it calls deliver(). EmailCode only skips deliver() for a sign-in email that carries a link.

Upgrading

  • Translations: apps that translated Your verification code is: should translate Your verification code is :code instead. The subject string is unchanged.
  • Subclasses: an EmailCode subclass that overrides deliver() and turns on withLink() should override deliverFor() too.
  • Pages: apps that turn the link on need a sign-in-link page. The docs have a Vue example, and a Nuxt one for headless frontends.
  • Copied routes: apps that copied routes/keyring.php into their own routes need both new routes, with their names and the RequireSignInLinks middleware, before turning the link on.

All of these are in UPGRADE.md and CHANGELOG.md.

Docs

  • factors.md: Signing In From the Email (including which host the link is built on, APP_URL or linkRootUsing(), and what that means for www or tenant subdomains), and code detection.
  • quick-start.md: a row for the sign-in link. Writing a Code Factor: the deliverFor() hook.
  • frontend.md: the page, one-time-code and inputmode.
  • headless.md, sign-in-flow.md, security.md and security-log-and-emails.md.
  • owasp-asvs.md: rows 6.3.6, 6.5.1, 6.5.5, 6.6.2, 6.6.3, 6.6.4 and 14.2.1, and the NIST AAL2 row.

Tests

  • The link: the same email carries the code and the button.
  • Opening it:
    • repeated scanner-style opens change nothing;
    • another browser, a signed-in browser, or another browser's attempt for the same account gets no code and can't finish anything;
    • the headers match the other emailed-link pages.
  • Signing in: the button signs in only in the browser that started, and starting over in that browser ends the link.
  • When it stops working:
    • it works once;
    • typing the code ends the link, and using the link uses up the code;
    • it ends with its code (expired, replaced, out of guesses) and with the attempt.
  • Shared limits: wrong codes from the button and typed ones count against the same guesses, the per-account rate limit is shared, and the page has its own limit.
  • Other emails: step-up and enrollment emails get no link.
  • Off by default: both routes give a 404 with links off.
  • Bad tokens and JSON:
    • JSON answers for headless frontends;
    • link_refused for garbled, foreign and incomplete tokens.
  • Subclasses and own notifications: a subclass's own deliver() is still used without a link and skipped with one, and an app's own verification_code notification without a $url is still sent and its code signs in.
  • The email: the code is in the body as one run of digits, not in the subject, and there are no images.

Locally:

  • SQLite with Redis at 100% coverage.
  • PostgreSQL and MySQL, including the Concurrency suite.
  • Rector, Pint, PHPStan and the PHPStan stubs config.
  • The PHP 8.6, lowest-dependency and JS jobs were left to CI.

Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

@RobertBoes RobertBoes changed the title Add an opt-in sign-in link beside the email code feat(email-code): add an opt-in sign-in link and make the code easier for mail apps to find Dec 3, 2026

@entity entity left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Left a few comments, none of them blocking :)

Comment thread src/Factors/EmailCode.php Resolved
Comment thread src/Factors/EmailCode.php Resolved
Comment thread docs/factors.md Resolved
Comment thread docs/frontend.md Resolved
Comment thread docs/frontend.md Resolved
Comment thread docs/headless.md Resolved
Comment thread docs/sign-in-flow.md Resolved
Comment thread src/Notifications/VerificationCode.php Resolved
Comment thread UPGRADE.md Resolved
@entity
entity merged commit d55bc32 into main Dec 3, 2026
7 checks passed
@entity
entity deleted the feat/email-sign-in-link branch December 3, 2026 16:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants