use bevy_ecs::prelude::{Resource, World};
use std::time::Instant;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum StdbAuthSessionSource {
Oidc,
Steam,
}
#[derive(Clone, Resource)]
pub struct StdbAuthSession {
pub access_token: String,
pub token_type: String,
pub expires_at: Option<Instant>,
pub can_refresh: bool,
pub scope: Option<String>,
pub client_id: Option<String>,
pub source: StdbAuthSessionSource,
pub post_logout_redirect_uri: Option<String>,
}
#[derive(Clone, Default, Resource)]
pub(crate) struct StdbAuthCredentialMaterial {
pub(crate) refresh_token: Option<String>,
pub(crate) id_token: Option<String>,
}
impl StdbAuthCredentialMaterial {
pub(crate) fn new(refresh_token: Option<String>, id_token: Option<String>) -> Self {
Self {
refresh_token,
id_token,
}
}
pub(crate) fn has_refresh_token(&self) -> bool {
self.refresh_token.is_some()
}
}
pub(crate) struct StdbAuthSessionParts {
pub(crate) session: StdbAuthSession,
pub(crate) credentials: StdbAuthCredentialMaterial,
}
impl StdbAuthSessionParts {
pub(crate) fn new(session: StdbAuthSession, credentials: StdbAuthCredentialMaterial) -> Self {
Self {
session,
credentials,
}
}
}
pub(crate) fn clear_session(world: &mut World) {
world.remove_resource::<StdbAuthSession>();
world.remove_resource::<StdbAuthCredentialMaterial>();
}
#[cfg(test)]
mod tests {
use super::*;
fn session() -> StdbAuthSession {
StdbAuthSession {
access_token: "access".to_string(),
token_type: "Bearer".to_string(),
expires_at: None,
can_refresh: true,
scope: None,
client_id: Some("client".to_string()),
source: StdbAuthSessionSource::Oidc,
post_logout_redirect_uri: None,
}
}
#[test]
fn clear_session_removes_session_and_credentials() {
let mut world = World::new();
world.insert_resource(session());
world.insert_resource(StdbAuthCredentialMaterial::new(
Some("refresh".to_string()),
Some("id".to_string()),
));
clear_session(&mut world);
assert!(!world.contains_resource::<StdbAuthSession>());
assert!(!world.contains_resource::<StdbAuthCredentialMaterial>());
}
}