mycelium-api 8.3.1-rc.1

Provide API ports to the mycelium project.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
use crate::{
    dtos::{Audience, GenericAccessTokenClaims, JWKS},
    middleware::get_email_or_provider_from_request,
    models::api_config::{ApiConfig, CacheConfig},
};

use actix_web::{web, HttpRequest};
use base64::{engine::general_purpose, Engine};
use jsonwebtoken::{decode, decode_header, DecodingKey, Validation};
use myc_core::domain::{
    dtos::email::Email,
    entities::{KVArtifactRead, KVArtifactWrite},
};
use myc_http_tools::{
    models::external_providers_config::ExternalProviderConfig,
    responses::GatewayError,
};
use myc_kv::repositories::KVAppModule;
use mycelium_base::entities::FetchResponseKind;
use openssl::{stack::Stack, x509::X509};
use serde::Deserialize;
use shaku::HasComponent;
use tracing::Instrument;

#[derive(Deserialize)]
struct UserInfo {
    email: Option<String>,
}

/// Try to populate profile to request header
///
/// This function is used to check credentials from multiple identity providers.
/// It returns a tuple with the email and the provider config if the provider is
/// a external one. If the provider is a internal one, the second element is
/// None.
///
#[tracing::instrument(
    name = "check_credentials_with_multi_identity_provider",
    skip_all,
    fields(
        myc.router.email = tracing::field::Empty,
        myc.router.provider = tracing::field::Empty,
    )
)]
pub(crate) async fn check_credentials_with_multi_identity_provider(
    req: HttpRequest,
) -> Result<(Email, Option<ExternalProviderConfig>), GatewayError> {
    let span = tracing::Span::current();

    tracing::trace!("Checking credentials with multiple identity providers");

    // ? -----------------------------------------------------------------------
    // ? Extract issuer and token from request
    //
    // If the function get_email_or_provider_from_request found an valid email
    // from internal provider, the found email is returned. Otherwise, the
    // function will return a vector of external providers. If the internal and
    // external providers are not found, the function will return an
    // Unauthorized error.
    //
    // ? -----------------------------------------------------------------------

    let (
        optional_email_from_internal_provider,
        optional_external_provider_config,
        token,
    ) = get_email_or_provider_from_request(req.clone())
        .instrument(span.to_owned())
        .await?;

    // ? -----------------------------------------------------------------------
    // ? If email from internal provider was found, return it
    //
    // An email response indicates that the request is coming from the internal
    // provider. Then, the function will return the email.
    //
    // ? -----------------------------------------------------------------------

    if let Some(email) = optional_email_from_internal_provider {
        span.record("myc.router.email", &Some(email.redacted_email()));

        tracing::info!(
            stage = "identity.email",
            outcome = "from_token",
            "Email obtained from internal token"
        );

        return Ok((email, None));
    }

    // ? -----------------------------------------------------------------------
    // ? Proceed to the external providers
    //
    // If the email is not found, the function will proceed to the external
    // providers.
    //
    // ? -----------------------------------------------------------------------

    if let Some(provider) = optional_external_provider_config {
        if let Ok(issuer) = provider.issuer.async_get_or_error().await {
            span.record("myc.router.provider", &Some(issuer));
        }

        return match get_email_from_external_provider(&provider, &token, &req)
            .instrument(span.to_owned())
            .await
        {
            Ok(email) => Ok((email, Some(provider))),
            Err(err) => Err(err),
        };
    }

    // ? -----------------------------------------------------------------------
    // ? If no provider is found, return an error
    // ? -----------------------------------------------------------------------

    tracing::error!("Unable to check user email or provider. Unauthorized");

    Err(GatewayError::Unauthorized(
        "Could not check issuer.".to_string(),
    ))
}

#[tracing::instrument(name = "get_email_from_external_provider", skip_all)]
async fn get_email_from_external_provider(
    provider: &ExternalProviderConfig,
    token: &str,
    req: &HttpRequest,
) -> Result<Email, GatewayError> {
    tracing::info!(
        stage = "identity.external",
        "Identity resolution via external provider started"
    );

    // ? -----------------------------------------------------------------------
    // ? Collect public keys from provider
    //
    // The public keys are collected from the provider. If the public keys are
    // not found, return an error. Public keys should be used to verify the
    // token signature.
    //
    // ? -----------------------------------------------------------------------

    let jwks_uri =
        provider.jwks_uri.async_get_or_error().await.map_err(|e| {
            GatewayError::InternalServerError(format!(
                "Error fetching JWKS: {e}"
            ))
        })?;

    //
    // Extract kid from token
    //
    let decoded_headers = decode_header(&token).map_err(|err| {
        tracing::error!("Error decoding header: {err}");

        GatewayError::Unauthorized(format!(
            "JWT token has not valid format. Unable to decode header: {token}"
        ))
    })?;

    //
    // Extract kid from token
    //
    let kid =
        decoded_headers
            .kid
            .ok_or(GatewayError::Unauthorized(format!(
                "JWT kid not found: {token}"
            )))?;

    //
    // Find JWK in JWKS
    //
    let jwks = fetch_jwks(&jwks_uri, req).await?;
    let jwk = jwks.find(&kid).ok_or(GatewayError::Unauthorized(format!(
        "JWT kid not found in JWKS: {kid}"
    )))?;

    // ? -----------------------------------------------------------------------
    // ? Start token verification
    //
    // The token verification is performed using the public key collected from
    // the provider. If the public key is not found, return an error.
    //
    // ? -----------------------------------------------------------------------

    let decoded_key = if let Some(x5c) = &jwk.x5c {
        //
        // Case token is signed with X5C perform the verification of the token
        // using the root certificate
        //
        let mut certs = Stack::new().map_err(|err| {
            tracing::error!("Error on create stack: {err}");

            GatewayError::Unauthorized("Error on parse token".to_string())
        })?;

        for cert in x5c {
            let cert_der =
                general_purpose::STANDARD.decode(cert).map_err(|err| {
                    tracing::error!("Error on decode X5C: {err}");

                    GatewayError::Unauthorized(
                        "Error on parse token".to_string(),
                    )
                })?;

            let x509 = X509::from_der(&cert_der).map_err(|err| {
                tracing::error!("Error on create X509 from der: {err}");

                GatewayError::Unauthorized("Error on parse token".to_string())
            })?;

            certs.push(x509).map_err(|err| {
                tracing::error!("Error on push X509 to stack: {err}");

                GatewayError::Unauthorized("Error on parse token".to_string())
            })?;
        }

        let root_cert = certs.get(0).ok_or(GatewayError::Unauthorized(
            "No certificates found".to_string(),
        ))?;

        let public_key = root_cert.public_key().map_err(|err| {
            tracing::error!("Error getting public key: {err}");

            GatewayError::Unauthorized("Error on parse token".to_string())
        })?;

        let leaf_cert =
            certs
                .get(certs.len() - 1)
                .ok_or(GatewayError::Unauthorized(
                    "No leaf certificate found".to_string(),
                ))?;

        leaf_cert.verify(public_key.as_ref()).map_err(|err| {
            tracing::error!("Error on verify X509: {err}");

            GatewayError::Unauthorized("Error on parse token".to_string())
        })?;

        let public_key_pem = public_key.public_key_to_pem().map_err(|err| {
            tracing::error!("Error on generate public key pem from X5C: {err}");

            GatewayError::Unauthorized("Error on parse token".to_string())
        })?;

        DecodingKey::from_rsa_pem(&public_key_pem).map_err(|err| {
            tracing::error!("Error on create RSA decoding key: {err}");

            GatewayError::Unauthorized("Error on parse token".to_string())
        })?
    } else {
        //
        // Case token is signed with RS256 perform the verification of the token
        // using the RSA components
        //
        DecodingKey::from_rsa_components(&jwk.n, &jwk.e).map_err(|err| {
            tracing::error!("Error creating RSA decoding key: {err}");

            GatewayError::Unauthorized("Error on parse token".to_string())
        })?
    };

    //
    // Extract expected audience from issuer v2
    //
    let expected_audience =
        provider.audience.async_get_or_error().await.map_err(|e| {
            tracing::error!("Error getting audience: {e}");

            GatewayError::Unauthorized("JWT audience not found".to_string())
        })?;

    //
    // Decode token
    //
    let mut validation = Validation::new(decoded_headers.alg);
    validation.set_audience(&[expected_audience.to_owned()]);

    let token_data =
        decode::<GenericAccessTokenClaims>(&token, &decoded_key, &validation)
            .map_err(|err| {
            tracing::error!("Error decoding token: {err}");

            GatewayError::Unauthorized("Error on parse token".to_string())
        })?;

    //
    // Validate audience
    //
    match token_data.claims.audience.to_owned() {
        Audience::Single(aud) => {
            if aud != expected_audience {
                tracing::trace!("Expected audience: {:?}", expected_audience);
                tracing::trace!("Token audience: {:?}", aud);

                return Err(GatewayError::Unauthorized(format!(
                    "Invalid audience: {expected_audience}"
                )));
            }
        }
        Audience::Multiple(auds) => {
            if !auds.contains(&expected_audience) {
                tracing::trace!("Expected audience: {:?}", expected_audience);
                tracing::trace!("Token audience: {:?}", auds);

                return Err(GatewayError::Unauthorized(format!(
                    "Invalid audience: {expected_audience}"
                )));
            }
        }
    }

    // ? -----------------------------------------------------------------------
    // ? Try to extract email from token
    //
    // In some the claims must include the email, upn or unique_name. Try to
    // extract the email from the token. If the email is not found, return an
    // error.
    //
    // ? -----------------------------------------------------------------------

    let token_email = {
        if let Some(email) = token_data.claims.email {
            Some(email)
        } else {
            None
        }
    };

    if let Some(email) = token_email {
        let parsed_email = Email::from_string(email).map_err(|err| {
            tracing::error!("Error on extract email from token: {err}");

            GatewayError::Unauthorized(
                "Error on extract email from token".to_string(),
            )
        })?;

        tracing::info!(
            stage = "identity.external",
            outcome = "ok",
            "Identity via external provider completed"
        );

        return Ok(parsed_email);
    };

    // ? -----------------------------------------------------------------------
    // ? Try to extract email from user info url
    //
    // Try to request the user info from the declared user info url. If the
    // user info is not found, return an error.
    //
    // ? -----------------------------------------------------------------------

    let token_identifier =
        if let Some(jid) = token_data.claims.json_web_token_id {
            jid
        } else {
            format!(
                "{sub}_{iat}",
                sub = token_data.claims.subject,
                iat = token_data.claims.issued_at
            )
        };

    if let Some(user_info_url) = &provider.user_info_url {
        let user_info_url =
            user_info_url.async_get_or_error().await.map_err(|e| {
                GatewayError::InternalServerError(format!(
                    "Error getting user info url: {e}"
                ))
            })?;

        let email = get_user_info_from_url(
            &user_info_url,
            token,
            token_identifier.to_owned(),
            req,
        )
        .await?;

        if let Some(email) = email {
            tracing::info!(
                stage = "identity.external",
                outcome = "ok",
                "Identity via external provider completed"
            );

            return Ok(email);
        }
    }

    // ? -----------------------------------------------------------------------
    // ? Try to extract user info url the authority
    // ? -----------------------------------------------------------------------

    if let Audience::Multiple(auds) = token_data.claims.audience {
        if let Some(user_info_url) =
            auds.iter().find(|aud| aud.ends_with("/userinfo"))
        {
            let email = get_user_info_from_url(
                &user_info_url,
                token,
                token_identifier,
                req,
            )
            .await?;

            if let Some(email) = email {
                tracing::info!(
                    stage = "identity.external",
                    outcome = "ok",
                    "Identity via external provider completed"
                );

                return Ok(email);
            }
        }
    }

    // ? -----------------------------------------------------------------------
    // ? If no email is found, return an error
    // ? -----------------------------------------------------------------------

    Err(GatewayError::Unauthorized("Email not found".to_string()))
}

/// Fetch JWKS from the given URI
///
/// This function is used to fetch the JWKS from the given URI.
#[tracing::instrument(name = "fetch_jwks", skip_all)]
async fn fetch_jwks(
    uri: &str,
    req: &HttpRequest,
) -> Result<JWKS, GatewayError> {
    tracing::info!(
        stage = "identity.jwks",
        jwks_uri = %uri,
        "Resolving JWKS"
    );

    // ? -----------------------------------------------------------------------
    // ? Try to fetch JWKS cache
    // ? -----------------------------------------------------------------------

    let search_key = format!("jwks_{uri}");

    let app_module = req.app_data::<web::Data<KVAppModule>>().ok_or(
        GatewayError::InternalServerError(
            "Unable to extract profile fetching module from request"
                .to_string(),
        ),
    )?;

    let kv_artifact_read: &dyn KVArtifactRead = app_module.resolve_ref();

    let jwks = kv_artifact_read
        .get_encoded_artifact(search_key.to_owned())
        .await
        .map_err(|e| {
            tracing::error!("Unexpected error on fetch JWKS from cache: {e}");

            GatewayError::InternalServerError(
                "Unexpected error on fetch JWKS from cache".to_string(),
            )
        })?;

    if let FetchResponseKind::Found(jwks) = jwks {
        let jwks_slice = match general_purpose::STANDARD.decode(jwks) {
            Ok(res) => res,
            Err(err) => {
                tracing::warn!(
                    "Unexpected error on fetch JWKS from cache: {err}"
                );

                return Err(GatewayError::InternalServerError(
                    "Unexpected error on parse JWKS".to_string(),
                ));
            }
        };

        match serde_json::from_slice::<JWKS>(&jwks_slice) {
            Ok(jwks) => {
                tracing::info!(
                    stage = "identity.jwks",
                    outcome = "from_cache",
                    jwks_uri = %uri,
                    "JWKS obtained from cache"
                );
                return Ok(jwks);
            }
            Err(err) => {
                tracing::error!("Unexpected error on parse JWKS: {err}");

                return Err(GatewayError::InternalServerError(
                    "Unexpected error on parse JWKS".to_string(),
                ));
            }
        }
    }

    // ? -----------------------------------------------------------------------
    // ? Try to fetch JWKS from the given URI
    // ? -----------------------------------------------------------------------

    let res = reqwest::get(uri).await.map_err(|e| {
        tracing::error!("Error fetching JWKS: {}", e);

        GatewayError::InternalServerError(
            "Unexpected error on fetch JWKS".to_string(),
        )
    })?;

    let jwks = res.json::<JWKS>().await.map_err(|e| {
        tracing::error!("Error parsing JWKS: {}", e);

        GatewayError::InternalServerError(
            "Unexpected error on parse JWKS".to_string(),
        )
    })?;

    set_jwks_in_cache(search_key, jwks.to_owned(), req).await;

    tracing::info!(
        stage = "identity.jwks",
        outcome = "resolved",
        jwks_uri = %uri,
        "JWKS resolved via URI"
    );

    return Ok(jwks);
}

#[tracing::instrument(name = "set_jwks_in_cache", skip_all)]
async fn set_jwks_in_cache(search_key: String, jwks: JWKS, req: &HttpRequest) {
    let app_module = match req.app_data::<web::Data<KVAppModule>>() {
        Some(app_module) => app_module,
        None => {
            tracing::error!(
                "Unable to extract profile fetching module from request"
            );

            return;
        }
    };

    let ttl = if let Some(api_config) = req.app_data::<web::Data<ApiConfig>>() {
        let default_cache_config = CacheConfig::default();
        let cache_config =
            api_config.cache.as_ref().unwrap_or(&default_cache_config);

        cache_config.jwks_ttl.unwrap_or(60)
    } else {
        60
    };

    let kv_artifact_write: &dyn KVArtifactWrite = app_module.resolve_ref();

    let serialized_jwks = match serde_json::to_string(&jwks) {
        Ok(serialized_jwks) => serialized_jwks,
        Err(err) => {
            tracing::error!("Unexpected error on serialize JWKS: {err}");

            return;
        }
    };

    let encoded_jwks =
        general_purpose::STANDARD.encode(serialized_jwks.as_bytes());

    match kv_artifact_write
        .set_encoded_artifact(search_key, encoded_jwks, ttl)
        .await
    {
        Ok(_) => (),
        Err(err) => {
            tracing::error!("Unexpected error on cache JWKS: {err}");

            return;
        }
    }
}

#[tracing::instrument(name = "fetch_email_from_cache", skip_all)]
async fn fetch_email_from_cache(
    token_identifier: String,
    req: &HttpRequest,
) -> Option<Email> {
    let app_module = match req.app_data::<web::Data<KVAppModule>>() {
        Some(app_module) => app_module,
        None => {
            tracing::error!(
                "Unable to extract profile fetching module from request"
            );

            return None;
        }
    };

    let kv_artifact_read: &dyn KVArtifactRead = app_module.resolve_ref();

    let profile_response = match kv_artifact_read
        .get_encoded_artifact(token_identifier)
        .await
    {
        Err(err) => {
            tracing::error!(
                "Unexpected error on fetch profile from cache: {err}"
            );

            return None;
        }
        Ok(res) => res,
    };

    let profile_base64 = match profile_response {
        FetchResponseKind::NotFound(_) => {
            tracing::info!(
                stage = "identity.email.cache",
                cache_hit = false,
                "Email cache: miss"
            );
            return None;
        }
        FetchResponseKind::Found(payload) => payload,
    };

    let profile_slice = match general_purpose::STANDARD.decode(profile_base64) {
        Ok(res) => res,
        Err(err) => {
            tracing::warn!(
                "Unexpected error on fetch profile from cache: {err}"
            );

            return None;
        }
    };

    match serde_json::from_slice::<Email>(&profile_slice) {
        Ok(email) => {
            tracing::info!(
                stage = "identity.email.cache",
                cache_hit = true,
                "Email cache: hit"
            );
            tracing::trace!("Cache email: {:?}", email.redacted_email());

            Some(email)
        }
        Err(err) => {
            tracing::warn!(
                "Unexpected error on fetch profile from cache: {err}"
            );

            return None;
        }
    }
}

#[tracing::instrument(name = "set_email_in_cache", skip_all)]
async fn set_email_in_cache(
    token_identifier: String,
    email: Email,
    req: &HttpRequest,
) {
    let app_module = match req.app_data::<web::Data<KVAppModule>>() {
        Some(app_module) => app_module,
        None => {
            tracing::error!(
                "Unable to extract profile caching module from request"
            );

            return;
        }
    };

    let ttl = if let Some(api_config) = req.app_data::<web::Data<ApiConfig>>() {
        let default_cache_config = CacheConfig::default();
        let cache_config =
            api_config.cache.as_ref().unwrap_or(&default_cache_config);

        cache_config.email_ttl.unwrap_or(60)
    } else {
        60
    };

    let kv_artifact_write: &dyn KVArtifactWrite = app_module.resolve_ref();

    let serialized_email = match serde_json::to_string(&email) {
        Ok(serialized_email) => serialized_email,
        Err(err) => {
            tracing::error!("Unexpected error on serialize email: {err}");

            return;
        }
    };

    let encoded_email =
        general_purpose::STANDARD.encode(serialized_email.as_bytes());

    match kv_artifact_write
        .set_encoded_artifact(token_identifier, encoded_email, ttl)
        .await
    {
        Ok(_) => (),
        Err(err) => {
            tracing::error!("Unexpected error on cache profile: {err}");

            return;
        }
    }
}

async fn get_user_info_from_url(
    user_info_url: &str,
    token: &str,
    token_identifier: String,
    req: &HttpRequest,
) -> Result<Option<Email>, GatewayError> {
    tracing::info!(
        stage = "identity.email",
        user_info_url = %user_info_url,
        "Resolving user email"
    );

    // ? -----------------------------------------------------------------------
    // ? Try to fetch email from cache
    //
    // If the email is found in the cache, return it. Otherwise, proceed to the
    // user info url request.
    //
    // ? -----------------------------------------------------------------------

    let email = fetch_email_from_cache(token_identifier.to_owned(), req).await;

    if let Some(email) = email {
        tracing::info!(
            stage = "identity.email",
            outcome = "from_cache",
            "Email obtained from cache"
        );
        return Ok(Some(email));
    }

    // ? -----------------------------------------------------------------------
    // ? Request user info url
    //
    // Request the user info url and extract the email from the response. If the
    // email is not found, return an error.
    //
    // ? -----------------------------------------------------------------------

    let res = reqwest::Client::new()
        .get(user_info_url)
        .header("Authorization", format!("Bearer {}", token))
        .send()
        .await
        .map_err(|e| {
            tracing::error!("Error fetching user info url: {e}");

            GatewayError::Unauthorized(
                "Error on fetch user info url".to_string(),
            )
        })?;

    let user_info = res.json::<UserInfo>().await.map_err(|e| {
        tracing::error!("Error parsing user info url: {e}");

        GatewayError::Unauthorized("Error on parse user info url".to_string())
    })?;

    let email = user_info.email.ok_or(GatewayError::Unauthorized(
        "Email not found in user info".to_string(),
    ))?;

    let parsed_email = Email::from_string(email).map_err(|err| {
        tracing::error!("Error on extract email from token: {err}");

        GatewayError::Unauthorized(
            "Error on extract email from token".to_string(),
        )
    })?;

    // ? -----------------------------------------------------------------------
    // ? Cache email
    //
    // Cache the email in the cache.
    //
    // ? -----------------------------------------------------------------------

    set_email_in_cache(token_identifier, parsed_email.to_owned(), req).await;

    tracing::info!(
        stage = "identity.email",
        outcome = "resolved",
        "Email resolved via userinfo"
    );

    // ? -----------------------------------------------------------------------
    // ? Return email
    //
    // Return the email found in the user info url.
    //
    // ? -----------------------------------------------------------------------

    Ok(Some(parsed_email))
}