Skip to main content

authx_plugins/oauth/providers/
mod.rs

1pub mod github;
2pub mod google;
3
4use async_trait::async_trait;
5
6use authx_core::error::Result;
7
8/// OAuth tokens returned from the authorization server.
9#[derive(Debug, Clone)]
10pub struct OAuthTokens {
11    pub access_token: String,
12    pub refresh_token: Option<String>,
13    /// Token lifetime in seconds as reported by the server, if present.
14    pub expires_in: Option<u64>,
15}
16
17/// Normalized user info fetched from the provider's user-info endpoint.
18#[derive(Debug, Clone)]
19pub struct OAuthUserInfo {
20    pub provider_user_id: String,
21    pub email: String,
22    pub name: Option<String>,
23}
24
25#[async_trait]
26pub trait OAuthProvider: Send + Sync {
27    fn name(&self) -> &'static str;
28
29    /// Build the provider authorization URL the user should be redirected to.
30    fn authorization_url(&self, state: &str, pkce_challenge: &str) -> String;
31
32    /// Exchange the authorization code for tokens.
33    async fn exchange_code(
34        &self,
35        code: &str,
36        pkce_verifier: &str,
37        redirect_uri: &str,
38    ) -> Result<OAuthTokens>;
39
40    /// Fetch user info using the access token.
41    async fn fetch_user_info(&self, access_token: &str) -> Result<OAuthUserInfo>;
42}