Skip to main content

alibi_api/plugins/oauth/providers/
google.rs

1use super::*;
2impl OAuthProvider {
3    #[must_use]
4    pub fn with_hosted_domain(mut self, domain: impl Into<String>) -> Self {
5        let domain = domain.into();
6        self.authorization_params.retain(|(key, _)| key != "hd");
7        self.authorization_params
8            .push(("hd".into(), domain.clone()));
9        self.hosted_domain = Some(domain);
10        self
11    }
12
13    #[must_use]
14    pub fn google(client_id: &str, client_secret: &str) -> Self {
15        Self {
16            client_id: client_id.to_owned(),
17            additional_client_ids: Vec::new(),
18            hosted_domain: None,
19            require_email_verification: false,
20            client_secret: client_secret.to_owned(),
21            auth_url: "https://accounts.google.com/o/oauth2/v2/auth".to_owned(),
22            token_url: "https://oauth2.googleapis.com/token".to_owned(),
23            user_info_url: Some("https://www.googleapis.com/oauth2/v3/userinfo".to_owned()),
24            scopes: vec![
25                "email".to_owned(),
26                "profile".to_owned(),
27                "openid".to_owned(),
28            ],
29            authorization: Some(OAuthAuthorizationPolicy {
30                require_client_secret: true,
31                ..Default::default()
32            }),
33            authorization_params: vec![("include_granted_scopes".to_owned(), "true".to_owned())],
34            account_subject: None,
35            map_user_info: Some(|v| {
36                Ok(OAuthUserInfo {
37                    additional_fields: Default::default(),
38                    id: v
39                        .get("sub")
40                        .and_then(|v| v.as_str())
41                        .ok_or("missing sub")?
42                        .to_owned(),
43                    email: v
44                        .get("email")
45                        .and_then(|v| v.as_str())
46                        .unwrap_or_default()
47                        .to_owned(),
48                    name: v.get("name").and_then(|v| v.as_str()).map(String::from),
49                    image: v.get("picture").and_then(|v| v.as_str()).map(String::from),
50                    email_verified: v
51                        .get("email_verified")
52                        .and_then(Value::as_bool)
53                        .unwrap_or(false),
54                })
55            }),
56            get_user_info: None,
57            refresh_access_token: None,
58            verify_id_token: None,
59            id_token: Some(super::super::id_token::OAuthIdTokenConfig::google()),
60            disable_id_token_sign_in: false,
61            disable_implicit_sign_up: false,
62            disable_sign_up: false,
63            override_user_info_on_sign_in: false,
64        }
65    }
66}