signinreq

package
v0.294.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package signinreq is the store behind "Sign in with the muster app" (migration 0174; docs/design/qr-sign-in.md): a signed-out browser starts a REQUEST and shows its code as a QR; a phone already signed in to muster opens the code, is shown the request's details and approves or denies it; the browser that started it redeems the approval for a session of the approver.

The flow, one method each:

  • Create: the browser starts a request. Two secrets come back: the CODE (the QR carries it) and the BINDING (the browser keeps it in an HttpOnly cookie). Only their sha256s are stored.
  • Status: the browser polls by its binding.
  • Open: the phone opened the code (pending -> scanned).
  • Decide: the phone approved or denied (pending|scanned -> approved|denied), recording the approver and the sign-in time of the session that approved.
  • Redeem: the browser spends the approval (approved -> redeemed). It needs the code AND the binding of the SAME row, so a code read off a screen, a log line or a photo cannot be redeemed by another browser.

🔴 EXPIRY IS NOT A STATE: every transition checks expires_at against the database's clock, so a request past it can be neither opened, decided nor redeemed whatever its state says (Status reports it as StateExpired).

🔴 SINGLE USE: each transition is one conditional UPDATE (or a row lock followed by one), so two redeems of one approval cannot both succeed, and a decided request cannot be decided again.

🔴 AN APPROVAL DIES WITH THE SESSION THAT GAVE IT. Redeem refuses an approval whose approver is disabled, or whose approving session's sign-in is at or before the approver's session cutoff (users.sessions_not_before) or latest Sign out everywhere (users.signed_out_at) — the rule a paired phone is held to (devices.LiveSQL), in whole seconds. Where the account rules govern sessions, that is (Redeem's accountRules: gotrue mode, not password mode).

The table has no owner (a request is anonymous until it is approved), so every query runs under authz.System through this package's one helper, sys (ledgered in internal/authz/ledger_test.go), and every query is keyed by the hash of a secret its caller holds.

Index

Constants

View Source
const (
	// TTL is how long a request lives, from its creation: the phone must open
	// and decide it, and the browser redeem it, within this.
	TTL = 2 * time.Minute
	// CodePrefix starts every code; the binding has none (it never leaves an
	// HttpOnly cookie).
	CodePrefix = "mqr_"

	// MaxAgentLen and MaxIPLen bound the details stored for the phone.
	MaxAgentLen = 120
	MaxIPLen    = 64
)

Variables

View Source
var (
	// ErrNotFound: no live request for that secret — unknown, expired, already
	// decided (for Open/Decide), already redeemed, or a code and binding that
	// are not one row's. One error for all of them.
	ErrNotFound = errors.New("signinreq: no such live sign-in request")
	// ErrNotApproved: Redeem of a request the phone has not decided yet.
	ErrNotApproved = errors.New("signinreq: the sign-in request is not approved yet")
	// ErrDenied: Redeem of a request the phone denied.
	ErrDenied = errors.New("signinreq: the sign-in request was denied")
	// ErrApproverEnded: the approver is disabled, or the session that approved
	// has been ended since (cutoff or Sign out everywhere).
	ErrApproverEnded = errors.New("signinreq: the session that approved this sign-in has been ended")
	// ErrNoApprover: Decide with no approver or no sign-in time.
	ErrNoApprover = errors.New("signinreq: an approval needs the approver and its session's sign-in time")
)

Functions

func BindingShapeOK

func BindingShapeOK(s string) bool

BindingShapeOK reports whether s is shaped like a minted binding.

func CodeShapeOK

func CodeShapeOK(s string) bool

CodeShapeOK reports whether s is shaped like a minted code.

func Object

func Object(id int64) string

Object is the audit_log `object` a request's rows carry.

Types

type PGStore

type PGStore struct {
	// contains filtered or unexported fields
}

PGStore is Store over Postgres (migration 0174).

func NewPG

func NewPG(pool *pgxpool.Pool) *PGStore

NewPG returns the store over pool.

func (*PGStore) Create

func (s *PGStore) Create(ctx context.Context, ip, agent string) (string, string, time.Time, error)

Create starts a request for the browser at ip with User-Agent summary agent.

func (*PGStore) Decide

func (s *PGStore) Decide(ctx context.Context, code string, approve bool, by users.Actor, signinAt time.Time) (Request, error)

Decide approves or denies a live, undecided request as by, whose session signed in at signinAt, and audits it in the same transaction.

func (*PGStore) Open

func (s *PGStore) Open(ctx context.Context, code string) (Request, error)

Open marks a live, undecided request as scanned and returns it. Opening it again (a reload) returns it unchanged.

func (*PGStore) Redeem

func (s *PGStore) Redeem(ctx context.Context, code, binding string, accountRules bool) (Request, error)

Redeem spends an approved request for the browser holding binding, and returns it (ApprovedBy is whose session to issue). code and binding must be the SAME row's.

🔴 IT TAKES THE APPROVER'S CREDENTIALS LOCK (users.LockCredentialsTx) before re-checking the approver, as a device pairing does: a disable, a reset or a Sign out everywhere that commits first is seen. ⚠ The lock ends with this transaction, BEFORE the caller has GoTrue issue the session, so one that commits in between is NOT seen here — and the session issued after it would survive it. The caller closes that gap by reading the account again once the session exists (internal/api handleAppSignInRedeem).

accountRules says whether the approver's account rules govern sessions on this server: true in gotrue mode. False in password mode, where the one operator session is a stateless cookie that no users row ends (no Sign out everywhere, no cutoff), so a cutoff column another mode once stamped on user 1 must not refuse it. Measured: the CI e2e leg that also runs the gotrue specs left user 1 signed out everywhere, and a password-mode approval from the still-valid operator session was refused.

func (*PGStore) SameBrowser

func (s *PGStore) SameBrowser(ctx context.Context, code, binding string) (bool, error)

SameBrowser reports whether binding is the binding of the request code names: the browser deciding is the one that started it. False for a malformed or absent binding, or no such request.

func (*PGStore) Status

func (s *PGStore) Status(ctx context.Context, binding string) (Request, error)

Status is the request the browser holding binding started, in its effective state.

type Request

type Request struct {
	ID             int64
	State          State
	RequesterIP    string
	RequesterAgent string
	CreatedAt      time.Time
	ExpiresAt      time.Time
	// ApprovedBy is the approver's user id, 0 until approved.
	ApprovedBy int64
	// ApproverSigninAt is the approving session's sign-in time; nil until
	// approved.
	ApproverSigninAt *time.Time
}

Request is one request's details.

type State

type State string

State is a request's state as Status reports it.

const (
	StatePending  State = "pending"
	StateScanned  State = "scanned"
	StateApproved State = "approved"
	StateDenied   State = "denied"
	StateRedeemed State = "redeemed"
	// StateExpired is never stored: it is any unredeemed row past expires_at.
	StateExpired State = "expired"
)

type Store

type Store interface {
	Create(ctx context.Context, ip, agent string) (code, binding string, expiresAt time.Time, err error)
	Status(ctx context.Context, binding string) (Request, error)
	Open(ctx context.Context, code string) (Request, error)
	Decide(ctx context.Context, code string, approve bool, by users.Actor, signinAt time.Time) (Request, error)
	Redeem(ctx context.Context, code, binding string, accountRules bool) (Request, error)
	SameBrowser(ctx context.Context, code, binding string) (bool, error)
}

Store is the sign-in request store.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL