so://phie

verbal

A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal

blob: 73d86a8c441d46eec09f8ce79523f2eb692ebe65 (1566 bytes)[doc][blame][raw]

1use diesel::prelude::*;

2use tide_diesel::DieselRequestExt;

3

4use crate::error::ErrorKind;

5use crate::models::*;

6use crate::response::Response;

7use crate::schema::account;

8use crate::schema::device;

9

10pub struct AuthMiddleware {}

11

12impl AuthMiddleware {

13 pub fn new() -> Self {

14 Self {}

15 }

16}

17

18#[tide::utils::async_trait]

19impl<State: Clone + Send + Sync + 'static> tide::Middleware<State> for AuthMiddleware {

20 async fn handle(

21 &self,

22 mut req: tide::Request<State>,

23 next: tide::Next<'_, State>,

24 ) -> tide::Result {

25 if req.url().path().starts_with("/api") {

26 let token = match req.header("verbal-token") {

27 Some(header) => header.as_str(),

28 None => {

29 return Response::throw(

30 ErrorKind::Identity,

31 "No request header 'verbal-token' provided!",

32 )

33 }

34 };

35 let conn = req.pg_conn().await?;

36 let identity: Option<(Account, Device)> = account::table

37 .inner_join(device::table)

38 .filter(device::dsl::token.eq(token))

39 .first(&conn)

40 .optional()?;

41 if identity.is_none() {

42 return Response::throw(

43 ErrorKind::Identity,

44 "No account found for provided header 'verbal-token'!",

45 );

46 }

47 req.set_ext(identity.unwrap());

48 }

49 let res = next.run(req).await;

50 Ok(res)

51 }

52}