use crate::error::GarrisonResult;
use async_trait::async_trait;
use serde_json::Value;
pub mod provider_names {
pub const WECHAT: &str = "wechat";
pub const ALIPAY: &str = "alipay";
pub const WECHAT_MINI_APP: &str = "wechat_mini_app";
}
#[derive(Clone)]
pub struct SocialUserInfo {
pub provider: String,
pub provider_user_id: String,
pub nickname: Option<String>,
pub avatar: Option<String>,
pub union_id: Option<String>,
pub raw: Value,
}
impl std::fmt::Debug for SocialUserInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SocialUserInfo")
.field("provider", &self.provider)
.field("provider_user_id", &self.provider_user_id)
.field("nickname", &self.nickname)
.field("avatar", &self.avatar)
.field("union_id", &self.union_id)
.field(
"raw",
&format_args!(
"<redacted: {} bytes>",
serde_json::to_vec(&self.raw).map(|v| v.len()).unwrap_or(0)
),
)
.finish()
}
}
#[cfg(feature = "social-wechat")]
pub mod wechat;
#[cfg(feature = "social-alipay")]
pub mod alipay;
pub mod urlencoding;
pub mod validation;
#[cfg(any(feature = "db-sqlite", feature = "db-postgres", feature = "db-mysql"))]
pub struct SocialBindingService {
pub dao: std::sync::Arc<dyn crate::dao::GarrisonDao>,
}
#[cfg(any(feature = "db-sqlite", feature = "db-postgres", feature = "db-mysql"))]
pub(crate) mod service;
pub mod registry;
pub use registry::ERR_SOCIAL_PROVIDER_NOT_REGISTERED;
pub use registry::ERR_SOCIAL_PROVIDER_NAME_INVALID;
#[async_trait]
pub trait SocialLoginProvider: Send + Sync {
async fn get_authorization_url(
&self,
state: &str,
redirect_uri: &str,
) -> GarrisonResult<String>;
async fn exchange_token(&self, code: &str, state: &str) -> GarrisonResult<SocialUserInfo>;
async fn get_user_info(&self, access_token: &str) -> GarrisonResult<SocialUserInfo>;
}
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;