mirror of
https://github.com/hanzoai/iam.git
synced 2026-11-19 18:37:16 +00:00
* oidc: link a verified address to the account that holds it A social sign-in whose provider verified the address now links onto the one account holding that address, however that account was made, and signs the person in to it. The link sets the provider subject and EmailVerified; the password, sessions, other sign-in methods, org and roles are unchanged. An address the provider did not verify, or one that names two accounts, is still refused. Each link writes a federation-link audit row in the same transaction, naming the provider, the subject, the client and whether the address was verified before. Claude-Session: https://claude.ai/code/session_016DKtyuzxU43Gsc6Pm9vKTF * oidc: write a federated link only for a vouched, completed sign-in linkOrProvision opens an existing account, by subject or by address, only when the provider vouches for it: a provider filed under a reserved org may open any account outside one; any other provider only accounts of the org that owns it, never a held org's. The link by address is resolved, not written. It rides the resume parameters and federationMint writes it after the account-state and second-factor gates, re-reading the row under its lock: the row must still hold the verified address, still be vouched for, and hold no other subject of that provider. The audit row is appended before the account is written. An account that already holds a different subject of the provider is refused, as /v1/iam/link refuses it. Both address reaches are read, and two different accounts between them are ErrEmailAmbiguous. Claude-Session: https://claude.ai/code/session_016DKtyuzxU43Gsc6Pm9vKTF
184 lines
5.9 KiB
Go
184 lines
5.9 KiB
Go
// Copyright 2026 Hanzo AI, Inc.
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
package users
|
|
|
|
// EmailVerified is a proof the SERVER recorded, and relying parties read it as the
|
|
// account's email_verified claim. A body that could state it would let the sender
|
|
// vouch for an address on the platform's behalf.
|
|
//
|
|
// So the bit is not settable from the body on EITHER write, and on both it is
|
|
// stated by the calling code instead: Create takes CreateInput.EmailVerified,
|
|
// which the federation broker sets and nothing else does; Update carries the
|
|
// stored value.
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/hanzoai/iam/pkg/schema"
|
|
)
|
|
|
|
// A create body cannot state the proof. This is the half a request reaches:
|
|
// the sender writes the row, so a settable bit lets the sender both choose the
|
|
// password on an account and declare its address proven.
|
|
func TestCreate_BodyCannotStateTheProof(t *testing.T) {
|
|
ctx := context.Background()
|
|
api, closeDB := openUsersTestDB(t)
|
|
defer closeDB()
|
|
|
|
created, err := api.Create(ctx, &CreateInput{
|
|
User: schema.User{
|
|
Owner: "hanzo", Name: "trap", Email: "[email protected]",
|
|
EmailVerified: true, // the body says the address was proven
|
|
},
|
|
Password: "the sender's own passphrase",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create: %v", err)
|
|
}
|
|
if created.EmailVerified {
|
|
t.Fatal("a request stated the address was proven and the row believed it")
|
|
}
|
|
// And the stored row, not just the response.
|
|
stored, err := api.lookup(ctx, "hanzo", "trap")
|
|
if err != nil || stored == nil {
|
|
t.Fatalf("read back: %v", err)
|
|
}
|
|
if stored.EmailVerified {
|
|
t.Fatal("the response hid the bit but the row carries it")
|
|
}
|
|
if stored.PasswordHash == "" {
|
|
t.Fatal("premise: the row must carry the sender's digest")
|
|
}
|
|
}
|
|
|
|
// An update body cannot state it either, and an ordinary edit does not un-prove
|
|
// an address the server did prove.
|
|
func TestUpdate_CarriesEmailVerified(t *testing.T) {
|
|
ctx := context.Background()
|
|
api, closeDB := openUsersTestDB(t)
|
|
defer closeDB()
|
|
|
|
unproven, err := api.Create(ctx, &CreateInput{
|
|
User: schema.User{Owner: "hanzo", Name: "alice", Email: "[email protected]"},
|
|
Password: "correct horse battery staple",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create: %v", err)
|
|
}
|
|
if unproven.EmailVerified {
|
|
t.Fatal("a password signup must not record the address as proven")
|
|
}
|
|
|
|
got, err := api.Update(ctx, &UpdateInput{
|
|
Owner: "hanzo", Name: "alice",
|
|
User: schema.User{Owner: "hanzo", Name: "alice", Email: "[email protected]", EmailVerified: true},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("update: %v", err)
|
|
}
|
|
if got.EmailVerified {
|
|
t.Fatal("a request stated the address was proven and the row believed it")
|
|
}
|
|
}
|
|
|
|
// The positive control, and the reason refusing the body is not the same as
|
|
// refusing everyone. The federation broker states its provider's proof through
|
|
// the create's own field, the bit is READABLE on the way back, and an ordinary
|
|
// edit that omits it does not un-prove the address. Without this, "ignore the
|
|
// bit" could be satisfied by ignoring it always — and every federated account
|
|
// would report its address unverified.
|
|
func TestProofIsStatedByTheCallingCode(t *testing.T) {
|
|
ctx := context.Background()
|
|
api, closeDB := openUsersTestDB(t)
|
|
defer closeDB()
|
|
|
|
// This is the shape the federation broker provisions: no password, address
|
|
// proven by the identity provider, stated beside the create.
|
|
proven, err := api.Create(ctx, &CreateInput{
|
|
User: schema.User{Owner: "hanzo", Name: "bob", Email: "[email protected]"},
|
|
Type: "normal-user",
|
|
EmailVerified: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create: %v", err)
|
|
}
|
|
if !proven.EmailVerified {
|
|
t.Fatal("a federated provision could not record the proof it has")
|
|
}
|
|
|
|
got, err := api.Update(ctx, &UpdateInput{
|
|
Owner: "hanzo", Name: "bob",
|
|
User: schema.User{Owner: "hanzo", Name: "bob", Email: "[email protected]", DisplayName: "Bob"},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("update: %v", err)
|
|
}
|
|
if !got.EmailVerified {
|
|
t.Fatal("an ordinary edit un-proved a verified address")
|
|
}
|
|
if got.DisplayName != "Bob" {
|
|
t.Fatalf("the profile edit was lost: %q", got.DisplayName)
|
|
}
|
|
}
|
|
|
|
// The proof is of an ADDRESS, so a write that changes the address drops it.
|
|
//
|
|
// Carrying the bit across an address change lets an org admin turn a proof of
|
|
// their own address into a proof of anyone's: rewrite the row's email to a
|
|
// stranger's, and the account's email_verified claim vouches for the stranger's
|
|
// address.
|
|
func TestUpdate_NewAddressIsUnproven(t *testing.T) {
|
|
ctx := context.Background()
|
|
api, closeDB := openUsersTestDB(t)
|
|
defer closeDB()
|
|
|
|
if _, err := api.Create(ctx, &CreateInput{
|
|
User: schema.User{Owner: "acme", Name: "carol", Email: "[email protected]"},
|
|
Type: "normal-user",
|
|
EmailVerified: true,
|
|
}); err != nil {
|
|
t.Fatalf("create: %v", err)
|
|
}
|
|
|
|
got, err := api.Update(ctx, &UpdateInput{
|
|
Owner: "acme", Name: "carol",
|
|
User: schema.User{Owner: "acme", Name: "carol", Email: "[email protected]"},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("update: %v", err)
|
|
}
|
|
if got.Email != "[email protected]" {
|
|
t.Fatalf("premise: the address must change, got %q", got.Email)
|
|
}
|
|
if got.EmailVerified {
|
|
t.Fatal("the proof of one address was carried onto another")
|
|
}
|
|
|
|
// Restating the same address in another spelling is not a change.
|
|
if _, err := api.Update(ctx, &UpdateInput{
|
|
Owner: "acme", Name: "carol",
|
|
User: schema.User{Owner: "acme", Name: "carol", Email: "[email protected]"},
|
|
}); err != nil {
|
|
t.Fatalf("update back: %v", err)
|
|
}
|
|
if _, err := api.Create(ctx, &CreateInput{
|
|
User: schema.User{Owner: "acme", Name: "dan", Email: "[email protected]"},
|
|
Type: "normal-user",
|
|
EmailVerified: true,
|
|
}); err != nil {
|
|
t.Fatalf("create: %v", err)
|
|
}
|
|
got, err = api.Update(ctx, &UpdateInput{
|
|
Owner: "acme", Name: "dan",
|
|
User: schema.User{Owner: "acme", Name: "dan", Email: " [email protected] "},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("update: %v", err)
|
|
}
|
|
if !got.EmailVerified {
|
|
t.Fatal("a respelling of the same address dropped its proof")
|
|
}
|
|
}
|