Repository navigation
Add pagination for token listing - #2833
Conversation
3015bb3 to
d224d1e
Compare
b36cab7 to
c192cf8
Compare
crivetimihai
left a comment
There was a problem hiding this comment.
Thanks for this PR, @madhav165! The pagination implementation is solid — correct total_count via separate count queries, good use of paginate_query in the admin partial, and the team visibility change is well-justified. A few items:
1. SQL wildcard injection via ilike (medium severity)
admin.py new code: EmailApiToken.name.ilike(f"%{q}%") — the q parameter is passed directly without escaping LIKE wildcards (%, _). A user searching for % or _ gets unintended broad matches. The codebase already has _escape_like() in email_auth_service.py:709-718. Please sanitize q the same way:
safe_q = q.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")2. list_all_tokens admin endpoint: total_count stays 0 when user_email is None
The else branch sets tokens = [] and total_count remains 0. This means the admin endpoint cannot paginate all tokens — it returns total: 0 when no user_email filter is provided.
3. N+1 revocation queries in API endpoints
You added get_token_revocations_batch and use it in admin_tokens_partial_html and admin_search_tokens — great improvement. But list_tokens, list_all_tokens, and list_team_tokens still call get_token_revocation per token in a loop. Consider using the batch method there too.
c192cf8 to
d69348b
Compare
cbc6383 to
ff2d6eb
Compare
- Add server-side paginated HTMX endpoint (GET /admin/tokens/partial) - Extract token listing into tokens_partial.html template with OOB pagination - Fix token list auto-refresh after create/revoke - Add count_user_tokens(), list_user_and_team_tokens() to TokenCatalogService - Add /tokens/search API for server-side token search by name - Add alembic migration for token permissions on team_admin role - Batch fetch revocation info to eliminate N+1 queries - Allow team admins to view and manage team tokens Closes #2764 Signed-off-by: Madhav Kandukuri <[email protected]> Signed-off-by: Mihai Criveti <[email protected]>
ff2d6eb to
00753ed
Compare
Rebase & Review NotesRebased onto Fixes applied
Review summaryReviewed for security, performance, correctness, and consistency with existing pagination patterns (teams, tools, servers, prompts, a2a). All looks good:
|
- Add server-side paginated HTMX endpoint (GET /admin/tokens/partial) - Extract token listing into tokens_partial.html template with OOB pagination - Fix token list auto-refresh after create/revoke - Add count_user_tokens(), list_user_and_team_tokens() to TokenCatalogService - Add /tokens/search API for server-side token search by name - Add alembic migration for token permissions on team_admin role - Batch fetch revocation info to eliminate N+1 queries - Allow team admins to view and manage team tokens Closes #2764 Signed-off-by: Madhav Kandukuri <[email protected]> Signed-off-by: Mihai Criveti <[email protected]> Co-authored-by: Mihai Criveti <[email protected]>
- Add server-side paginated HTMX endpoint (GET /admin/tokens/partial) - Extract token listing into tokens_partial.html template with OOB pagination - Fix token list auto-refresh after create/revoke - Add count_user_tokens(), list_user_and_team_tokens() to TokenCatalogService - Add /tokens/search API for server-side token search by name - Add alembic migration for token permissions on team_admin role - Batch fetch revocation info to eliminate N+1 queries - Allow team admins to view and manage team tokens Closes #2764 Signed-off-by: Madhav Kandukuri <[email protected]> Signed-off-by: Mihai Criveti <[email protected]> Co-authored-by: Mihai Criveti <[email protected]> Signed-off-by: Vishu Bhatnagar <[email protected]>
- Add server-side paginated HTMX endpoint (GET /admin/tokens/partial) - Extract token listing into tokens_partial.html template with OOB pagination - Fix token list auto-refresh after create/revoke - Add count_user_tokens(), list_user_and_team_tokens() to TokenCatalogService - Add /tokens/search API for server-side token search by name - Add alembic migration for token permissions on team_admin role - Batch fetch revocation info to eliminate N+1 queries - Allow team admins to view and manage team tokens Closes IBM#2764 Signed-off-by: Madhav Kandukuri <[email protected]> Signed-off-by: Mihai Criveti <[email protected]> Co-authored-by: Mihai Criveti <[email protected]>
- Add server-side paginated HTMX endpoint (GET /admin/tokens/partial) - Extract token listing into tokens_partial.html template with OOB pagination - Fix token list auto-refresh after create/revoke - Add count_user_tokens(), list_user_and_team_tokens() to TokenCatalogService - Add /tokens/search API for server-side token search by name - Add alembic migration for token permissions on team_admin role - Batch fetch revocation info to eliminate N+1 queries - Allow team admins to view and manage team tokens Closes IBM#2764 Signed-off-by: Madhav Kandukuri <[email protected]> Signed-off-by: Mihai Criveti <[email protected]> Co-authored-by: Mihai Criveti <[email protected]> Signed-off-by: Vishu Bhatnagar <[email protected]>
- Add server-side paginated HTMX endpoint (GET /admin/tokens/partial) - Extract token listing into tokens_partial.html template with OOB pagination - Fix token list auto-refresh after create/revoke - Add count_user_tokens(), list_user_and_team_tokens() to TokenCatalogService - Add /tokens/search API for server-side token search by name - Add alembic migration for token permissions on team_admin role - Batch fetch revocation info to eliminate N+1 queries - Allow team admins to view and manage team tokens Closes IBM#2764 Signed-off-by: Madhav Kandukuri <[email protected]> Signed-off-by: Mihai Criveti <[email protected]> Co-authored-by: Mihai Criveti <[email protected]>
✨ Feature / Enhancement PR
🔗 Epic / Issue
Closes #2764
Summary
GET /admin/tokens/partial) for the tokens tab, matching the existing pattern used by teams, tools, and other admin tablestokens_partial.htmltemplate with OOB pagination controlshtmx.ajax()to element-basedhtmx.trigger()which properly processes OOB swapscount_user_tokens()toTokenCatalogServiceand return accuratetotalin the REST API'sTokenListResponseChanges
Backend
mcpgateway/admin.py: NewGET /admin/tokens/partialendpoint with pagination, team filtering, inactive filtering, andrender=controlsmodemcpgateway/services/token_catalog_service.py: Addedcount_user_tokens()for accurate total countsmcpgateway/routers/tokens.py: Usecount_user_tokens()in list endpoint sototalreflects all matching tokens, not just the current pageFrontend
mcpgateway/templates/tokens_partial.html: New partial template (bare cards + OOB pagination div), follows the same pattern asteams_partial.htmlmcpgateway/templates/admin.html: Tokens table usesinnerHTMLswap withrefreshTokenscustom trigger; show-inactive checkbox also usesinnerHTMLmcpgateway/static/admin.js:loadTokensList()setshx-geton#tokens-tableand fireshtmx.trigger(el, "refreshTokens")instead ofhtmx.ajax(); modal dismiss triggers list refreshTests
tests/unit/mcpgateway/routers/test_tokens.py: Added test forcount_user_tokens🧪 Checks
make lintpassesmake testpasses