1
+ use std:: error;
2
+
1
3
use hyper:: header:: AUTHORIZATION ;
2
4
use hyper:: { Body , Request } ;
3
5
use redis:: aio:: SharedConnection ;
4
6
5
7
use primitives:: adapter:: { Adapter , Session as AdapterSession } ;
6
8
7
- use crate :: { ResponseError , Session } ;
9
+ use crate :: Session ;
8
10
9
11
/// Check `Authorization` header for `Bearer` scheme with `Adapter::session_from_token`.
10
12
/// If the `Adapter` fails to create an `AdapterSession`, `ResponseError::BadRequest` will be returned.
11
13
pub ( crate ) async fn for_request (
12
14
mut req : Request < Body > ,
13
15
adapter : & impl Adapter ,
14
16
redis : SharedConnection ,
15
- ) -> Result < Request < Body > , ResponseError > {
17
+ ) -> Result < Request < Body > , Box < dyn error :: Error > > {
16
18
let authorization = req. headers ( ) . get ( AUTHORIZATION ) ;
17
19
18
20
let prefix = "Bearer " ;
@@ -36,16 +38,8 @@ pub(crate) async fn for_request(
36
38
. arg ( token)
37
39
. query_async :: < _ , Option < String > > ( & mut redis. clone ( ) )
38
40
. await ?
39
- . and_then ( |session_str| {
40
- match serde_json:: from_str :: < AdapterSession > ( & session_str) {
41
- Ok ( session) => Some ( session) ,
42
- Err ( serde_error) => {
43
- // log message instead
44
- println ! ( "{}" , serde_error) ;
45
- None
46
- }
47
- }
48
- } ) {
41
+ . and_then ( |session_str| serde_json:: from_str :: < AdapterSession > ( & session_str) . ok ( ) )
42
+ {
49
43
Some ( adapter_session) => adapter_session,
50
44
None => {
51
45
// If there was a problem with the Session or the Token, this will error
@@ -85,14 +79,17 @@ fn get_request_ip(req: &Request<Body>) -> Option<String> {
85
79
86
80
#[ cfg( test) ]
87
81
mod test {
88
- use super :: * ;
89
- use crate :: db:: redis_connection;
90
- use adapter:: DummyAdapter ;
91
82
use hyper:: Request ;
83
+
84
+ use adapter:: DummyAdapter ;
92
85
use primitives:: adapter:: DummyAdapterOptions ;
93
86
use primitives:: config:: configuration;
94
87
use primitives:: util:: tests:: prep_db:: { AUTH , IDS } ;
95
88
89
+ use crate :: db:: redis_connection;
90
+
91
+ use super :: * ;
92
+
96
93
async fn setup ( ) -> ( DummyAdapter , SharedConnection ) {
97
94
let adapter_options = DummyAdapterOptions {
98
95
dummy_identity : IDS [ "leader" ] . clone ( ) ,
@@ -143,9 +140,9 @@ mod test {
143
140
. body ( Body :: empty ( ) )
144
141
. unwrap ( ) ;
145
142
match for_request ( non_existent_token_req, & dummy_adapter, redis) . await {
146
- Err ( ResponseError :: BadRequest ( error) ) => {
143
+ Err ( error) => {
147
144
assert ! ( error. to_string( ) . contains( "no session token for this auth: wrong-token" ) , "Wrong error received" ) ;
148
- } ,
145
+ }
149
146
_ => panic ! ( "We shouldn't get a success response nor a different Error than BadRequest for this call" ) ,
150
147
} ;
151
148
}
0 commit comments