summaryrefslogtreecommitdiffstats
path: root/routers/api/packages/nuget/auth.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2022-12-28 13:53:28 +0800
committerGitHub <noreply@github.com>2022-12-28 13:53:28 +0800
commitca67c5a8a72f3d26bb0808ba7a63cd4875cd5229 (patch)
tree8190bc13c43dce1ad3aa78d29920b8e08c0931a8 /routers/api/packages/nuget/auth.go
parent7cc7db73b922143a1288d26b39eee9e8e59f7106 (diff)
downloadgitea-ca67c5a8a72f3d26bb0808ba7a63cd4875cd5229.tar.gz
gitea-ca67c5a8a72f3d26bb0808ba7a63cd4875cd5229.zip
refactor auth interface to return error when verify failure (#22119)
This PR changed the Auth interface signature from `Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *user_model.User` to `Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) (*user_model.User, error)`. There is a new return argument `error` which means the verification condition matched but verify process failed, we should stop the auth process. Before this PR, when return a `nil` user, we don't know the reason why it returned `nil`. If the match condition is not satisfied or it verified failure? For these two different results, we should have different handler. If the match condition is not satisfied, we should try next auth method and if there is no more auth method, it's an anonymous user. If the condition matched but verify failed, the auth process should be stop and return immediately. This will fix #20563 Co-authored-by: KN4CK3R <admin@oldschoolhack.me> Co-authored-by: Jason Song <i@wolfogre.com>
Diffstat (limited to 'routers/api/packages/nuget/auth.go')
-rw-r--r--routers/api/packages/nuget/auth.go9
1 files changed, 5 insertions, 4 deletions
diff --git a/routers/api/packages/nuget/auth.go b/routers/api/packages/nuget/auth.go
index 890c930184..54b33d89c0 100644
--- a/routers/api/packages/nuget/auth.go
+++ b/routers/api/packages/nuget/auth.go
@@ -20,19 +20,20 @@ func (a *Auth) Name() string {
}
// https://docs.microsoft.com/en-us/nuget/api/package-publish-resource#request-parameters
-func (a *Auth) Verify(req *http.Request, w http.ResponseWriter, store auth.DataStore, sess auth.SessionStore) *user_model.User {
+func (a *Auth) Verify(req *http.Request, w http.ResponseWriter, store auth.DataStore, sess auth.SessionStore) (*user_model.User, error) {
token, err := auth_model.GetAccessTokenBySHA(req.Header.Get("X-NuGet-ApiKey"))
if err != nil {
if !(auth_model.IsErrAccessTokenNotExist(err) || auth_model.IsErrAccessTokenEmpty(err)) {
log.Error("GetAccessTokenBySHA: %v", err)
+ return nil, err
}
- return nil
+ return nil, nil
}
u, err := user_model.GetUserByID(req.Context(), token.UID)
if err != nil {
log.Error("GetUserByID: %v", err)
- return nil
+ return nil, err
}
token.UpdatedUnix = timeutil.TimeStampNow()
@@ -40,5 +41,5 @@ func (a *Auth) Verify(req *http.Request, w http.ResponseWriter, store auth.DataS
log.Error("UpdateAccessToken: %v", err)
}
- return u
+ return u, nil
}