mirror of
https://github.com/hanzoai/iam.git
synced 2026-11-19 18:37:16 +00:00
Adding somebody to a team starts from the email they were typed in as. cloud's
sendInvite resolves the invitee with GET /v1/iam/users/get?owner=<org>&email=<addr>
and this read took {owner, name}, both required, with no email field at all — so
every invite answered 400 field "name" is required and the RPC reported "no Hanzo
account for <addr> in this org". A schema error wearing the message of a business
rule, which is why it read as working-as-intended: of a member, of a stranger, of
anybody, the answer was the same and it sounded correct.
The read now takes Lookup{Owner, Name|Email} and both arms go through store, which
owns what an address IS — the username fold and the email normalization. Restating
either here is how this surface and the login that authenticates the same address
come to name two different people.
An address that names two accounts names none. The store already fails closed on
that (no per-field UNIQUE constraint, so one org can hold two rows on one address)
and the refusal is now REPORTED rather than flattened into not-found: handing back
an arbitrary one of two rows is how a person joins a team under a colleague's
identity. Exactly one handle addresses a person, so neither and both are refused.
Authorization is unchanged by construction. A REST read's target rides in the
query and authz.ReadTarget reads owner/name, so an address read is authorized as
?owner=<org> already was, and it discloses strictly less than the org listing the
same caller may already fetch. Ref keeps both halves required and still addresses
the WRITES: an address is a mutable attribute, and resolving one to decide who
gets written puts a rename between the authorization and the write.
Each guard was watched failing with its own message before it was kept —
flattening the ambiguity, dropping the one-handle check, and reading the address
outside the stated org each fail the new tests.
Co-authored-by: Hanzo Dev <[email protected]>
98 lines
3.6 KiB
Go
98 lines
3.6 KiB
Go
// Copyright 2026 Hanzo AI, Inc.
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
package users
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hanzoai/iam/pkg/schema"
|
|
)
|
|
|
|
// Adding somebody to a team starts from the address they were typed in as, so a
|
|
// read has to accept one. It never accepts an ambiguous one: two rows in an org
|
|
// can carry a single address (the store hangs no per-field UNIQUE constraint),
|
|
// and answering with an arbitrary one of them is how a person joins a team under
|
|
// a colleague's identity.
|
|
|
|
func seedAddress(t *testing.T, api *API, name, email string) {
|
|
t.Helper()
|
|
if _, err := api.Create(context.Background(), &CreateInput{
|
|
User: schema.User{Owner: "acme", Name: name, Email: email},
|
|
Password: "correct horse battery staple",
|
|
}); err != nil {
|
|
t.Fatalf("seed %s: %v", name, err)
|
|
}
|
|
}
|
|
|
|
func TestGetByEmailResolvesTheSamePersonAsByName(t *testing.T) {
|
|
api := New(consentTestDB(t))
|
|
seedAddress(t, api, "alice", "[email protected]")
|
|
|
|
byName, err := api.Get(context.Background(), &Lookup{Owner: "acme", Name: "alice"})
|
|
if err != nil {
|
|
t.Fatalf("get by name: %v", err)
|
|
}
|
|
// The address is stored normalized and matched normalized, so the spelling the
|
|
// caller happens to hold — a colleague pasting it out of an email client — is
|
|
// the same principal. Restating the rule here instead of calling store's would
|
|
// be how this surface and login come to disagree about who an address names.
|
|
byEmail, err := api.Get(context.Background(), &Lookup{Owner: "acme", Email: " [email protected] "})
|
|
if err != nil {
|
|
t.Fatalf("get by email: %v", err)
|
|
}
|
|
if byEmail.Name != byName.Name || byEmail.Owner != byName.Owner {
|
|
t.Fatalf("address and username named different people: %s/%s vs %s/%s",
|
|
byEmail.Owner, byEmail.Name, byName.Owner, byName.Name)
|
|
}
|
|
if byEmail.PasswordHash != "" || byEmail.AccessSecret != "" {
|
|
t.Fatal("the address read skipped the redaction the username read applies")
|
|
}
|
|
}
|
|
|
|
func TestGetByEmailRefusesAnAddressThatNamesTwoAccounts(t *testing.T) {
|
|
api := New(consentTestDB(t))
|
|
seedAddress(t, api, "alice", "[email protected]")
|
|
seedAddress(t, api, "alice2", "[email protected]")
|
|
|
|
u, err := api.Get(context.Background(), &Lookup{Owner: "acme", Email: "[email protected]"})
|
|
if err == nil {
|
|
t.Fatalf("an address naming two accounts resolved to %s/%s", u.Owner, u.Name)
|
|
}
|
|
if !strings.Contains(err.Error(), "more than one account") {
|
|
t.Fatalf("the refusal does not say why: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestGetRefusesNeitherHandleAndBoth(t *testing.T) {
|
|
api := New(consentTestDB(t))
|
|
seedAddress(t, api, "alice", "[email protected]")
|
|
|
|
for _, in := range []*Lookup{
|
|
{Owner: "acme"},
|
|
{Owner: "acme", Name: "alice", Email: "[email protected]"},
|
|
} {
|
|
if _, err := api.Get(context.Background(), in); err == nil {
|
|
t.Fatalf("Lookup{Name:%q, Email:%q} was accepted; exactly one handle addresses a person", in.Name, in.Email)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A read is scoped to one organization the same way whichever handle it carries.
|
|
// The address form must not become a way to reach across tenants — the caller
|
|
// states the org, and an address in another one is simply not there.
|
|
func TestGetByEmailNeverLeavesTheStatedOrg(t *testing.T) {
|
|
api := New(consentTestDB(t))
|
|
if _, err := api.Create(context.Background(), &CreateInput{
|
|
User: schema.User{Owner: "hanzo", Name: "alice", Email: "[email protected]"},
|
|
Password: "correct horse battery staple",
|
|
}); err != nil {
|
|
t.Fatalf("seed hanzo/alice: %v", err)
|
|
}
|
|
if u, err := api.Get(context.Background(), &Lookup{Owner: "acme", Email: "[email protected]"}); err == nil {
|
|
t.Fatalf("an address resolved out of org acme into %s/%s", u.Owner, u.Name)
|
|
}
|
|
}
|