@@ -4,12 +4,12 @@ use crate::middleware::log_request::RequestLogExt;
44use crate :: middleware:: session:: RequestSession ;
55use crate :: models:: token:: { CrateScope , EndpointScope } ;
66use crate :: models:: { ApiToken , User } ;
7- use crate :: util:: diesel:: Conn ;
87use crate :: util:: errors:: {
98 account_locked, forbidden, internal, AppResult , InsecurelyGeneratedTokenRevoked ,
109} ;
1110use crate :: util:: token:: HashedToken ;
1211use chrono:: Utc ;
12+ use diesel_async:: AsyncPgConnection ;
1313use http:: header;
1414use http:: request:: Parts ;
1515
@@ -58,8 +58,12 @@ impl AuthCheck {
5858 }
5959
6060 #[ instrument( name = "auth.check" , skip_all) ]
61- pub fn check ( & self , parts : & Parts , conn : & mut impl Conn ) -> AppResult < Authentication > {
62- let auth = authenticate ( parts, conn) ?;
61+ pub async fn check (
62+ & self ,
63+ parts : & Parts ,
64+ conn : & mut AsyncPgConnection ,
65+ ) -> AppResult < Authentication > {
66+ let auth = authenticate ( parts, conn) . await ?;
6367
6468 if let Some ( token) = auth. api_token ( ) {
6569 if !self . allow_token {
@@ -168,9 +172,9 @@ impl Authentication {
168172}
169173
170174#[ instrument( skip_all) ]
171- fn authenticate_via_cookie (
175+ async fn authenticate_via_cookie (
172176 parts : & Parts ,
173- conn : & mut impl Conn ,
177+ conn : & mut AsyncPgConnection ,
174178) -> AppResult < Option < CookieAuthentication > > {
175179 let user_id_from_session = parts
176180 . session ( )
@@ -181,7 +185,7 @@ fn authenticate_via_cookie(
181185 return Ok ( None ) ;
182186 } ;
183187
184- let user = User :: find ( conn, id) . map_err ( |err| {
188+ let user = User :: async_find ( conn, id) . await . map_err ( |err| {
185189 parts. request_log ( ) . add ( "cause" , err) ;
186190 internal ( "user_id from cookie not found in database" )
187191 } ) ?;
@@ -194,9 +198,9 @@ fn authenticate_via_cookie(
194198}
195199
196200#[ instrument( skip_all) ]
197- fn authenticate_via_token (
201+ async fn authenticate_via_token (
198202 parts : & Parts ,
199- conn : & mut impl Conn ,
203+ conn : & mut AsyncPgConnection ,
200204) -> AppResult < Option < TokenAuthentication > > {
201205 let maybe_authorization = parts
202206 . headers ( )
@@ -210,14 +214,16 @@ fn authenticate_via_token(
210214 let token =
211215 HashedToken :: parse ( header_value) . map_err ( |_| InsecurelyGeneratedTokenRevoked :: boxed ( ) ) ?;
212216
213- let token = ApiToken :: find_by_api_token ( conn, & token) . map_err ( |e| {
214- let cause = format ! ( "invalid token caused by {e}" ) ;
215- parts. request_log ( ) . add ( "cause" , cause) ;
217+ let token = ApiToken :: async_find_by_api_token ( conn, & token)
218+ . await
219+ . map_err ( |e| {
220+ let cause = format ! ( "invalid token caused by {e}" ) ;
221+ parts. request_log ( ) . add ( "cause" , cause) ;
216222
217- forbidden ( "authentication failed" )
218- } ) ?;
223+ forbidden ( "authentication failed" )
224+ } ) ?;
219225
220- let user = User :: find ( conn, token. user_id ) . map_err ( |err| {
226+ let user = User :: async_find ( conn, token. user_id ) . await . map_err ( |err| {
221227 parts. request_log ( ) . add ( "cause" , err) ;
222228 internal ( "user_id from token not found in database" )
223229 } ) ?;
@@ -231,16 +237,16 @@ fn authenticate_via_token(
231237}
232238
233239#[ instrument( skip_all) ]
234- fn authenticate ( parts : & Parts , conn : & mut impl Conn ) -> AppResult < Authentication > {
240+ async fn authenticate ( parts : & Parts , conn : & mut AsyncPgConnection ) -> AppResult < Authentication > {
235241 controllers:: util:: verify_origin ( parts) ?;
236242
237- match authenticate_via_cookie ( parts, conn) {
243+ match authenticate_via_cookie ( parts, conn) . await {
238244 Ok ( None ) => { }
239245 Ok ( Some ( auth) ) => return Ok ( Authentication :: Cookie ( auth) ) ,
240246 Err ( err) => return Err ( err) ,
241247 }
242248
243- match authenticate_via_token ( parts, conn) {
249+ match authenticate_via_token ( parts, conn) . await {
244250 Ok ( None ) => { }
245251 Ok ( Some ( auth) ) => return Ok ( Authentication :: Token ( auth) ) ,
246252 Err ( err) => return Err ( err) ,
0 commit comments