2022-08-09 16:36:49 +02:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-08-09 16:36:49 +02:00
|
|
|
|
|
|
|
package nuget
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2022-08-25 04:31:57 +02:00
|
|
|
auth_model "code.gitea.io/gitea/models/auth"
|
2022-08-09 16:36:49 +02:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
|
|
"code.gitea.io/gitea/services/auth"
|
|
|
|
)
|
|
|
|
|
2023-09-05 17:58:30 +02:00
|
|
|
var _ auth.Method = &Auth{}
|
|
|
|
|
2022-08-09 16:36:49 +02:00
|
|
|
type Auth struct{}
|
|
|
|
|
|
|
|
func (a *Auth) Name() string {
|
|
|
|
return "nuget"
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://docs.microsoft.com/en-us/nuget/api/package-publish-resource#request-parameters
|
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>
2022-12-28 06:53:28 +01:00
|
|
|
func (a *Auth) Verify(req *http.Request, w http.ResponseWriter, store auth.DataStore, sess auth.SessionStore) (*user_model.User, error) {
|
2023-09-15 08:13:19 +02:00
|
|
|
token, err := auth_model.GetAccessTokenBySHA(req.Context(), req.Header.Get("X-NuGet-ApiKey"))
|
2022-08-09 16:36:49 +02:00
|
|
|
if err != nil {
|
2022-08-25 04:31:57 +02:00
|
|
|
if !(auth_model.IsErrAccessTokenNotExist(err) || auth_model.IsErrAccessTokenEmpty(err)) {
|
2022-08-09 16:36:49 +02:00
|
|
|
log.Error("GetAccessTokenBySHA: %v", err)
|
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>
2022-12-28 06:53:28 +01:00
|
|
|
return nil, err
|
2022-08-09 16:36:49 +02:00
|
|
|
}
|
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>
2022-12-28 06:53:28 +01:00
|
|
|
return nil, nil
|
2022-08-09 16:36:49 +02:00
|
|
|
}
|
|
|
|
|
2022-12-03 03:48:26 +01:00
|
|
|
u, err := user_model.GetUserByID(req.Context(), token.UID)
|
2022-08-09 16:36:49 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error("GetUserByID: %v", err)
|
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>
2022-12-28 06:53:28 +01:00
|
|
|
return nil, err
|
2022-08-09 16:36:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
token.UpdatedUnix = timeutil.TimeStampNow()
|
2023-09-15 08:13:19 +02:00
|
|
|
if err := auth_model.UpdateAccessToken(req.Context(), token); err != nil {
|
2022-08-09 16:36:49 +02:00
|
|
|
log.Error("UpdateAccessToken: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-09-05 09:05:42 +02:00
|
|
|
store.GetData()["IsApiToken"] = true
|
|
|
|
store.GetData()["ApiToken"] = token
|
|
|
|
|
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>
2022-12-28 06:53:28 +01:00
|
|
|
return u, nil
|
2022-08-09 16:36:49 +02:00
|
|
|
}
|