@@ -6,7 +6,6 @@ package auth
6
6
7
7
import (
8
8
"context"
9
- "errors"
10
9
"net/http"
11
10
"strings"
12
11
"time"
@@ -17,7 +16,6 @@ import (
17
16
"code.gitea.io/gitea/modules/log"
18
17
"code.gitea.io/gitea/modules/setting"
19
18
"code.gitea.io/gitea/modules/timeutil"
20
- "code.gitea.io/gitea/modules/util"
21
19
"code.gitea.io/gitea/modules/web/middleware"
22
20
"code.gitea.io/gitea/services/actions"
23
21
"code.gitea.io/gitea/services/oauth2_provider"
@@ -57,6 +55,18 @@ func CheckOAuthAccessToken(ctx context.Context, accessToken string) int64 {
57
55
return grant .UserID
58
56
}
59
57
58
+ // CheckTaskID verifies that the TaskID corresponds to a running task
59
+ func CheckTaskID (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
+
60
70
// OAuth2 implements the Auth interface and authenticates requests
61
71
// (API requests only) by looking for an OAuth token in query parameters or the
62
72
// "Authorization" header.
@@ -100,6 +110,16 @@ func parseToken(req *http.Request) (string, bool) {
100
110
func (o * OAuth2 ) userIDFromToken (ctx context.Context , tokenSHA string , store DataStore ) int64 {
101
111
// Let's see if token is valid.
102
112
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 CheckTaskID (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
103
123
uid := CheckOAuthAccessToken (ctx , tokenSHA )
104
124
if uid != 0 {
105
125
store .GetData ()["IsApiToken" ] = true
@@ -134,40 +154,6 @@ func (o *OAuth2) userIDFromToken(ctx context.Context, tokenSHA string, store Dat
134
154
return t .UID
135
155
}
136
156
137
- // parseActionJWT identifies actions runner JWTs that look like an
138
- // OAuth token, but needs to be parsed by its code
139
- func parseActionsJWT (req * http.Request , store DataStore ) (* user_model.User , error ) {
140
- taskID , err := actions .ParseAuthorizationToken (req )
141
- if err != nil || taskID == 0 {
142
- return nil , nil
143
- }
144
-
145
- // Verify the task exists
146
- task , err := actions_model .GetTaskByID (req .Context (), taskID )
147
- if err != nil {
148
- if errors .Is (err , util .ErrNotExist ) {
149
- return nil , nil
150
- }
151
-
152
- return nil , err
153
- }
154
-
155
- // Verify that it's running
156
- if task .Status != actions_model .StatusRunning {
157
- return nil , nil
158
- }
159
-
160
- store .GetData ()["IsActionsToken" ] = true
161
- store .GetData ()["ActionsTaskID" ] = taskID
162
-
163
- user , err := user_model .GetPossibleUserByID (req .Context (), user_model .ActionsUserID )
164
- if err != nil {
165
- return nil , err
166
- }
167
-
168
- return user , nil
169
- }
170
-
171
157
// Verify extracts the user ID from the OAuth token in the query parameters
172
158
// or the "Authorization" header and returns the corresponding user object for that ID.
173
159
// If verification is successful returns an existing user object.
@@ -179,15 +165,6 @@ func (o *OAuth2) Verify(req *http.Request, w http.ResponseWriter, store DataStor
179
165
return nil , nil
180
166
}
181
167
182
- user , err := parseActionsJWT (req , store )
183
- if err != nil {
184
- return nil , err
185
- }
186
-
187
- if user != nil {
188
- return user , nil
189
- }
190
-
191
168
token , ok := parseToken (req )
192
169
if ! ok {
193
170
return nil , nil
@@ -200,7 +177,7 @@ func (o *OAuth2) Verify(req *http.Request, w http.ResponseWriter, store DataStor
200
177
}
201
178
log .Trace ("OAuth2 Authorization: Found token for user[%d]" , id )
202
179
203
- user , err = user_model .GetPossibleUserByID (req .Context (), id )
180
+ user , err : = user_model .GetPossibleUserByID (req .Context (), id )
204
181
if err != nil {
205
182
if ! user_model .IsErrUserNotExist (err ) {
206
183
log .Error ("GetUserByName: %v" , err )
0 commit comments