ayun-auth 0.23.0

The RUST Framework for Web Rustceans.
Documentation
pub mod config;
mod instance;
pub mod jwt;

use crate::jwt::{UserClaims, JWT};
use ayun_core::{Error, Result};

pub struct Auth {
    inner: JWT, // todo dyn auth trait
    config: config::Auth,
}

impl Auth {
    pub fn new(inner: JWT, config: config::Auth) -> Self {
        Self { inner, config }
    }

    pub fn try_from_config(config: config::Auth) -> Result<Self, Error> {
        Ok(Self::new(
            JWT::builder()
                .secret(config.jwt.secret.to_string())
                .algorithm(config.jwt.algorithm)
                .build(),
            config,
        ))
    }

    pub fn config(self) -> config::Auth {
        self.config
    }

    pub fn authorize(&self, uid: String) -> Result<String, Error> {
        Ok(self
            .inner
            .generate_token(uid, self.config.jwt.expiration, None)?)
    }

    pub fn check(&self, token: &str) -> Result<UserClaims, Error> {
        Ok(self.inner.validate(token)?.claims)
    }
}

impl std::ops::Deref for Auth {
    type Target = JWT;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}