# Extending Keyring

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

## Introduction

Once the provider is added and the migrations have run, Keyring works. Most of what it does is yours to change, through settings, hooks on the provider, and the code your app owns: your user provider and your actions. Nothing is published, so the crate keeps getting fixes.

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

## What You Can Change

| To Change | Use | See |
|---|---|---|
| Which features are on | `features` | [Adding Features](https://keyringauth.com/docs/0.x/rust/quick-start#adding-features) |
| Any number, lifetime or limit | `KeyringConfig` | [Configuration](https://keyringauth.com/docs/0.x/rust/configuration) |
| How users sign in | `factors`, `email_codes`, `webauthn`, and the provider's `code_factor()`, `second_factor()` and `primary_factor()` | [Factors](https://keyringauth.com/docs/0.x/rust/factors) |
| What they must prove | `policy`, or the provider's `policy()` | [Policies](https://keyringauth.com/docs/0.x/rust/policies) |
| The steps a verification runs | The provider's `pipeline()` | [The Verification Pipeline](https://keyringauth.com/docs/0.x/rust/sign-in-flow#the-verification-pipeline) |
| Signing in with Google, Microsoft, GitHub or OpenID Connect | `features.sso` and `sso.providers` | [Single Sign-On](https://keyringauth.com/docs/0.x/rust/sso) |
| An identity provider Keyring has no driver for, or a change to one it has | `sso.extend()`, your own `IdentityProvider` | [Custom Drivers](https://keyringauth.com/docs/0.x/rust/sso#custom-drivers) |
| Signing in from a route of your own | `sign_in::attempt()` and `sign_in::as_user()` | [Starting a Sign In Yourself](https://keyringauth.com/docs/0.x/rust/sign-in-flow#starting-a-sign-in-yourself) |
| How a user is found, and how usernames are normalised | Your provider's `find_for_login()`, and the provider's `normalize_usernames_using()` | [Finding the User](https://keyringauth.com/docs/0.x/rust/sign-in-flow#finding-the-user) |
| The field users sign in with, and the one holding their address | `username` and `email` | [Guard and Users](https://keyringauth.com/docs/0.x/rust/configuration#guard-and-users) |
| Creating users, resetting and changing passwords, changing the address | Your `CreatesNewUsers`, `ResetsUserPasswords`, `UpdatesUserPasswords` and `ChangesEmails` | [Protected Changes](https://keyringauth.com/docs/0.x/rust/protected-changes) |
| Every email: wording, template, or whether it's sent | `notifications`: `turn_off()`, `mail()`, `mail_using` and the flow emails' methods | [Security Emails](https://keyringauth.com/docs/0.x/rust/security-log-and-emails#security-emails) |
| What a page renders | `pages.page()` and `pages.render_using()` | [Pages](https://keyringauth.com/docs/0.x/rust/sign-in-flow#pages) |
| What Keyring answers with | `responses.respond_with()` and `http::KeyringResponse` | [Responses](https://keyringauth.com/docs/0.x/rust/sign-in-flow#responses) |
| Validation and status messages | Not available: English only | [Messages and Translations](https://keyringauth.com/docs/0.x/rust/configuration#messages-and-translations) |
| The route prefix and domain | `prefix` and `domain` | [Routes](https://keyringauth.com/docs/0.x/rust/configuration#routes) |
| How recent a verification account changes need | `changes_fresh_for` | [Account Changes and Enrollment](https://keyringauth.com/docs/0.x/rust/configuration#account-changes-and-enrollment) |
| Starting an impersonation: what it takes, how long it lasts, whether the user sees it | `impersonation` | [Impersonation](https://keyringauth.com/docs/0.x/rust/configuration#impersonation) |
| The form rate limits | `limits` | [Rate Limits](https://keyringauth.com/docs/0.x/rust/configuration#pending-logins-timebox-and-rate-limits) |
| Who may sign in at all | The provider's `allow_sign_in_when()` | [Who May Sign In](https://keyringauth.com/docs/0.x/rust/sign-in-flow#who-may-sign-in) |
| Who may add a sign-in method | The provider's `allow_enrollment_when()` | [Who May Enroll](https://keyringauth.com/docs/0.x/rust/authenticators#who-may-enroll) |
| Who may impersonate whom | The provider's `allow_impersonation_when()` | [Who May Impersonate Whom](https://keyringauth.com/docs/0.x/rust/impersonation#who-may-impersonate-whom) |
| Whether registration says when an address is taken | `registration.reveal_taken` | [Registration](https://keyringauth.com/docs/0.x/rust/sign-in-flow#registration) |
| Whether registering signs the user in | The provider's `sign_in_after_registering()` | [Saying When an Address Is Taken](https://keyringauth.com/docs/0.x/rust/sign-in-flow#saying-when-an-address-is-taken) |
| Risk signals and what they do | `risk.signals`, `risk.respond`, your own `Signal` | [Risk](https://keyringauth.com/docs/0.x/rust/risk) |
| Where a request is located | `devices.locator`, your own `LocatesIps` | [Locations](https://keyringauth.com/docs/0.x/rust/devices-and-sessions#locations) |
| Whose key attestation is trusted | `webauthn.attestation_roots`, your own `TrustsAuthenticators` | [Passkeys & Security Keys](https://keyringauth.com/docs/0.x/rust/passkeys#trusting-each-model-separately) |
| How a browser is described | `devices.describe_using()` | [Device Names](https://keyringauth.com/docs/0.x/rust/devices-and-sessions#device-names) |
| Where sessions are registered | `sessions.driver`, `sessions.extend()`, your own `SessionRegistry` | [Writing a Driver](https://keyringauth.com/docs/0.x/rust/devices-and-sessions#writing-a-driver) |
| Whether Keyring checks sessions on the `web` group | `sessions.middleware` | [The Session Middleware](https://keyringauth.com/docs/0.x/rust/devices-and-sessions#the-session-middleware) |
| Routes of your own that take the session lock | `sessions.block` | [Requests That Overlap a Sign In](https://keyringauth.com/docs/0.x/rust/devices-and-sessions#requests-that-overlap-a-sign-in) |
| What's stored as an IP address | `record_ips_using` | [What's Stored](https://keyringauth.com/docs/0.x/rust/security-log-and-emails#whats-stored) |
| Where emailed links start | The provider's `link_root_using()` | [Emailed Links](https://keyringauth.com/docs/0.x/rust/security#emailed-links) |
| Which attributes are protected | `protected`, `protection::without_protection()` | [Protected Changes](https://keyringauth.com/docs/0.x/rust/protected-changes) |

Every feature is off until you turn it on, and `risk.signals` starts empty. The [security emails](https://keyringauth.com/docs/0.x/rust/security-log-and-emails#security-emails) are Keyring's own, and are sent from the start.

## Replacing a Controller or Service

Not available: the crate's handlers and services aren't types you can swap through the container. Use the settings and hooks above, and the services the crate exposes for your own routes: `Sessions`, `StepUp`, `SessionAssurance`, `UserKeyring`, `sign_in`, `impersonation`, `sso`, `security_log` and `protection`. For a route that should work differently, register your own at another path and call those, or turn `routes` off and register Keyring's with `http::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/rust/differences#requiring-a-second-factor).

## Changing the Routes

`prefix` and `domain` cover most changes. To register the routes inside a group of your own, for middleware, set `routes` to `false` and call `http::routes::<Users>(route, &config)` inside that group. Set the prefix and domain in the config, not on the group, as Keyring builds its URLs from them. There's no routes file to copy. See [Routes](https://keyringauth.com/docs/0.x/rust/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.

Keyring also needs a user provider with integer IDs and laravel-rust's session guard, and one user type per application. The form field names, the paths and the status slugs are fixed, as the browser package relies on them.
