user update
This commit is contained in:
parent
d7a55f6579
commit
e5fe40e4be
8 changed files with 205 additions and 77 deletions
36
src/auth/csrf.rs
Normal file
36
src/auth/csrf.rs
Normal file
|
@ -0,0 +1,36 @@
|
|||
use super::User;
|
||||
use crate::get_pg;
|
||||
use std::str::FromStr;
|
||||
|
||||
pub trait CSRF {
|
||||
fn get_csrf(&self) -> impl std::future::Future<Output = uuid::Uuid>;
|
||||
fn verify_csrf(&self, csrf: &str) -> impl std::future::Future<Output = bool>;
|
||||
}
|
||||
|
||||
impl CSRF for User {
|
||||
/// Get CSRF Token for the current session
|
||||
async fn get_csrf(&self) -> uuid::Uuid {
|
||||
let res: (uuid::Uuid,) = sqlx::query_as("SELECT csrf FROM user_session WHERE token = $1")
|
||||
.bind(&self.session)
|
||||
.fetch_one(get_pg!())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
res.0
|
||||
}
|
||||
|
||||
/// Verify CSRF and generate a new one
|
||||
async fn verify_csrf(&self, csrf: &str) -> bool {
|
||||
if self.get_csrf().await == uuid::Uuid::from_str(csrf).unwrap() {
|
||||
sqlx::query("UPDATE user_session SET csrf = gen_random_uuid() WHERE token = $1")
|
||||
.bind(&self.session)
|
||||
.execute(get_pg!())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue