db: rename methods to clarify token kind
This commit is contained in:
parent
a50660ddc3
commit
d1f8e461b1
2 changed files with 24 additions and 10 deletions
|
@ -113,13 +113,13 @@ impl UserAuthenticator {
|
||||||
|
|
||||||
let mut db = self.db.lock().await;
|
let mut db = self.db.lock().await;
|
||||||
let token =
|
let token =
|
||||||
if let Some(UserTokenEntry::Valid(old_token, _, _)) = db.get_token(&username).await? {
|
if let Some(UserTokenEntry::Valid(old_token, _, _)) = db.get_user_token(&username).await? {
|
||||||
old_token
|
old_token
|
||||||
} else {
|
} else {
|
||||||
let new_token =
|
let new_token =
|
||||||
UserToken::from(Alphanumeric.sample_string(&mut thread_rng(), Self::TOKEN_LEN));
|
UserToken::from(Alphanumeric.sample_string(&mut thread_rng(), Self::TOKEN_LEN));
|
||||||
|
|
||||||
db.save_token(&username, &new_token).await?;
|
db.save_user_token(&username, &new_token).await?;
|
||||||
|
|
||||||
new_token
|
new_token
|
||||||
};
|
};
|
||||||
|
@ -138,9 +138,9 @@ impl UserAuthenticator {
|
||||||
) -> Result<(), AuthenticationError> {
|
) -> Result<(), AuthenticationError> {
|
||||||
let mut db = self.db.lock().await;
|
let mut db = self.db.lock().await;
|
||||||
|
|
||||||
if let Some(UserTokenEntry::Valid(user_token, ..)) = &db.get_token(username).await? {
|
if let Some(UserTokenEntry::Valid(user_token, ..)) = &db.get_user_token(username).await? {
|
||||||
if token == user_token {
|
if token == user_token {
|
||||||
db.update_token_last_used(username, token).await?;
|
db.update_user_token_last_used(username, token).await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
26
src/db.rs
26
src/db.rs
|
@ -25,11 +25,18 @@ pub enum UserTokenEntry {
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait Database: Debug {
|
pub trait Database: Debug {
|
||||||
async fn get_token(&mut self, username: &str) -> Result<Option<UserTokenEntry>, sqlx::Error>;
|
async fn get_user_token(
|
||||||
|
&mut self,
|
||||||
|
username: &str,
|
||||||
|
) -> Result<Option<UserTokenEntry>, sqlx::Error>;
|
||||||
|
|
||||||
async fn save_token(&mut self, username: &str, token: &UserToken) -> Result<(), sqlx::Error>;
|
async fn save_user_token(
|
||||||
|
&mut self,
|
||||||
|
username: &str,
|
||||||
|
token: &UserToken,
|
||||||
|
) -> Result<(), sqlx::Error>;
|
||||||
|
|
||||||
async fn update_token_last_used(
|
async fn update_user_token_last_used(
|
||||||
&mut self,
|
&mut self,
|
||||||
username: &str,
|
username: &str,
|
||||||
token: &UserToken,
|
token: &UserToken,
|
||||||
|
@ -77,7 +84,10 @@ impl SqliteDatabase {
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl Database for SqliteDatabase {
|
impl Database for SqliteDatabase {
|
||||||
#[instrument]
|
#[instrument]
|
||||||
async fn get_token(&mut self, username: &str) -> Result<Option<UserTokenEntry>, sqlx::Error> {
|
async fn get_user_token(
|
||||||
|
&mut self,
|
||||||
|
username: &str,
|
||||||
|
) -> Result<Option<UserTokenEntry>, sqlx::Error> {
|
||||||
struct TokenRow {
|
struct TokenRow {
|
||||||
token: String,
|
token: String,
|
||||||
valid: bool,
|
valid: bool,
|
||||||
|
@ -110,7 +120,11 @@ impl Database for SqliteDatabase {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument]
|
#[instrument]
|
||||||
async fn save_token(&mut self, username: &str, token: &UserToken) -> Result<(), sqlx::Error> {
|
async fn save_user_token(
|
||||||
|
&mut self,
|
||||||
|
username: &str,
|
||||||
|
token: &UserToken,
|
||||||
|
) -> Result<(), sqlx::Error> {
|
||||||
let token_inner = token.0.expose_secret();
|
let token_inner = token.0.expose_secret();
|
||||||
query!(
|
query!(
|
||||||
"INSERT INTO user_tokens
|
"INSERT INTO user_tokens
|
||||||
|
@ -128,7 +142,7 @@ impl Database for SqliteDatabase {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument]
|
#[instrument]
|
||||||
async fn update_token_last_used(
|
async fn update_user_token_last_used(
|
||||||
&mut self,
|
&mut self,
|
||||||
username: &str,
|
username: &str,
|
||||||
token: &UserToken,
|
token: &UserToken,
|
||||||
|
|
Loading…
Reference in a new issue