tuwunel_service/oauth/user_info.rs
1use serde::{Deserialize, Serialize};
2use tuwunel_core::utils::deserialize_lenient_bool;
3
4/// Selection of userinfo response claims.
5#[derive(Clone, Debug, Default, Deserialize, Serialize)]
6pub struct UserInfo {
7 /// Unique identifier number or login username. Usually a number on most
8 /// services. We consider a concatenation of the `iss` and `sub` to be a
9 /// universally unique identifier for some user/identity; we index that in
10 /// `oauthidpsub_oauthid`.
11 ///
12 /// Considered for user mxid only if none of the better fields are defined.
13 /// `login` alias intended for github.
14 #[serde(alias = "login")]
15 pub sub: String,
16
17 /// The login username we first consider when defined.
18 pub preferred_username: Option<String>,
19
20 /// The login username considered.
21 pub username: Option<String>,
22
23 /// The login username considered if none preferred.
24 pub nickname: Option<String>,
25
26 /// Full name.
27 pub name: Option<String>,
28
29 /// First name.
30 pub given_name: Option<String>,
31
32 /// Last name.
33 pub family_name: Option<String>,
34
35 /// Email address (`email` scope).
36 pub email: Option<String>,
37
38 /// Whether the provider verified `email`, false for any claim other than a
39 /// boolean or its string spelling.
40 #[serde(default, deserialize_with = "deserialize_lenient_bool")]
41 pub email_verified: Option<bool>,
42
43 /// URL to pfp (github/gitlab)
44 pub avatar_url: Option<String>,
45
46 /// URL to pfp (google)
47 pub picture: Option<String>,
48}
49
50impl UserInfo {
51 /// The email address the provider vouches for.
52 ///
53 /// An `email_verified` claim decides when present. Without one, only a
54 /// trusted provider vouches for the address it reports.
55 #[inline]
56 #[must_use]
57 pub fn verified_email(&self, trusted: bool) -> Option<&str> {
58 self.email
59 .as_deref()
60 .filter(|_| self.email_verified.unwrap_or(trusted))
61 }
62}