1pub mod baidu;
2pub mod error;
3pub mod facebook;
4pub mod github;
5pub mod qq;
6pub mod twitter;
7pub mod wechat_open;
8pub mod weibo;
9
10mod utils;
11
12use std::collections::HashMap;
13
14use crate::error::Result;
15use async_trait::async_trait;
16use serde_json::Value;
17
18pub struct AuthConfig {
19 client_id: String,
20 client_secret: Option<String>,
21 redirect_uri: String,
22 scope: Option<Vec<String>>,
23}
24
25#[macro_export]
26macro_rules! auth_server_builder {
27 () => {
28 #[derive(Default)]
29 pub struct AuthConfigBuilder {
30 client_id: Option<String>,
31 client_secret: Option<String>,
32 redirect_uri: Option<String>,
33 scope: Option<Vec<String>>,
34 }
35
36 impl AuthorizationServer {
37 pub fn builder() -> AuthConfigBuilder {
38 AuthConfigBuilder::default()
39 }
40 }
41
42 impl AuthConfigBuilder {
43 pub fn client_id<S: Into<String>>(mut self, client_id: S) -> Self {
44 self.client_id = Some(client_id.into());
45 self
46 }
47 pub fn client_secret<S: Into<String>>(mut self, client_secret: S) -> Self {
48 self.client_secret = Some(client_secret.into());
49 self
50 }
51 pub fn redirect_uri<S: Into<String>>(mut self, redirect_uri: S) -> Self {
52 self.redirect_uri = Some(redirect_uri.into());
53 self
54 }
55 pub fn scope<S: Into<String>>(mut self, scope: S) -> Self {
56 match &mut self.scope {
57 Some(vec) => vec.push(scope.into()),
58 None => self.scope = Some(vec![scope.into()]),
59 }
60 self
61 }
62 pub fn build(self) -> AuthorizationServer {
63 AuthorizationServer {
64 config: AuthConfig {
65 client_id: self.client_id.unwrap_or_default(),
66 client_secret: self.client_secret,
67 redirect_uri: self.redirect_uri.unwrap_or_default(),
68 scope: self.scope,
69 },
70 }
71 }
72 }
73 };
74}
75
76pub trait AuthUrlProvider {
77 type AuthRequest;
78 type TokenRequest;
79 type UserInfoRequest;
80 fn authorize_url(request: Self::AuthRequest) -> Result<String>;
84
85 fn access_token_url(request: Self::TokenRequest) -> Result<String>;
88
89 fn user_info_url(request: Self::UserInfoRequest) -> Result<String>;
92}
93
94#[async_trait]
95pub trait AuthAction {
96 type AuthCallback: Send;
97 type AuthToken: Send;
98 type AuthUser;
99
100 async fn get_access_token(&self, callback: Self::AuthCallback) -> Result<Self::AuthToken>;
101
102 async fn get_user_info(&self, token: Self::AuthToken) -> Result<Self::AuthUser>;
103}
104
105#[async_trait]
106pub trait GenericAuthAction {
107 async fn authorize<S: Into<String> + Send>(&self, state: S) -> Result<String>;
108
109 async fn login<S: Into<String> + Send>(&self, callback_raw_query: S) -> Result<AuthUser>;
110}
111
112pub struct AuthUser {
113 pub user_id: String,
114 pub name: String,
115 pub access_token: String,
116 pub refresh_token: String,
117 pub expires_in: i64,
118 pub extra: HashMap<String, Value>,
119}