refactor: api token scope check (#38862)

"ApiTokenScope" already means "IsApiToken=true".

All "IsApiToken" should be removed.

---------

Signed-off-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
wxiaoguang
2026-08-11 15:37:44 +08:00
committed by GitHub
parent 7e3eeca779
commit b3f547ac16
12 changed files with 19 additions and 47 deletions
+1 -3
View File
@@ -81,7 +81,6 @@ func (b *Basic) VerifyAuthToken(req *http.Request, w http.ResponseWriter, store
}
store.GetData()["LoginMethod"] = OAuth2TokenMethodName
store.GetData()["IsApiToken"] = true
store.GetData()["ApiTokenScope"] = accessTokenScope
return u, nil
}
@@ -102,7 +101,6 @@ func (b *Basic) VerifyAuthToken(req *http.Request, w http.ResponseWriter, store
}
store.GetData()["LoginMethod"] = AccessTokenMethodName
store.GetData()["IsApiToken"] = true
store.GetData()["ApiTokenScope"] = token.Scope
return u, nil
} else if !errors.Is(err, util.ErrNotExist) {
@@ -187,7 +185,7 @@ func validateTOTP(req *http.Request, u *user_model.User) error {
}
func GetAccessScope(store DataStore) auth_model.AccessTokenScope {
if scope, ok := store.GetData()["ApiTokenScope"].(auth_model.AccessTokenScope); ok {
if scope, hasApiTokenScope := store.GetData()["ApiTokenScope"].(auth_model.AccessTokenScope); hasApiTokenScope {
return scope
}
switch store.GetData()["LoginMethod"] {
-3
View File
@@ -77,9 +77,6 @@ func (h *HTTPSign) Verify(req *http.Request, w http.ResponseWriter, store DataSt
log.Error("GetUserByID: %v", err)
return nil, err
}
store.GetData()["IsApiToken"] = true
log.Trace("HTTP Sign: Logged in user %-v", u)
return u, nil
+1 -4
View File
@@ -106,8 +106,7 @@ func parseToken(req *http.Request) (string, bool) {
}
// userFromToken returns the user corresponding to the OAuth token.
// It will set 'IsApiToken' to true if the token is an API token and
// set 'ApiTokenScope' to the scope of the access token (TODO: this behavior should be fixed, don't set ctx.Data)
// It will set 'ApiTokenScope' to the scope of the access token (TODO: this behavior should be fixed, don't set ctx.Data)
func (o *OAuth2) userFromToken(ctx context.Context, tokenSHA string, store DataStore) (*user_model.User, error) {
// Let's see if token is valid.
if strings.Contains(tokenSHA, ".") {
@@ -121,7 +120,6 @@ func (o *OAuth2) userFromToken(ctx context.Context, tokenSHA string, store DataS
// Otherwise, check if this is an OAuth access token
accessTokenScope, uid := GetOAuthAccessTokenScopeAndUserID(ctx, tokenSHA)
if uid != 0 {
store.GetData()["IsApiToken"] = true
store.GetData()["ApiTokenScope"] = accessTokenScope
}
return user_model.GetUserByID(ctx, uid)
@@ -142,7 +140,6 @@ func (o *OAuth2) userFromToken(ctx context.Context, tokenSHA string, store DataS
if err = auth_model.UpdateAccessToken(ctx, t); err != nil {
log.Error("UpdateAccessToken: %v", err)
}
store.GetData()["IsApiToken"] = true
store.GetData()["ApiTokenScope"] = t.Scope
return user_model.GetUserByID(ctx, t.UID)
}