use std::any::Any;
use std::fmt::Debug;
use async_trait::async_trait;
use pubky_common::crypto::PublicKey;
use super::SessionInfo;
use reqwest::{RequestBuilder, Response, StatusCode};
use crate::{PubkyHttpClient, errors::Result};
#[cfg(target_arch = "wasm32")]
use crate::actors::auth::grant::shared_session::GrantSessionLease;
pub(crate) fn credential_session_missing(response: &Response) -> bool {
matches!(
response.status(),
StatusCode::NOT_FOUND | StatusCode::UNAUTHORIZED
)
}
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
pub(crate) trait SessionCredential: Debug + Send + Sync {
fn info(&self) -> SessionInfo;
async fn signout(&self, client: &PubkyHttpClient) -> Result<()>;
async fn attach(&self, rb: RequestBuilder, client: &PubkyHttpClient) -> Result<RequestBuilder>;
async fn send(&self, rb: RequestBuilder, client: &PubkyHttpClient) -> Result<Response> {
Ok(self.attach(rb, client).await?.send().await?)
}
#[cfg(target_arch = "wasm32")]
async fn prepare_external(
&self,
rb: RequestBuilder,
client: &PubkyHttpClient,
) -> Result<(RequestBuilder, Option<Box<dyn GrantSessionLease>>)> {
Ok((self.attach(rb, client).await?, None))
}
#[cfg(target_arch = "wasm32")]
async fn refresh_rejected_bearer(
&self,
_bearer: &str,
_client: &PubkyHttpClient,
) -> Result<bool> {
Ok(false)
}
async fn can_attach_to(&self, homeserver: &PublicKey) -> bool;
async fn revalidate(
&self,
client: &PubkyHttpClient,
user: &PublicKey,
) -> Result<Option<SessionInfo>>;
fn as_any(&self) -> &dyn Any;
}