1
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-02-02 15:09:33 -05:00

feat: GetRunningTaskByToken

This commit is contained in:
Jason Song 2022-11-23 14:24:13 +08:00
parent e346581344
commit 6ad8bddabf
4 changed files with 14 additions and 20 deletions

View File

@ -50,7 +50,7 @@ type Task struct {
Token string `xorm:"-"`
TokenHash string `xorm:"UNIQUE"` // sha256 of token
TokenSalt string
TokenLastEight string `xorm:"token_last_eight"`
TokenLastEight string `xorm:"index token_last_eight"`
LogFilename string // file name of log
LogInStorage bool // read log from database or from storage
@ -237,7 +237,7 @@ func GetTaskByID(ctx context.Context, id int64) (*Task, error) {
return &task, nil
}
func GetTaskByToken(ctx context.Context, token string) (*Task, error) {
func GetRunningTaskByToken(ctx context.Context, token string) (*Task, error) {
errNotExist := fmt.Errorf("task with token %q: %w", token, util.ErrNotExist)
if token == "" {
return nil, errNotExist
@ -270,7 +270,7 @@ func GetTaskByToken(ctx context.Context, token string) (*Task, error) {
}
var tasks []*Task
err := db.GetEngine(ctx).Where("token_last_eight = ?", lastEight).Find(&tasks)
err := db.GetEngine(ctx).Where("token_last_eight = ? AND status = ?", lastEight, StatusRunning).Find(&tasks)
if err != nil {
return nil, err
} else if len(tasks) == 0 {

View File

@ -115,7 +115,7 @@ func addBotTables(x *xorm.Engine) error {
TokenHash string `xorm:"UNIQUE"` // sha256 of token
TokenSalt string
TokenLastEight string `xorm:"token_last_eight"`
TokenLastEight string `xorm:"index token_last_eight"`
LogFilename string // file name of log
LogInStorage bool // read log from database or from storage

View File

@ -110,17 +110,14 @@ func (b *Basic) Verify(req *http.Request, w http.ResponseWriter, store DataStore
}
// check task token
task, err := bots_model.GetTaskByToken(db.DefaultContext, authToken)
task, err := bots_model.GetRunningTaskByToken(db.DefaultContext, authToken)
if err == nil && task != nil {
if task.Status.IsRunning() {
log.Trace("Basic Authorization: Valid AccessToken for task[%d]", task.ID)
log.Trace("Basic Authorization: Valid AccessToken for task[%d]", task.ID)
store.GetData()["IsBotToken"] = true
store.GetData()["BotTaskID"] = task.ID
store.GetData()["IsBotToken"] = true
store.GetData()["BotTaskID"] = task.ID
return user_model.NewBotUser()
}
log.Warn("task %v status is %v but auth request sent: %v", task.ID, task.Status, req.RemoteAddr)
return user_model.NewBotUser()
}
if !setting.Service.EnableBasicAuth {

View File

@ -95,17 +95,14 @@ func (o *OAuth2) userIDFromToken(req *http.Request, store DataStore) int64 {
if err != nil {
if auth_model.IsErrAccessTokenNotExist(err) {
// check task token
task, err := bots_model.GetTaskByToken(db.DefaultContext, tokenSHA)
task, err := bots_model.GetRunningTaskByToken(db.DefaultContext, tokenSHA)
if err == nil && task != nil {
if task.Status.IsRunning() {
log.Trace("Basic Authorization: Valid AccessToken for task[%d]", task.ID)
log.Trace("Basic Authorization: Valid AccessToken for task[%d]", task.ID)
store.GetData()["IsBotToken"] = true
store.GetData()["BotTaskID"] = task.ID
store.GetData()["IsBotToken"] = true
store.GetData()["BotTaskID"] = task.ID
return user_model.BotUserID
}
log.Warn("task %v status is %v but auth request sent: %v", task.ID, task.Status, req.RemoteAddr)
return user_model.BotUserID
}
} else if !auth_model.IsErrAccessTokenNotExist(err) && !auth_model.IsErrAccessTokenEmpty(err) {
log.Error("GetAccessTokenBySHA: %v", err)