@@ -11,6 +11,7 @@ use crate::util::errors::{
11
11
use crate :: util:: token:: HashedToken ;
12
12
use chrono:: Utc ;
13
13
use http:: header;
14
+ use http:: request:: Parts ;
14
15
15
16
#[ derive( Debug , Clone ) ]
16
17
pub struct AuthCheck {
@@ -57,18 +58,14 @@ impl AuthCheck {
57
58
}
58
59
59
60
#[ instrument( name = "auth.check" , skip_all) ]
60
- pub fn check < T : RequestPartsExt > (
61
- & self ,
62
- request : & T ,
63
- conn : & mut impl Conn ,
64
- ) -> AppResult < Authentication > {
65
- let auth = authenticate ( request, conn) ?;
61
+ pub fn check ( & self , parts : & Parts , conn : & mut impl Conn ) -> AppResult < Authentication > {
62
+ let auth = authenticate ( parts, conn) ?;
66
63
67
64
if let Some ( token) = auth. api_token ( ) {
68
65
if !self . allow_token {
69
66
let error_message =
70
67
"API Token authentication was explicitly disallowed for this API" ;
71
- request . request_log ( ) . add ( "cause" , error_message) ;
68
+ parts . request_log ( ) . add ( "cause" , error_message) ;
72
69
73
70
return Err ( forbidden (
74
71
"this action can only be performed on the crates.io website" ,
@@ -77,7 +74,7 @@ impl AuthCheck {
77
74
78
75
if !self . endpoint_scope_matches ( token. endpoint_scopes . as_ref ( ) ) {
79
76
let error_message = "Endpoint scope mismatch" ;
80
- request . request_log ( ) . add ( "cause" , error_message) ;
77
+ parts . request_log ( ) . add ( "cause" , error_message) ;
81
78
82
79
return Err ( forbidden (
83
80
"this token does not have the required permissions to perform this action" ,
@@ -86,7 +83,7 @@ impl AuthCheck {
86
83
87
84
if !self . crate_scope_matches ( token. crate_scopes . as_ref ( ) ) {
88
85
let error_message = "Crate scope mismatch" ;
89
- request . request_log ( ) . add ( "cause" , error_message) ;
86
+ parts . request_log ( ) . add ( "cause" , error_message) ;
90
87
91
88
return Err ( forbidden (
92
89
"this token does not have the required permissions to perform this action" ,
@@ -171,11 +168,11 @@ impl Authentication {
171
168
}
172
169
173
170
#[ instrument( skip_all) ]
174
- fn authenticate_via_cookie < T : RequestPartsExt > (
175
- req : & T ,
171
+ fn authenticate_via_cookie (
172
+ parts : & Parts ,
176
173
conn : & mut impl Conn ,
177
174
) -> AppResult < Option < CookieAuthentication > > {
178
- let user_id_from_session = req
175
+ let user_id_from_session = parts
179
176
. session ( )
180
177
. get ( "user_id" )
181
178
. and_then ( |s| s. parse :: < i32 > ( ) . ok ( ) ) ;
@@ -185,23 +182,23 @@ fn authenticate_via_cookie<T: RequestPartsExt>(
185
182
} ;
186
183
187
184
let user = User :: find ( conn, id) . map_err ( |err| {
188
- req . request_log ( ) . add ( "cause" , err) ;
185
+ parts . request_log ( ) . add ( "cause" , err) ;
189
186
internal ( "user_id from cookie not found in database" )
190
187
} ) ?;
191
188
192
189
ensure_not_locked ( & user) ?;
193
190
194
- req . request_log ( ) . add ( "uid" , id) ;
191
+ parts . request_log ( ) . add ( "uid" , id) ;
195
192
196
193
Ok ( Some ( CookieAuthentication { user } ) )
197
194
}
198
195
199
196
#[ instrument( skip_all) ]
200
- fn authenticate_via_token < T : RequestPartsExt > (
201
- req : & T ,
197
+ fn authenticate_via_token (
198
+ parts : & Parts ,
202
199
conn : & mut impl Conn ,
203
200
) -> AppResult < Option < TokenAuthentication > > {
204
- let maybe_authorization = req
201
+ let maybe_authorization = parts
205
202
. headers ( )
206
203
. get ( header:: AUTHORIZATION )
207
204
. and_then ( |h| h. to_str ( ) . ok ( ) ) ;
@@ -215,43 +212,43 @@ fn authenticate_via_token<T: RequestPartsExt>(
215
212
216
213
let token = ApiToken :: find_by_api_token ( conn, & token) . map_err ( |e| {
217
214
let cause = format ! ( "invalid token caused by {e}" ) ;
218
- req . request_log ( ) . add ( "cause" , cause) ;
215
+ parts . request_log ( ) . add ( "cause" , cause) ;
219
216
220
217
forbidden ( "authentication failed" )
221
218
} ) ?;
222
219
223
220
let user = User :: find ( conn, token. user_id ) . map_err ( |err| {
224
- req . request_log ( ) . add ( "cause" , err) ;
221
+ parts . request_log ( ) . add ( "cause" , err) ;
225
222
internal ( "user_id from token not found in database" )
226
223
} ) ?;
227
224
228
225
ensure_not_locked ( & user) ?;
229
226
230
- req . request_log ( ) . add ( "uid" , token. user_id ) ;
231
- req . request_log ( ) . add ( "tokenid" , token. id ) ;
227
+ parts . request_log ( ) . add ( "uid" , token. user_id ) ;
228
+ parts . request_log ( ) . add ( "tokenid" , token. id ) ;
232
229
233
230
Ok ( Some ( TokenAuthentication { user, token } ) )
234
231
}
235
232
236
233
#[ instrument( skip_all) ]
237
- fn authenticate < T : RequestPartsExt > ( req : & T , conn : & mut impl Conn ) -> AppResult < Authentication > {
238
- controllers:: util:: verify_origin ( req ) ?;
234
+ fn authenticate ( parts : & Parts , conn : & mut impl Conn ) -> AppResult < Authentication > {
235
+ controllers:: util:: verify_origin ( parts ) ?;
239
236
240
- match authenticate_via_cookie ( req , conn) {
237
+ match authenticate_via_cookie ( parts , conn) {
241
238
Ok ( None ) => { }
242
239
Ok ( Some ( auth) ) => return Ok ( Authentication :: Cookie ( auth) ) ,
243
240
Err ( err) => return Err ( err) ,
244
241
}
245
242
246
- match authenticate_via_token ( req , conn) {
243
+ match authenticate_via_token ( parts , conn) {
247
244
Ok ( None ) => { }
248
245
Ok ( Some ( auth) ) => return Ok ( Authentication :: Token ( auth) ) ,
249
246
Err ( err) => return Err ( err) ,
250
247
}
251
248
252
249
// Unable to authenticate the user
253
250
let cause = "no cookie session or auth header found" ;
254
- req . request_log ( ) . add ( "cause" , cause) ;
251
+ parts . request_log ( ) . add ( "cause" , cause) ;
255
252
256
253
return Err ( forbidden ( "this action requires authentication" ) ) ;
257
254
}
0 commit comments