Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions routers/api/packages/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func CommonRoutes() *web.Router {
&auth.OAuth2{},
&auth.Basic{},
&nuget.Auth{},
&conan.Auth{},
&Auth{},
&chef.Auth{},
})

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

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

// TODO: Content Discovery / References (not implemented yet)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package conan
package packages

import (
"net/http"
Expand All @@ -14,10 +14,13 @@ import (

var _ auth.Method = &Auth{}

type Auth struct{}
// Auth is for conan and container
type Auth struct {
AllowGhostUser bool
}

func (a *Auth) Name() string {
return "conan"
return "packages"
}

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

u, err := user_model.GetUserByID(req.Context(), packageMeta.UserID)
if err != nil {
return nil, err
var u *user_model.User
switch packageMeta.UserID {
case user_model.GhostUserID:
if !a.AllowGhostUser {
return nil, nil
}
u = user_model.NewGhostUser()
case user_model.ActionsUserID:
u = user_model.NewActionsUserWithTaskID(packageMeta.ActionsUserTaskID)
default:
u, err = user_model.GetUserByID(req.Context(), packageMeta.UserID)
if err != nil {
return nil, err
}
}

if packageMeta.Scope != "" {
store.GetData()["IsApiToken"] = true
store.GetData()["ApiTokenScope"] = packageMeta.Scope
Expand Down
47 changes: 0 additions & 47 deletions routers/api/packages/container/auth.go

This file was deleted.

11 changes: 7 additions & 4 deletions services/packages/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,24 @@ type packageClaims struct {
PackageMeta
}
type PackageMeta struct {
UserID int64
Scope auth_model.AccessTokenScope
UserID int64
Scope auth_model.AccessTokenScope
ActionsUserTaskID int64
}

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

actionsUserTaskID, _ := user_model.GetActionsUserTaskID(u)
claims := packageClaims{
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(now.Add(24 * time.Hour)),
NotBefore: jwt.NewNumericDate(now),
},
PackageMeta: PackageMeta{
UserID: u.ID,
Scope: packageScope,
UserID: u.ID,
Scope: packageScope,
ActionsUserTaskID: actionsUserTaskID,
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
Expand Down