feat: support dummy sk- prefix for token (#82)
This commit is contained in:
parent
efc744ca35
commit
73d5e0f283
@ -85,6 +85,8 @@ func RootAuth() func(c *gin.Context) {
|
|||||||
func TokenAuth() func(c *gin.Context) {
|
func TokenAuth() func(c *gin.Context) {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
key := c.Request.Header.Get("Authorization")
|
key := c.Request.Header.Get("Authorization")
|
||||||
|
key = strings.TrimPrefix(key, "Bearer ")
|
||||||
|
key = strings.TrimPrefix(key, "sk-")
|
||||||
parts := strings.Split(key, "-")
|
parts := strings.Split(key, "-")
|
||||||
key = parts[0]
|
key = parts[0]
|
||||||
token, err := model.ValidateUserToken(key)
|
token, err := model.ValidateUserToken(key)
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
_ "gorm.io/driver/sqlite"
|
_ "gorm.io/driver/sqlite"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"one-api/common"
|
"one-api/common"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Token struct {
|
type Token struct {
|
||||||
@ -38,7 +37,6 @@ func ValidateUserToken(key string) (token *Token, err error) {
|
|||||||
if key == "" {
|
if key == "" {
|
||||||
return nil, errors.New("未提供 token")
|
return nil, errors.New("未提供 token")
|
||||||
}
|
}
|
||||||
key = strings.Replace(key, "Bearer ", "", 1)
|
|
||||||
token = &Token{}
|
token = &Token{}
|
||||||
err = DB.Where("`key` = ?", key).First(token).Error
|
err = DB.Where("`key` = ?", key).First(token).Error
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user