fix(governance): invite a member by email without an identity provider (#2110)
Adding a member by email refused with a 409 whenever no OIDC issuer was set, so an install with local accounts only could not invite anyone by address. An address now reserves the identity under a local issuer instead, and a local account made with that address adopts it: the invitation's roles carry over and the organisation shows one member, not two. A provider subject still needs the provider that asserts it, and the refusal says so.
This commit is contained in:
7 files changed
+147
-7
No files matched your search
@@ -217,7 +217,28 @@ type Subject struct {
|
||||
// EmailSubject is the placeholder binding of a person added by email until
|
||||
// their first sign-in claims it.
|
||||
func EmailSubject(issuer, email string) Subject {
|
||||
return Subject{Issuer: issuer, Subject: "email:" + strings.ToLower(strings.TrimSpace(email))}
|
||||
return Subject{Issuer: issuer, Subject: EmailPlaceholder(email)}
|
||||
}
|
||||
|
||||
// LocalIssuer is the issuer an invitation binds under when no identity provider
|
||||
// is configured. Setting up a local account with the invited address adopts the
|
||||
// identity it holds, so the invitation needs no provider to be claimable.
|
||||
const LocalIssuer = "archie-local"
|
||||
|
||||
// InvitationSubject is the binding an invitation to email holds: the provider
|
||||
// asserts the verified address when one is configured, and the local issuer
|
||||
// stands in when none is.
|
||||
func InvitationSubject(oidcIssuer, email string) Subject {
|
||||
if strings.TrimSpace(oidcIssuer) == "" {
|
||||
oidcIssuer = LocalIssuer
|
||||
}
|
||||
return EmailSubject(oidcIssuer, email)
|
||||
}
|
||||
|
||||
// EmailPlaceholder is the subject half of an invitation binding: the address,
|
||||
// lower-cased and trimmed, behind a prefix that no provider asserts.
|
||||
func EmailPlaceholder(email string) string {
|
||||
return "email:" + strings.ToLower(strings.TrimSpace(email))
|
||||
}
|
||||
|
||||
// Validate checks that s names both an issuer and a subject.
|
||||
|
||||
@@ -121,3 +121,23 @@ func TestUsePersonalToken(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestInvitationNeedsNoProvider pins that an invitation to an address binds
|
||||
// under the local issuer when no identity provider is configured, so a local
|
||||
// account made with that address claims it. It guards #2110: adding a member
|
||||
// by email used to refuse on a provider-less install.
|
||||
func TestInvitationNeedsNoProvider(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, issuer, email string
|
||||
want Subject
|
||||
}{
|
||||
{name: "the provider's issuer when one is configured", issuer: "https://identity.example", email: "Sam@Example.COM", want: Subject{Issuer: "https://identity.example", Subject: "email:sam@example.com"}},
|
||||
{name: "the local issuer when none is", email: " sam@example.com ", want: Subject{Issuer: LocalIssuer, Subject: "email:sam@example.com"}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := InvitationSubject(tc.issuer, tc.email); got != tc.want {
|
||||
t.Fatalf("InvitationSubject(%q, %q) = %#v, want %#v", tc.issuer, tc.email, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -114,7 +114,7 @@ func writeAccountTx(ctx context.Context, tx *sqlx.Tx, p RegisterAccountParams, o
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `
|
||||
INSERT INTO governance.memberships (identity_id, org_id, role) VALUES ($1, $2, $3)
|
||||
ON CONFLICT (identity_id, org_id, COALESCE(workspace_id, '')) DO UPDATE SET role = EXCLUDED.role`,
|
||||
ON CONFLICT (identity_id, org_id, COALESCE(workspace_id, '')) DO NOTHING`,
|
||||
p.IdentityID, p.OrgID, p.Role); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -353,3 +353,25 @@ func (s *Store) SaveAuthenticationSettings(ctx context.Context, row AuthSettings
|
||||
n, err := res.RowsAffected()
|
||||
return n == 1, err
|
||||
}
|
||||
|
||||
// InvitedIdentity returns the identity an invitation to subject reserved, when
|
||||
// the address is still waiting for an account: the placeholder binding holds an
|
||||
// identity with none of its own.
|
||||
func (s *Store) InvitedIdentity(ctx context.Context, subject string) (IdentityRow, bool, error) {
|
||||
var row IdentityRow
|
||||
err := s.db.GetContext(ctx, &row, `
|
||||
SELECT `+identityCols+`
|
||||
FROM governance.identities i
|
||||
JOIN governance.identity_subjects s ON s.identity_id = i.id
|
||||
WHERE s.subject = $1
|
||||
AND NOT EXISTS (SELECT 1 FROM governance.accounts a WHERE a.identity_id = i.id)
|
||||
ORDER BY s.bound_at
|
||||
LIMIT 1`, subject)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return IdentityRow{}, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return IdentityRow{}, false, err
|
||||
}
|
||||
return row, true, nil
|
||||
}
|
||||
@@ -289,12 +289,15 @@ func (a *memberAPI) add(ctx context.Context, in *addMemberInput) (*httpapi.Reply
|
||||
if err != nil {
|
||||
return nil, a.failed(ctx, "read the authentication settings", err)
|
||||
}
|
||||
if settings.OIDCIssuer == "" {
|
||||
return nil, huma.Error409Conflict("Adding a person by email needs an identity provider. Set one in the authentication settings, or create a local account.")
|
||||
// A provider subject means nothing without the provider that asserts it.
|
||||
// An address does: the invitation holds it, and a local account made with
|
||||
// that address claims it.
|
||||
if subject != "" && settings.OIDCIssuer == "" {
|
||||
return nil, huma.Error409Conflict("Adding a person by subject needs an identity provider. Set one in the authentication settings, or add them by email.")
|
||||
}
|
||||
bound, name := Subject{Issuer: settings.OIDCIssuer, Subject: subject}, subject
|
||||
bound, name := InvitationSubject(settings.OIDCIssuer, email), subject
|
||||
if email != "" {
|
||||
bound, name = EmailSubject(settings.OIDCIssuer, email), email
|
||||
bound, name = InvitationSubject(settings.OIDCIssuer, email), email
|
||||
}
|
||||
if display := strings.TrimSpace(in.Body.DisplayName); display != "" {
|
||||
name = display
|
||||
|
||||
@@ -279,3 +279,67 @@ func suspendMember(identity string) func(*memberAPI, context.Context) error {
|
||||
}
|
||||
|
||||
func slicesClone(m []Member) []Member { return append([]Member(nil), m...) }
|
||||
|
||||
// fakePeople stands in for the identity store an invitation writes to.
|
||||
type fakePeople struct {
|
||||
settings AuthenticationSettings
|
||||
created []Identity
|
||||
bound []Subject
|
||||
}
|
||||
|
||||
func (f *fakePeople) ResolveSubject(_ context.Context, s Subject) (Identity, error) {
|
||||
return Identity{}, ErrIdentityNotFound
|
||||
}
|
||||
|
||||
func (f *fakePeople) Create(_ context.Context, i Identity, _ IdentityAudit) (Identity, error) {
|
||||
i.Lifecycle = LifecycleActive
|
||||
f.created = append(f.created, i)
|
||||
return i, nil
|
||||
}
|
||||
|
||||
func (f *fakePeople) BindSubject(_ context.Context, _ IdentityID, s Subject, _ IdentityAudit) error {
|
||||
f.bound = append(f.bound, s)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakePeople) AuthenticationSettings(context.Context) (AuthenticationSettings, error) {
|
||||
return f.settings, nil
|
||||
}
|
||||
|
||||
// TestInviteByEmailNeedsNoProvider guards #2110: an install with local accounts
|
||||
// only could not add a member by email, because the invitation insisted on an
|
||||
// identity provider to assert the address. The address alone reserves the
|
||||
// identity now, and a subject still needs the provider that asserts it.
|
||||
func TestInviteByEmailNeedsNoProvider(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
email string
|
||||
subject string
|
||||
want int
|
||||
wantBound Subject
|
||||
}{
|
||||
{name: "an address is enough", email: "NewComer@Example.com", want: http.StatusOK, wantBound: Subject{Issuer: LocalIssuer, Subject: "email:newcomer@example.com"}},
|
||||
{name: "a subject needs the provider that asserts it", subject: "auth0|1234", want: http.StatusConflict},
|
||||
{name: "neither names a person", want: http.StatusBadRequest},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
dir := &fakeDirectory{members: []Member{member("root", RoleOwner, LifecycleActive)}}
|
||||
people := &fakePeople{}
|
||||
api := memberRoutes(dir)
|
||||
api.people = people
|
||||
in := &addMemberInput{ID: string(DefaultOrgID)}
|
||||
in.Body.Email, in.Body.Subject, in.Body.Role = tt.email, tt.subject, string(RoleViewer)
|
||||
ctx := WithPrincipal(t.Context(), actor("root", RoleOwner))
|
||||
_, err := api.add(ctx, in)
|
||||
if got := status(t, err); got != tt.want {
|
||||
t.Fatalf("status = %d (%v), want %d", got, err, tt.want)
|
||||
}
|
||||
if len(people.bound) != 1 && tt.want == http.StatusOK {
|
||||
t.Fatalf("bound %v, want one binding", people.bound)
|
||||
}
|
||||
if tt.want == http.StatusOK && people.bound[0] != tt.wantBound {
|
||||
t.Fatalf("bound %v, want %v", people.bound[0], tt.wantBound)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -376,6 +376,15 @@ func (m *Module) RegisterAccount(ctx context.Context, r AccountRegistration, boo
|
||||
id := IdentityID(rand.Text())
|
||||
if bootstrap {
|
||||
id = OperatorID()
|
||||
} else if invited, found, err := m.store.InvitedIdentity(ctx, EmailPlaceholder(r.Email)); err != nil {
|
||||
return Identity{}, mapIdentityErr(err)
|
||||
} else if found {
|
||||
// A person invited by this address holds a placeholder binding and no
|
||||
// account yet. Registering with that address is the setup the
|
||||
// invitation asked for, so the account is theirs: a second identity
|
||||
// would leave two members for one person and orphan the roles the
|
||||
// invitation granted.
|
||||
id = IdentityID(invited.ID)
|
||||
}
|
||||
if _, err := NewIdentity(id, KindUser, r.DisplayName); err != nil {
|
||||
return Identity{}, err
|
||||
|
||||
@@ -70,7 +70,8 @@ const visibleMembers = computed(() => selected.value
|
||||
const picked = ref("");
|
||||
const pickedRole = ref<Role>("viewer");
|
||||
// A person who has not signed in yet, named by email or by the provider's
|
||||
// subject. They sign in as this role once the provider vouches for them.
|
||||
// subject. Adding an address reserves it: a local account made with that
|
||||
// address takes over the identity, or the provider's first assertion of it.
|
||||
const person = ref("");
|
||||
const personRole = ref<Role>("viewer");
|
||||
const pending = ref<Member | null>(null);
|
||||
|
||||
Reference in new issue
Block a user