02db188a50
- The Conan and Container packages use a different type of
authentication. It first authenticates via the regular way (api tokens
or user:password, handled via `auth.Basic`) and then generates a JWT
token that is used by the package software (such as Docker) to do the
action they wanted to do. This JWT token didn't properly propagate the
API scopes that the token was generated for, and thus could lead to a
'scope escalation' within the Conan and Container packages, read
access to write access.
- Store the API scope in the JWT token, so it can be propagated on
subsequent calls that uses that JWT token.
- Integration test added.
- Resolves #5128
(cherry picked from commit 5a871f6095
)
48 lines
1 KiB
Go
48 lines
1 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package conan
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/services/auth"
|
|
"code.gitea.io/gitea/services/packages"
|
|
)
|
|
|
|
var _ auth.Method = &Auth{}
|
|
|
|
type Auth struct{}
|
|
|
|
func (a *Auth) Name() string {
|
|
return "conan"
|
|
}
|
|
|
|
// Verify extracts the user from the Bearer token
|
|
func (a *Auth) Verify(req *http.Request, w http.ResponseWriter, store auth.DataStore, sess auth.SessionStore) (*user_model.User, error) {
|
|
uid, scope, err := packages.ParseAuthorizationToken(req)
|
|
if err != nil {
|
|
log.Trace("ParseAuthorizationToken: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
if uid == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
// Propagate scope of the authorization token.
|
|
if scope != "" {
|
|
store.GetData()["IsApiToken"] = true
|
|
store.GetData()["ApiTokenScope"] = scope
|
|
}
|
|
|
|
u, err := user_model.GetUserByID(req.Context(), uid)
|
|
if err != nil {
|
|
log.Error("GetUserByID: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
return u, nil
|
|
}
|