A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: 765c78b59520e275746f9d50fbc0b4c36b97d8bd (1742 bytes)[doc][blame][raw]
1
use sqlx::postgres::Postgres;2
use sqlx::Acquire;3
use tide_sqlx::SQLxRequestExt;5
use crate::error::ErrorKind;6
use crate::model;7
use crate::response::Response;9
pub struct AuthMiddleware {}11
impl AuthMiddleware {12
pub fn new() -> Self {13
Self {}14
}15
}17
#[tide::utils::async_trait]18
impl<State: Clone + Send + Sync + 'static> tide::Middleware<State> for AuthMiddleware {19
async fn handle(20
&self,21
mut req: tide::Request<State>,22
next: tide::Next<'_, State>,23
) -> tide::Result {24
if req.url().path().starts_with("/api") {25
let token = match req.header("verbal-token") {26
Some(header) => header.as_str(),27
None => {28
return Response::throw(29
ErrorKind::Identity,30
"No request header 'verbal-token' provided!",31
)32
}33
};34
let query = format!(35
"SELECT * FROM account INNER JOIN device ON account.id = device.account_id WHERE device.token = '{}'",36
&token,37
);38
let mut conn = req.sqlx_conn::<Postgres>().await;39
match sqlx::query_as::<_, model::Account>(&query)40
.fetch_one(conn.acquire().await.unwrap())41
.await42
{43
Ok(account) => {44
req.set_ext(account);45
}46
Err(_) => {47
return Response::throw(48
ErrorKind::Identity,49
"No account found for provided header 'verbal-token'!",50
);51
}52
}53
}54
let res = next.run(req).await;55
Ok(res)56
}57
}