Compare commits
No commits in common. "731f6f2f6ca60c276b61178fdd7fa452c0ed05c4" and "db133d68d8c32823211464c854a5fbe50592af6b" have entirely different histories.
731f6f2f6c
...
db133d68d8
3 changed files with 13 additions and 20 deletions
|
@ -84,7 +84,7 @@ impl UserAuthenticator {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument]
|
#[instrument]
|
||||||
pub async fn get_user_token(
|
pub async fn create_user_token(
|
||||||
&self,
|
&self,
|
||||||
username: &str,
|
username: &str,
|
||||||
password: &Password,
|
password: &Password,
|
||||||
|
@ -108,23 +108,13 @@ impl UserAuthenticator {
|
||||||
return Err(AuthenticationError::InvalidUserOrPassword);
|
return Err(AuthenticationError::InvalidUserOrPassword);
|
||||||
};
|
};
|
||||||
|
|
||||||
// if a valid token exists, return it; if not, create a new one
|
let new_token =
|
||||||
|
UserToken::from(Alphanumeric.sample_string(&mut thread_rng(), Self::TOKEN_LEN));
|
||||||
|
|
||||||
let mut db = self.db.lock().await;
|
let mut db = self.db.lock().await;
|
||||||
let token =
|
db.save_token(&username, &new_token).await?;
|
||||||
if let Some(UserTokenEntry::Valid(old_token, _, _)) = db.get_token(&username).await? {
|
|
||||||
// TODO: update old_token's last_used value
|
|
||||||
|
|
||||||
old_token
|
Ok((username, new_token))
|
||||||
} else {
|
|
||||||
let new_token =
|
|
||||||
UserToken::from(Alphanumeric.sample_string(&mut thread_rng(), Self::TOKEN_LEN));
|
|
||||||
|
|
||||||
db.save_token(&username, &new_token).await?;
|
|
||||||
|
|
||||||
new_token
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok((username, token))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument]
|
#[instrument]
|
||||||
|
|
|
@ -98,9 +98,12 @@ impl Database for SqliteDatabase {
|
||||||
valid,
|
valid,
|
||||||
created,
|
created,
|
||||||
last_used,
|
last_used,
|
||||||
}| match valid {
|
}| {
|
||||||
true => UserTokenEntry::Valid(UserToken::from(token), created, last_used),
|
if valid {
|
||||||
false => UserTokenEntry::Invalid(UserToken::from(token), created, last_used),
|
UserTokenEntry::Valid(UserToken::from(token), created, last_used)
|
||||||
|
} else {
|
||||||
|
UserTokenEntry::Invalid(UserToken::from(token), created, last_used)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
|
@ -114,7 +114,7 @@ async fn api_login(
|
||||||
event!(Level::INFO, "Generating user key");
|
event!(Level::INFO, "Generating user key");
|
||||||
|
|
||||||
let (username, user_token) = user_authenticator
|
let (username, user_token) = user_authenticator
|
||||||
.get_user_token(&username, &password)
|
.create_user_token(&username, &password)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(Json(LoginResponse {
|
Ok(Json(LoginResponse {
|
||||||
|
|
Loading…
Reference in a new issue