mirror of
https://github.com/hanzoai/iam.git
synced 2026-11-19 18:37:16 +00:00
hanzo.ai asks IAM at api.hanzo.ai, so the EIP-4361 domain was "api.hanzo.ai" and a wallet that checks it against the page warned the person the sign-in was for another site. A nonce asked with a clientId, from an Origin that one of that application's redirect URIs registers (oidc.Landing, the rule the emailed link already uses), now names that origin; otherwise the Host, as before. Verify derives the site by the same function, accepts the burned row when it names the site or the Host, and VerifyProof still expects the burned row's domain. A forged Origin picks only among the application's own origins. The nonce's address is bounded as verify's is, and every mint sweeps expired challenges, 100 rows at most, once per ttl per process. Purge existed and nothing called it, so every anonymous GET left a row. Claude-Session: https://claude.ai/code/session_017vrVus6PPBgP5YRzKV1iqW
195 lines
6.6 KiB
Go
195 lines
6.6 KiB
Go
// Copyright 2026 Hanzo AI, Inc.
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
package wallet
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/hanzoai/orm"
|
|
|
|
"github.com/hanzoai/iam/pkg/schema"
|
|
"github.com/hanzoai/iam/pkg/store"
|
|
)
|
|
|
|
// Persistence for wallet sign-in (HIP-0111): the challenge (mint, atomic burn,
|
|
// purge) and the (chain, address) -> user link.
|
|
//
|
|
// hanzoai/orm has no conditional UPDATE and no UNIQUE constraint, so the two
|
|
// races this flow must lose are closed by transactions here, not by the schema:
|
|
// the burn (one captured proof, two concurrent verifies) and the link (one
|
|
// wallet, two concurrent first-time sign-ins). Both are the ONLY backstop.
|
|
|
|
// owner is the reserved owner of every challenge. A challenge is minted before
|
|
// any application or tenant is known, so the challenge store is global.
|
|
const owner = "built-in"
|
|
|
|
// errChallenge is the ONE opaque failure covering "no such nonce", "already
|
|
// used", and "expired". A prober must not learn which — an oracle that
|
|
// distinguishes them reveals whether a captured nonce was ever real.
|
|
var errChallenge = errors.New("web3: nonce invalid, already used, or expired")
|
|
|
|
// id is the orm key of a challenge: the nonce is the row name.
|
|
func id(nonce string) string { return owner + "/" + nonce }
|
|
|
|
// mint persists a fresh challenge, then sweeps the expired ones. It is the only
|
|
// writer, so the rows it adds are the rows it clears.
|
|
func mint(ctx context.Context, db orm.DB, ch *schema.Challenge) error {
|
|
row := orm.New[schema.Challenge](db)
|
|
model := row.Model
|
|
*row = *ch
|
|
row.Model = model
|
|
row.SetId(id(ch.Name))
|
|
if err := row.CreateCtx(ctx); err != nil {
|
|
return err
|
|
}
|
|
if now, err := time.Parse(time.RFC3339, ch.CreatedTime); err == nil {
|
|
sweep(ctx, db, now)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// batch bounds the rows one sweep deletes, so its cost in the request that runs
|
|
// it is fixed however large a backlog is; the next sweep continues it.
|
|
const batch = 100
|
|
|
|
// swept is the unix second this process last swept expired challenges.
|
|
var swept atomic.Int64
|
|
|
|
// sweep deletes up to batch expired challenges (Purge), at most once per ttl in
|
|
// this process: nothing else removes one. A failure is logged and leaves the rows
|
|
// to the next sweep.
|
|
func sweep(ctx context.Context, db orm.DB, now time.Time) {
|
|
last := swept.Load()
|
|
if now.Unix()-last < int64(ttl/time.Second) || !swept.CompareAndSwap(last, now.Unix()) {
|
|
return
|
|
}
|
|
if _, err := Purge(ctx, db, now, batch); err != nil {
|
|
log.Printf("wallet: sweep: %v", err)
|
|
}
|
|
}
|
|
|
|
// burn atomically consumes a challenge, returning the row iff it existed, was
|
|
// unused, and is unexpired. It is the replay guard, and it MUST run before any
|
|
// cryptographic check: a captured proof then cannot be redeemed twice even when
|
|
// its signature is perfectly valid.
|
|
//
|
|
// The transaction IS the guard. orm exposes no conditional UPDATE (v1 burned
|
|
// with a single `WHERE used=false` UPDATE and read the affected count), so a
|
|
// Get-then-Put would leave a window in which two concurrent verifies of one
|
|
// proof both observe used=false and both win. GetForUpdate takes the row lock
|
|
// for the life of the transaction — it is an error outside one — so the loser
|
|
// blocks until the winner commits and then reads used=true.
|
|
func burn(ctx context.Context, db orm.DB, nonce string, now time.Time) (*schema.Challenge, error) {
|
|
if nonce == "" {
|
|
return nil, errChallenge
|
|
}
|
|
var out *schema.Challenge
|
|
err := db.RunInTransaction(ctx, func(tx orm.DB) error {
|
|
ch, err := orm.GetForUpdate[schema.Challenge](tx, id(nonce))
|
|
if err != nil {
|
|
if errors.Is(err, orm.ErrNotFound) {
|
|
return errChallenge
|
|
}
|
|
return err
|
|
}
|
|
if ch.Used {
|
|
return errChallenge
|
|
}
|
|
// An unparseable expiry is treated as no constraint, matching v1.
|
|
if exp, e := time.Parse(time.RFC3339, ch.ExpireTime); e == nil && now.After(exp) {
|
|
return errChallenge
|
|
}
|
|
ch.Used = true
|
|
if err := ch.UpdateCtx(ctx); err != nil {
|
|
return err
|
|
}
|
|
out = ch
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// Purge deletes up to limit expired challenges and reports how many went. Every
|
|
// anonymous nonce GET writes a row, so without this janitor (sweep) the challenge
|
|
// store grows without bound. Ports v1's PurgeExpiredWeb3Nonces.
|
|
func Purge(ctx context.Context, db orm.DB, now time.Time, limit int) (int, error) {
|
|
stale, err := orm.TypedQuery[schema.Challenge](db).
|
|
Filter("ExpireTime<", now.UTC().Format(time.RFC3339)).
|
|
Limit(limit).
|
|
GetAll(ctx)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
n := 0
|
|
for _, ch := range stale {
|
|
if err := ch.DeleteCtx(ctx); err != nil {
|
|
return n, err
|
|
}
|
|
n++
|
|
}
|
|
return n, nil
|
|
}
|
|
|
|
// link resolves a verified (chain, address) to its wallet when an account of org
|
|
// holds it: one that lives in org, or one an application of org registered, which
|
|
// works in an org of its own once the application has founded it one. The org is a
|
|
// resolved server value (the application's), never a request parameter. Returns
|
|
// (nil, nil) when no wallet is on file, or when another org's account holds it.
|
|
func link(ctx context.Context, db orm.DB, org, chain, address string) (*schema.Wallet, error) {
|
|
if org == "" {
|
|
return nil, nil
|
|
}
|
|
w, err := anywhere(ctx, db, chain, address)
|
|
if err != nil || w == nil || w.Owner == org {
|
|
return w, err
|
|
}
|
|
u, err := store.GetUserByName(ctx, db, w.Owner, w.User)
|
|
if err != nil || u == nil {
|
|
return nil, err
|
|
}
|
|
if ok, err := store.RegisteredIn(ctx, db, org, u); err != nil || !ok {
|
|
return nil, err
|
|
}
|
|
return w, nil
|
|
}
|
|
|
|
// anywhere resolves a verified (chain, address) across EVERY organization. The
|
|
// cross-tenant reach is deliberate and is the point: (chain, address) is
|
|
// globally unique, so this enforces one-wallet-one-identity before a new link is
|
|
// created. It reads no tenant data out — only whether the wallet is spoken for.
|
|
func anywhere(ctx context.Context, db orm.DB, chain, address string) (*schema.Wallet, error) {
|
|
if chain == "" || address == "" {
|
|
return nil, nil
|
|
}
|
|
return first(orm.TypedQuery[schema.Wallet](db).
|
|
Filter("Chain=", chain).Filter("Address=", address))
|
|
}
|
|
|
|
// bind persists a new wallet link.
|
|
func bind(ctx context.Context, db orm.DB, w *schema.Wallet) error {
|
|
row := orm.New[schema.Wallet](db)
|
|
model := row.Model
|
|
*row = *w
|
|
row.Model = model
|
|
row.SetId(w.Owner + "/" + w.User + "/" + w.Chain + "/" + w.Address)
|
|
return row.CreateCtx(ctx)
|
|
}
|
|
|
|
// first collapses orm's not-found into (nil, nil) — absence is not an error at
|
|
// this layer; the caller's policy decides what it means.
|
|
func first(q *orm.ModelQuery[schema.Wallet]) (*schema.Wallet, error) {
|
|
w, err := q.First()
|
|
if errors.Is(err, orm.ErrNotFound) {
|
|
return nil, nil
|
|
}
|
|
return w, err
|
|
}
|