[PR #5183] [MERGED] fix: accurately retrieve and generate API tokens via CLI with hashed storage (#5145) #5809

Closed
opened 2026-11-02 04:04:13 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/5183
Author: @rqzbeh
Created: 6/10/2026
Status: ✅ Merged
Merged: 6/10/2026
Merged by: @MHSanaei

Base: main ← Head: fix/cli-api-token


📝 Commits (2)

  • 1c04bb2 fix: accurately retrieve and generate API tokens via CLI with hashed storage (#5145)
  • 596ebf9 Merge branch 'main' into fix/cli-api-token

📊 Changes

1 file changed (+18 additions, -1 deletions)

View changed files

📝 main.go (+18 -1)

📄 Description

Fixes #5145

Summary

This PR addresses the silent failure when running /app/x-ui setting -getApiToken\ via CLI.

Since commit \4813a2fe, API tokens are hashed at rest and the plaintext is shown only exactly once at creation time. The CLI command -getApiToken\ was still attempting to print the token value from the database, which is now always empty. In addition, the CLI command was failing to initialize the database, meaning it failed entirely.

To resolve this, the PR implements the following updates to \GetApiToken():

  1. Properly initializes the database connection before making queries.
  2. Identifies if tokens already exist. If they do, it warns the user that existing tokens cannot be recovered in plaintext (as they are hashed).
  3. Gracefully creates a new fallback token (named \cli-fallback-) and immediately prints its plaintext so the user is not left stranded without a usable token if they only have CLI access.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable).
  • \go build ./...\ and the test suite pass locally.
  • For frontend changes:
    pm run lint,
    pm run typecheck, and
    pm run build\ pass.
  • I updated the Wiki / README / API docs if user-facing behavior changed.
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/5183 **Author:** [@rqzbeh](https://github.com/rqzbeh) **Created:** 6/10/2026 **Status:** ✅ Merged **Merged:** 6/10/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/cli-api-token` --- ### 📝 Commits (2) - [`1c04bb2`](https://github.com/MHSanaei/3x-ui/commit/1c04bb284c28a6628db79c4bed16d61c9a31805e) fix: accurately retrieve and generate API tokens via CLI with hashed storage (#5145) - [`596ebf9`](https://github.com/MHSanaei/3x-ui/commit/596ebf9166920903e2c5001fd6a5e45a595e6b8d) Merge branch 'main' into fix/cli-api-token ### 📊 Changes **1 file changed** (+18 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `main.go` (+18 -1) </details> ### 📄 Description Fixes #5145 ## Summary This PR addresses the silent failure when running \/app/x-ui setting -getApiToken\ via CLI. Since commit \4813a2fe\, API tokens are hashed at rest and the plaintext is shown only exactly once at creation time. The CLI command \-getApiToken\ was still attempting to print the token value from the database, which is now always empty. In addition, the CLI command was failing to initialize the database, meaning it failed entirely. To resolve this, the PR implements the following updates to \GetApiToken()\: 1. Properly initializes the database connection before making queries. 2. Identifies if tokens already exist. If they do, it warns the user that existing tokens cannot be recovered in plaintext (as they are hashed). 3. Gracefully creates a new fallback token (named \cli-fallback-<timestamp>\) and immediately prints its plaintext so the user is not left stranded without a usable token if they only have CLI access. ## Type of change - [x] Bug fix - [ ] New feature - [ ] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [ ] Frontend (UI / panel pages) - [ ] Backend (API endpoints, login, settings) - [ ] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [ ] Statistics / traffic counters - [ ] Database / migrations - [x] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [ ] Telegram bot ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [ ] I added or updated tests for the new behavior (when applicable). - [x] \go build ./...\ and the test suite pass locally. - [ ] For frontend changes: \ pm run lint\, \ pm run typecheck\, and \ pm run build\ pass. - [ ] I updated the Wiki / README / API docs if user-facing behavior changed. - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:04:13 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#5809
No description provided.