Files

61 lines
1.5 KiB
Go
Raw Permalink Normal View History

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2022-03-30 10:42:47 +02:00
package packages
2022-03-30 10:42:47 +02:00
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"
)
2023-09-05 17:58:30 +02:00
var _ auth.Method = &Auth{}
// Auth is for conan and container
type Auth struct {
AllowGhostUser bool
}
2022-03-30 10:42:47 +02:00
func (a *Auth) Name() string {
return "packages"
2022-03-30 10:42:47 +02:00
}
// 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) {
packageMeta, err := packages.ParseAuthorizationRequest(req)
2022-03-30 10:42:47 +02:00
if err != nil {
log.Trace("ParseAuthorizationToken: %v", err)
return nil, err
2022-03-30 10:42:47 +02:00
}
if packageMeta == nil || packageMeta.UserID == 0 {
2026-02-16 10:57:18 +01:00
return nil, nil //nolint:nilnil // the auth method is not applicable
2022-03-30 10:42:47 +02:00
}
var u *user_model.User
switch packageMeta.UserID {
case user_model.GhostUserID:
if !a.AllowGhostUser {
2026-02-16 10:57:18 +01:00
return nil, nil //nolint:nilnil // the auth method is not applicable
}
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
}
2022-03-30 10:42:47 +02:00
}
if packageMeta.Scope != "" {
store.GetData()["IsApiToken"] = true
store.GetData()["ApiTokenScope"] = packageMeta.Scope
}
2022-03-30 10:42:47 +02:00
return u, nil
2022-03-30 10:42:47 +02:00
}