Skip to main content

alibi_api/plugins/oauth/providers/
vercel.rs

1//! Dedicated published Better Auth 1.7.6 Vercel factory.
2use super::remaining_profile::{ProfileKind, PublishedProfile};
3use super::{OAuthAuthorizationPolicy, OAuthProvider, OAuthTokenEndpointAuth, OAuthUserInfo};
4use serde_json::Value;
5
6#[derive(Clone)]
7pub struct VercelOptions {
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}
18impl VercelOptions {
19    #[must_use]
20    pub fn new(client_id: impl Into<String>, client_secret: Option<String>) -> Self {
21        Self {
22            client_id: client_id.into(),
23            client_secret,
24            client_key: None,
25            authorization_endpoint: None,
26            redirect_uri: None,
27            user_info_endpoint: None,
28            map_profile_to_user: None,
29            scope: Vec::new(),
30        }
31    }
32}
33impl std::fmt::Debug for VercelOptions {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        f.debug_struct("VercelOptions")
36            .field("client_id", &self.client_id)
37            .field("authorization_endpoint", &self.authorization_endpoint)
38            .finish_non_exhaustive()
39    }
40}
41impl OAuthProvider {
42    #[must_use]
43    pub fn vercel(client_id: &str, client_secret: Option<&str>) -> Self {
44        Self::vercel_with_options(VercelOptions::new(
45            client_id,
46            client_secret.map(str::to_owned),
47        ))
48    }
49    #[must_use]
50    pub fn vercel_with_options(options: VercelOptions) -> Self {
51        let mut policy = OAuthAuthorizationPolicy {
52            preserve_raw_profile_scalars: true,
53            source_profile_exceptions: true,
54            require_client_id: true,
55            login_hint: false,
56            pkce: true,
57            configured_scopes: options.scope,
58            disable_default_scopes: false,
59            authorization_code_client_key: options.client_key,
60            redirect_uri: options.redirect_uri,
61            token_endpoint_auth: Some(
62                if options
63                    .client_secret
64                    .as_ref()
65                    .is_some_and(|v| !v.is_empty())
66                {
67                    OAuthTokenEndpointAuth::ClientSecretPost
68                } else {
69                    OAuthTokenEndpointAuth::None
70                },
71            ),
72            allow_missing_access_token: true,
73            preserve_raw_email_errors: true,
74            ..OAuthAuthorizationPolicy::default()
75        };
76        let mut provider = Self {
77            client_id: options.client_id,
78            client_secret: options.client_secret.unwrap_or_default(),
79            additional_client_ids: Vec::new(),
80            hosted_domain: None,
81            require_email_verification: false,
82            auth_url: "https://vercel.com/oauth/authorize".into(),
83            token_url: "https://api.vercel.com/login/oauth/token".into(),
84            user_info_url: Some("https://api.vercel.com/login/oauth/userinfo".into()),
85            scopes: vec![],
86            authorization: None,
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            override_user_info_on_sign_in: false,
98        };
99        provider.auth_url = options
100            .authorization_endpoint
101            .filter(|v| !v.is_empty())
102            .unwrap_or(provider.auth_url);
103        provider.user_info_url = options.user_info_endpoint.or(provider.user_info_url);
104        policy.supports_refresh = false;
105        provider.authorization = Some(policy);
106        provider.get_user_info = Some(std::sync::Arc::new(PublishedProfile {
107            kind: ProfileKind::Vercel,
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::Vercel, profile)
119}