Skip to main content

alibi_plugins/oauth/providers/
spotify.rs

1//! Dedicated published Better Auth 1.7.6 Spotify factory.
2use super::remaining_profile::{ProfileKind, PublishedProfile};
3use super::{OAuthAuthorizationPolicy, OAuthProvider, OAuthTokenEndpointAuth, OAuthUserInfo};
4use serde_json::Value;
5
6#[derive(Clone)]
7pub struct SpotifyOptions {
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 SpotifyOptions {
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 SpotifyOptions {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        f.debug_struct("SpotifyOptions")
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 spotify(client_id: &str, client_secret: Option<&str>) -> Self {
46        Self::spotify_with_options(SpotifyOptions::new(
47            client_id,
48            client_secret.map(str::to_owned),
49        ))
50    }
51    #[must_use]
52    pub fn spotify_with_options(options: SpotifyOptions) -> Self {
53        let policy = OAuthAuthorizationPolicy {
54            preserve_raw_profile_scalars: true,
55            source_profile_exceptions: true,
56            require_client_id: true,
57            login_hint: false,
58            pkce: true,
59            configured_scopes: options.scope,
60            disable_default_scopes: options.disable_default_scope,
61            authorization_code_client_key: options.client_key,
62            redirect_uri: options.redirect_uri,
63            token_endpoint_auth: Some(
64                if options
65                    .client_secret
66                    .as_ref()
67                    .is_some_and(|v| !v.is_empty())
68                {
69                    OAuthTokenEndpointAuth::ClientSecretPost
70                } else {
71                    OAuthTokenEndpointAuth::None
72                },
73            ),
74            allow_missing_access_token: true,
75            preserve_raw_email_errors: true,
76            ..OAuthAuthorizationPolicy::default()
77        };
78        let mut provider = Self {
79            client_id: options.client_id,
80            client_secret: options.client_secret.unwrap_or_default(),
81            additional_client_ids: Vec::new(),
82            hosted_domain: None,
83            require_email_verification: false,
84            auth_url: "https://accounts.spotify.com/authorize".into(),
85            token_url: "https://accounts.spotify.com/api/token".into(),
86            user_info_url: Some("https://api.spotify.com/v1/me".into()),
87            scopes: vec!["user-read-email".into()],
88            authorization: None,
89            allowed_request_params: Vec::new(),
90            authorization_params: Vec::new(),
91            account_subject: Some(subject),
92            map_user_info: None,
93            get_user_info: None,
94            refresh_access_token: None,
95            verify_id_token: None,
96            id_token: None,
97            disable_id_token_sign_in: false,
98            disable_implicit_sign_up: false,
99            disable_sign_up: false,
100            allow_idp_initiated: false,
101            override_user_info_on_sign_in: false,
102        };
103        provider.auth_url = options
104            .authorization_endpoint
105            .filter(|v| !v.is_empty())
106            .unwrap_or(provider.auth_url);
107        provider.user_info_url = options.user_info_endpoint.or(provider.user_info_url);
108        provider.authorization = Some(policy);
109        provider.get_user_info = Some(std::sync::Arc::new(PublishedProfile {
110            kind: ProfileKind::Spotify,
111            endpoint: provider.user_info_url.clone(),
112            email_endpoint: None,
113            client_id: provider.client_id.clone(),
114            mapper: options.map_profile_to_user,
115            application_mapper: None,
116        }));
117        provider
118    }
119}
120fn subject(profile: &Value) -> Result<String, String> {
121    super::remaining_profile::subject(ProfileKind::Spotify, profile)
122}