Repository navigation
feat: query usage of token #1161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,11 +1,13 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package controller | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/gin-gonic/gin" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "net/http" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "one-api/common" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "one-api/model" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "strconv" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/gin-gonic/gin" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func GetAllTokens(c *gin.Context) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -106,6 +108,56 @@ func GetTokenStatus(c *gin.Context) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func GetTokenUsage(c *gin.Context) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| authHeader := c.GetHeader("Authorization") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if authHeader == "" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| c.JSON(http.StatusUnauthorized, gin.H{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "success": false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "message": "No Authorization header", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parts := strings.Split(authHeader, " ") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if len(parts) != 2 || strings.ToLower(parts[0]) != "bearer" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| c.JSON(http.StatusUnauthorized, gin.H{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "success": false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "message": "Invalid Bearer token", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tokenKey := parts[1] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| token, err := model.GetTokenByKey(tokenKey, true) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| c.JSON(http.StatusOK, gin.H{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "success": false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "message": err.Error(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expiredAt := token.ExpiredTime | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if expiredAt == -1 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expiredAt = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| c.JSON(http.StatusOK, gin.H{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "code": true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "message": "ok", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "data": gin.H{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "object": "token_usage", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "id": token.Id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "name": token.Name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "total_granted": token.RemainQuota + token.UsedQuota, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "total_used": token.UsedQuota, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "total_available": token.RemainQuota, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "unlimited_quota": token.UnlimitedQuota, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "expires_at": expiredAt, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+120
to
+134
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Align response envelope and expires_at units with existing endpoints Other token endpoints use "success" and return expires_at in milliseconds (see GetTokenStatus). Keep this consistent to avoid client breakage. Apply: - c.JSON(http.StatusOK, gin.H{
- "code": true,
- "message": "ok",
- "data": gin.H{
+ c.JSON(http.StatusOK, gin.H{
+ "success": true,
+ "message": "",
+ "data": gin.H{
"object": "token_usage",
"id": token.Id,
"name": token.Name,
"total_granted": token.RemainQuota + token.UsedQuota,
"total_used": token.UsedQuota,
"total_available": token.RemainQuota,
"unlimited_quota": token.UnlimitedQuota,
- "expires_at": expiredAt,
+ "expires_at": expiredAt * 1000,
},
})📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+86
to
+135
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainAdd endpoint tests for contract and auth behavior Cover: 401 no header (if header-parsing kept), 401 invalid scheme, happy path shape (envelope + ms for expires_at), and ownership enforcement (403) if you continue accepting arbitrary token keys. Proposed verification script to ensure routing and consistency: 🏁 Script executed: #!/bin/bash
# Confirm the route is protected by TokenAuth
rg -nC3 'GetTokenUsage' -- router | cat
rg -n 'TokenAuth\(' router | cat
# Confirm GetTokenStatus returns expires_at in ms for consistency reference
rg -nC2 'GetTokenStatus|expires_at.*\* *1000' controller | catLength of output: 1989 Add tests for Please address the following before merging:
Files/locations to update:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func AddToken(c *gin.Context) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| token := model.Token{} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| err := c.ShouldBindJSON(&token) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Authorization bypass across users — validate ownership and use cache path
Any authenticated user can currently supply any known token key and retrieve its usage. Enforce that the token belongs to the current user. While here, prefer the cache path for lookups.
Apply:
📝 Committable suggestion
🤖 Prompt for AI Agents