Files
zeekay f9a1d15fd5 oidc: hold a sign-in to what its authorize request demanded
The session branch learned prompt from the page, so a page that
forwarded nothing still got a code from an old session: hanzo.ai's
resume for the CLI answered prompt=login and prompt=select_account from
whatever it found, and max_age reached neither the page nor the branch.

Authorize now records what a request it hands to the hosted page
demanded (prompt=login, select_account or several accounts with none
named, max_age) as a LoginChallenge of kind prompt, named by the
request's client, redirect, state and PKCE challenge, for thirty
minutes.  The session branch and a silent re-entry of the same request
both read it: login is met only by a sign-in made after the request,
choose only by a named account, max_age by a sign-in that recent.  A
store fault refuses.  sessions.Open restamps auth_time only when a
credential was just shown; a resume (resumeGrant) keeps the old one.

Review fixes: logout_hint ends every account it names (an address two
orgs share names both, so admin/alice is never the one left signed
in); the sign-out walk's chain is sealed with a MAC, so a crafted link
cannot skip an origin; the issuer is compared in sign-in origin
spelling, so https://hanzo.id:443 is not a second stop; and device
approval binds the account its page named.

TestBindHasOneCaller walked a symlinked root, which WalkDir does not
descend; CI runs the gates from one, so it found no caller there.

Claude-Session: https://claude.ai/code/session_015XYKLnBbrbajqj1GpRydF8
2026-11-07 13:07:35 -08:00

106 lines
2.8 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package principal_test
import (
"go/ast"
"go/parser"
"go/token"
"io/fs"
"path/filepath"
"strconv"
"strings"
"testing"
)
// path is this package, as an import names it.
const path = "github.com/hanzoai/iam/internal/principal"
// caller is where authentication lives: the one place in the tree that resolves a
// bearer, and therefore the one place that may say who a request acts as.
const caller = "internal/authz/authz.go"
// Bind is the only way a principal enters a request context, and Scope reads
// whatever it finds there — so a second caller of Bind is a second answer to
// "who is this?", one of them taken without verifying anything. The ctx key is
// unexported, which leaves Bind as the whole surface; this walks the tree and
// holds it to its one caller.
//
// It reads the SOURCE rather than the package graph on purpose. A widening is
// somebody writing principal.Bind somewhere new, and that is exactly what a
// syntax walk sees.
func TestBindHasOneCaller(t *testing.T) {
root, err := filepath.Abs("../..")
if err != nil {
t.Fatal(err)
}
// CI runs the gates from a symlink to the checkout, and WalkDir does not
// descend a root that is a link: it visited one entry and found no caller.
if root, err = filepath.EvalSymlinks(root); err != nil {
t.Fatal(err)
}
fset := token.NewFileSet()
var found []string
err = filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
switch d.Name() {
case ".git", "node_modules", "web":
return fs.SkipDir
}
return nil
}
if !strings.HasSuffix(p, ".go") || strings.HasSuffix(p, "_test.go") {
return nil
}
f, err := parser.ParseFile(fset, p, nil, 0)
if err != nil {
return err
}
// What this file calls the package, if it imports it at all.
name := ""
for _, im := range f.Imports {
v, err := strconv.Unquote(im.Path.Value)
if err != nil || v != path {
continue
}
name = "principal"
if im.Name != nil {
name = im.Name.Name
}
}
if name == "" {
return nil
}
ast.Inspect(f, func(n ast.Node) bool {
call, ok := n.(*ast.CallExpr)
if !ok {
return true
}
sel, ok := call.Fun.(*ast.SelectorExpr)
if !ok || sel.Sel.Name != "Bind" {
return true
}
if id, ok := sel.X.(*ast.Ident); ok && id.Name == name {
rel, _ := filepath.Rel(root, p)
found = append(found, rel)
}
return true
})
return nil
})
if err != nil {
t.Fatal(err)
}
if len(found) != 1 || found[0] != caller {
t.Fatalf("Bind is called from %v, want exactly [%s].\n"+
"A principal enters a request context where a bearer was verified and nowhere "+
"else — every Scope answer downstream is only as good as that.", found, caller)
}
}