@@ -17,6 +17,7 @@ import (
1717	"code.gitea.io/gitea/modules/setting" 
1818	"code.gitea.io/gitea/modules/timeutil" 
1919	"code.gitea.io/gitea/modules/web/middleware" 
20+ 	"code.gitea.io/gitea/services/actions" 
2021	"code.gitea.io/gitea/services/oauth2_provider" 
2122)
2223
@@ -54,6 +55,18 @@ func CheckOAuthAccessToken(ctx context.Context, accessToken string) int64 {
5455	return  grant .UserID 
5556}
5657
58+ // CheckTaskIsRunning verifies that the TaskID corresponds to a running task 
59+ func  CheckTaskIsRunning (ctx  context.Context , taskID  int64 ) bool  {
60+ 	// Verify the task exists 
61+ 	task , err  :=  actions_model .GetTaskByID (ctx , taskID )
62+ 	if  err  !=  nil  {
63+ 		return  false 
64+ 	}
65+ 
66+ 	// Verify that it's running 
67+ 	return  task .Status  ==  actions_model .StatusRunning 
68+ }
69+ 
5770// OAuth2 implements the Auth interface and authenticates requests 
5871// (API requests only) by looking for an OAuth token in query parameters or the 
5972// "Authorization" header. 
@@ -97,6 +110,16 @@ func parseToken(req *http.Request) (string, bool) {
97110func  (o  * OAuth2 ) userIDFromToken (ctx  context.Context , tokenSHA  string , store  DataStore ) int64  {
98111	// Let's see if token is valid. 
99112	if  strings .Contains (tokenSHA , "." ) {
113+ 		// First attempt to decode an actions JWT, returning the actions user 
114+ 		if  taskID , err  :=  actions .TokenToTaskID (tokenSHA ); err  ==  nil  {
115+ 			if  CheckTaskIsRunning (ctx , taskID ) {
116+ 				store .GetData ()["IsActionsToken" ] =  true 
117+ 				store .GetData ()["ActionsTaskID" ] =  taskID 
118+ 				return  user_model .ActionsUserID 
119+ 			}
120+ 		}
121+ 
122+ 		// Otherwise, check if this is an OAuth access token 
100123		uid  :=  CheckOAuthAccessToken (ctx , tokenSHA )
101124		if  uid  !=  0  {
102125			store .GetData ()["IsApiToken" ] =  true 
0 commit comments