Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
96 lines
2.7 KiB
Go
96 lines
2.7 KiB
Go
package keycloak
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"slices"
|
|
)
|
|
|
|
// PurgeTenant implements Adapter: it removes the organization whose alias is
|
|
// slug and the accounts that belong to this tenant, found among the org's
|
|
// members and by the extra e-mails (the tenant's first admin, whose org
|
|
// membership may already be gone). An account is only deleted when its
|
|
// tenant_id attribute names this tenant, so a person who also belongs to
|
|
// another workspace is never removed. A missing org or user is not an error.
|
|
func (a *HTTPAdapter) PurgeTenant(ctx context.Context, slug, tenantID string, emails []string) (int, error) {
|
|
var userIDs []string
|
|
orgID, err := a.findOrgByAlias(ctx, slug)
|
|
switch {
|
|
case err == nil:
|
|
var members []memberRepresentation
|
|
resp, err := a.adminCall(ctx, http.MethodGet, fmt.Sprintf("/organizations/%s/members?max=500", orgID), nil, &members)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if resp.StatusCode/100 != 2 {
|
|
_ = resp.Body.Close()
|
|
return 0, fmt.Errorf("list members: %d", resp.StatusCode)
|
|
}
|
|
for _, m := range members {
|
|
userIDs = append(userIDs, m.ID)
|
|
}
|
|
case errors.Is(err, ErrOrgNotFound):
|
|
orgID = ""
|
|
default:
|
|
return 0, err
|
|
}
|
|
for _, e := range emails {
|
|
if e == "" {
|
|
continue
|
|
}
|
|
id, err := a.findUserByEmail(ctx, e)
|
|
if err == nil && !slices.Contains(userIDs, id) {
|
|
userIDs = append(userIDs, id)
|
|
} else if err != nil && !errors.Is(err, errUserLookupMiss) {
|
|
return 0, err
|
|
}
|
|
}
|
|
|
|
removed := 0
|
|
for _, id := range userIDs {
|
|
var u memberRepresentation
|
|
resp, err := a.adminCall(ctx, http.MethodGet, "/users/"+id, nil, &u)
|
|
if err != nil {
|
|
return removed, err
|
|
}
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
_ = resp.Body.Close()
|
|
continue
|
|
}
|
|
if resp.StatusCode/100 != 2 {
|
|
_ = resp.Body.Close()
|
|
return removed, fmt.Errorf("get user: %d", resp.StatusCode)
|
|
}
|
|
if !slices.Contains(u.Attributes["tenant_id"], tenantID) {
|
|
continue
|
|
}
|
|
if err := a.deleteResource(ctx, "/users/"+id); err != nil {
|
|
return removed, fmt.Errorf("delete user: %w", err)
|
|
}
|
|
removed++
|
|
}
|
|
if orgID != "" {
|
|
if err := a.deleteResource(ctx, "/organizations/"+orgID); err != nil {
|
|
return removed, fmt.Errorf("delete org: %w", err)
|
|
}
|
|
}
|
|
return removed, nil
|
|
}
|
|
|
|
// deleteResource DELETEs an admin resource; 404 counts as already gone.
|
|
func (a *HTTPAdapter) deleteResource(ctx context.Context, path string) error {
|
|
resp, err := a.adminCall(ctx, http.MethodDelete, path, nil, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
if resp.StatusCode/100 == 2 || resp.StatusCode == http.StatusNotFound {
|
|
return nil
|
|
}
|
|
body, _ := io.ReadAll(io.LimitReader(resp.Body, 256))
|
|
return fmt.Errorf("%d %s", resp.StatusCode, body)
|
|
}
|