Skip to content

Commit 6dc8fab

Browse files
ChristopherHXwxiaoguang
authored andcommitted
Refactor merge conan and container auth preserve actions taskID (go-gitea#36560)
* Remove duplicated code * Allow further ActionsUser package permission checks --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
1 parent 32a03b7 commit 6dc8fab

4 files changed

Lines changed: 32 additions & 60 deletions

File tree

routers/api/packages/api.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func CommonRoutes() *web.Router {
117117
&auth.OAuth2{},
118118
&auth.Basic{},
119119
&nuget.Auth{},
120-
&conan.Auth{},
120+
&Auth{},
121121
&chef.Auth{},
122122
})
123123

@@ -537,7 +537,8 @@ func ContainerRoutes() *web.Router {
537537

538538
verifyAuth(r, []auth.Method{
539539
&auth.Basic{},
540-
&container.Auth{},
540+
// container auth requires an token, so container.Authenticate issues a Ghost user token for anonymous access
541+
&Auth{AllowGhostUser: true},
541542
})
542543

543544
// TODO: Content Discovery / References (not implemented yet)
Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// Copyright 2022 The Gitea Authors. All rights reserved.
1+
// Copyright 2026 The Gitea Authors. All rights reserved.
22
// SPDX-License-Identifier: MIT
33

4-
package conan
4+
package packages
55

66
import (
77
"net/http"
@@ -14,10 +14,13 @@ import (
1414

1515
var _ auth.Method = &Auth{}
1616

17-
type Auth struct{}
17+
// Auth is for conan and container
18+
type Auth struct {
19+
AllowGhostUser bool
20+
}
1821

1922
func (a *Auth) Name() string {
20-
return "conan"
23+
return "packages"
2124
}
2225

2326
// Verify extracts the user from the Bearer token
@@ -32,10 +35,22 @@ func (a *Auth) Verify(req *http.Request, w http.ResponseWriter, store auth.DataS
3235
return nil, nil
3336
}
3437

35-
u, err := user_model.GetUserByID(req.Context(), packageMeta.UserID)
36-
if err != nil {
37-
return nil, err
38+
var u *user_model.User
39+
switch packageMeta.UserID {
40+
case user_model.GhostUserID:
41+
if !a.AllowGhostUser {
42+
return nil, nil
43+
}
44+
u = user_model.NewGhostUser()
45+
case user_model.ActionsUserID:
46+
u = user_model.NewActionsUserWithTaskID(packageMeta.ActionsUserTaskID)
47+
default:
48+
u, err = user_model.GetUserByID(req.Context(), packageMeta.UserID)
49+
if err != nil {
50+
return nil, err
51+
}
3852
}
53+
3954
if packageMeta.Scope != "" {
4055
store.GetData()["IsApiToken"] = true
4156
store.GetData()["ApiTokenScope"] = packageMeta.Scope

routers/api/packages/container/auth.go

Lines changed: 0 additions & 47 deletions
This file was deleted.

services/packages/auth.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,24 @@ type packageClaims struct {
2323
PackageMeta
2424
}
2525
type PackageMeta struct {
26-
UserID int64
27-
Scope auth_model.AccessTokenScope
26+
UserID int64
27+
Scope auth_model.AccessTokenScope
28+
ActionsUserTaskID int64
2829
}
2930

3031
func CreateAuthorizationToken(u *user_model.User, packageScope auth_model.AccessTokenScope) (string, error) {
3132
now := time.Now()
3233

34+
actionsUserTaskID, _ := user_model.GetActionsUserTaskID(u)
3335
claims := packageClaims{
3436
RegisteredClaims: jwt.RegisteredClaims{
3537
ExpiresAt: jwt.NewNumericDate(now.Add(24 * time.Hour)),
3638
NotBefore: jwt.NewNumericDate(now),
3739
},
3840
PackageMeta: PackageMeta{
39-
UserID: u.ID,
40-
Scope: packageScope,
41+
UserID: u.ID,
42+
Scope: packageScope,
43+
ActionsUserTaskID: actionsUserTaskID,
4144
},
4245
}
4346
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)

0 commit comments

Comments
 (0)