6
6
7
7
#![ allow( clippy:: module_name_repetitions) ]
8
8
9
- use std:: sync:: Arc ;
9
+ use std:: { net :: IpAddr , ops :: Deref , sync:: Arc } ;
10
10
11
11
use async_graphql:: {
12
12
extensions:: Tracing ,
@@ -240,7 +240,7 @@ async fn get_requester(
240
240
session_info : SessionInfo ,
241
241
token : Option < & str > ,
242
242
) -> Result < Requester , RouteError > {
243
- let requester = if let Some ( token) = token {
243
+ let entity = if let Some ( token) = token {
244
244
// If we haven't enabled undocumented_oauth2_access on the listener, we bail out
245
245
if !undocumented_oauth2_access {
246
246
return Err ( RouteError :: InvalidToken ) ;
@@ -285,7 +285,7 @@ async fn get_requester(
285
285
return Err ( RouteError :: MissingScope ) ;
286
286
}
287
287
288
- Requester :: OAuth2Session ( Box :: new ( ( session, user) ) )
288
+ RequestingEntity :: OAuth2Session ( Box :: new ( ( session, user) ) )
289
289
} else {
290
290
let maybe_session = session_info. load_session ( & mut repo) . await ?;
291
291
@@ -295,8 +295,14 @@ async fn get_requester(
295
295
. await ;
296
296
}
297
297
298
- Requester :: from ( maybe_session)
298
+ RequestingEntity :: from ( maybe_session)
299
299
} ;
300
+
301
+ let requester = Requester {
302
+ entity,
303
+ ip_address : activity_tracker. ip ( ) ,
304
+ } ;
305
+
300
306
repo. cancel ( ) . await ?;
301
307
Ok ( requester)
302
308
}
@@ -312,7 +318,6 @@ pub async fn post(
312
318
cookie_jar : CookieJar ,
313
319
content_type : Option < TypedHeader < ContentType > > ,
314
320
authorization : Option < TypedHeader < Authorization < Bearer > > > ,
315
- requester_fingerprint : RequesterFingerprint ,
316
321
body : Body ,
317
322
) -> Result < impl IntoResponse , RouteError > {
318
323
let body = body. into_data_stream ( ) ;
@@ -339,7 +344,6 @@ pub async fn post(
339
344
MultipartOptions :: default ( ) ,
340
345
)
341
346
. await ?
342
- . data ( requester_fingerprint)
343
347
. data ( requester) ; // XXX: this should probably return another error response?
344
348
345
349
let span = span_for_graphql_request ( & request) ;
@@ -366,7 +370,6 @@ pub async fn get(
366
370
activity_tracker : BoundActivityTracker ,
367
371
cookie_jar : CookieJar ,
368
372
authorization : Option < TypedHeader < Authorization < Bearer > > > ,
369
- requester_fingerprint : RequesterFingerprint ,
370
373
RawQuery ( query) : RawQuery ,
371
374
) -> Result < impl IntoResponse , FancyError > {
372
375
let token = authorization
@@ -383,9 +386,8 @@ pub async fn get(
383
386
)
384
387
. await ?;
385
388
386
- let request = async_graphql:: http:: parse_query_string ( & query. unwrap_or_default ( ) ) ?
387
- . data ( requester)
388
- . data ( requester_fingerprint) ;
389
+ let request =
390
+ async_graphql:: http:: parse_query_string ( & query. unwrap_or_default ( ) ) ?. data ( requester) ;
389
391
390
392
let span = span_for_graphql_request ( & request) ;
391
393
let response = schema. execute ( request) . instrument ( span) . await ;
@@ -417,9 +419,32 @@ pub fn schema_builder() -> SchemaBuilder {
417
419
. register_output_type :: < CreationEvent > ( )
418
420
}
419
421
422
+ pub struct Requester {
423
+ entity : RequestingEntity ,
424
+ ip_address : Option < IpAddr > ,
425
+ }
426
+
427
+ impl Requester {
428
+ pub fn fingerprint ( & self ) -> RequesterFingerprint {
429
+ if let Some ( ip) = self . ip_address {
430
+ RequesterFingerprint :: new ( ip)
431
+ } else {
432
+ RequesterFingerprint :: EMPTY
433
+ }
434
+ }
435
+ }
436
+
437
+ impl Deref for Requester {
438
+ type Target = RequestingEntity ;
439
+
440
+ fn deref ( & self ) -> & Self :: Target {
441
+ & self . entity
442
+ }
443
+ }
444
+
420
445
/// The identity of the requester.
421
446
#[ derive( Debug , Clone , Default , PartialEq , Eq ) ]
422
- pub enum Requester {
447
+ pub enum RequestingEntity {
423
448
/// The requester presented no authentication information.
424
449
#[ default]
425
450
Anonymous ,
@@ -480,7 +505,7 @@ impl OwnerId for UserId {
480
505
}
481
506
}
482
507
483
- impl Requester {
508
+ impl RequestingEntity {
484
509
fn browser_session ( & self ) -> Option < & BrowserSession > {
485
510
match self {
486
511
Self :: BrowserSession ( session) => Some ( session) ,
@@ -532,17 +557,21 @@ impl Requester {
532
557
Self :: BrowserSession ( _) | Self :: Anonymous => false ,
533
558
}
534
559
}
560
+
561
+ fn is_unauthenticated ( & self ) -> bool {
562
+ matches ! ( self , Self :: Anonymous )
563
+ }
535
564
}
536
565
537
- impl From < BrowserSession > for Requester {
566
+ impl From < BrowserSession > for RequestingEntity {
538
567
fn from ( session : BrowserSession ) -> Self {
539
568
Self :: BrowserSession ( Box :: new ( session) )
540
569
}
541
570
}
542
571
543
- impl < T > From < Option < T > > for Requester
572
+ impl < T > From < Option < T > > for RequestingEntity
544
573
where
545
- T : Into < Requester > ,
574
+ T : Into < RequestingEntity > ,
546
575
{
547
576
fn from ( session : Option < T > ) -> Self {
548
577
session. map ( Into :: into) . unwrap_or_default ( )
0 commit comments