feat(webui): add a member by provider subject or email
POST /api/orgs/{id}/members binds a person by subject, or by email as a
placeholder. The first sign-in whose provider vouches for that email
claims it: the State Store moves the binding onto the provider's subject
in one statement, so a second account with the same address finds
nothing to claim and later sign-ins rest on the subject alone.
This commit is contained in:
1 parent
9e1a40ce99
commit
37cbf2ef47
17 files changed
+2085
-1505
No files matched your search
@@ -1,9 +1,11 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
@@ -206,6 +208,112 @@ func (s *Server) handleOrgMemberSet(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, map[string]any{"membership": membership})
|
||||
}
|
||||
|
||||
// handleOrgMemberAdd adds a person by the subject their provider asserts or by
|
||||
// their email, creating the identity and binding it so that person's next
|
||||
// sign-in resolves to it with this role. An already-bound subject keeps its
|
||||
// identity and only gains the membership.
|
||||
func (s *Server) handleOrgMemberAdd(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.orgReady(w) {
|
||||
return
|
||||
}
|
||||
id, ok := s.orgInPath(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if s.Subjects == nil || s.Identities == nil || s.Issuer == "" {
|
||||
http.Error(w, "adding a person needs a configured identity provider", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
request, bound, name, ok := s.decodeMemberAdd(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
existing, err := s.Subjects.ResolveSubject(r.Context(), bound)
|
||||
if err != nil && !errors.Is(err, identity.ErrNotFound) {
|
||||
writeIdentityError(w, err)
|
||||
return
|
||||
}
|
||||
unbound := err != nil
|
||||
membership := org.Membership{
|
||||
IdentityID: existing.ID, OrgID: id,
|
||||
WorkspaceID: org.WorkspaceID(request.Workspace), Role: org.Role(request.Role),
|
||||
}
|
||||
if unbound {
|
||||
if membership.IdentityID, err = randomIdentityID(); err != nil {
|
||||
http.Error(w, "cannot create identity ID", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
if !s.mayChangeMembership(w, r, membership, membership.Role) {
|
||||
return
|
||||
}
|
||||
if unbound && !s.createBoundIdentity(w, r, membership.IdentityID, name, bound) {
|
||||
return
|
||||
}
|
||||
if err := s.Orgs.EnsureMembership(r.Context(), membership); err != nil {
|
||||
writeOrgError(w, err)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
writeJSON(w, map[string]any{"membership": membership})
|
||||
}
|
||||
|
||||
type memberAddRequest struct {
|
||||
Subject string `json:"subject"`
|
||||
Email string `json:"email"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Role string `json:"role"`
|
||||
Workspace string `json:"workspace"`
|
||||
}
|
||||
|
||||
// decodeMemberAdd reads and validates an add-member body, returning the
|
||||
// provider subject it names and the display name to give a new identity. It
|
||||
// has written the error when ok is false.
|
||||
func (s *Server) decodeMemberAdd(w http.ResponseWriter, r *http.Request) (request memberAddRequest, bound identity.Subject, name string, ok bool) {
|
||||
request, ok = decodeBody[memberAddRequest](w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
name = strings.TrimSpace(request.DisplayName)
|
||||
subject, email := strings.TrimSpace(request.Subject), strings.TrimSpace(request.Email)
|
||||
switch {
|
||||
case (subject == "") == (email == ""):
|
||||
http.Error(w, "give either a subject or an email", http.StatusBadRequest)
|
||||
return request, bound, name, false
|
||||
case subject != "":
|
||||
bound, name = identity.Subject{Issuer: s.Issuer, Subject: subject}, cmp.Or(name, subject)
|
||||
default:
|
||||
bound, name = identity.EmailSubject(s.Issuer, email), cmp.Or(name, email)
|
||||
}
|
||||
if err := org.Role(request.Role).Validate(); err != nil {
|
||||
writeOrgError(w, err)
|
||||
return request, bound, name, false
|
||||
}
|
||||
return request, bound, name, true
|
||||
}
|
||||
|
||||
// createBoundIdentity makes a user identity and binds subject to it, having
|
||||
// written the error and returned false when either step fails.
|
||||
func (s *Server) createBoundIdentity(w http.ResponseWriter, r *http.Request, id identity.IdentityID, name string, subject identity.Subject) bool {
|
||||
audit, err := webAudit(r.Context())
|
||||
if err != nil {
|
||||
http.Error(w, "cannot create request ID", http.StatusInternalServerError)
|
||||
return false
|
||||
}
|
||||
value, err := identity.New(id, identity.KindUser, name)
|
||||
if err == nil {
|
||||
_, err = s.Identities.Create(r.Context(), value, audit)
|
||||
}
|
||||
if err == nil {
|
||||
err = s.Subjects.BindSubject(r.Context(), id, subject, audit)
|
||||
}
|
||||
if err != nil {
|
||||
writeIdentityError(w, err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *Server) handleOrgMemberRemove(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.orgReady(w) {
|
||||
return
|
||||
|
||||
@@ -2,6 +2,7 @@ package webui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -111,6 +112,7 @@ func orgRoutes(s *Server) http.Handler {
|
||||
mux.HandleFunc("GET /api/orgs/{id}/workspaces", s.handleOrgWorkspacesList)
|
||||
mux.HandleFunc("POST /api/orgs/{id}/workspaces", s.handleOrgWorkspaceCreate)
|
||||
mux.HandleFunc("GET /api/orgs/{id}/members", s.handleOrgMembersList)
|
||||
mux.HandleFunc("POST /api/orgs/{id}/members", s.handleOrgMemberAdd)
|
||||
mux.HandleFunc("PUT /api/orgs/{id}/members/{identity}", s.handleOrgMemberSet)
|
||||
mux.HandleFunc("DELETE /api/orgs/{id}/members/{identity}", s.handleOrgMemberRemove)
|
||||
mux.HandleFunc("PUT /api/orgs/{id}/agents/{identity}", s.handleOrgAgentAssign)
|
||||
@@ -281,3 +283,144 @@ func TestInstanceAdminOrgs(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// fakeSubjects is an in-memory identity store with subject bindings.
|
||||
type fakeSubjects struct {
|
||||
identity.Repository
|
||||
byID map[identity.IdentityID]identity.Identity
|
||||
bindings map[identity.Subject]identity.IdentityID
|
||||
}
|
||||
|
||||
func (f *fakeSubjects) Create(_ context.Context, v identity.Identity, _ identity.Audit) (identity.Identity, error) {
|
||||
f.byID[v.ID] = v
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (f *fakeSubjects) ResolveSubject(_ context.Context, s identity.Subject) (identity.Identity, error) {
|
||||
if id, ok := f.bindings[s]; ok {
|
||||
return f.byID[id], nil
|
||||
}
|
||||
return identity.Identity{}, identity.ErrNotFound
|
||||
}
|
||||
|
||||
func (f *fakeSubjects) ClaimSubject(_ context.Context, placeholder, s identity.Subject) (identity.Identity, error) {
|
||||
id, ok := f.bindings[placeholder]
|
||||
if !ok {
|
||||
return identity.Identity{}, identity.ErrNotFound
|
||||
}
|
||||
delete(f.bindings, placeholder)
|
||||
f.bindings[s] = id
|
||||
return f.byID[id], nil
|
||||
}
|
||||
|
||||
func (f *fakeSubjects) BindSubject(_ context.Context, id identity.IdentityID, s identity.Subject, _ identity.Audit) error {
|
||||
f.bindings[s] = id
|
||||
return nil
|
||||
}
|
||||
|
||||
// TestOrgMemberAdd holds adding a person by provider subject or email: it
|
||||
// needs the admin grant, owner needs an owner, one identifier is required, and
|
||||
// the person who was added signs in as the identity the membership names, by
|
||||
// subject or by the provider's verified email.
|
||||
func TestOrgMemberAdd(t *testing.T) {
|
||||
const issuer = "https://idp.example"
|
||||
engine := infraaccess.New(access.ShippedOrgPolicies(org.DefaultOrgID))
|
||||
members := "/api/orgs/" + string(org.DefaultOrgID) + "/members"
|
||||
tests := []struct {
|
||||
name string
|
||||
caller string
|
||||
body string
|
||||
wantStatus int
|
||||
signIn identity.Credential
|
||||
wantRole org.Role
|
||||
// later is a second account showing the same verified email after
|
||||
// the first sign-in claimed it; it must not sign in.
|
||||
later identity.Credential
|
||||
}{
|
||||
{
|
||||
"an admin adds by subject", "admin", `{"subject":"sub-1","role":"developer"}`, http.StatusCreated,
|
||||
identity.Credential{Subject: identity.Subject{Issuer: issuer, Subject: "sub-1"}},
|
||||
org.RoleDeveloper,
|
||||
identity.Credential{},
|
||||
},
|
||||
{
|
||||
"an admin adds by email and the verified email signs in", "admin", `{"email":"Ann@Example.com","role":"viewer"}`, http.StatusCreated,
|
||||
identity.Credential{Subject: identity.Subject{Issuer: issuer, Subject: "unseen"}, Email: "ann@example.com"},
|
||||
org.RoleViewer,
|
||||
identity.Credential{Subject: identity.Subject{Issuer: issuer, Subject: "impostor"}, Email: "ann@example.com"},
|
||||
},
|
||||
{
|
||||
"an unverified email does not sign in", "admin", `{"email":"ann@example.com","role":"viewer"}`, http.StatusCreated,
|
||||
identity.Credential{Subject: identity.Subject{Issuer: issuer, Subject: "unseen"}},
|
||||
"",
|
||||
identity.Credential{},
|
||||
},
|
||||
{"an admin cannot add an owner", "admin", `{"subject":"sub-1","role":"owner"}`, http.StatusForbidden, identity.Credential{}, "", identity.Credential{}},
|
||||
{
|
||||
"an owner adds an owner", "owner", `{"subject":"sub-1","role":"owner"}`, http.StatusCreated,
|
||||
identity.Credential{Subject: identity.Subject{Issuer: issuer, Subject: "sub-1"}},
|
||||
org.RoleOwner,
|
||||
identity.Credential{},
|
||||
},
|
||||
{"a developer cannot add", "developer", `{"subject":"sub-1","role":"viewer"}`, http.StatusForbidden, identity.Credential{}, "", identity.Credential{}},
|
||||
{"both identifiers are refused", "admin", `{"subject":"a","email":"a@b.c","role":"viewer"}`, http.StatusBadRequest, identity.Credential{}, "", identity.Credential{}},
|
||||
{"neither identifier is refused", "admin", `{"role":"viewer"}`, http.StatusBadRequest, identity.Credential{}, "", identity.Credential{}},
|
||||
{"an unknown role is refused", "admin", `{"subject":"sub-1","role":"superuser"}`, http.StatusBadRequest, identity.Credential{}, "", identity.Credential{}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
orgs := newFakeOrgs()
|
||||
store := &fakeSubjects{byID: map[identity.IdentityID]identity.Identity{}, bindings: map[identity.Subject]identity.IdentityID{}}
|
||||
s := &Server{
|
||||
Access: engine, Principals: rolePrincipals{}, Orgs: orgs,
|
||||
Identities: store, Subjects: store, Issuer: issuer,
|
||||
Authenticate: func(_ context.Context, token string) (identity.Identity, error) {
|
||||
return identity.Identity{ID: identity.IdentityID(token), Kind: identity.KindUser}, nil
|
||||
},
|
||||
}
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, members, strings.NewReader(tt.body))
|
||||
req.Header.Set("Authorization", "Bearer "+tt.caller)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rec := httptest.NewRecorder()
|
||||
orgRoutes(s).ServeHTTP(rec, req)
|
||||
if rec.Code != tt.wantStatus {
|
||||
t.Fatalf("status = %d, want %d, body %s", rec.Code, tt.wantStatus, rec.Body.String())
|
||||
}
|
||||
if tt.wantStatus != http.StatusCreated {
|
||||
if len(store.byID) != 0 {
|
||||
t.Fatalf("a refused request created %d identities", len(store.byID))
|
||||
}
|
||||
return
|
||||
}
|
||||
got, err := identity.Resolve(t.Context(), store, tt.signIn)
|
||||
if tt.wantRole == "" {
|
||||
if !errors.Is(err, identity.ErrSubjectUnbound) {
|
||||
t.Fatalf("Resolve = %v, want ErrSubjectUnbound", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("Resolve: %v", err)
|
||||
}
|
||||
var role org.Role
|
||||
for _, m := range orgs.members[org.DefaultOrgID] {
|
||||
if m.IdentityID == got.ID {
|
||||
role = m.Role
|
||||
}
|
||||
}
|
||||
if role != tt.wantRole {
|
||||
t.Fatalf("signed-in role = %q, want %q", role, tt.wantRole)
|
||||
}
|
||||
if tt.later.Subject.Subject == "" {
|
||||
return
|
||||
}
|
||||
if _, err := identity.Resolve(t.Context(), store, tt.later); !errors.Is(err, identity.ErrSubjectUnbound) {
|
||||
t.Fatalf("second account with the same email: Resolve = %v, want ErrSubjectUnbound", err)
|
||||
}
|
||||
again, err := identity.Resolve(t.Context(), store, identity.Credential{Subject: tt.signIn.Subject})
|
||||
if err != nil || again.ID != got.ID {
|
||||
t.Fatalf("first account by subject alone = %v, %v, want %s", again.ID, err, got.ID)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -134,6 +134,12 @@ type Server struct {
|
||||
Principals access.PrincipalSource
|
||||
Denials access.DenialRecorder
|
||||
|
||||
// Subjects binds a provider subject to an identity, and Issuer is the
|
||||
// provider those subjects are asserted under. Nil or empty answers the
|
||||
// add-member route Unavailable.
|
||||
Subjects identity.SubjectBinding
|
||||
Issuer string
|
||||
|
||||
// Orgs serves the org pages: the caller's org, its workspaces, members and
|
||||
// agent assignments. Nil answers the org routes Unavailable.
|
||||
Orgs org.API
|
||||
@@ -333,6 +339,7 @@ func (s *Server) registerConfigAndLogRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /api/orgs/{id}/workspaces", s.handleOrgWorkspacesList)
|
||||
mux.HandleFunc("POST /api/orgs/{id}/workspaces", s.handleOrgWorkspaceCreate)
|
||||
mux.HandleFunc("GET /api/orgs/{id}/members", s.handleOrgMembersList)
|
||||
mux.HandleFunc("POST /api/orgs/{id}/members", s.handleOrgMemberAdd)
|
||||
mux.HandleFunc("PUT /api/orgs/{id}/members/{identity}", s.handleOrgMemberSet)
|
||||
mux.HandleFunc("DELETE /api/orgs/{id}/members/{identity}", s.handleOrgMemberRemove)
|
||||
mux.HandleFunc("PUT /api/orgs/{id}/agents/{identity}", instanceOnly(s.handleOrgAgentAssign))
|
||||
|
||||
Reference in new issue
Block a user