use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use rustauth_core::db::User;
use rustauth_core::error::RustAuthError;
use rustauth_oauth::oauth2::OAuth2Tokens;
use crate::linking_impl::NormalizedSsoProfile;
use crate::store::SsoProviderRecord;
type TxtResolverFuture = Pin<Box<dyn Future<Output = Result<Vec<String>, RustAuthError>> + Send>>;
type ProvidersLimitFuture = Pin<Box<dyn Future<Output = Result<usize, RustAuthError>> + Send>>;
type OrganizationRoleFuture = Pin<Box<dyn Future<Output = Result<String, RustAuthError>> + Send>>;
type ProvisionUserFuture = Pin<Box<dyn Future<Output = Result<(), RustAuthError>> + Send>>;
#[derive(Clone)]
pub struct DnsTxtResolver {
resolver: Arc<dyn Fn(String) -> TxtResolverFuture + Send + Sync>,
}
impl DnsTxtResolver {
pub fn new<F, Fut>(resolver: F) -> Self
where
F: Fn(String) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<Vec<String>, RustAuthError>> + Send + 'static,
{
Self {
resolver: Arc::new(move |name| Box::pin(resolver(name))),
}
}
pub async fn resolve(&self, name: &str) -> Result<Vec<String>, RustAuthError> {
(self.resolver)(name.to_owned()).await
}
}
impl std::fmt::Debug for DnsTxtResolver {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("DnsTxtResolver(..)")
}
}
impl PartialEq for DnsTxtResolver {
fn eq(&self, _other: &Self) -> bool {
true
}
}
impl Eq for DnsTxtResolver {}
#[derive(Clone)]
pub struct ProvidersLimitResolver {
resolver: Arc<dyn Fn(User) -> ProvidersLimitFuture + Send + Sync>,
}
impl ProvidersLimitResolver {
pub fn new<F, Fut>(resolver: F) -> Self
where
F: Fn(User) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<usize, RustAuthError>> + Send + 'static,
{
Self {
resolver: Arc::new(move |user| Box::pin(resolver(user))),
}
}
pub async fn resolve(&self, user: User) -> Result<usize, RustAuthError> {
(self.resolver)(user).await
}
}
impl std::fmt::Debug for ProvidersLimitResolver {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("ProvidersLimitResolver(..)")
}
}
impl PartialEq for ProvidersLimitResolver {
fn eq(&self, _other: &Self) -> bool {
true
}
}
impl Eq for ProvidersLimitResolver {}
#[derive(Debug, Clone, PartialEq)]
pub struct OrganizationRoleInput {
pub user: User,
pub profile: NormalizedSsoProfile,
pub provider: SsoProviderRecord,
pub token: Option<OAuth2Tokens>,
}
#[derive(Clone)]
pub struct OrganizationRoleResolver {
resolver: Arc<dyn Fn(OrganizationRoleInput) -> OrganizationRoleFuture + Send + Sync>,
}
impl OrganizationRoleResolver {
pub fn new<F, Fut>(resolver: F) -> Self
where
F: Fn(OrganizationRoleInput) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<String, RustAuthError>> + Send + 'static,
{
Self {
resolver: Arc::new(move |input| Box::pin(resolver(input))),
}
}
pub async fn resolve(&self, input: OrganizationRoleInput) -> Result<String, RustAuthError> {
(self.resolver)(input).await
}
}
impl std::fmt::Debug for OrganizationRoleResolver {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("OrganizationRoleResolver(..)")
}
}
impl PartialEq for OrganizationRoleResolver {
fn eq(&self, _other: &Self) -> bool {
true
}
}
impl Eq for OrganizationRoleResolver {}
#[derive(Debug, Clone, PartialEq)]
pub struct ProvisionUserInput {
pub user: User,
pub profile: NormalizedSsoProfile,
pub provider: SsoProviderRecord,
pub token: Option<OAuth2Tokens>,
pub is_register: bool,
}
#[derive(Clone)]
pub struct ProvisionUserResolver {
resolver: Arc<dyn Fn(ProvisionUserInput) -> ProvisionUserFuture + Send + Sync>,
}
impl ProvisionUserResolver {
pub fn new<F, Fut>(resolver: F) -> Self
where
F: Fn(ProvisionUserInput) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<(), RustAuthError>> + Send + 'static,
{
Self {
resolver: Arc::new(move |input| Box::pin(resolver(input))),
}
}
pub async fn resolve(&self, input: ProvisionUserInput) -> Result<(), RustAuthError> {
(self.resolver)(input).await
}
}
impl std::fmt::Debug for ProvisionUserResolver {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("ProvisionUserResolver(..)")
}
}
impl PartialEq for ProvisionUserResolver {
fn eq(&self, _other: &Self) -> bool {
true
}
}
impl Eq for ProvisionUserResolver {}