shield 0.2.1

Web authentication for Rust.
Documentation
use crate::{
    error::ShieldError,
    form::{Form, Input, InputType, InputTypeSubmit, InputValue},
    provider::Provider,
    session::MethodSession,
};

const ACTION_ID: &str = "sign-out";
const ACTION_NAME: &str = "Sign out";

// TODO: Sign out should be a global action that is independent of the method.
// TODO: Add hooks, so the method can still perform custom sign out.

pub struct SignOutAction;

impl SignOutAction {
    pub fn id() -> String {
        ACTION_ID.to_owned()
    }

    pub fn name() -> String {
        ACTION_NAME.to_owned()
    }

    pub fn condition<P: Provider, S>(
        provider: &P,
        session: &MethodSession<S>,
    ) -> Result<bool, ShieldError> {
        Ok(session
            .base
            .authentication
            .as_ref()
            .is_some_and(|authentication| {
                authentication.method_id == provider.method_id()
                    && authentication.provider_id == provider.id()
            }))
    }

    pub async fn forms<P: Provider>(_provider: P) -> Result<Vec<Form>, ShieldError> {
        Ok(vec![Form {
            inputs: vec![Input {
                name: "submit".to_owned(),
                label: None,
                r#type: InputType::Submit(InputTypeSubmit {}),
                value: Some(InputValue::String {
                    value: Self::name(),
                }),
                addon_start: None,
                addon_end: None,
            }],
        }])
    }
}