use std::fmt;
pub use day_bridge::Support;
pub mod apple;
pub mod authorize;
pub mod crypto;
#[cfg(all(feature = "demo-provider", not(target_arch = "wasm32")))]
pub mod demo_provider;
mod flow;
pub mod google;
mod http;
pub mod jwt;
mod native;
pub mod passkeys;
pub mod provider;
pub mod session;
pub mod tokens;
mod urls;
pub use authorize::{Authorization, Client, Redirect};
pub use provider::Provider;
pub use session::SessionOptions;
pub use tokens::{IdToken, TokenSet};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AuthError {
Unsupported,
Cancelled,
Timeout,
InvalidRequest(String),
Provider {
error: String,
description: Option<String>,
},
StateMismatch,
InvalidToken(String),
Network(String),
Platform(String),
}
impl fmt::Display for AuthError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AuthError::Unsupported => write!(f, "not supported on this platform"),
AuthError::Cancelled => write!(f, "cancelled"),
AuthError::Timeout => write!(f, "the callback did not arrive in time"),
AuthError::InvalidRequest(why) => write!(f, "invalid request: {why}"),
AuthError::Provider { error, description } => match description {
Some(d) => write!(f, "{error}: {d}"),
None => write!(f, "{error}"),
},
AuthError::StateMismatch => write!(f, "the callback's state does not match"),
AuthError::InvalidToken(why) => write!(f, "invalid token: {why}"),
AuthError::Network(why) => write!(f, "network: {why}"),
AuthError::Platform(why) => write!(f, "platform error: {why}"),
}
}
}
impl std::error::Error for AuthError {}
impl From<day_bridge::Error> for AuthError {
fn from(e: day_bridge::Error) -> Self {
match e {
day_bridge::Error::Unsupported => AuthError::Unsupported,
day_bridge::Error::Foreign(message) => match message.as_str() {
"cancelled" => AuthError::Cancelled,
"unsupported" => AuthError::Unsupported,
_ => AuthError::Platform(message),
},
day_bridge::Error::Encoding => AuthError::Platform("bridge encoding error".into()),
day_bridge::Error::Runtime => {
AuthError::Platform("platform runtime unavailable".into())
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn errors_display_and_convert() {
for e in [
AuthError::Unsupported,
AuthError::Cancelled,
AuthError::Timeout,
AuthError::InvalidRequest("x".into()),
AuthError::Provider {
error: "e".into(),
description: None,
},
AuthError::Provider {
error: "e".into(),
description: Some("d".into()),
},
AuthError::StateMismatch,
AuthError::InvalidToken("x".into()),
AuthError::Network("x".into()),
AuthError::Platform("x".into()),
] {
assert_ne!(e.to_string(), "");
}
assert_eq!(
AuthError::from(day_bridge::Error::Foreign("cancelled".into())),
AuthError::Cancelled
);
assert_eq!(
AuthError::from(day_bridge::Error::Unsupported),
AuthError::Unsupported
);
}
}