Files
new-api/controller/access_token.go
T
CaIon d8cb177440 feat(security): add access token management and audit logs
Move account security settings into a dedicated page and add token status, rotation, revocation, and access history.

Store audit events with role snapshots and JSON metadata, add audit.read authorization and an independent audit page, and upgrade the ClickHouse driver to v2.46.0.
2026-09-06 22:48:58 +08:00

79 lines
2.3 KiB
Go

package controller
import (
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/model"
"github.com/gin-gonic/gin"
"strconv"
)
func GetAccessTokenStatus(c *gin.Context) {
status, err := model.GetUserAccessTokenStatus(c.GetInt("id"))
if err != nil {
common.ApiError(c, err)
return
}
common.ApiSuccess(c, status)
}
func RevokeAccessToken(c *gin.Context) {
ref, err := model.RevokeUserAccessToken(c.GetInt("id"))
if err != nil {
common.ApiError(c, err)
return
}
if ref != "" {
recordUserSecurityAudit(c, c.GetInt("id"), "access_token.revoke", map[string]interface{}{"token_ref": ref})
}
common.ApiSuccess(c, nil)
}
func GetAuditLogs(c *gin.Context) {
page := common.GetPageQuery(c)
if page.Page < 1 || page.PageSize < 1 || page.Page > 100000000 {
common.ApiErrorMsg(c, "Invalid audit pagination")
return
}
filter := model.AuditLogFilter{Username: c.Query("username"), Category: c.Query("category"), TokenRef: c.Query("token_ref"), ExcludeTokenRef: c.Query("exclude_token_ref"), RequestId: c.Query("request_id")}
viewerRole := c.GetInt("role")
if c.FullPath() == "/api/audit/self" {
filter.UserId = c.GetInt("id")
filter.Username = ""
filter.SelfView = true
}
if !model.ValidAuditCategory(filter.Category) || !model.ValidTokenFingerprint(filter.TokenRef) || !model.ValidTokenFingerprint(filter.ExcludeTokenRef) {
common.ApiErrorMsg(c, "Invalid audit filters")
return
}
for name, target := range map[string]*int64{"start_timestamp": &filter.StartTimestamp, "end_timestamp": &filter.EndTimestamp} {
if raw := c.Query(name); raw != "" {
parsed, err := strconv.ParseInt(raw, 10, 64)
if err != nil || parsed < 0 {
common.ApiErrorMsg(c, "Invalid audit time range")
return
}
*target = parsed
}
}
if filter.EndTimestamp > 0 && filter.EndTimestamp < filter.StartTimestamp {
common.ApiErrorMsg(c, "Invalid audit time range")
return
}
if raw := c.Query("success"); raw != "" {
if raw != "true" && raw != "false" {
common.ApiErrorMsg(c, "Invalid audit result")
return
}
success := raw == "true"
filter.Success = &success
}
logs, total, err := model.GetAuditLogs(filter, page.GetStartIdx(), page.GetPageSize(), viewerRole)
if err != nil {
common.ApiError(c, err)
return
}
page.SetItems(logs)
page.SetTotal(int(total))
common.ApiSuccess(c, page)
}