# Quick Start

Source: https://keyringauth.com/docs/0.x/go/quick-start

## Introduction

This page takes a Laravel for Go application to a working sign-in: the install, the pages to build, your settings page, and the features and sign-in methods to add next.

Signing in with a password, signing out, challenges and step-up are always on. Unlike the PHP package, registration, resets, password changes, email verification and email changes start off, since they run code of yours or need your user type to verify addresses. The security log is on.

## Installing

The quickest way in is the [starter kit](https://github.com/keyringauth/keyring/blob/main/go/starter-kit/README.md): a Laravel for Go application with Inertia, React, TypeScript and Tailwind CSS on SQLite, with a page for every page Keyring renders but the sign-in-link page (which only matters with `WithLink()` on), settings pages for the email address, the password, sign-in methods, and sessions and devices, and a second factor required of everyone. Copy `go/starter-kit` out and follow its README:

```bash
go run . key:generate
go run . migrate
bun install
bun run build
go run .
```

To add Keyring to an application of your own, add the `keyringauth.com/go` module:

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

Then list Keyring in your packages, with the actions for the features you turn on:

```go
k := keyring.New[models.User](func(cfg *keyring.Config) {
	cfg.Features.Registration = true
	cfg.Features.ResetPasswords = true
	cfg.Features.EmailVerification = true
	cfg.Features.UpdatePasswords = true
}).
	CreateUsersWith(actions.CreateNewUser{}).
	ResetPasswordsWith(actions.ResetUserPassword{}).
	UpdatePasswordsWith(actions.UpdateUserPassword{})

return laravel.Options{
	Packages: []laravel.Package{k},
	Middleware: func(m *laravel.Middleware, _ *config.Config) {
		m.Web.Append(inertia.Middleware{})
	},
	// ...
}
```

`models.User` is your user type, and the three actions are yours: see [Installation](https://keyringauth.com/docs/0.x/go/installation#preparing-your-user-model) and [Registration](https://keyringauth.com/docs/0.x/go/sign-in-flow#registration). The [contract app's actions](https://github.com/keyringauth/keyring/tree/main/go/contract-app/actions/) are the PHP package's stubs written in Go, ready to copy. Keyring brings its migrations, so run `go run . migrate`.

With `Features.EmailVerification` on, your user type needs an `EmailVerifiedAt` field, as a new Laravel for Go application's has. That keeps the registration form from revealing who has an account, since a new user stays signed out until they verify their address.

> [!NOTE]
> New users can't sign in until they follow the verification link, so your app must send mail. With `MAIL_MAILER=log`, the link is written to the log.

## Your Pages

Keyring handles the requests, and you build the pages. It renders them as the Inertia components `auth/{page}`, so they live in `resources/js/pages/auth/`. For a frontend Laravel for Go doesn't serve, see [Headless Frontends](https://keyringauth.com/docs/0.x/go/headless). The [starter kit](https://github.com/keyringauth/keyring/blob/main/go/starter-kit/README.md) has every page in React. Build the pages for the features you turn on:

| Page | When It's Needed |
|---|---|
| `login` | Always. |
| `challenge` | When you add a second sign-in method. It asks for the next factor. |
| `confirm` | For step-up, which changing the password and adding sign-in methods use. It may render the same component as `challenge`. |
| `register` | With `Features.Registration`. Posts `name`, `email`, `password` and `password_confirmation` to `/register`, or whatever your `CreatesNewUsers` reads. |
| `forgot-password`, `reset-password` | With `Features.ResetPasswords`. |
| `verify-email` | With `Features.EmailVerification`, for a signed-in user who hasn't verified. Only needed if you set `Registration.RevealTaken`. |
| `email-verification` | With `Features.EmailVerification`. The verification link opens it, with one button. Only needed if you set `Registration.RevealTaken`. |
| `email-change` | With `Features.EmailChanges`. The confirm and undo links open it. |
| `device-report` | With `Features.Devices`. The "this wasn't me" link opens it. |
| `sign-in-link` | With `keyring.EmailCodesAsExtraStep().WithLink()`. The link beside the code in the sign-in email opens it. See [Signing In From the Email](https://keyringauth.com/docs/0.x/go/factors#signing-in-from-the-email). |

The login form posts `email`, `password` and an optional `remember` field to `/login`. Signed-in users go to `Home`, which is `/dashboard` by default (`KEYRING_HOME`). Every page's props are in [Pages](https://keyringauth.com/docs/0.x/go/sign-in-flow#pages), and they're the PHP package's.

## Your Settings Page

Users manage their sign-in methods on a settings page you build, at any URL. Keyring doesn't register one. Your handler passes in what the page shows, and the page calls Keyring's routes to make changes. `SignInMethods` gives what such a page needs:

```go
package routes

import (
	"go.laravel.com/auth"
	"go.laravel.com/config"
	"go.laravel.com/inertia"
	"go.laravel.com/routing"
	"go.laravel.com/web"

	keyring "keyringauth.com/go"

	"app/models"
)

func Web(route *routing.Router, _ *config.Config) {
	route.Middleware(keyring.RequireFreshVerification[models.User]{}).Group(func(route *routing.Router) {
		route.Get("/settings/security", security)
	})
}

func security(r *web.Request) *web.Response {
	user := auth.User[models.User](r)
	methods, err := keyring.From[models.User](r).SignInMethods(r, user)
	if err != nil {
		return web.Fail(err)
	}

	return inertia.Render("settings/security", inertia.Props{"methods": methods})
}
```

| Prop | Contents |
|---|---|
| `methods.authenticators` | The user's sign-in methods, with `factor`, `id`, `name`, `createdAt`, `lastUsedAt` (ISO 8601), `isDefault`, and `frozen` when the freeze holds one. Email codes add a masked `hint`; recovery codes add `remaining` and `replace`. The password has no row, so it's left out. |
| `methods.enrollable` | The factors a user may add, such as `totp` and `passkey`. Recovery codes only once the account has a factor for them to stand in for. |
| `methods.passwordToAdd` | Those that take the current password to add. |
| `methods.frozen`, `methods.hasSecondFactor` | The freeze, and whether the account has a second factor. |

There's no recent activity to show beside them yet: the PHP package's `Keyring::activity()` isn't ported. See [Differences From PHP](https://keyringauth.com/docs/0.x/go/differences#the-security-log).

The page then uses [`/user/authenticators`](https://keyringauth.com/docs/0.x/go/authenticators#routes), [`/user/devices`](https://keyringauth.com/docs/0.x/go/devices-and-sessions#routes), [`/user/sessions`](https://keyringauth.com/docs/0.x/go/devices-and-sessions#listing-and-ending-sessions), [`PUT /user/password`](https://keyringauth.com/docs/0.x/go/protected-changes#changing-the-password) and [`POST /user/email`](https://keyringauth.com/docs/0.x/go/protected-changes#changing-the-email-address). The [browser package](https://keyringauth.com/docs/0.x/go/frontend#managing-authenticators) wraps the authenticator routes. There's no devices list to pass in: the page loads `GET /user/devices` itself.

Routes that change something need a [fresh verification](https://keyringauth.com/docs/0.x/go/step-up#fresh-verification), which lasts 15 minutes by default. Listing doesn't. Put `RequireFreshVerification` on the page's route, as above, so users confirm once when it opens.

Signing in counts as a verification. If the page stays open longer, the next JSON request gets a `423` response with a `redirect` to the confirm page. Send the browser there. Once the user confirms, Keyring returns them to the page to try again. The [Handling Step-Up in JavaScript](https://keyringauth.com/docs/0.x/go/frontend#handling-step-up-in-javascript) section has the code.

## Testing

Laravel for Go's `laraveltest` drives Keyring's routes like any others. The `keyringtest` package adds the rest: `keyringtest.New[models.User](t, client)` has refused checks answer at once rather than waiting out `Config.Timebox`, and gives `kt.ActingAs` for a session at an assurance level, `kt.Fake()` to pass or fail factors without real codes, and `kt.Add` for authenticator rows. `keyringtest.Conformance` checks that your setup keeps Keyring's guarantees. See [Testing](https://keyringauth.com/docs/0.x/go/testing).

## Adding Features

Features are `bool`s in `Config.Features`, and each adds its routes only once it's on. Set them one at a time: assigning the whole struct leaves `SecurityLog` off, which turns the security log's table off.

```go
cfg.Features.Registration = true
cfg.Features.ResetPasswords = true
cfg.Features.EmailVerification = true
cfg.Features.UpdatePasswords = true
cfg.Features.ManageAuthenticators = true
cfg.Features.Devices = true
cfg.Features.EmailChanges = true
cfg.Features.Impersonation = true
```

| Feature | What It Adds | Documentation |
|---|---|---|
| `Registration` | `/register`, through your `CreatesNewUsers`, given with `CreateUsersWith`. Needs `EmailVerification` unless `Registration.RevealTaken` is on. | [Registration](https://keyringauth.com/docs/0.x/go/sign-in-flow#registration) |
| `ResetPasswords` | Forgot and reset password, through your `ResetsUserPasswords`, given with `ResetPasswordsWith`. | [Resetting the Password](https://keyringauth.com/docs/0.x/go/protected-changes#resetting-the-password) |
| `EmailVerification` | The verification routes, and the `verify-email` and `email-verification` pages. Needs an `EmailVerifiedAt` field on the user type. | [Email Verification](https://keyringauth.com/docs/0.x/go/protected-changes#email-verification) |
| `UpdatePasswords` | `PUT /user/password`, through your `UpdatesUserPasswords`, given with `UpdatePasswordsWith`. The password becomes protected. | [Changing the Password](https://keyringauth.com/docs/0.x/go/protected-changes#changing-the-password) |
| `ManageAuthenticators` | Adding, renaming and removing sign-in methods. Required for users to set any up. | [Managing Authenticators](https://keyringauth.com/docs/0.x/go/authenticators) |
| `Devices` | A device cookie, "trust this device", the list of signed-in devices, and "this wasn't me" links. | [Devices & Sessions](https://keyringauth.com/docs/0.x/go/devices-and-sessions) |
| `EmailChanges` | Email changes, confirmed from both addresses, through your `ValidatesEmailChanges`, given with `ValidateEmailChangesWith`. The address becomes protected. | [Protected Changes](https://keyringauth.com/docs/0.x/go/protected-changes#changing-the-email-address) |
| `Impersonation` | `POST /impersonate/{user}` and `POST` or `DELETE /impersonate`. Nobody may impersonate until you give Keyring `AllowImpersonationWhen`. | [Impersonation](https://keyringauth.com/docs/0.x/go/impersonation) |
| `SecurityLog` | The security log in `keyring_events`. On by default. | [Security Log & Emails](https://keyringauth.com/docs/0.x/go/security-log-and-emails) |

Single sign-on is `Factors.SingleSignOn`, with providers in `SSO.Providers`. See [Single Sign-On](https://keyringauth.com/docs/0.x/go/sso).

A few settings live outside the list:

- **[Security emails](https://keyringauth.com/docs/0.x/go/security-log-and-emails#security-emails)**: sent from the start, queued, so run a queue worker or use the `sync` queue. `Notifications.Off` lists those to leave out.
- **[Risk signals](https://keyringauth.com/docs/0.x/go/risk)**: added with `RiskSignals`, which starts with none. When you turn on devices, add `keyring.NewDevice{}` and `keyring.InactiveAccount{}`.
- **[Support contact](https://keyringauth.com/docs/0.x/go/security-log-and-emails#a-support-contact)**: `SupportContact` sets who the security emails tell users to contact.
- **[Second factor requirement](https://keyringauth.com/docs/0.x/go/policies#requiring-a-second-factor)**: the `RequireEnrollment` middleware, with `EnrollmentURL` set to your page for adding one.

## Adding Sign-In Methods

The password, authenticator apps, recovery codes and passkeys are on by default. The PHP package's `Keyring::factors()` with nothing listed has the password alone. Email codes are opt in, and security keys aren't ported yet:

```go
k := keyring.New[models.User](func(cfg *keyring.Config) {
	cfg.Factors.EmailCodes = keyring.EmailCodesAsExtraStep()
}).Require(func(_ context.Context, _ *models.User, _ keyring.Attempt) keyring.Requirement {
	return keyring.AAL2.Requirement().IfEnrolled().NoDowngrade()
})
```

Set `Factors.Passkeys` to `false` to leave passkeys out. See [Registering Factors](https://keyringauth.com/docs/0.x/go/factors#registering-factors).

AAL is NIST's authenticator assurance level, a scale for how strongly a session was proved. A password alone is AAL1. A password plus an authenticator app, or a passkey on its own, reaches AAL2. See [Assurance Levels](https://keyringauth.com/docs/0.x/go/policies#assurance-levels).

`IfEnrolled()` asks for a second factor once a user has one, and is the default policy. `NoDowngrade()` stops a user with an established passkey, one held for 7 days by default, from being talked into something weaker.

To finish:

1. Turn on `Features.ManageAuthenticators`.
2. Build the `challenge` and `confirm` pages, and the part of [your settings page](https://keyringauth.com/docs/0.x/go/quick-start#your-settings-page) that adds and removes methods.
3. To require a second factor for everyone, append `RequireEnrollment` to the `web` group and set `EnrollmentURL`. See [Requiring a Second Factor](https://keyringauth.com/docs/0.x/go/policies#requiring-a-second-factor).

For Google, Microsoft, GitHub or another identity provider, turn on `Factors.SingleSignOn` and list them in `SSO.Providers`. See [Single Sign-On](https://keyringauth.com/docs/0.x/go/sso).

## What Keyring Does Without Being Asked

These protections are on by default, as in the PHP package:

| Protection | Default | How to Change It |
|---|---|---|
| Sign-in rate limits | 5 a minute per account, 20 per IP address. After 100 failures in a row, a method is refused until a password reset, `Unlock`, or removing and re-adding the second factor. | `Limits` in [Configuration](https://keyringauth.com/docs/0.x/go/configuration#pending-logins-timebox-and-rate-limits) |
| Refused checks take at least 500ms | 500ms | `Timebox` in [Configuration](https://keyringauth.com/docs/0.x/go/configuration#pending-logins-timebox-and-rate-limits) |
| Registration answers the same for taken and free addresses | Enabled | [`Registration.RevealTaken`](https://keyringauth.com/docs/0.x/go/sign-in-flow#registration) |
| Password rules | 15 characters without a second factor, 8 with one. Common passwords, and ones built from the user's name or email or your app's name, are refused. Breached passwords are refused in production. Your actions check with `PasswordRules`. | [Password Rules](https://keyringauth.com/docs/0.x/go/protected-changes#password-rules) |
| Sessions are recorded with IP address and browser | Sessions end after 2 hours idle, or 30 days after sign-in. "Remember me" returns a browser for up to 14 days away. | `Sessions` in [Configuration](https://keyringauth.com/docs/0.x/go/configuration#sessions) |
| Assurance levels lapse, as NIST recommends | AAL2 lapses after 1 hour idle or 24 hours. AAL3 lapses after 15 minutes idle or 12 hours. | `Sessions.Levels` |
| Browsers clear cache and storage on sign out | Enabled | Not changeable in Keyring for Go. See [Responses](https://keyringauth.com/docs/0.x/go/sign-in-flow#responses). |
| Step-up middleware | `RequireAssurance` and `RequireFreshVerification` | [Step-Up](https://keyringauth.com/docs/0.x/go/step-up) |
