# Installation

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

## Introduction

This page covers adding Keyring for Go to a Laravel for Go application. The [Quick Start](https://keyringauth.com/docs/0.x/go/quick-start) has the short version, which starts from the starter kit.

The package is early. It isn't tagged yet, so an application depends on this repository, pinned to a commit, and its API will change before 1.0. Releases will be tagged `go/keyring/vX.Y.Z`.

## Requirements

- Go 1.27 or later.
- `go.laravel.com` v0.1.1. Laravel for Go is a beta, and makes no promises between 0.x releases.
- A database driver for Laravel for Go, listed in `laravel.Options.Drivers`. Keyring's tables are checked against the PHP package's on SQLite and PostgreSQL. MySQL isn't checked yet, and its timestamp columns differ (see [Differences From PHP](https://keyringauth.com/docs/0.x/go/differences#not-available-yet)).
- A cache store with locks. For more than one server, a store every server shares (see [Running on More Than One Server](https://keyringauth.com/docs/0.x/go/security#a-shared-lock-store)).
- Inertia. Keyring's pages are always Inertia components, so add Laravel for Go's `inertia.Middleware` to the `web` group. A frontend Laravel for Go doesn't serve can use JSON instead. See [Pages](https://keyringauth.com/docs/0.x/go/sign-in-flow#pages) and [Headless Frontends](https://keyringauth.com/docs/0.x/go/headless).

Passwords are bcrypt hashes, the only kind Laravel for Go checks. A PHP application that hashes with Argon2 can't share its users' passwords. There's no `sodium` requirement: Ed25519 passkeys are checked with Go's standard library. See [Signature Algorithms](https://keyringauth.com/docs/0.x/go/passkeys#signature-algorithms).

## Installing Keyring

Add the `keyringauth.com/go` module:

```bash
go get keyringauth.com/go@latest
```

Its package is named `keyring`, so the examples import it as `keyring "keyringauth.com/go"`. The module lives in this repository's `go/keyring` directory, and its releases are tagged `go/keyring/vX.Y.Z`, which `go get` reads as `vX.Y.Z`.

Then create Keyring for your user type and list it in `laravel.Options.Packages`:

```go
package bootstrap

import (
	laravel "go.laravel.com"
	"go.laravel.com/config"
	"go.laravel.com/db"
	"go.laravel.com/db/postgres"
	"go.laravel.com/inertia"

	keyring "keyringauth.com/go"

	"app/models"
	"app/routes"
)

func Options() laravel.Options {
	k := keyring.New[models.User](func(cfg *keyring.Config) {
		cfg.AuthenticatableType = `App\Models\User`
		cfg.SessionUserType = "pgsql:users"
	})

	return laravel.Options{
		Drivers:  []db.Driver{postgres.Driver},
		Packages: []laravel.Package{k},
		Middleware: func(m *laravel.Middleware, _ *config.Config) {
			m.Web.Append(inertia.Middleware{})
		},
		Routes: laravel.Routes{Web: routes.Web},
	}
}
```

`models.User` is your user type (see [Preparing Your User Model](https://keyringauth.com/docs/0.x/go/installation#preparing-your-user-model)). The `AuthenticatableType` and `SessionUserType` lines only need changing when the defaults don't match what a PHP application sharing the database writes. See [The Two User Types](https://keyringauth.com/docs/0.x/go/installation#the-two-user-types).

`keyring.From[models.User](ctx)` gives the installed `*Keyring[models.User]` back anywhere you have a request or its context, for the methods on later pages.

There's no installer to run first, and no question to answer. `keyring:install` exists, but it only prints the `keyring.New` for the factors and features you name, since your config is Go code:

```bash
go run . keyring:install --factor=totp --factor=passkeys --feature=registration --feature=manage-authenticators
```

`--factor` takes `totp`, `passkeys`, `email-codes` or `none`, with no `security-keys`, and `--feature` the PHP command's features. Naming a factor turns on `manage-authenticators`, and `registration` turns on `email-verification`, saying so. Since Keyring for Go has passkeys and the security log on unless they're turned off, it prints `cfg.Factors.Passkeys = false` and `cfg.Features.SecurityLog = false` when you don't name them. With an authenticator app or passkeys, it also prints `EnrollmentURL` and the `RequireEnrollment` middleware that requires a second factor of everyone, unless you pass `--optional-second-factor`. With `update-passwords` or `email-changes` on, its list of what's left asks for the user model's `Updating` hook (see [Preparing Your User Model](https://keyringauth.com/docs/0.x/go/installation#preparing-your-user-model)), as the PHP command asks for the `HasKeyring` trait. It writes nothing, asks nothing, and has no `--force`.

Run your application's `migrate` command to create Keyring's tables:

```bash
go run . migrate
```

If you're moving from Fortify, read [Upgrading From Fortify](https://keyringauth.com/docs/0.x/go/upgrading-from-fortify) before you migrate.

## What Gets Published

Nothing is published. `Register`, which Laravel for Go calls for each package, brings everything with it:

| What | Where It Comes From |
|---|---|
| Migrations | Keyring registers them. They create the PHP package's tables, with the same names and columns: `keyring_authenticators`, `keyring_devices`, `keyring_sessions`, `keyring_pending_changes`, `keyring_events`, `keyring_failure_counts`, `keyring_password_holds` and `keyring_sign_in_changes`. |
| Settings | The `keyring.Config` you change in the functions given to `keyring.New`. There's no `config/keyring.php`. See [Configuration](https://keyringauth.com/docs/0.x/go/configuration). |
| Routes and middleware | Keyring adds them to the `web` group, and a global middleware that notes each request for the security log. |
| Listeners | Keyring listens to its own events, to write the security log and queue the security emails, ahead of your listeners in `laravel.Options.Listeners`. |
| Actions | Yours to write, as interfaces Keyring takes: `CreatesNewUsers`, `ResetsUserPasswords`, `UpdatesUserPasswords` and `ValidatesEmailChanges`. See [Configuration](https://keyringauth.com/docs/0.x/go/configuration#registration). |
| Emails | Keyring's own plain-text and HTML emails. Replace them with `Notifications.Mail` and `Notifications.AccountMail`. See [Security Emails](https://keyringauth.com/docs/0.x/go/security-log-and-emails#security-emails). |

Laravel for Go keeps password reset tokens in the cache, where the PHP package keeps them in `password_reset_tokens`. Keyring keeps them in that table itself, so a link either side sends works on the other. A Laravel for Go application doesn't have the table, so one of Keyring's migrations creates it where it's missing, in Laravel's schema. A migration of your own that creates it has to sort before `2026_12_03_000001_create_password_reset_tokens_table_if_missing`.

The tables' `authenticatable_id` columns are big integers, so user IDs must be integers.

Keyring's timestamp columns keep whole seconds, as the PHP package's do, and Keyring cuts every time it writes to the second. What Laravel for Go writes itself, such as `email_verified_at` when `auth.ModelUsers` marks an address verified, is at the connection's precision, so set `DB_TIME_PRECISION=0` when the database is shared with PHP.

## Preparing Your User Model

There's no `HasKeyring` trait. Keyring is generic over your user type, as Laravel for Go's `auth.Session[U]` is, and reads the same fields `auth` does:

```go
package models

import (
	"context"
	"time"

	"go.laravel.com/db"

	keyring "keyringauth.com/go"
)

type User struct {
	ID              int64      `json:"id"`
	Name            string     `json:"name"`
	Email           string     `json:"email"`
	EmailVerifiedAt *time.Time `json:"email_verified_at"`
	Password        *string    `json:"-"`
	RememberToken   *string    `json:"-"`
	CreatedAt       *time.Time `json:"created_at"`
	UpdatedAt       *time.Time `json:"updated_at"`
}

// Updating refuses a change to a protected attribute outside Keyring's
// flows. See Protected Changes.
func (u *User) Updating(ctx context.Context, values db.Values) error {
	return keyring.From[User](ctx).GuardChanges(ctx, u, values)
}
```

`Register` refuses a user type without an integer `ID`, and an `Email`, `Password` and `RememberToken` that are each a `string` or a `*string`. A `*string` password lets a user have none, as one who signed up with an identity provider has. Security emails go to `Email`.

Keyring finds users with `auth.ModelUsers[U]` on `Config.Username`, `email` by default. Laravel for Go keeps the provider you register in `laravel.Options.Users` to itself, so if you have one, give Keyring the same with `FindUsersWith`. Otherwise a user your provider leaves out could still sign in through Keyring:

```go
k := keyring.New[models.User]().FindUsersWith(models.Users{})
```

Lookups trim and lowercase the typed username, so store addresses the same way. See [Finding the User](https://keyringauth.com/docs/0.x/go/sign-in-flow#finding-the-user).

What the PHP trait does, your application does where it saves and deletes users:

- **`$user->keyring`** has no counterpart. `SignInMethods` gives a settings page what it shows. See [Your Settings Page](https://keyringauth.com/docs/0.x/go/quick-start#your-settings-page) and [From Go](https://keyringauth.com/docs/0.x/go/authenticators#from-go).
- **Protected attributes.** Call `GuardChanges` with the columns a write changes, from the model's `Updating` hook as above, or before any write that doesn't go through the model. `Register` refuses a user type without the hook while anything is protected, unless `Config.GuardChangesManually` says you call it yourself. It refuses a protected attribute outside Keyring's flows, and an address held for someone else. Creating a user isn't a change: check its address with `AddressReserved`. See [Protected Attributes](https://keyringauth.com/docs/0.x/go/protected-changes#protected-attributes).
- **Deleting a user.** Nothing in Keyring for Go tidies up after a deleted user yet. See [Differences From PHP](https://keyringauth.com/docs/0.x/go/differences#protected-attributes-and-users).

`Features.EmailVerification` stands in for PHP's `MustVerifyEmail`. Laravel for Go reads an `EmailVerifiedAt` field any user type may have, so having the field alone isn't taken as opting in. With the feature on, the type needs an `EmailVerifiedAt` (a `*time.Time`, `time.Time` or `sql.NullTime`) or an `EmailVerified bool`, and the users Keyring finds them with must be an `auth.EmailVerifier`, as `auth.ModelUsers` is. `Register` checks both. As in the PHP package:

- A user whose address isn't verified gets no security emails, and can't have a device trusted.
- With registration on and hiding taken addresses, `Register` refuses to start unless email verification is on. [`Registration.RevealTaken`](https://keyringauth.com/docs/0.x/go/configuration#registration) turns that rule off.
- With registration and managing sign-in methods on, `Register` refuses to start unless email verification is on or `AllowEnrollmentWhen` decides who may add one. See [Managing Authenticators](https://keyringauth.com/docs/0.x/go/authenticators#email-verification).

A Go application sharing users with a PHP application that verifies addresses should turn `Features.EmailVerification` on.

### The Two User Types

The PHP package writes a user's type in two places, and Keyring for Go has a setting for each:

| Setting | Column | What the PHP Package Writes | Default |
|---|---|---|---|
| `AuthenticatableType` | `keyring_authenticators.authenticatable_type`, `keyring_failure_counts.authenticatable_type`, `keyring_devices.authenticatable_type` | The user model's morph class, such as `App\Models\User`, or its morph map alias | `App\Models\User` |
| `SessionUserType` (`KEYRING_SESSION_USER_TYPE`) | `keyring_sessions.user_type` | The connection and table the user's ID is unique in, as `connection:table`, such as `pgsql:users` | `DB_CONNECTION:users`, or the PHP name of `DATABASE_URL`'s driver (`pgsql`, `mysql`, `mariadb` or `sqlite`) |

When the two share a database, set both to what the PHP side writes. `keyring:install` prints the `SessionUserType` in use. A sign in for a user with authenticators stored under any other `AuthenticatableType` is refused with an error naming the setting, rather than signed in on the password alone. So an application supports one user type. A mismatch in `keyring_devices` or `keyring_failure_counts` isn't caught: it loses devices and counts, but lets no one past a factor.

Keyring loads a user's authenticators by `authenticatable_id` alone, to find those under another type. Both of PHP's indexes on `keyring_authenticators` lead with `authenticatable_type`, so on SQLite and MySQL each right password scans the table. The schema stays PHP's, so that's a known cost.

## Frontend

If you offer passkeys, install the browser package, `@keyringjs/keyring`. It also has helpers for the challenge page, and works with Keyring for Go unchanged, since it answers the same requests:

```bash
npm install @keyringjs/keyring
```

Keyring's pages are the Inertia components `auth/login` and the rest. See [Frontend](https://keyringauth.com/docs/0.x/go/frontend).

## Routes Keyring Registers

Keyring adds its routes to the `web` group, so they get the session, cookies and CSRF check. They carry the PHP package's names (`login`, `register`, `password.reset`, `verification.verify`, `keyring.login.challenge` and the rest), so don't give your own routes those names or paths.

The full list is in [The Sign In Flow](https://keyringauth.com/docs/0.x/go/sign-in-flow#routes), [Step-Up](https://keyringauth.com/docs/0.x/go/step-up), [Managing Authenticators](https://keyringauth.com/docs/0.x/go/authenticators#routes), [Devices & Sessions](https://keyringauth.com/docs/0.x/go/devices-and-sessions#routes) and [Protected Changes](https://keyringauth.com/docs/0.x/go/protected-changes#routes). To change the prefix, or place the routes yourself, see [Configuration](https://keyringauth.com/docs/0.x/go/configuration#routes).

Keyring also adds middleware: a global one first of all, which notes each request's address, browser and ID for the security log; the session lock and the overlapping requests guard around the `web` group's `StartSession`; and `AuthenticateSession` and `RequirePasswordChange` at the end of the `web` group. `AuthenticateSession` runs `auth.Session` for your user type itself, so don't add `auth.Session` to the group as well. See [The Session Middleware](https://keyringauth.com/docs/0.x/go/devices-and-sessions#the-session-middleware).

## After Installing

- Set `APP_URL` to the address users visit. Keyring builds emailed links and the single sign-on callback from it, and passkeys only work on that origin unless you configure others. See [Passkeys & Security Keys](https://keyringauth.com/docs/0.x/go/passkeys#relying-party-and-origins).
- Use the PHP side's `APP_KEY` when the two share a database. Laravel for Go reads what PHP's encrypter writes, and Keyring encrypts pending logins and authenticators' secrets with it, and keys recovery codes, sent codes and session handles with it.
- Build your [pages](https://keyringauth.com/docs/0.x/go/quick-start#your-pages). If you require a second factor, build the page at `EnrollmentURL` too.
- Run a queue worker (`go run . queue:work`), or use the `sync` queue connection. [Security emails](https://keyringauth.com/docs/0.x/go/security-log-and-emails#security-emails) are queued, and Laravel for Go uses the `database` queue whenever `DATABASE_URL` is set, so an application without a worker sends none. Sign-in codes, verification links and reset links are sent straight away.
- Run your application's scheduler every minute (`go run . schedule:run`, or the built binary's `schedule:run`, from cron), or keep `schedule:work` running. Keyring schedules `keyring:prune` daily at `PruneAt`, which deletes old devices, finished email changes, old security log entries, ended sessions, old sign-in changes, and password holds lifted a year ago. See [Pruning](https://keyringauth.com/docs/0.x/go/devices-and-sessions#pruning).
- Sessions end after two hours without activity unless the browser ticked "remember me". To change that, set `Sessions.Idle`. See [Timeouts](https://keyringauth.com/docs/0.x/go/devices-and-sessions#timeouts).
- Keep signed-in routes in the `web` group, where `AuthenticateSession` runs. Otherwise, a revoked or timed-out session keeps working on them. See [The Session Middleware](https://keyringauth.com/docs/0.x/go/devices-and-sessions#the-session-middleware).
- To hear about every session that ends, set `Sessions.SweepEvery`. Keyring then schedules `keyring:sweep`. See [Sweeping Ended Sessions](https://keyringauth.com/docs/0.x/go/devices-and-sessions#sweeping-ended-sessions).
- `keyring:sign-out` signs one user out everywhere, and `--all` signs everyone out. See [For Administrators](https://keyringauth.com/docs/0.x/go/devices-and-sessions#for-administrators).
- Laravel for Go's own `sessions` table keeps `last_activity` as a timestamp, where PHP's is an integer. An application sharing a database with PHP and using database sessions needs a `SESSION_TABLE` of its own on each side. Only Keyring's tables are shared.
- Behind a load balancer or proxy, list it in `laravel.Middleware.Proxies`. See [Trusted Proxies](https://keyringauth.com/docs/0.x/go/security#trusted-proxies).
