@@ -55,26 +55,8 @@ impl AuthCheck {
55
55
}
56
56
57
57
pub fn check < B > ( & self , request : & Request < B > ) -> AppResult < AuthenticatedUser > {
58
- controllers:: util:: verify_origin ( request) ?;
59
-
60
58
let auth = authenticate_user ( request) ?;
61
59
62
- if let Some ( reason) = & auth. user ( ) . account_lock_reason {
63
- let still_locked = if let Some ( until) = auth. user ( ) . account_lock_until {
64
- until > Utc :: now ( ) . naive_utc ( )
65
- } else {
66
- true
67
- } ;
68
- if still_locked {
69
- return Err ( account_locked ( reason, auth. user ( ) . account_lock_until ) ) ;
70
- }
71
- }
72
-
73
- request. add_custom_metadata ( "uid" , auth. user_id ( ) ) ;
74
- if let Some ( id) = auth. api_token_id ( ) {
75
- request. add_custom_metadata ( "tokenid" , id) ;
76
- }
77
-
78
60
if let Some ( token) = auth. api_token ( ) {
79
61
if !self . allow_token {
80
62
let error_message =
@@ -153,6 +135,8 @@ impl AuthenticatedUser {
153
135
}
154
136
155
137
fn authenticate_user < B > ( req : & Request < B > ) -> AppResult < AuthenticatedUser > {
138
+ controllers:: util:: verify_origin ( req) ?;
139
+
156
140
let conn = req. app ( ) . db_write ( ) ?;
157
141
158
142
let user_id_from_session = req
@@ -163,6 +147,10 @@ fn authenticate_user<B>(req: &Request<B>) -> AppResult<AuthenticatedUser> {
163
147
let user = User :: find ( & conn, id)
164
148
. map_err ( |err| err. chain ( internal ( "user_id from cookie not found in database" ) ) ) ?;
165
149
150
+ ensure_not_locked ( & user) ?;
151
+
152
+ req. add_custom_metadata ( "uid" , id) ;
153
+
166
154
return Ok ( AuthenticatedUser { user, token : None } ) ;
167
155
}
168
156
@@ -184,6 +172,11 @@ fn authenticate_user<B>(req: &Request<B>) -> AppResult<AuthenticatedUser> {
184
172
let user = User :: find ( & conn, token. user_id )
185
173
. map_err ( |err| err. chain ( internal ( "user_id from token not found in database" ) ) ) ?;
186
174
175
+ ensure_not_locked ( & user) ?;
176
+
177
+ req. add_custom_metadata ( "uid" , token. user_id ) ;
178
+ req. add_custom_metadata ( "tokenid" , token. id ) ;
179
+
187
180
return Ok ( AuthenticatedUser {
188
181
user,
189
182
token : Some ( token) ,
@@ -194,6 +187,21 @@ fn authenticate_user<B>(req: &Request<B>) -> AppResult<AuthenticatedUser> {
194
187
return Err ( internal ( "no cookie session or auth header found" ) . chain ( forbidden ( ) ) ) ;
195
188
}
196
189
190
+ fn ensure_not_locked ( user : & User ) -> AppResult < ( ) > {
191
+ if let Some ( reason) = & user. account_lock_reason {
192
+ let still_locked = if let Some ( until) = user. account_lock_until {
193
+ until > Utc :: now ( ) . naive_utc ( )
194
+ } else {
195
+ true
196
+ } ;
197
+ if still_locked {
198
+ return Err ( account_locked ( reason, user. account_lock_until ) ) ;
199
+ }
200
+ }
201
+
202
+ Ok ( ( ) )
203
+ }
204
+
197
205
#[ cfg( test) ]
198
206
mod tests {
199
207
use super :: * ;
0 commit comments