platform icon indicating copy to clipboard operation
platform copied to clipboard

feat: API token management in workspace settings

Open dnplkndll opened this issue 8 months ago • 8 comments

Summary

Workspace-scoped API tokens with a built-in REST API, managed from workspace settings. Tokens are JWTs with configurable expiry (7–365 days) and optional coarse scopes. Expiry and revocation are enforced centrally so the policy is reusable across services.

What changed

API token management (server/account/)

  • ApiToken type + apiToken collection (Mongo + Postgres). Migration V27 creates api_tokens (including the scopes TEXT[] column).
  • RPC methods: createApiToken, listApiTokens, revokeApiToken, plus owner-scoped listWorkspaceApiTokens / revokeWorkspaceApiToken.
  • Creation/revocation require AccountRole.User or higher (not guests); per-account token cap.

Centralized token verification (server-token) — addresses @aonnikov's review

  • New verifyToken() alongside decodeToken(): verifies signature, checks expiry, and — for revokable API tokens — revocation via a pluggable checker (setApiTokenRevocationChecker). Reusable by any service (transactor, blob access, …); the 60s revocation cache now lives here.
  • The account is authoritative: wrap() rejects revoked/expired API tokens, so existing methods (selectWorkspace, getWorkspaceInfo, …) naturally return 401. The redundant checkApiTokenRevoked RPC was removed; the transactor's checker reuses the existing getLoginInfoByToken.

Scope enforcement (pods/server/src/rpc.ts) — Phase 1

  • Coarse scopes read:* / write:* / delete:*, enforced per method. Scopes are parsed once per request and threaded through (no re-decode in the tx handler). No scopes = full access (legacy-compatible).

Frontend (plugins/setting-resources/)

  • ApiTokens.svelte settings page; ApiTokenCreatePopup.svelte (i18n dropdowns for scope preset + expiry); ApiDocsSection.svelte.
  • REST base URL is derived from the account-provided transactor endpoint (ws→http), per @aonnikov — not constructed from window.location.

i18n

  • API token strings added and translated across all locales (cs/de/es/fr/it/ja/pt/pt-br/ru/tr/zh). en/ru locale-parity test passes.

Docs

  • openapi.yaml describing the REST API.

Test plan

  • [x] Unit: verifyToken (signature/expiry/revocation), scope helpers, scope enforcement, token CRUD.
  • [ ] Read-only token → find-all works, tx returns 403.
  • [ ] Read & Write → tx works, TxRemoveDoc returns 403; Full Access → all ops work.
  • [ ] Legacy token (no scopes) → full access.
  • [ ] Revoke a token → rejected within the cache TTL (~60s); account returns 401.
  • [ ] V27 migration runs on fresh and existing databases.

Ref: #10622

dnplkndll avatar Mar 11 '26 14:03 dnplkndll

Connected to Huly®: UBERF-15850

huly-github-staging avatar Mar 11 '26 14:03 huly-github-staging

Follow-up: REST API should apply sensible defaults for TxCreateDoc

While testing the token management flow end-to-end (mint token → create issues via REST), I discovered that POST /api/v1/tx/{workspace} passes TxCreateDoc transactions through with zero default-filling. Issues created this way are missing fields that the UI auto-populates, causing them to not appear in project views (only in all-issues).

Missing fields for tracker:class:Issue

When the UI creates an issue, it uses addCollection() which wraps the TxCreateDoc in a TxCollectionCUD, setting:

  • attachedTo: tracker:ids:NoParent
  • attachedToClass: tracker:class:Issue
  • collection: "subIssues"

Plus these attributes that get no server-side defaults:

  • kind → must be tracker:taskTypes:Issue (not tracker:ids:ClassingProjectType)
  • childInfo: [], parents: [], subIssues: 0, comments: 0, reports: 0
  • assignee: null, component: null, dueDate: null
  • estimation: 0, remainingTime: 0, reportedTime: 0

Impact

Issues created via bare TxCreateDoc (the natural REST usage) are invisible in project views because the Svelte live query filters on attachedTo/collection. They show up in the all-issues aggregated view only.

Possible approaches

  1. Document it — update REST API docs to show the required TxCollectionCUD wrapper pattern for issue creation
  2. Server-side defaults middleware — a new middleware in the tx pipeline that merges missing fields from the model schema for known classes (significant arch change)
  3. Higher-level REST endpoints — add POST /api/v1/create-issue/{workspace} that accepts a simplified payload and constructs the full TxCollectionCUD server-side, like the importer does

Option 3 seems most API-friendly without changing the platform's "caller provides everything" philosophy. Happy to contribute this if there's interest.

dnplkndll avatar Mar 13 '26 13:03 dnplkndll

Hi @dnplkndll Thank you for your contribution. Could you please check formatting for server/account/src/operations.ts? The corresponding CI step is failed: https://github.com/hcengineering/platform/actions/runs/23164707679/job/67348370400?pr=10624 You can use rush format to format all project files or rushx format for a specific package.

ArtyomSavchenko avatar Mar 17 '26 06:03 ArtyomSavchenko

@ArtyomSavchenko Thanks for the review! Formatting has been fixed — the issue was a prettier version mismatch (local 3.8.1 vs project's 3.6.2). New code now matches the existing codebase style with no formatting noise on existing lines.

Changes in this update

3 commits:

  1. feat: API token management — core feature (create/list/revoke tokens, UI, DB, i18n)
  2. docs: token scope roadmap — roadmap comments for future scope-based access control
  3. feat: enforce API token revocation — addresses a gap where revoked tokens remained usable until JWT expiry

What's new since last push

  • Token revocation enforcement — the transactor now checks a per-token revocation cache (60s TTL) before granting API access. Revoked tokens are rejected within ~60 seconds instead of remaining valid until expiry.
  • Owner-level workspace token visibility — workspace owners can list and revoke any member's API tokens via listWorkspaceApiTokens / revokeWorkspaceApiToken (OWNER role required).
  • Ledo instance URL removed — openapi.yaml now uses a generic https://{host} server variable.

Suggestions for future consideration

  1. Token scopes — the roadmap commit documents a proposed extra.scopes JWT field for fine-grained access control (read-only tokens, class-restricted access). This would enable safe MCP/AI agent integrations without MCP-layer changes.

  2. Higher-level REST endpoints — as noted in my earlier comment, POST /api/v1/tx requires the caller to construct full TxCollectionCUD wrappers. A POST /api/v1/create-issue style endpoint would make the API much more accessible for integrations.

  3. Revocation cache scaling — the current per-token cache works well for moderate usage. For high-traffic deployments, a Redis-backed revocation set or short-lived tokens (< 1 hour) with refresh would be more robust.

dnplkndll avatar Mar 17 '26 15:03 dnplkndll

Hi @dnplkndll Could you please resolve conflicts?

ArtyomSavchenko avatar Mar 26 '26 03:03 ArtyomSavchenko

sure what is your opinion on the use of MCP versus api? My motivation was that I would prefer to have tool access in a controlled predicable / scriptable way with permissions on things. then you can test on a staging build out script you can repeat. there are a few things I ran into with the api that the UI handles that should probably be refined, like defaults, too. to simplify not putting records in a bad state.

dnplkndll avatar Mar 26 '26 12:03 dnplkndll

Thanks for the contribution Overall the idea is great, but need to fix architectural and UI issues.

We also have a button to generate token on workspace settings page, we will need to remove it in favor of the new approach.

aonnikov avatar Apr 07 '26 06:04 aonnikov

Thanks for the reviews — pushed an update addressing the feedback. Conflicts with develop are resolved (merged current develop; the API token migration is renumbered to V27 after develop's V26 pending_configuration).

@aonnikov

  • Revocation logic / redundant boolean method: reworked. Token expiry + revocation now live in a new verifyToken() next to decodeToken() in server-token, with a pluggable checker (setApiTokenRevocationChecker) so the policy is reusable across services. The account is now authoritative — wrap() rejects revoked/expired API tokens, so selectWorkspace/getWorkspaceInfo/etc. return 401 naturally. The dedicated checkApiTokenRevoked RPC is removed; the transactor's checker reuses the existing getLoginInfoByToken.
  • Transactor endpoint in ApiDocsSection: fixed — the REST base URL is now derived from the account-provided transactor endpoint (ws→http), matching the ServerManagerGeneral pattern, instead of window.location.
  • Manual translations in the create popup: it uses DropdownLabelsIntl with i18n strings (no hand-rolled translations).

@ArtyomSavchenko

  • Role restriction: token creation/revocation now require AccountRole.User or higher via verifyAllowedRole(...) (guests excluded).
  • Locale tests / untranslated strings: en/ru parity restored and the API token strings are now translated across all locales (cs/de/es/fr/it/ja/pt/pt-br/ru/tr/zh).
  • Formatting: re-ran the project formatter; operations.ts and the scope test are clean.

Unit tests added for verifyToken (signature/expiry/revocation) and scope enforcement. Happy to iterate further.

dnplkndll avatar May 30 '26 19:05 dnplkndll