A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: e8cbb25dbd16f9e071df807b9cadca0b81db80b3 (2027 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
"36
SELECT account.*37
FROM account38
INNER JOIN device39
ON account.id = device.account_id40
WHERE device.token = '{}'41
",42
&token,43
);44
let option_account;45
{46
let mut conn = req.sqlx_conn::<Postgres>().await;47
option_account = sqlx::query_as::<_, model::Account>(&query)48
.fetch_optional(conn.acquire().await.unwrap())49
.await50
.unwrap()51
}52
match option_account {53
Some(account) => {54
req.set_ext(account);55
}56
None => {57
return Response::throw(58
ErrorKind::Identity,59
"No account found for provided header 'verbal-token'!",60
);61
}62
}63
}64
let res = next.run(req).await;65
Ok(res)66
}67
}