@@ -9,6 +9,7 @@ use crate::util::errors::{
9
9
account_locked, forbidden, internal, AppError , AppResult , InsecurelyGeneratedTokenRevoked ,
10
10
} ;
11
11
use chrono:: Utc ;
12
+ use diesel:: PgConnection ;
12
13
use http:: header;
13
14
14
15
#[ derive( Debug , Clone ) ]
@@ -56,7 +57,7 @@ impl AuthCheck {
56
57
}
57
58
58
59
pub fn check < T : RequestPartsExt > ( & self , request : & T ) -> AppResult < Authentication > {
59
- let auth = authenticate_user ( request) ?;
60
+ let auth = authenticate ( request) ?;
60
61
61
62
if let Some ( token) = auth. api_token ( ) {
62
63
if !self . allow_token {
@@ -152,50 +153,71 @@ impl Authentication {
152
153
}
153
154
}
154
155
155
- fn authenticate_user < T : RequestPartsExt > ( req : & T ) -> AppResult < Authentication > {
156
- controllers:: util:: verify_origin ( req) ?;
157
-
158
- let conn = req. app ( ) . db_write ( ) ?;
159
-
156
+ fn authenticate_via_cookie < T : RequestPartsExt > (
157
+ req : & T ,
158
+ conn : & PgConnection ,
159
+ ) -> AppResult < Option < CookieAuthentication > > {
160
160
let user_id_from_session = req
161
161
. session_get ( "user_id" )
162
162
. and_then ( |s| s. parse :: < i32 > ( ) . ok ( ) ) ;
163
163
164
- if let Some ( id) = user_id_from_session {
165
- let user = User :: find ( & conn, id)
166
- . map_err ( |err| err. chain ( internal ( "user_id from cookie not found in database" ) ) ) ?;
164
+ let Some ( id) = user_id_from_session else { return Ok ( None ) } ;
167
165
168
- ensure_not_locked ( & user) ?;
166
+ let user = User :: find ( conn, id)
167
+ . map_err ( |err| err. chain ( internal ( "user_id from cookie not found in database" ) ) ) ?;
169
168
170
- req . add_custom_metadata ( "uid" , id ) ;
169
+ ensure_not_locked ( & user ) ? ;
171
170
172
- return Ok ( Authentication :: Cookie ( CookieAuthentication { user } ) ) ;
173
- }
171
+ req. add_custom_metadata ( "uid" , id) ;
174
172
175
- // Otherwise, look for an `Authorization` header on the request
173
+ Ok ( Some ( CookieAuthentication { user } ) )
174
+ }
175
+
176
+ fn authenticate_via_token < T : RequestPartsExt > (
177
+ req : & T ,
178
+ conn : & PgConnection ,
179
+ ) -> AppResult < Option < TokenAuthentication > > {
176
180
let maybe_authorization = req
177
181
. headers ( )
178
182
. get ( header:: AUTHORIZATION )
179
183
. and_then ( |h| h. to_str ( ) . ok ( ) ) ;
180
184
181
- if let Some ( header_value) = maybe_authorization {
182
- let token = ApiToken :: find_by_api_token ( & conn, header_value) . map_err ( |e| {
183
- if e. is :: < InsecurelyGeneratedTokenRevoked > ( ) {
184
- e
185
- } else {
186
- e. chain ( internal ( "invalid token" ) ) . chain ( forbidden ( ) )
187
- }
188
- } ) ?;
185
+ let Some ( header_value) = maybe_authorization else { return Ok ( None ) } ;
189
186
190
- let user = User :: find ( & conn, token. user_id )
191
- . map_err ( |err| err. chain ( internal ( "user_id from token not found in database" ) ) ) ?;
187
+ let token = ApiToken :: find_by_api_token ( conn, header_value) . map_err ( |e| {
188
+ if e. is :: < InsecurelyGeneratedTokenRevoked > ( ) {
189
+ e
190
+ } else {
191
+ e. chain ( internal ( "invalid token" ) ) . chain ( forbidden ( ) )
192
+ }
193
+ } ) ?;
192
194
193
- ensure_not_locked ( & user) ?;
195
+ let user = User :: find ( conn, token. user_id )
196
+ . map_err ( |err| err. chain ( internal ( "user_id from token not found in database" ) ) ) ?;
194
197
195
- req. add_custom_metadata ( "uid" , token. user_id ) ;
196
- req. add_custom_metadata ( "tokenid" , token. id ) ;
198
+ ensure_not_locked ( & user) ?;
199
+
200
+ req. add_custom_metadata ( "uid" , token. user_id ) ;
201
+ req. add_custom_metadata ( "tokenid" , token. id ) ;
202
+
203
+ Ok ( Some ( TokenAuthentication { user, token } ) )
204
+ }
205
+
206
+ fn authenticate < T : RequestPartsExt > ( req : & T ) -> AppResult < Authentication > {
207
+ controllers:: util:: verify_origin ( req) ?;
208
+
209
+ let conn = req. app ( ) . db_write ( ) ?;
210
+
211
+ match authenticate_via_cookie ( req, & conn) {
212
+ Ok ( None ) => { }
213
+ Ok ( Some ( auth) ) => return Ok ( Authentication :: Cookie ( auth) ) ,
214
+ Err ( err) => return Err ( err) ,
215
+ }
197
216
198
- return Ok ( Authentication :: Token ( TokenAuthentication { user, token } ) ) ;
217
+ match authenticate_via_token ( req, & conn) {
218
+ Ok ( None ) => { }
219
+ Ok ( Some ( auth) ) => return Ok ( Authentication :: Token ( auth) ) ,
220
+ Err ( err) => return Err ( err) ,
199
221
}
200
222
201
223
// Unable to authenticate the user
0 commit comments