summaryrefslogtreecommitdiff
path: root/server/src/models/auth.rs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/models/auth.rs')
-rw-r--r--server/src/models/auth.rs10
1 files changed, 10 insertions, 0 deletions
diff --git a/server/src/models/auth.rs b/server/src/models/auth.rs
index 573f5d1..8b8f61c 100644
--- a/server/src/models/auth.rs
+++ b/server/src/models/auth.rs
@@ -14,15 +14,21 @@ struct Keys {
decoding: DecodingKey,
}
+/// Claims struct
#[derive(Serialize, Deserialize)]
pub struct Claims {
+ /// ID from the user model
user_id: i32,
+ /// Expiration timestamp
exp: usize,
}
+/// Body used as response to login
#[derive(Serialize)]
pub struct AuthBody {
+ /// Access token string
access_token: String,
+ /// "Bearer" string
token_type: String,
}
@@ -41,6 +47,7 @@ impl Keys {
}
impl Claims {
+ /// Create a new Claim using the `user_id` and the current timestamp + 2 days
pub fn new(user_id: i32) -> Self {
let expiration = Local::now() + Duration::days(2);
@@ -50,6 +57,8 @@ impl Claims {
}
}
+ /// Returns the token as a string. If a token is not encoded, raises an
+ /// `AppError::TokenCreation`
pub fn get_token(&self) -> Result<String, AppError> {
let token = encode(&Header::default(), &self, &KEYS.encoding)
.map_err(|_| AppError::TokenCreation)?;
@@ -67,6 +76,7 @@ impl AuthBody {
}
}
+/// Parse a request to get the Authorization header and then decode it checking its validation
#[async_trait]
impl<B> FromRequest<B> for Claims
where