|
| 1 | +use self::grpc::{ |
| 2 | + authentication_server::{Authentication, AuthenticationServer}, |
| 3 | + pet_shop_server::{PetShop, PetShopServer}, |
| 4 | + BuyPetReply, BuyPetRequest, GetPetsReply, GetPetsRequest, LoginReply, LoginRequest, |
| 5 | + LogoutReply, LogoutRequest, Pet, |
| 6 | +}; |
| 7 | +use crate::PetDb; |
| 8 | +use async_trait::async_trait; |
| 9 | +use tonic::Result; |
| 10 | +use tonic::{Request, Response, Status}; |
| 11 | + |
| 12 | +/// contains generated gRPC code |
| 13 | +mod grpc { |
| 14 | + tonic::include_proto!("auth.v1"); |
| 15 | + tonic::include_proto!("shop.v1"); |
| 16 | +} |
| 17 | + |
| 18 | +/// creates auth server |
| 19 | +pub fn auth() -> AuthenticationServer<AuthenticationService> { |
| 20 | + AuthenticationServer::new(AuthenticationService) |
| 21 | +} |
| 22 | + |
| 23 | +/// creates shop server |
| 24 | +pub fn shop(db: PetDb) -> PetShopServer<ShopService> { |
| 25 | + PetShopServer::new(ShopService { db }) |
| 26 | +} |
| 27 | + |
| 28 | +/// top secret user token returned, after login |
| 29 | +const TOKEN: &'static str = "secrettoken"; |
| 30 | + |
| 31 | +/// meta key used for auth |
| 32 | +const AUTH_META: &'static str = "authentication"; |
| 33 | + |
| 34 | +#[derive(Clone)] |
| 35 | +pub struct AuthenticationService; |
| 36 | + |
| 37 | +#[async_trait] |
| 38 | +impl Authentication for AuthenticationService { |
| 39 | + async fn login(&self, request: Request<LoginRequest>) -> Result<Response<LoginReply>> { |
| 40 | + let request = request.into_inner(); |
| 41 | + if request.email == "user@email.com" && request.password == "password" { |
| 42 | + Ok(Response::new(LoginReply { |
| 43 | + usertoken: TOKEN.to_owned(), |
| 44 | + })) |
| 45 | + } else { |
| 46 | + Err(Status::unauthenticated("invalid credentials")) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + async fn logout(&self, request: Request<LogoutRequest>) -> Result<Response<LogoutReply>> { |
| 51 | + check_auth_meta(&request)?; |
| 52 | + |
| 53 | + Ok(Response::new(LogoutReply {})) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +#[derive(Clone)] |
| 58 | +pub struct ShopService { |
| 59 | + db: PetDb, |
| 60 | +} |
| 61 | + |
| 62 | +#[async_trait] |
| 63 | +impl PetShop for ShopService { |
| 64 | + async fn get_pets(&self, request: Request<GetPetsRequest>) -> Result<Response<GetPetsReply>> { |
| 65 | + check_auth_meta(&request)?; |
| 66 | + |
| 67 | + let data = self.db.data.lock().await; |
| 68 | + Ok(Response::new(GetPetsReply { |
| 69 | + pets: data |
| 70 | + .iter() |
| 71 | + .map(|pet| Pet { |
| 72 | + id: pet.id, |
| 73 | + age: pet.age, |
| 74 | + name: pet.name.clone(), |
| 75 | + }) |
| 76 | + .collect(), |
| 77 | + })) |
| 78 | + } |
| 79 | + |
| 80 | + async fn buy_pet(&self, request: Request<BuyPetRequest>) -> Result<Response<BuyPetReply>> { |
| 81 | + check_auth_meta(&request)?; |
| 82 | + |
| 83 | + let mut data = self.db.data.lock().await; |
| 84 | + data.retain(|pet| pet.id != request.get_ref().id); |
| 85 | + |
| 86 | + Ok(Response::new(BuyPetReply {})) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/// checks whether request has correct auth meta set |
| 91 | +fn check_auth_meta<T>(request: &Request<T>) -> Result<()> { |
| 92 | + let meta = request.metadata(); |
| 93 | + if let Some(authentication) = meta.get(AUTH_META) { |
| 94 | + if authentication == format!("Bearer {TOKEN}").as_str() { |
| 95 | + Ok(()) |
| 96 | + } else { |
| 97 | + Err(Status::unauthenticated("bad authorization token")) |
| 98 | + } |
| 99 | + } else { |
| 100 | + Err(Status::unauthenticated("not authorization meta given")) |
| 101 | + } |
| 102 | +} |
0 commit comments