mirror of
https://github.com/go-gitea/gitea.git
synced 2025-07-21 12:55:23 +02:00
feat: GetRunningTaskByToken
This commit is contained in:
parent
e346581344
commit
6ad8bddabf
@ -50,7 +50,7 @@ type Task struct {
|
|||||||
Token string `xorm:"-"`
|
Token string `xorm:"-"`
|
||||||
TokenHash string `xorm:"UNIQUE"` // sha256 of token
|
TokenHash string `xorm:"UNIQUE"` // sha256 of token
|
||||||
TokenSalt string
|
TokenSalt string
|
||||||
TokenLastEight string `xorm:"token_last_eight"`
|
TokenLastEight string `xorm:"index token_last_eight"`
|
||||||
|
|
||||||
LogFilename string // file name of log
|
LogFilename string // file name of log
|
||||||
LogInStorage bool // read log from database or from storage
|
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
|
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)
|
errNotExist := fmt.Errorf("task with token %q: %w", token, util.ErrNotExist)
|
||||||
if token == "" {
|
if token == "" {
|
||||||
return nil, errNotExist
|
return nil, errNotExist
|
||||||
@ -270,7 +270,7 @@ func GetTaskByToken(ctx context.Context, token string) (*Task, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var tasks []*Task
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else if len(tasks) == 0 {
|
} else if len(tasks) == 0 {
|
||||||
|
@ -115,7 +115,7 @@ func addBotTables(x *xorm.Engine) error {
|
|||||||
|
|
||||||
TokenHash string `xorm:"UNIQUE"` // sha256 of token
|
TokenHash string `xorm:"UNIQUE"` // sha256 of token
|
||||||
TokenSalt string
|
TokenSalt string
|
||||||
TokenLastEight string `xorm:"token_last_eight"`
|
TokenLastEight string `xorm:"index token_last_eight"`
|
||||||
|
|
||||||
LogFilename string // file name of log
|
LogFilename string // file name of log
|
||||||
LogInStorage bool // read log from database or from storage
|
LogInStorage bool // read log from database or from storage
|
||||||
|
@ -110,9 +110,8 @@ func (b *Basic) Verify(req *http.Request, w http.ResponseWriter, store DataStore
|
|||||||
}
|
}
|
||||||
|
|
||||||
// check task token
|
// 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 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()["IsBotToken"] = true
|
||||||
@ -120,8 +119,6 @@ func (b *Basic) Verify(req *http.Request, w http.ResponseWriter, store DataStore
|
|||||||
|
|
||||||
return user_model.NewBotUser()
|
return user_model.NewBotUser()
|
||||||
}
|
}
|
||||||
log.Warn("task %v status is %v but auth request sent: %v", task.ID, task.Status, req.RemoteAddr)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !setting.Service.EnableBasicAuth {
|
if !setting.Service.EnableBasicAuth {
|
||||||
return nil
|
return nil
|
||||||
|
@ -95,9 +95,8 @@ func (o *OAuth2) userIDFromToken(req *http.Request, store DataStore) int64 {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
if auth_model.IsErrAccessTokenNotExist(err) {
|
if auth_model.IsErrAccessTokenNotExist(err) {
|
||||||
// check task token
|
// 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 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()["IsBotToken"] = true
|
||||||
@ -105,8 +104,6 @@ func (o *OAuth2) userIDFromToken(req *http.Request, store DataStore) int64 {
|
|||||||
|
|
||||||
return user_model.BotUserID
|
return user_model.BotUserID
|
||||||
}
|
}
|
||||||
log.Warn("task %v status is %v but auth request sent: %v", task.ID, task.Status, req.RemoteAddr)
|
|
||||||
}
|
|
||||||
} else if !auth_model.IsErrAccessTokenNotExist(err) && !auth_model.IsErrAccessTokenEmpty(err) {
|
} else if !auth_model.IsErrAccessTokenNotExist(err) && !auth_model.IsErrAccessTokenEmpty(err) {
|
||||||
log.Error("GetAccessTokenBySHA: %v", err)
|
log.Error("GetAccessTokenBySHA: %v", err)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user