# Step-Up

Source: https://keyringauth.com/docs/0.x/go/step-up

## Introduction

Step-up asks a signed-in user to prove something again before a sensitive action, such as changing their password. Keyring has two middleware for it:

- `RequireAssurance[U]` requires the session to have reached an [assurance level](https://keyringauth.com/docs/0.x/go/policies#assurance-levels), as `keyring:{level}`.
- `RequireFreshVerification[U]` requires a verification within a time window, as `keyring.fresh:{window}`.

Laravel Go's middleware are typed values with fields, so they go on a route or group as values rather than strings:

```go
func Web(route *routing.Router, _ *config.Config) {
	route.Middleware(keyring.RequireAssurance[models.User]{}).Group(func(route *routing.Router) {
		route.Get("/billing", billing.Show)

		route.Middleware(keyring.RequireFreshVerification[models.User]{Within: 5 * time.Minute}).Group(func(route *routing.Router) {
			route.Delete("/account", account.Destroy)
		})
	})
}
```

Put them on routes in the `web` group, inside `AuthenticateSession`, which Keyring adds to the group. Without the session they fail closed with a `500`, never a way past them. A guest is turned away as Laravel Go's `auth.Authenticate` turns one away: a `401` for JSON, and a browser is sent to the login page.

What the session proved counts only for the user its record belongs to. See [Second Factors and Step-Up](https://keyringauth.com/docs/0.x/go/security#second-factors-and-step-up).

## Raising Assurance

`RequireAssurance[models.User]{}` lets the request through at AAL2 or higher, as `keyring:aal2`. A zero `Level` is AAL2. `Level: keyring.AAL1` and `Level: keyring.AAL3` ask for the others.

AAL3 needs a key added with a verified attestation, and Keyring for Go doesn't check attestation yet, so no key it adds reaches AAL3. `DeviceBound: true` asks for a phishing-resistant key that stays on its device, as `keyring:aal2,device-bound`, and `PhishingResistant: true` for any passkey, as `keyring:aal2,phishing-resistant`. [`RequireEnrollment`](https://keyringauth.com/docs/0.x/go/policies#requiring-a-second-factor) takes the same fields.

```go
route.Get("/admin", admin.Show).Middleware(keyring.RequireAssurance[models.User]{Level: keyring.AAL2, PhishingResistant: true})
```

A session short of the level starts a confirm that builds on what still counts toward it, as in the PHP package. One difference: `Level: keyring.AAL1` takes no recovery code alone, where the PHP package's `keyring:aal1` does. A recovery code stands in for a lost factor beside another one, never as the whole of what's asked, as the Rust crate's audit settled.

A level lapses on its own clock, as in the PHP package. At AAL2, it lapses after an hour without a request, or 24 hours overall. See [Timeouts](https://keyringauth.com/docs/0.x/go/devices-and-sessions#timeouts). The idle limit is kept by `AuthenticateSession`. A [risk response](https://keyringauth.com/docs/0.x/go/risk#checkpoints) can clear a level, and removing an authenticator stops it counting everywhere.

A browser [impersonating](https://keyringauth.com/docs/0.x/go/impersonation#whats-refused-while-impersonating) the user can't step up as them, and gets the `403` `impersonating` instead of a confirm.

A user without the authenticators for a level gets the `403` `enroll-first`, and a browser is sent to your [`EnrollmentURL`](https://keyringauth.com/docs/0.x/go/configuration#account-changes-and-enrollment).

There's no public way yet to read the session's level from your own code, as the PHP package's `SessionAssurance`. See [Differences From PHP](https://keyringauth.com/docs/0.x/go/differences#not-available-yet).

## Fresh Verification

`RequireFreshVerification[models.User]{}` lets the request through when the user verified, in the last 15 minutes, the strongest combination their account can give, and at least what signing in would ask. `Within` gives a route a window of its own, as `keyring.fresh:5m` is `Within: 5 * time.Minute`. What counts as the strongest combination is the PHP package's, as its [docs](https://keyringauth.com/docs/0.x/step-up#fresh-verification) describe.

Keyring asks for this on its own sensitive routes through `ChangesMiddleware`, as the PHP package's `changes_middleware`. It starts as `RequireFreshVerification` for your user type, with the 15-minute window. It covers managing authenticators, ending sessions, signing out devices, changing the password and asking for a new address. Set it to `nil` to ask for nothing more than a signed-in user, or list others in its place:

```go
cfg.ChangesMiddleware = []web.Middleware{keyring.RequireFreshVerification[models.User]{Within: 5 * time.Minute}}
```

`ChangesMiddleware` only changes Keyring's own routes. A route of yours that runs `RequireFreshVerification` still asks.

## Asking From a Controller

Not available yet. The PHP package's `StepUp` makes the middleware's decision in a controller, for an integration such as an OpenID Connect provider that decides what to ask from `acr_values` and `max_age`. Keyring for Go has no counterpart: put `RequireAssurance` or `RequireFreshVerification` on the route instead. See [Differences From PHP](https://keyringauth.com/docs/0.x/go/differences#not-available-yet).

### Passing a Fixed Moment

Not available yet, as above. The [PHP docs](https://keyringauth.com/docs/0.x/step-up#passing-a-fixed-moment) explain why a moment that moves with every request can never be met.

### Asking Without Starting a Confirm

Not available yet, as above. The PHP package's `StepUp::meets()` answers without showing the user anything, for `prompt=none`.

### An Example

See the [PHP example](https://keyringauth.com/docs/0.x/step-up#an-example). It needs `StepUp`, which Keyring for Go doesn't have yet.

## Reading What Was Proved

Not available yet. The PHP package's `SessionAssurance` reads what the session proved, as `amr`, `acr` and `auth_time` for an ID token. Keyring for Go keeps the same verifications in the session, but has no public way to read them. A [policy](https://keyringauth.com/docs/0.x/go/policies#deciding-per-user-and-attempt) or a [pipeline step](https://keyringauth.com/docs/0.x/go/sign-in-flow#the-verification-pipeline) sees the attempt's own `Verifications`, and `Requirement.IsMetBy(verifications)` checks a set against a requirement. See [Verification Properties](https://keyringauth.com/docs/0.x/go/step-up#verification-properties).

## The Confirm Flow

When a request needs step-up, Keyring starts a confirm attempt and redirects to the confirm page. It works like the [login challenge](https://keyringauth.com/docs/0.x/go/sign-in-flow#the-challenge-page), with its own routes, under a session key of its own (`keyring.attempt.confirm`), so options a sign in was given never answer a confirm:

| Method | URI |
|---|---|
| `GET` | `/confirm/{factor?}` |
| `POST` | `/confirm/{factor}` |
| `POST` | `/confirm/{factor}/send` |
| `GET` | `/confirm/sso/redirect` |

The page is `confirm`, with `purpose: "confirm"` and `canTrustDevice: false`. Visiting `/confirm` with no attempt in progress starts one. Each factor is a page of its own (`202` `{ "status": "challenge", "page": "confirm" }` between, with a new session ID), and a passkey that verified the user is both. `/confirm/{factor}/send` sends a code factor's code, and `/confirm/sso/redirect` confirms through an identity provider (see [Single Sign-On](https://keyringauth.com/docs/0.x/go/sso)).

Once the user has verified enough, Keyring answers `200` `{ "status": "authenticated", "redirect" }`, moves the browser to a new session ID and record (a copy of the session from before is signed out), records what it proved, raises the record's level, and dispatches `Confirmed`. Its `redirect` is where the step-up was asked for: the page itself for a `GET` that isn't for JSON, otherwise the last page the session visited when it's on this site, or `Home`.

The confirm page has the sign in's limits: the failures in a row, the limit per IP address, and the timebox. An account with a sign-in method of a factor Keyring for Go can't check never passes a step-up: it gets the `423`, and the confirm page answers the `unavailable` `422`, as at sign in.

## JSON Requests

A JSON request, which isn't an Inertia visit, gets a `423 Locked` instead of a redirect, as in the PHP package:

```json
{
  "message": "Please confirm it's you to continue.",
  "page": "confirm",
  "redirect": "https://example.com/confirm"
}
```

Send the user to `redirect` and let them try again. See [Handling Step-Up in JavaScript](https://keyringauth.com/docs/0.x/go/frontend#handling-step-up-in-javascript).

## Laravel's Password Confirmation

Confirming through Keyring sets `auth.password_confirmed_at` in the session, as in the PHP package, for anything of yours that reads it. Laravel Go has no `password.confirm` middleware to satisfy.

## Events

`Confirmed[U]` is dispatched with `User` and `Factors` (the factors the confirm counted, in the order they were proved) when a confirmation completes. `FactorVerified` and `FactorFailed` fire as they do at login, with `PurposeConfirm`. A wrong code on the confirm page logs `factor_failed` alone, not `second_factor_failed`. See [Events](https://keyringauth.com/docs/0.x/go/events).

## Verification Properties

Each `keyring.Verification` has these fields:

| Field | Type | Meaning |
|---|---|---|
| `Factor` | `string` | The factor's key, such as `password`, `totp`, `passkey` or `recovery`. |
| `AuthenticatorID` | `string` | The stored authenticator's ID, or `""` for a factor with no row of its own, such as the password. |
| `Categories` | `[]string` | What it proved, such as `possession`. |
| `PhishingResistant` | `bool` | Whether it was a passkey. |
| `DeviceBound` | `bool` | Whether the key can't leave its device. |
| `Attested` | `bool` | Whether the key was added with a verified attestation. |
| `At` | `time.Time` | When it was verified, or the zero time when that isn't known. |
| `AuthenticatedAt` | `*time.Time` | When the user really proved it, if earlier than `At`, from an identity provider's `auth_time`. `nil` when it's `At`, and a pointer to the zero time when the provider should have said and didn't, so it never counts as recent. |
| `ProviderAMR`, `ProviderACR` | `[]string`, `string` | What an identity provider's ID token sent. Counted toward nothing. |

## How It Works

Fresh verification's other rules are the PHP package's, as its [How It Works](https://keyringauth.com/docs/0.x/step-up#how-it-works) describes: recovery codes stand in for a lost key beside the password, never alone; a policy the user can't fully meet yet counts as far as they can go; a policy closure is asked about this browser and about one nothing is known of, and the stricter answer stands; the window doesn't slide with activity; and a sign in counts as a verification. The limits on what a copy of a just-signed-in session can add to an account (the password for a first second factor, established authenticators and the freeze) are the same. See [Frozen Sign-In Methods](https://keyringauth.com/docs/0.x/go/authenticators#frozen-sign-in-methods).
