# Extending Keyring

Source: https://keyringauth.com/docs/0.x/go/extending

## Introduction

Once the package is in `laravel.Options.Packages` and the migrations have run, Keyring works. Most of what it does is yours to change, through `keyring.Config`, the hooks on `*keyring.Keyring[U]`, and the code your app owns: your user type, your `auth.Users` and your actions. Nothing is published, so the package keeps getting fixes.

Start with the table below. The [Recipes](https://keyringauth.com/docs/0.x/go/recipes) page has worked examples of common changes.

## What You Can Change

| To Change | Use | See |
|---|---|---|
| Which features are on | `Config.Features` | [Adding Features](https://keyringauth.com/docs/0.x/go/quick-start#adding-features) |
| Any number, lifetime or limit | `keyring.Config` | [Configuration](https://keyringauth.com/docs/0.x/go/configuration) |
| How users sign in | `Config.Factors` (`EmailCodes`, `Passkeys`, `SingleSignOn` and the TOTP settings), `Config.WebAuthn`, and `Keyring.AddCodeFactor` | [Factors](https://keyringauth.com/docs/0.x/go/factors) |
| Primary factors of your own | `Keyring.AddPrimaryFactor`, with your own `IdentifiesUsers` | [Primary Factors](https://keyringauth.com/docs/0.x/go/factors#primary-factors) |
| What they must prove | `Keyring.Require` | [Policies](https://keyringauth.com/docs/0.x/go/policies) |
| The steps a verification runs | `Keyring.Pipeline` | [The Verification Pipeline](https://keyringauth.com/docs/0.x/go/sign-in-flow#the-verification-pipeline) |
| Signing in with Google, Microsoft, GitHub or OpenID Connect | `Config.Factors.SingleSignOn` and `Config.SSO.Providers` | [Single Sign-On](https://keyringauth.com/docs/0.x/go/sso) |
| An identity provider Keyring has no driver for, or a change to one it has | `Config.SSO.Extend`, your own `IdentityProvider` | [Custom Drivers](https://keyringauth.com/docs/0.x/go/sso#custom-drivers) |
| Signing in from a route of your own | `Keyring.Attempt`, `Keyring.AttemptSSO` and `Keyring.SignInAs` | [Starting a Sign In Yourself](https://keyringauth.com/docs/0.x/go/sign-in-flow#starting-a-sign-in-yourself) |
| How a user is found | Your `auth.Users` given to `Keyring.FindUsersWith`, and its `FindForLogin` | [Finding the User](https://keyringauth.com/docs/0.x/go/sign-in-flow#finding-the-user) |
| The field users sign in with, and the one holding their address | `Config.Username` and `Config.Email` | [Guard and Users](https://keyringauth.com/docs/0.x/go/configuration#guard-and-users) |
| Creating users, resetting and changing passwords, changing the address | Your `CreatesNewUsers`, `ResetsUserPasswords`, `UpdatesUserPasswords` and `ValidatesEmailChanges`, given with `CreateUsersWith`, `ResetPasswordsWith`, `UpdatePasswordsWith` and `ValidateEmailChangesWith` | [Protected Changes](https://keyringauth.com/docs/0.x/go/protected-changes) |
| Every email: wording, template, or whether it's sent | `Config.Notifications`: `Off`, `Mail` and `AccountMail`, and `Factors.EmailCodes.Mail` | [Security Emails](https://keyringauth.com/docs/0.x/go/security-log-and-emails#security-emails) |
| What a page renders | Not available: your Inertia components, under `auth/` | [Pages](https://keyringauth.com/docs/0.x/go/sign-in-flow#pages) |
| What Keyring answers with | Not available | [Responses](https://keyringauth.com/docs/0.x/go/sign-in-flow#responses) |
| Validation and status messages | Not available: English only | [Messages and Translations](https://keyringauth.com/docs/0.x/go/configuration#messages-and-translations) |
| The route prefix | `Config.Prefix` | [Routes](https://keyringauth.com/docs/0.x/go/configuration#routes) |
| What account changes ask for first | `Config.ChangesMiddleware` | [Account Changes and Enrollment](https://keyringauth.com/docs/0.x/go/configuration#account-changes-and-enrollment) |
| Starting an impersonation: what it takes, how long it lasts, whether the user sees it | `Config.Impersonation` | [Impersonation](https://keyringauth.com/docs/0.x/go/configuration#impersonation) |
| The form rate limits | `Config.Limits` | [Rate Limits](https://keyringauth.com/docs/0.x/go/configuration#pending-logins-timebox-and-rate-limits) |
| Who may sign in at all | `Keyring.AllowSignInWhen` | [Who May Sign In](https://keyringauth.com/docs/0.x/go/sign-in-flow#who-may-sign-in) |
| Who may add a sign-in method | `Keyring.AllowEnrollmentWhen` | [Who May Enroll](https://keyringauth.com/docs/0.x/go/authenticators#who-may-enroll) |
| Who may impersonate whom | `Keyring.AllowImpersonationWhen` | [Who May Impersonate Whom](https://keyringauth.com/docs/0.x/go/impersonation#who-may-impersonate-whom) |
| Whether registration says when an address is taken | `Config.Registration.RevealTaken` | [Registration](https://keyringauth.com/docs/0.x/go/sign-in-flow#registration) |
| Risk signals and what they do | `Keyring.RiskSignals`, `Keyring.RespondToRisk`, your own `Signal` | [Risk](https://keyringauth.com/docs/0.x/go/risk) |
| Where a request is located | `Config.Devices.Locator`, your own `LocatesIPs` | [Locations](https://keyringauth.com/docs/0.x/go/devices-and-sessions#locations) |
| How a browser is described | `Config.Devices.Describe` | [Device Names](https://keyringauth.com/docs/0.x/go/devices-and-sessions#device-names) |
| Whether Keyring checks sessions on the `web` group | `Config.Sessions.Middleware` | [The Session Middleware](https://keyringauth.com/docs/0.x/go/devices-and-sessions#the-session-middleware) |
| Routes of your own that take the session lock | `keyring.BlockSession` on the route | [Requests That Overlap a Sign In](https://keyringauth.com/docs/0.x/go/devices-and-sessions#requests-that-overlap-a-sign-in) |
| Where emailed links start | `Config.LinkRoot` | [Emailed Links](https://keyringauth.com/docs/0.x/go/security#emailed-links) |
| Which attributes are protected | `Config.Protected`, `Keyring.GuardChanges` and `Keyring.WithoutProtection` | [Protected Changes](https://keyringauth.com/docs/0.x/go/protected-changes) |

A few rows of the PHP table have no Go counterpart yet: whose key attestation is trusted (only `none` attestation is checked), where sessions are registered (always the database), and what's stored as an IP address (always `r.IP()`). See [Differences From PHP](https://keyringauth.com/docs/0.x/go/differences).

The account features start off, and `Keyring.RiskSignals` starts with none. `Features.SecurityLog` and `Factors.Passkeys` start on. The [security emails](https://keyringauth.com/docs/0.x/go/security-log-and-emails#security-emails) are Keyring's own, and are sent from the start.

## Replacing a Controller or Service

Not available: Keyring's handlers and services aren't values you can swap. Use the settings and hooks above, and the methods on `*keyring.Keyring[U]` for your own routes, such as `SignInMethods`, `RequirePasswordChange`, `PendingEmailChange`, `Record`, `PullAlerts`, `Unlock`, `Impersonate` and `WithoutProtection`. Reach it from a request with `keyring.From[models.User](r)`. For a route that should work differently, register your own at another path and call those, or turn `Config.Routes` off and register Keyring's with `Keyring.Routes`.

There's no `Detours` to override: the page a user was going to is saved for a page the browser navigated to, as in the PHP package. See [Differences From PHP](https://keyringauth.com/docs/0.x/go/differences#requiring-a-second-factor).

## Changing the Routes

`Config.Prefix` covers most changes. To register the routes inside a group of your own, for middleware, set `Config.Routes` to `false` and call `k.Routes(route)` inside that group. Keep it in the web group, since several routes take the session lock, which needs it. Keyring adds the prefix itself, so set it in the config, not on the group. There's no domain setting and no routes file to copy. See [Routes](https://keyringauth.com/docs/0.x/go/configuration#routes).

## What Stays Fixed

As in the [PHP docs](https://keyringauth.com/docs/0.x/extending#what-stays-fixed): email codes are only the extra step, a user can't remove their last way to sign in, an email change is confirmed from the new address and announced to the old one, trusting a device takes a real second factor, the pipeline's mandatory steps can only be changed dangerously, and emailed links are never built from the request's host. `EnsureNotThrottled`, `VerifyFactor` and `CompleteAttempt` can't be changed even dangerously, and your steps only go between the last two.

Keyring also needs a user type with an integer `ID`, an `Email`, a `Password` and a `RememberToken`, which `Register` checks, Laravel Go's session guard, and one user type per application. The form field names, the paths, the route names and the status slugs are fixed, as the browser package relies on them.
