mirror of
https://github.com/hanzoai/iam.git
synced 2026-11-19 18:37:16 +00:00
Update carried EmailVerified from the stored row whatever the body did to the address, so a write that changed the email kept the old address's proof. The federation broker asks that bit before linking a social identity onto a row, so an admin could rewrite a verified row's address to a stranger's and the stranger's first Google sign-in would be linked onto it. The proof now carries only while the address is the one it was proven for. Co-authored-by: Hanzo Dev <[email protected]>
187 lines
6.3 KiB
Go
187 lines
6.3 KiB
Go
// Copyright 2026 Hanzo AI, Inc.
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
package users
|
|
|
|
// EmailVerified is a proof the SERVER recorded. The federation broker asks it
|
|
// before linking a social identity onto an existing local account: it adopts a row
|
|
// only when that row's own address was proven, or when the row carries no password
|
|
// anybody could already sign in with. A body that could state it would answer that
|
|
// question for the broker — a row carrying a chosen password AND a stated proof
|
|
// passes a check that exists to say no.
|
|
//
|
|
// 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. internal/oidc/federation_proof_test.go runs the whole chain.
|
|
|
|
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 — which would make every
|
|
// federated account unlinkable on its second sign-in.
|
|
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 federation broker, asked whether that address was proven,
|
|
// answers yes and links the stranger's social identity onto the row.
|
|
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")
|
|
}
|
|
}
|