A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: 20b770cd50319c781e72f2a32ab4a20e5252c43f (2349 bytes)[doc][blame][raw]
1
use sqlx::postgres::Postgres;2
use sqlx::Acquire;3
use tide::http::Method;4
use tide_sqlx::SQLxRequestExt;6
use crate::model;7
use crate::unwrap_option_or_throw;8
use crate::unwrap_result_or_throw;9
use crate::Response;11
#[derive(Default)]12
pub struct Auth;14
#[tide::utils::async_trait]15
impl<State: Clone + Send + Sync + 'static> tide::Middleware<State> for Auth {16
async fn handle(17
&self,18
mut req: tide::Request<State>,19
next: tide::Next<'_, State>,20
) -> tide::Result {21
let path_url = req.url().path();22
if path_url.starts_with("/api") {23
match req.header("verbal-token") {24
Some(header) => {25
let token = header.as_str();26
let option_account;27
{28
let query = format!(29
"30
SELECT account.*31
FROM account32
INNER JOIN device33
ON account.id = device.account_id34
WHERE device.uid = '{}'35
",36
&token,37
);38
let mut conn = req.sqlx_conn::<Postgres>().await;39
option_account = unwrap_result_or_throw!(40
sqlx::query_as::<_, model::Account>(&query)41
.fetch_optional(unwrap_result_or_throw!(42
conn.acquire().await,43
"cannot acquire connection to database!"44
))45
.await,46
"cannot acquire account for given device!"47
);48
}49
req.set_ext(unwrap_option_or_throw!(50
option_account,51
"no account found for given token!"52
));53
}54
None => {55
if !(path_url.eq("/api/account") && req.method() == Method::Post) {56
return Response::throw("no token was provided!");57
}58
}59
}60
}61
let res = next.run(req).await;62
Ok(res)63
}64
}