alibi_plugins/oauth/providers/
vk.rs1use 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 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 allowed_request_params: Vec::new(),
87 authorization_params: Vec::new(),
88 account_subject: Some(subject),
89 map_user_info: None,
90 get_user_info: None,
91 refresh_access_token: None,
92 verify_id_token: None,
93 id_token: None,
94 disable_id_token_sign_in: false,
95 disable_implicit_sign_up: false,
96 disable_sign_up: false,
97 allow_idp_initiated: false,
98 override_user_info_on_sign_in: false,
99 };
100 provider.auth_url = options
101 .authorization_endpoint
102 .filter(|v| !v.is_empty())
103 .unwrap_or(provider.auth_url);
104 provider.user_info_url = options.user_info_endpoint.or(provider.user_info_url);
105 provider.authorization = Some(policy);
106 provider.get_user_info = Some(std::sync::Arc::new(PublishedProfile {
107 kind: ProfileKind::Vk,
108 endpoint: provider.user_info_url.clone(),
109 email_endpoint: None,
110 client_id: provider.client_id.clone(),
111 mapper: options.map_profile_to_user,
112 application_mapper: None,
113 }));
114 provider
115 }
116}
117fn subject(profile: &Value) -> Result<String, String> {
118 super::remaining_profile::subject(ProfileKind::Vk, profile)
119}