mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-12-06 08:03:46 +00:00
Each user can now hold up to 20 named access tokens in the user_access_tokens table instead of a single token on the user row. Tokens are shown once at creation (nap_ prefix) and stored as a SHA-256 digest. They expire after 30 days by default; a token without expiry is allowed with a warning. A token only reaches the routes its scopes allow. Scopes are picked from the same permission matrix as admin permissions and are limited to what the owner can do; every management route declares the scope it needs, and routes without a declaration reject tokens. Token management itself always requires a browser session. Tokens can complete step-up verification, so verified actions such as changing a password or 2FA settings work with a token; such changes sign out all browser sessions. Disabling a user keeps their tokens, deleting the account removes them. The existing dashboard token keeps working for 30 days after the upgrade. It can be revoked but no longer regenerated, and afterwards it is rejected with ACCESS_TOKEN_LEGACY_RETIRED. The security page lists tokens with their access records, and audit details show scope names.
91 lines
3.8 KiB
Go
91 lines
3.8 KiB
Go
package middleware
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/QuantumNous/new-api/model"
|
|
"github.com/QuantumNous/new-api/service"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// SecureVerificationRequired protects channel key disclosure. Other sensitive
|
|
// operations validate their narrower proof scopes in their controller.
|
|
func SecureVerificationRequired() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
channelID, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil || channelID <= 0 {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"success": false, "code": "SECURITY_CONTEXT_INVALID", "message": service.ErrVerificationContextInvalid.Error()})
|
|
return
|
|
}
|
|
context, err := common.Marshal(service.ChannelKeyReadContext{ChannelID: channelID})
|
|
if err != nil {
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if RequireSecurityProof(c, service.VerificationOperation{Scope: service.VerificationScopeChannelKeyRead, Context: context}) == nil {
|
|
return
|
|
}
|
|
c.Set("secure_verified", true)
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
// RequireSecurityProof validates a proof against the authenticated dashboard
|
|
// session or scoped access token and writes the shared proof error contract on
|
|
// failure.
|
|
func RequireSecurityProof(c *gin.Context, operation service.VerificationOperation) *model.AuthFlowAuthorization {
|
|
identity, ok := GetStepUpIdentity(c)
|
|
if !ok {
|
|
securityProofError(c, "SECURITY_PROOF_INVALID", "安全验证状态无效")
|
|
return nil
|
|
}
|
|
raw := strings.TrimSpace(c.GetHeader("X-Security-Proof"))
|
|
if raw == "" {
|
|
securityProofError(c, "SECURITY_PROOF_REQUIRED", "需要安全验证")
|
|
return nil
|
|
}
|
|
authorization, err := service.ConsumeOperationProof(raw, identity, operation)
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, service.ErrAuthTokenExpired):
|
|
securityProofError(c, "SECURITY_PROOF_EXPIRED", "安全验证已过期")
|
|
case errors.Is(err, service.ErrProofScope):
|
|
securityProofError(c, "SECURITY_PROOF_SCOPE_MISMATCH", "安全验证范围不匹配")
|
|
case errors.Is(err, service.ErrVerificationContextInvalid):
|
|
c.Set("security_error_code", "SECURITY_CONTEXT_INVALID")
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"success": false, "code": "SECURITY_CONTEXT_INVALID", "message": service.ErrVerificationContextInvalid.Error()})
|
|
case errors.Is(err, service.ErrVerificationUnavailable):
|
|
securityProofError(c, "SECURITY_METHOD_UNAVAILABLE", service.ErrVerificationUnavailable.Error())
|
|
case errors.Is(err, service.ErrProofMethod):
|
|
securityProofError(c, "SECURITY_PROOF_METHOD_MISMATCH", "安全验证方式不匹配")
|
|
case errors.Is(err, service.ErrProofConsumed):
|
|
securityProofError(c, "SECURITY_PROOF_CONSUMED", "This verification has already been used. Please verify again.")
|
|
case errors.Is(err, service.ErrProofContext):
|
|
securityProofError(c, "SECURITY_PROOF_CONTEXT_MISMATCH", "Verification does not match this action's details. Please verify again.")
|
|
case errors.Is(err, service.ErrVerificationForbidden):
|
|
securityProofError(c, "SECURITY_ACTION_FORBIDDEN", service.ErrVerificationForbidden.Error())
|
|
case errors.Is(err, service.ErrAuthTokenInvalid), errors.Is(err, service.ErrLoginSessionInvalid), errors.Is(err, service.ErrLoginSessionRevoked), errors.Is(err, model.ErrUserSessionInactive):
|
|
securityProofError(c, "SECURITY_PROOF_INVALID", "安全验证状态无效")
|
|
default:
|
|
_ = c.Error(err)
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"success": false, "code": "AUTH_INTERNAL_ERROR", "message": "Please try again later."})
|
|
}
|
|
return nil
|
|
}
|
|
return authorization
|
|
}
|
|
|
|
func securityProofError(c *gin.Context, code, message string) {
|
|
c.Set("security_error_code", code)
|
|
c.JSON(http.StatusForbidden, gin.H{
|
|
"success": false,
|
|
"message": message,
|
|
"code": code,
|
|
})
|
|
c.Abort()
|
|
}
|