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 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
|
||||
} else {
|
||||
let new_token =
|
||||
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
|
||||
};
|
||||
|
@ -138,9 +138,9 @@ impl UserAuthenticator {
|
|||
) -> Result<(), AuthenticationError> {
|
||||
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 {
|
||||
db.update_token_last_used(username, token).await?;
|
||||
db.update_user_token_last_used(username, token).await?;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
|
26
src/db.rs
26
src/db.rs
|
@ -25,11 +25,18 @@ pub enum UserTokenEntry {
|
|||
|
||||
#[async_trait]
|
||||
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,
|
||||
username: &str,
|
||||
token: &UserToken,
|
||||
|
@ -77,7 +84,10 @@ impl SqliteDatabase {
|
|||
#[async_trait]
|
||||
impl Database for SqliteDatabase {
|
||||
#[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 {
|
||||
token: String,
|
||||
valid: bool,
|
||||
|
@ -110,7 +120,11 @@ impl Database for SqliteDatabase {
|
|||
}
|
||||
|
||||
#[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();
|
||||
query!(
|
||||
"INSERT INTO user_tokens
|
||||
|
@ -128,7 +142,7 @@ impl Database for SqliteDatabase {
|
|||
}
|
||||
|
||||
#[instrument]
|
||||
async fn update_token_last_used(
|
||||
async fn update_user_token_last_used(
|
||||
&mut self,
|
||||
username: &str,
|
||||
token: &UserToken,
|
||||
|
|
Loading…
Reference in a new issue