jojo/routers/api/v1/user
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
..
action.go chore(lint): Add exceptions for dbfs_model and unittest (#10275) 2025-12-09 14:34:06 +01:00
app.go feat(api): add admin routes to manage user access tokens (#12323) 2026-05-11 16:55:22 +02:00
avatar.go chore(api): update swagger method descripitons (#8728) 2025-07-30 18:08:28 +02:00
email.go chore(api): update swagger method descripitons (#8728) 2025-07-30 18:08:28 +02:00
follower.go feat: Follow remote users; feed tab (#10380) 2026-04-12 03:31:03 +02:00
gpg_key.go Update Swagger annotations to reflect actual behavior (#9138) 2025-09-15 11:55:49 +02:00
helper.go fix: do visibility check for user redirect lookup 2025-08-30 09:37:25 +02:00
hook.go feat: remove admin-level permissions from repo-specific & public-only access tokens (#11468) 2026-03-04 16:17:41 +01:00
key.go feat: remove admin-level permissions from repo-specific & public-only access tokens (#11468) 2026-03-04 16:17:41 +01:00
quota.go Update Swagger annotations to reflect actual behavior (#9138) 2025-09-15 11:55:49 +02:00
repo.go fix: continued API response processing after error in /repos/search API (#12143) 2026-04-16 17:59:37 +02:00
runners.go feat: add visible flag to HTTP API endpoints that return runners (#11670) 2026-03-14 04:22:01 +01:00
settings.go chore(api): update swagger method descripitons (#8728) 2025-07-30 18:08:28 +02:00
star.go feat: implement fine-grained access tokens in /user/starred & /users/{username}/starred 2026-03-01 17:05:53 +01:00
user.go feat: remove admin-level permissions from repo-specific & public-only access tokens (#11468) 2026-03-04 16:17:41 +01:00
watch.go feat: implement fine-grained access tokens in /user/subscriptions & /users/{username}/subscriptions 2026-03-01 17:05:53 +01:00