Skip to content

plugins/admin

The operator API under /admin: users, sessions, MFA, organizations, the audit trail, audit drains, statistics, operator grants and operator API keys (pak_…), for a dashboard, a support tool or an integration. Every operator action is recorded in the audit trail as admin.<action>. It needs plugins/audit registered too (any order — Init fails without it).

import (
"github.com/univeros/polaris.go/plugins/admin"
"github.com/univeros/polaris.go/plugins/audit"
)
cfg, err := polaris.FromEnv(db, audit.New(audit.Config{}), admin.New(admin.Config{}))
p, err := polaris.New(cfg)
_, err = p.Migrate(ctx) // creates polaris_admin_grant, polaris_admin_key (and audit's tables)
// the first operator (or CLI: polaris admin:grant <email> <role>, polaris admin:key <name> <role>)
_, err = admin.Of(p.Graph()).Grants().Grant(ctx, admin.Grant{UserID: id, Role: admin.RoleOwner})
_, secret, err := admin.Of(p.Graph()).Keys().Create(ctx, admin.Key{Name: "dashboard", Role: admin.RoleViewer})
// secret is shown once: send it as "Authorization: Bearer pak_…"

admin.Config{} has no options: the zero value is the full behaviour.

A caller is an admin.Principal: a user with core’s superadmin role (owner on the instance, no grant), a user with a live admin.Grant (one per user: role, scope, optional expiry) calling with their access token, or an admin.Key (pak_, role, scope, optional IP allowlist and expiry, stored as a keyed hash). Scope is admin.ScopeInstance or one organization id; an organization-scoped operator acts on that organization only. Roles, each including the ones before it:

RoleAdds capability
RoleViewerRead: users, sessions, MFA, organizations, audit, stats
RoleSupportSupport: revoke sessions
RoleAdminManage (ban, passwords, MFA, member roles, deletions), Impersonate
RoleOwnerOwn: grants, operator keys, drains, other plugins’ /admin routes

Manage on an account (ban, unban, password, MFA removal and reset, delete) stops at operators: an operator below RoleOwner gets 403 admin/forbidden when the account is a superadmin’s or belongs to an operator whose grant is of the caller’s role or above. Only an owner manages those.

MethodPathPurpose
GET/admin/users, /admin/users/{id}list (filters email, status) / read users
POST/admin/users/{id}/ban, /admin/users/{id}/unbandisable / enable an account (Manage)
POST/admin/users/{id}/passwordset a user’s password (Manage)
DELETE/admin/users/{id}delete a user (Manage)
POST/admin/users/{id}/impersonateaccess token for the user, no session (Impersonate)
GET, DELETE/admin/users/{id}/sessionslist / revoke every session (Support to revoke)
DELETE/admin/users/{id}/sessions/{sessionId}revoke one session (Support)
GET, DELETE/admin/users/{id}/mfalist factors / reset MFA (Manage to reset)
DELETE/admin/users/{id}/mfa/{factorId}remove one factor (Manage)
GET/admin/organizations, /admin/organizations/{id}list / read organizations
DELETE/admin/organizations/{id}delete an organization (Manage)
PATCH/admin/organizations/{id}/members/{userId}/rolesset a member’s roles (Manage)
GET, POST/admin/organizations/{id}/drainslist / create audit drains (Own to create)
DELETE/admin/organizations/{id}/drains/{drainId}delete a drain (Own)
POST/admin/organizations/{id}/drains/{drainId}/testsend a test delivery (Own)
GET/admin/auditquery the audit trail
GET/admin/statsinstance statistics
GET, POST/admin/keyslist / create operator API keys (Own)
DELETE/admin/keys/{id}delete a key, effective at once (Own)
GET, POST/admin/grantslist / create operator grants (Own)
DELETE/admin/grants/{id}revoke a grant (Own)

Lists are cursor-paginated: cursor (the previous page’s next_cursor), limit (a positive integer written in digits only, default admin.DefaultLimit, at most admin.MaxLimit; 1.5, 1e1 or 2 answer 422). Times are RFC 3339 in UTC (2026-09-11T10:00:00Z); a time sent with an offset is accepted and answered in UTC.

Records one audit name per action (admin.AuditUserBanned … admin.AuditGrantDeleted, added to the catalog by Plugin.AuditNames); routes that change core state also emit core’s event (user.disabled, user.deleted, org.deleted, mfa.factor_removed, …). Contributes no core permissions: access is the role matrix above, not RBAC.

https://polaris.univeros.io/problems/admin/unauthorized (401, no operator, lapsed grant or key), …/admin/forbidden (403, role or scope too small), …/admin/impersonation_denied (403, an impersonation token), …/admin/not_found (404), …/admin/conflict (409), …/admin/invalid_input (422, errors lists the fields), …/audit/invalid_query (422, /admin/audit).

  • Erasing a user deletes its operator grant inside the erasure’s transaction (wiring.UserEraser).
  • An impersonation token carries amr ["impersonation"], no auth_time, and the admin.ClaimImpersonatedBy / admin.ClaimImpersonatorType claims; it cannot be refreshed and is never an operator. It reads as the user and does what the user’s permissions allow, but it is not the account holder: every route that needs no permission and writes, destroys or is a step-up route answers 403 (pipeline.DeniesImpersonation: password, factors, passkeys, API keys, sessions, organizations created or joined, deletion). token.Token.ImpersonatedBy reads the claim in your own handlers, and audit rows written while serving such a token carry impersonated_by. A superadmin cannot be impersonated, and below RoleOwner neither can an operator of the caller’s role or above (the reach rule above).
  • A session an organization’s identity provider opened (plugins/sso, Principal.OrganizationBound) is never an operator: /admin answers it 401 whatever grant or role its user holds. Operators sign in to the account itself.
  • An organization-scoped owner grants roles on its organization, to users who hold no grant or one on that organization. Replacing a grant on the instance or on another organization, or granting to a superadmin, is for an instance owner (403 admin/forbidden).
  • A drain’s endpoint is an https URL of a public host without credentials; a literal private, loopback or link-local address and localhost are refused (422), and delivery itself connects to public addresses only (plugins/audit). last_error is a category: refused, timeout, unreachable or HTTP <status>.
  • Your own admin route: declare it with tag admin.Tag and auth: public, then start the endpoint with op, res, ok := admin.Authorize(ctx, admin.Own, orgID) and record with admin.Of(g).Record(ctx, name, op, subjectID, orgID, data).
  • Outside /admin: Plugin.OperatorOf(r), Plugin.IsOperator(ctx, credential), Plugin.Resolve(ctx, bearer, ip). A plugin that does not import this package type-asserts g.Plugin("admin") to an interface with AuthorizeOperator / OperatorID.
  • An operator’s access token is checked against the denylist when enabled (a session that ended is no operator’s); a drain’s filter must be a JSON list; dates are ISO 8601 only.
  • One grant per user is enforced by the database (polaris_admin_grant_user_unique on user_id): a grant replaces the user’s previous one, and of two concurrent first grants the second is applied as that replacement. A database from v0.4.1 or earlier may already hold two grants for one user (only that race made them); Migrate then stops with an error naming polaris_admin_grant and user_id. List them with SELECT user_id, id, role, scope FROM polaris_admin_grant WHERE user_id IN (SELECT user_id FROM polaris_admin_grant GROUP BY user_id HAVING COUNT(*) > 1), revoke the grants you do not want (DELETE /admin/grants/{id} or Grants().Revoke(ctx, id)) and run Migrate again.

Example in example_test.go — go doc github.com/univeros/polaris.go/plugins/admin.