jojo/templates
steven.guiheux ba1c3e0288 feat(api): add admin routes to manage user access tokens (#12323)
# Feature Request: Admin API route to manage access tokens for any user
## Problem
The existing API route to create access tokens (POST /api/v1/users/{username}/tokens) requires Basic authentication (username + password) via the reqBasicOrRevProxyAuth() middleware. This is by design: a token should not be created from another token.

However, this creates a blocker for environments where Basic authentication is disabled (ENABLE_BASIC_AUTHENTICATION = false), typically when authentication is delegated to an external SSO provider (e.g., OpenID Connect).

In such setups, bot/service accounts are provisioned by an external system that needs to:

Create a user via POST /api/v1/admin/users (works fine with an admin token)
Create an access token for that user (currently impossible without Basic auth or direct CLI/DB access)
The only workaround today is to SSH into the Forgejo server and run:

This is not suitable when the provisioning system has no direct access to the Forgejo host.

## Proposed solution
Add new admin-only API routes under the existing /api/v1/admin/users/{username} group to manage access tokens:

| Method |	Route |	Description |
|:-------- |:--------:| --------:|
| GET	| /api/v1/admin/users/{username}/tokens |	List access tokens for a user|
|POST	| /api/v1/admin/users/{username}/tokens |	Create an access token for a user|
|DELETE |	/api/v1/admin/users/{username}/tokens/{id} |	Delete an access token for a user|

These routes would:

Require a site admin token (reqToken() + reqSiteAdmin()) — no Basic auth needed
Use the AccessTokenScopeCategoryAdmin token scope
Reuse the existing handler logic from user.CreateAccessToken / user.ListAccessTokens / user.DeleteAccessToken
Accept the same request/response payloads as the existing user-facing routes

### Why this belongs in the admin API
It follows the existing pattern: admins can already create users, repos, orgs, SSH keys, and emails for any user via the admin API
It does not weaken security: only site administrators can call it, and it requires a valid admin-scoped token
It fills a gap: the admin CLI command forgejo admin user generate-access-token already provides this capability, but only locally

<!--start release-notes-assistant-->

## Release notes
<!--URL:https://codeberg.org/forgejo/forgejo-->
- Features
  - [PR](https://codeberg.org/forgejo/forgejo/pulls/12323): <!--number 12323 --><!--line 0 --><!--description ZmVhdChhcGkpOiBhZGQgYWRtaW4gcm91dGVzIHRvIG1hbmFnZSB1c2VyIGFjY2VzcyB0b2tlbnM=-->feat(api): add admin routes to manage user access tokens<!--description-->
<!--end release-notes-assistant-->

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12323
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
2026-05-11 16:55:22 +02:00
..
admin feat: migrate show-modal to native dialogs (#10287) 2026-05-03 06:42:14 +02:00
api/packages/pypi Update templates/api/packages/pypi/simple.tmpl 2024-04-12 16:43:30 +00:00
base feat: Follow remote users; feed tab (#10380) 2026-04-12 03:31:03 +02:00
custom [FEAT] Repository flags 2024-02-05 16:09:42 +01:00
demo chore: dialog modal max-width rendering failure (#12469) 2026-05-08 08:01:34 +02:00
explore fix(ui): use octicon-repo-forked in repo list (#10227) 2025-11-26 11:21:20 +01:00
htmx fix(ui): Make 'Clear milestone' work with HTMX (#8266) 2025-08-11 20:10:10 +02:00
mail i18n(mailer): Fix special usage of .Locale in admin_new_user (#12009) 2026-04-14 07:20:16 +02:00
moderation feat: replace cross origin protection (#9830) 2025-10-29 22:43:22 +01:00
org fix: Disable spellcheck on repo name field (#12506) 2026-05-10 21:42:21 +02:00
package feat: migrate show-modal to native dialogs (#10287) 2026-05-03 06:42:14 +02:00
projects feat: migrate show-modal to native dialogs (#10287) 2026-05-03 06:42:14 +02:00
repo feat: make it possible to remove workflow runs (#12478) 2026-05-11 16:02:36 +02:00
shared feat: migrate show-modal to native dialogs (#10287) 2026-05-03 06:42:14 +02:00
status fix(ui): remove extra helpers from statuspages (#9753) 2025-10-20 12:03:58 +02:00
swagger feat(api): add admin routes to manage user access tokens (#12323) 2026-05-11 16:55:22 +02:00
user chore(i18n): move 89 strings to JSON (#12280) 2026-04-27 16:07:51 +02:00
webhook chore: rename SafeHTML to TrustHTML (#11481) 2026-03-08 02:41:37 +01:00
home.tmpl Split Forgejo landing page template to allow patching or removing Forgejo introduction section (#6675) 2025-02-03 08:41:52 +00:00
home_forgejo.tmpl Split Forgejo landing page template to allow patching or removing Forgejo introduction section (#6675) 2025-02-03 08:41:52 +00:00
install.tmpl Added alt's to <img> 2025-01-31 21:22:00 +00:00
post-install.tmpl [BRANDING] Custom loading animation for Forgejo 2024-02-05 16:02:13 +01:00