Skip to main content

alibi_plugins/oauth/providers/
google.rs

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