distinguish invalid token from database error
This commit is contained in:
parent
e56c7525e2
commit
51865c365c
2 changed files with 9 additions and 10 deletions
|
@ -63,10 +63,10 @@ impl UserAuthenticator {
|
|||
) -> Result<(), AuthenticationError> {
|
||||
let mut db = self.db.lock().await;
|
||||
|
||||
// TODO: (in db) distinguish between invalid token and SQLX error
|
||||
db.get_token(username).await?;
|
||||
|
||||
Ok(())
|
||||
match db.get_token(username).await? {
|
||||
Some(_) => Ok(()),
|
||||
None => Err(AuthenticationError::InvalidToken),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
11
src/db.rs
11
src/db.rs
|
@ -11,7 +11,7 @@ const DB_URI_DEFAULT: &str = "sqlite://sqlite.db";
|
|||
const TABLE_USER_TOKENS: &str = "user_tokens";
|
||||
|
||||
pub trait Database {
|
||||
async fn get_token(&mut self, username: &str) -> Result<UserToken, sqlx::Error>;
|
||||
async fn get_token(&mut self, username: &str) -> Result<Option<UserToken>, sqlx::Error>;
|
||||
|
||||
async fn save_token(&mut self, username: &str, token: &UserToken) -> Result<(), sqlx::Error>;
|
||||
}
|
||||
|
@ -56,9 +56,8 @@ impl SqliteDatabase {
|
|||
}
|
||||
|
||||
impl Database for SqliteDatabase {
|
||||
// TODO: distinguish between invalid token and SQLX error
|
||||
async fn get_token(&mut self, username: &str) -> Result<UserToken, sqlx::Error> {
|
||||
let row: (String,) = query_as(
|
||||
async fn get_token(&mut self, username: &str) -> Result<Option<UserToken>, sqlx::Error> {
|
||||
let row: Option<(String,)> = query_as(
|
||||
"SELECT token
|
||||
FROM user_tokens
|
||||
WHERE
|
||||
|
@ -68,10 +67,10 @@ impl Database for SqliteDatabase {
|
|||
created DESC",
|
||||
)
|
||||
.bind(username)
|
||||
.fetch_one(&mut self.conn)
|
||||
.fetch_optional(&mut self.conn)
|
||||
.await?;
|
||||
|
||||
Ok(UserToken::from(row.0))
|
||||
Ok(row.map(|(token_str, ..)| UserToken::from(token_str)))
|
||||
}
|
||||
|
||||
async fn save_token(&mut self, username: &str, token: &UserToken) -> Result<(), sqlx::Error> {
|
||||
|
|
Loading…
Reference in a new issue