d4e8042b94
internal/keycloak/ — Adapter interface with two implementations:
HTTPAdapter cached client-credentials token; CreateOrgAndInvite +
SyncClaims + Health against the real KC Admin API.
Mock in-process map for unit tests + dev convenience when
KEYCLOAK_ADMIN_URL is empty. Used by the eachStore harness.
POST /v1/tenants now accepts admin_email + admin_name. When set, the
adapter creates a KC organization, invites the user as IT_ADMIN, and
triggers VERIFY_EMAIL + UPDATE_PASSWORD. Response wraps the tenant
with TenantCreated{tenant, invite_url}. KC failures DO NOT roll the
tenant back — they emit a keycloak.provision_failed audit event.
Successful invites emit keycloak.invite_sent.
POST /v1/internal/keycloak/claims resolves a tenant's current claim
bundle (tenant_id, slug, products, plan, status). Lookup chain:
body.tenant_id → body.tenant_slug → user_attrs.tenant_id →
user_attrs.tenant_slug.
Config: KEYCLOAK_ADMIN_URL / REALM / CLIENT_ID / CLIENT_SECRET;
empty URL falls back to Mock.
Tests:
internal/keycloak/mock_test.go conflict surfacing, FailNext hook,
SyncClaims persistence.
internal/keycloak/client_test.go HTTPAdapter against an in-process
stub KC: health, full create-org-
and-invite, conflict, token-cache,
401 retry, ErrUnavailable.
internal/server/keycloak_test.go eachStore integration: provisions
via mock; failure path emits
provision_failed audit; claims
endpoint via every lookup variant
+ 404 + 400.
OpenAPI extended with TenantCreated + Claims schemas and the new
claims endpoint. Contract test asserts the new path.
CI: include internal/keycloak/... in the test package list so
HTTPAdapter coverage counts. Total project line coverage: 71.6%.
Refs: M4.3
69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package keycloak
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sync"
|
|
)
|
|
|
|
// Mock is the test-friendly Adapter. Records every call; predictable IDs.
|
|
// Use in unit tests + as the default adapter when KEYCLOAK_BASE_URL is empty
|
|
// (dev convenience).
|
|
type Mock struct {
|
|
mu sync.Mutex
|
|
Orgs map[string]string // tenantID → orgID
|
|
Users map[string]string // email → userID
|
|
Claims map[string]Claims // userID → last synced
|
|
FailNext error // set to force the next call to fail
|
|
}
|
|
|
|
func NewMock() *Mock {
|
|
return &Mock{
|
|
Orgs: map[string]string{},
|
|
Users: map[string]string{},
|
|
Claims: map[string]Claims{},
|
|
}
|
|
}
|
|
|
|
func (m *Mock) Health(_ context.Context) error { return nil }
|
|
|
|
func (m *Mock) CreateOrgAndInvite(_ context.Context, in InviteInput) (*InviteResult, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if m.FailNext != nil {
|
|
err := m.FailNext
|
|
m.FailNext = nil
|
|
return nil, err
|
|
}
|
|
if _, taken := m.Orgs[in.TenantID]; taken {
|
|
return nil, ErrOrgConflict
|
|
}
|
|
if _, taken := m.Users[in.AdminEmail]; taken {
|
|
return nil, ErrUserConflict
|
|
}
|
|
orgID := "mock-org-" + in.Slug
|
|
userID := "mock-user-" + in.AdminEmail
|
|
m.Orgs[in.TenantID] = orgID
|
|
m.Users[in.AdminEmail] = userID
|
|
return &InviteResult{
|
|
OrganizationID: orgID,
|
|
UserID: userID,
|
|
InviteURL: "http://mock-keycloak/invite/" + userID,
|
|
}, nil
|
|
}
|
|
|
|
func (m *Mock) SyncClaims(_ context.Context, userID string, c Claims) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if m.FailNext != nil {
|
|
err := m.FailNext
|
|
m.FailNext = nil
|
|
return err
|
|
}
|
|
if userID == "" {
|
|
return errors.New("mock: user_id required")
|
|
}
|
|
m.Claims[userID] = c
|
|
return nil
|
|
}
|