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
use axum::response::Redirect;
use openidconnect::EndUserUsername;

use crate::web::utils::redirect_to_home;

use super::CustomClaimType;

pub trait CustomClaimTypeThings {
    fn get_displayname(&self) -> String;
    fn get_email(&self) -> Result<String, Redirect>;
    fn get_username(&self) -> String;
}

impl CustomClaimTypeThings for CustomClaimType {
    fn get_email(&self) -> Result<String, Redirect> {
        let email: String;
        if let Some(user_email) = self.email() {
            email = user_email.to_string();
        } else if let Some(user_email) = self.preferred_username() {
            email = user_email.to_string();
        } else {
            log::error!("Couldn't extract email address from claim: {self:?}");
            return Err(redirect_to_home());
        }
        Ok(email)
    }
    fn get_displayname(&self) -> String {
        let mut displayname: String = "Anonymous Kid".to_string();
        if let Some(name) = self.name() {
            if let Some(username) = name.iter().next() {
                displayname = username.1.to_string();
            }
        }
        displayname
    }
    fn get_username(&self) -> String {
        let default = EndUserUsername::new("".to_string());
        self.preferred_username().unwrap_or(&default).to_string()
    }
}

#[test]
fn custom_claim_type_things() {
    use url::Url;

    use openidconnect::{
        EmptyAdditionalClaims, EndUserEmail, IssuerUrl, StandardClaims, SubjectIdentifier,
    };
    let cct = CustomClaimType::new(
        IssuerUrl::from_url(Url::parse("https://example.com").unwrap()),
        vec![],
        chrono::Utc::now(),
        chrono::Utc::now(),
        StandardClaims::new(SubjectIdentifier::new("example_identifier".to_string()))
            .set_email(Some(EndUserEmail::new("billy@goat.net".to_string()))),
        EmptyAdditionalClaims::default(),
    );

    assert_eq!(cct.get_displayname(), "Anonymous Kid".to_string());

    assert_eq!("".to_string(), cct.get_username());

    assert_eq!("billy@goat.net".to_string(), cct.get_email().unwrap());
}