Files
iam/internal/oidc/whoami.go
zeekay 942ee0547a impersonate: let a SuperAdmin open a site as a person, marked, for fifteen minutes
POST /v1/iam/impersonate answers a single-use login_hint, never a token.
The site starts its own OIDC sign-in with it (third-party-initiated login),
so the code is bound to the site's client, redirect_uri and PKCE, and is
redeemed at the ordinary token endpoint. Authorize honours the hint only in
a browser holding the operator's own session, and only for a client named
in IAM_IMPERSONATION_APPS (unset: none).

The token is the target's (sub, owner, name, orgs) with act {sub, owner,
name} and imp: true, at most fifteen minutes, with no refresh token. The
rule (operator is a SuperAdmin on the row; target a person in a tenant) is
asked again at the hint, the code, the token and every Guard request.

An imp token reads IAM and writes nothing: the Guard refuses its non-GETs,
self-service writes resolve nobody for it, /signin refuses its code, and an
exchange keeps the mark and the clock. act and imp ride UserInfo and
introspection. Every step and refusal is an ActionImpersonate row naming
the operator, filed under the target's org.

LLM.md records the change of stance and the contract cloud and the sites
must ship before the allow-list names them.

Claude-Session: https://claude.ai/code/session_01HvACxex9ZZTzJCCHgirsGy
2026-10-06 20:08:40 -07:00

54 lines
1.9 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package oidc
import (
"github.com/hanzoai/orm"
"github.com/zap-proto/zip"
"github.com/hanzoai/iam/pkg/store"
)
// GET /v1/iam/whoami — the current caller's identity, lighter than get-account:
// just who you are (owner, name, id, isAdmin), not the full masked profile + org.
// Resolution is the same callerOf as get-account (session cookie first, then bearer
// access token), so one identity is reported whichever credential carried it. An
// anonymous caller gets the casibase {status:"error"} (200), never a leak.
// PathWhoami is the canonical lightweight-identity endpoint.
const PathWhoami = "/v1/iam/whoami"
// whoamiHandler tells you who the current caller is — the lightweight check a
// page makes on load to decide whether to render signed-in or signed-out.
//
// It answers for a session cookie or a bearer token alike, and says plainly when
// nobody is signed in rather than failing.
func whoamiHandler(db orm.DB) zip.Handler {
return func(c *zip.Ctx) error {
ctx := c.Context()
owner, name, ok := readerOf(ctx, c, db)
if !ok {
return c.JSON(200, accountResponse{Status: "error", Msg: "please sign in first"})
}
// The identity essentials. `id`/`sub` is the STABLE subject (the UUID a v2
// token carries), read from the user row; isAdmin/displayName enrich
// best-effort. Absent a user row (a machine token, or a since-deleted user)
// the owner/name is still a truthful identity.
sub := owner + "/" + name
data := map[string]any{"owner": owner, "name": name, "id": sub}
if u, err := store.GetUserByName(ctx, db, owner, name); err == nil && u != nil {
sub = subjectOf(u)
data["id"] = sub
data["isAdmin"] = u.IsAdmin
data["displayName"] = u.DisplayName
}
return c.JSON(200, accountResponse{
Status: "ok",
Sub: sub,
Name: name,
Data: data,
})
}
}