-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eaad709
commit f726e42
Showing
29 changed files
with
1,699 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE authentication_key; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE TABLE authentication_key ( | ||
id bigserial PRIMARY KEY NOT NULL, | ||
user_id bigint NOT NULL, | ||
key text NOT NULL, | ||
key_type text NOT NULL, | ||
name text NOT NULL, | ||
created timestamp without time zone NOT NULL, | ||
FOREIGN KEY(user_id) REFERENCES "user"(id) ON DELETE CASCADE | ||
-- TODO: Hardware key relation | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use std::fmt::{self, Display, Formatter}; | ||
|
||
use chrono::{NaiveDateTime, Utc}; | ||
use model_derive::Model; | ||
use sqlx::{query_as, Error as SqlxError}; | ||
|
||
use crate::db::DbPool; | ||
|
||
#[derive(Clone, Deserialize, Model, Serialize, Debug)] | ||
#[table(authentication_key)] | ||
pub struct AuthenticationKey { | ||
id: Option<i64>, | ||
pub user_id: i64, | ||
pub key: String, | ||
pub name: String, | ||
pub key_type: String, | ||
pub created: NaiveDateTime, | ||
} | ||
|
||
impl Display for AuthenticationKey { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | ||
match self.id { | ||
Some(id) => write!(f, "[ID {}] {}", id, self.name), | ||
None => write!(f, "{}", self.name), | ||
} | ||
} | ||
} | ||
|
||
impl AuthenticationKey { | ||
#[must_use] | ||
pub fn new(user_id: i64, key: String, name: String, key_type: String) -> Self { | ||
Self { | ||
id: None, | ||
user_id, | ||
key, | ||
name, | ||
key_type, | ||
created: Utc::now().naive_utc(), | ||
} | ||
} | ||
|
||
pub async fn fetch_user_authentication_keys( | ||
pool: &DbPool, | ||
user_id: i64, | ||
) -> Result<Vec<Self>, SqlxError> { | ||
query_as!( | ||
Self, | ||
"SELECT id \"id?\", user_id, key, name, key_type, created | ||
FROM authentication_key WHERE user_id = $1", | ||
user_id, | ||
) | ||
.fetch_all(pool) | ||
.await | ||
} | ||
|
||
pub async fn fetch_user_authentication_keys_by_type( | ||
pool: &DbPool, | ||
user_id: i64, | ||
key_type: &str, | ||
) -> Result<Vec<Self>, SqlxError> { | ||
query_as!( | ||
Self, | ||
"SELECT id \"id?\", user_id, key, name, key_type, created | ||
FROM authentication_key WHERE user_id = $1 AND key_type = $2", | ||
user_id, | ||
key_type, | ||
) | ||
.fetch_all(pool) | ||
.await | ||
} | ||
|
||
pub async fn find_by_user( | ||
pool: &DbPool, | ||
user_id: i64, | ||
key: String, | ||
) -> Result<Option<Self>, SqlxError> { | ||
query_as!( | ||
Self, | ||
"SELECT id \"id?\", user_id, key, name, key_type, created | ||
FROM authentication_key WHERE user_id = $1 AND key = $2", | ||
user_id, | ||
key, | ||
) | ||
.fetch_optional(pool) | ||
.await | ||
} | ||
|
||
pub async fn find_authentication_key(&self, pool: &DbPool) -> Result<Option<Self>, SqlxError> { | ||
query_as!( | ||
Self, | ||
"SELECT id \"id?\", user_id, key, name, key_type, created | ||
FROM authentication_key WHERE user_id = $1 AND key = $2", | ||
self.user_id, | ||
self.key, | ||
) | ||
.fetch_optional(pool) | ||
.await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.