firebase_rs_sdk/auth/oauth/
mod.rs

1pub mod credential;
2pub mod pkce;
3pub mod provider;
4pub mod providers;
5pub mod redirect;
6
7use pkce::PkcePair;
8
9use std::collections::HashMap;
10
11use crate::auth::error::AuthResult;
12use crate::auth::model::AuthCredential;
13
14/// Parameters needed to initiate an OAuth identity provider flow.
15///
16/// Consumers construct the final authorization URL using the provided endpoint
17/// and parameters. These values mirror the Firebase JS SDK `AuthEventManager`
18/// inputs, allowing a 1:1 translation for popup and redirect handlers.
19#[derive(Debug, Clone)]
20pub struct OAuthRequest {
21    /// Provider identifier (e.g. `google.com`).
22    pub provider_id: String,
23    /// Fully qualified authorization URL.
24    pub auth_url: String,
25    /// Optional human-readable hint to display in custom UI.
26    pub display_name: Option<String>,
27    /// Optional locale hint.
28    pub language_code: Option<String>,
29    /// Additional query parameters to include when opening the provider.
30    pub custom_parameters: HashMap<String, String>,
31    /// Optional PKCE verifier/challenge pair for this request.
32    pub pkce: Option<PkcePair>,
33}
34
35impl OAuthRequest {
36    pub fn new(provider_id: impl Into<String>, auth_url: impl Into<String>) -> Self {
37        Self {
38            provider_id: provider_id.into(),
39            auth_url: auth_url.into(),
40            display_name: None,
41            language_code: None,
42            custom_parameters: HashMap::new(),
43            pkce: None,
44        }
45    }
46
47    pub fn with_display_name(mut self, value: impl Into<String>) -> Self {
48        self.display_name = Some(value.into());
49        self
50    }
51
52    pub fn with_language_code(mut self, value: impl Into<String>) -> Self {
53        self.language_code = Some(value.into());
54        self
55    }
56
57    pub fn with_custom_parameters(mut self, parameters: HashMap<String, String>) -> Self {
58        self.custom_parameters = parameters;
59        self
60    }
61
62    pub fn with_pkce(mut self, pkce: Option<PkcePair>) -> Self {
63        self.pkce = pkce;
64        self
65    }
66
67    pub fn pkce(&self) -> Option<&PkcePair> {
68        self.pkce.as_ref()
69    }
70}
71
72/// Handles OAuth popup operations for interactive sign-in flows.
73///
74/// Implementations should open a browser window/dialog, complete the
75/// authorization handshake, and return an [`crate::auth::model::AuthCredential`] produced from the
76/// provider response. The handler is free to block the current thread or spawn
77/// an async task; the library does not impose scheduling requirements.
78pub trait OAuthPopupHandler: Send + Sync {
79    fn open_popup(&self, request: OAuthRequest) -> AuthResult<AuthCredential>;
80}
81
82/// Handles OAuth redirect-based flows.
83///
84/// Redirect flows require two phases:
85/// 1. Call `initiate_redirect` before leaving the current context.
86/// 2. After the application reloads/returns, call `complete_redirect` to
87///    resolve the awaited credential.
88pub trait OAuthRedirectHandler: Send + Sync {
89    fn initiate_redirect(&self, request: OAuthRequest) -> AuthResult<()>;
90    fn complete_redirect(&self) -> AuthResult<Option<AuthCredential>>;
91}