use crate::{
config,
support::{UserClaims, JWT},
Auth, AuthResult,
};
impl Auth {
pub fn new(inner: JWT, config: config::Auth) -> Self {
Self { inner, config }
}
pub fn try_from_config(config: config::Auth) -> AuthResult<Self> {
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) -> AuthResult<String> {
self.inner
.generate_token(uid, self.config.jwt.expiration, None)
}
pub fn check(&self, token: &str) -> AuthResult<UserClaims> {
Ok(self.inner.validate(token)?.claims)
}
}
impl std::ops::Deref for Auth {
type Target = JWT;
fn deref(&self) -> &Self::Target {
&self.inner
}
}