Files
iam/internal/oidc/federation_proof_test.go
zeekayandGitHub 12fdb51464 oidc: link a verified address to the account that holds it (#28)
* 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
2026-11-01 23:51:19 -08:00

57 lines
2.1 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package oidc
// A federated provision records the address proof its provider gave, on the
// create's own field (users.CreateInput.EmailVerified), and a second identity on
// that verified address signs in to the same account.
import (
"testing"
"github.com/hanzoai/iam/pkg/schema"
"github.com/hanzoai/iam/pkg/store"
)
// A genuine federated provision records the proof its provider gave and carries
// no password, and another provider's identity on the same verified address
// resolves to the account rather than being refused or duplicated.
func TestFederation_ProvisionRecordsTheProviderProof(t *testing.T) {
app, db := newServer(t)
seedApp(t, db, appOpts{clientID: "webapp", signup: true, redirectURIs: []string{testRedirect}})
m := newMockOIDC(t, fedGoogleCID)
seedOIDCProvider(t, db, "webapp", m)
runOIDCLogin(t, app, db, m, "webapp", nil)
provisioned, err := store.GetUserByEmail(tctx(), db, "hanzo", m.email)
if err != nil || provisioned == nil {
t.Fatalf("read back the provisioned account: %v", err)
}
if !provisioned.EmailVerified {
t.Fatal("the provider proved the address and the row does not say so")
}
if provisioned.PasswordHash != "" {
t.Fatal("a federated account must carry no password")
}
before := countUsers(t, db)
app2, _ := store.GetApplicationByClientId(tctx(), db, "webapp")
prov, _ := store.GetProvider(tctx(), db, "admin", fedProvGoogle)
github := &schema.Provider{Type: "GitHub"}
github.Owner, github.Name = prov.Owner, "github"
linked, link, err := linkOrProvision(tctx(), db, app2, github, federatedIdentity{
subject: "github-sub-second-identity", email: m.email, emailVerified: true,
})
if err != nil {
t.Fatalf("a verified address refused a second identity: %v", err)
}
if linked.Name != provisioned.Name || link == nil {
t.Fatalf("linked onto %q (link %+v), want the existing account %q", linked.Name, link, provisioned.Name)
}
if countUsers(t, db) != before {
t.Fatal("a second row was created on an address one account already holds")
}
}