ayun_auth/support/
auth.rs1use crate::{
2 config,
3 support::{UserClaims, JWT},
4 Auth, AuthResult,
5};
6
7impl Auth {
8 pub fn new(inner: JWT, config: config::Auth) -> Self {
9 Self { inner, config }
10 }
11
12 pub fn try_from_config(config: config::Auth) -> AuthResult<Self> {
13 Ok(Self::new(
14 JWT::builder()
15 .secret(config.jwt.secret.to_string())
16 .algorithm(config.jwt.algorithm)
17 .build(),
18 config,
19 ))
20 }
21
22 pub fn config(self) -> config::Auth {
23 self.config
24 }
25
26 pub fn authorize(&self, uid: String) -> AuthResult<String> {
27 self.inner
28 .generate_token(uid, self.config.jwt.expiration, None)
29 }
30
31 pub fn check(&self, token: &str) -> AuthResult<UserClaims> {
32 Ok(self.inner.validate(token)?.claims)
33 }
34}
35
36impl std::ops::Deref for Auth {
37 type Target = JWT;
38
39 fn deref(&self) -> &Self::Target {
40 &self.inner
41 }
42}