Skip to main content

alibi_api/plugins/oauth/providers/
vk.rs

1//! Dedicated published Better Auth 1.7.6 Vk factory.
2use super::remaining_profile::{ProfileKind, PublishedProfile};
3use super::{OAuthAuthorizationPolicy, OAuthProvider, OAuthTokenEndpointAuth, OAuthUserInfo};
4use serde_json::Value;
5
6#[derive(Clone)]
7pub struct VkOptions {
8    pub client_id: String,
9    pub client_secret: Option<String>,
10    pub client_key: Option<String>,
11    pub authorization_endpoint: Option<String>,
12    pub redirect_uri: Option<String>,
13    /// Trusted application transport override; it does not change profile semantics.
14    pub user_info_endpoint: Option<String>,
15    pub map_profile_to_user: Option<fn(Value) -> Result<OAuthUserInfo, String>>,
16    pub scope: Vec<String>,
17    pub disable_default_scope: bool,
18}
19impl VkOptions {
20    #[must_use]
21    pub fn new(client_id: impl Into<String>, client_secret: Option<String>) -> Self {
22        Self {
23            client_id: client_id.into(),
24            client_secret,
25            client_key: None,
26            authorization_endpoint: None,
27            redirect_uri: None,
28            user_info_endpoint: None,
29            map_profile_to_user: None,
30            scope: Vec::new(),
31            disable_default_scope: false,
32        }
33    }
34}
35impl std::fmt::Debug for VkOptions {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        f.debug_struct("VkOptions")
38            .field("client_id", &self.client_id)
39            .field("authorization_endpoint", &self.authorization_endpoint)
40            .finish_non_exhaustive()
41    }
42}
43impl OAuthProvider {
44    #[must_use]
45    pub fn vk(client_id: &str, client_secret: Option<&str>) -> Self {
46        Self::vk_with_options(VkOptions::new(client_id, client_secret.map(str::to_owned)))
47    }
48    #[must_use]
49    pub fn vk_with_options(options: VkOptions) -> Self {
50        let policy = OAuthAuthorizationPolicy {
51            preserve_raw_profile_scalars: true,
52            source_profile_exceptions: true,
53            require_client_id: true,
54            login_hint: false,
55            pkce: true,
56            configured_scopes: options.scope,
57            disable_default_scopes: options.disable_default_scope,
58            authorization_code_client_key: options.client_key,
59            redirect_uri: options.redirect_uri,
60            token_endpoint_auth: Some(
61                if options
62                    .client_secret
63                    .as_ref()
64                    .is_some_and(|v| !v.is_empty())
65                {
66                    OAuthTokenEndpointAuth::ClientSecretPost
67                } else {
68                    OAuthTokenEndpointAuth::None
69                },
70            ),
71            allow_missing_access_token: true,
72            preserve_raw_email_errors: true,
73            ..OAuthAuthorizationPolicy::default()
74        };
75        let mut provider = Self {
76            client_id: options.client_id,
77            client_secret: options.client_secret.unwrap_or_default(),
78            additional_client_ids: Vec::new(),
79            hosted_domain: None,
80            require_email_verification: false,
81            auth_url: "https://id.vk.com/authorize".into(),
82            token_url: "https://id.vk.com/oauth2/auth".into(),
83            user_info_url: Some("https://id.vk.com/oauth2/user_info".into()),
84            scopes: vec!["email".into(), "phone".into()],
85            authorization: None,
86            authorization_params: Vec::new(),
87            account_subject: Some(subject),
88            map_user_info: None,
89            get_user_info: None,
90            refresh_access_token: None,
91            verify_id_token: None,
92            id_token: None,
93            disable_id_token_sign_in: false,
94            disable_implicit_sign_up: false,
95            disable_sign_up: false,
96            override_user_info_on_sign_in: false,
97        };
98        provider.auth_url = options
99            .authorization_endpoint
100            .filter(|v| !v.is_empty())
101            .unwrap_or(provider.auth_url);
102        provider.user_info_url = options.user_info_endpoint.or(provider.user_info_url);
103        provider.authorization = Some(policy);
104        provider.get_user_info = Some(std::sync::Arc::new(PublishedProfile {
105            kind: ProfileKind::Vk,
106            endpoint: provider.user_info_url.clone(),
107            email_endpoint: None,
108            client_id: provider.client_id.clone(),
109            mapper: options.map_profile_to_user,
110            application_mapper: None,
111        }));
112        provider
113    }
114}
115fn subject(profile: &Value) -> Result<String, String> {
116    super::remaining_profile::subject(ProfileKind::Vk, profile)
117}