diff options
author | Santo Cariotti <santo@dcariotti.me> | 2022-08-23 18:57:16 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2022-08-23 18:57:16 +0200 |
commit | 2231ab6730552d3b58b00ae42d490743a29d7c8b (patch) | |
tree | 4a9612ec08398b8abc2e4ce7c30825f7ef18659c /server/src/models | |
parent | bba5f252f265e6102d2f052e9ca100efe318508f (diff) |
Add docs
Diffstat (limited to 'server/src/models')
-rw-r--r-- | server/src/models/auth.rs | 10 | ||||
-rw-r--r-- | server/src/models/user.rs | 9 |
2 files changed, 19 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 diff --git a/server/src/models/user.rs b/server/src/models/user.rs index dd96f90..06cde0a 100644 --- a/server/src/models/user.rs +++ b/server/src/models/user.rs @@ -4,6 +4,7 @@ use crate::errors::AppError; use serde::{Deserialize, Serialize}; use validator::Validate; +/// User model #[derive(Deserialize, Serialize, Validate)] pub struct User { id: i32, @@ -14,13 +15,16 @@ pub struct User { is_staff: Option<bool>, } +/// Response used to print a user (or a users list) #[derive(Deserialize, Serialize)] pub struct UserList { + // It is public because it used by `Claims` creation pub id: i32, email: String, is_staff: Option<bool>, } +/// Payload used for user creation #[derive(Deserialize)] pub struct UserCreate { pub email: String, @@ -28,6 +32,7 @@ pub struct UserCreate { } impl User { + /// By default an user has id = 0. It is not created yet pub fn new(email: String, password: String) -> Self { Self { id: 0, @@ -37,6 +42,7 @@ impl User { } } + /// Create a new user from the model using a SHA256 crypted password pub async fn create(user: User) -> Result<UserList, AppError> { let pool = unsafe { get_client() }; @@ -61,6 +67,7 @@ impl User { Ok(rec) } + /// Find a user using the model. It used for login pub async fn find(user: User) -> Result<UserList, AppError> { let pool = unsafe { get_client() }; @@ -81,6 +88,7 @@ impl User { Ok(rec) } + /// Returns the user with id = `user_id` pub async fn find_by_id(user_id: i32) -> Result<UserList, AppError> { let pool = unsafe { get_client() }; @@ -98,6 +106,7 @@ impl User { Ok(rec) } + /// List all users pub async fn list() -> Result<Vec<UserList>, AppError> { let pool = unsafe { get_client() }; let rows = sqlx::query_as!(UserList, r#"SELECT id, email, is_staff FROM users"#) |