-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
107 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,87 @@ | ||
use std::task::{Context, Poll}; | ||
|
||
use trusted_delivery_api::auth_server::AuthServer; | ||
use trusted_delivery_core::crypto::CryptoSuite; | ||
|
||
pub use self::auth::{InvalidToken, TokenVerifier}; | ||
|
||
mod auth; | ||
|
||
pub struct AuthService<C: CryptoSuite>(AuthServer<auth::Auth<C>>); | ||
|
||
impl<C: CryptoSuite> AuthService<C> { | ||
pub fn new() -> (Self, TokenVerifier<C>) { | ||
let (auth, token_verifier) = auth::Auth::new(); | ||
(AuthService(AuthServer::new(auth)), token_verifier) | ||
} | ||
} | ||
|
||
macro_rules! derive_service_for_wrapper { | ||
($($service:ident as $underlying:ty),+) => { | ||
use http::{Request, Response}; | ||
use tonic::body::BoxBody; | ||
use tonic::transport::Body; | ||
use tonic::transport::NamedService; | ||
use tower_service::Service; | ||
|
||
$( | ||
impl<C: CryptoSuite> Service<Request<Body>> for $service<C> { | ||
type Response = Response<BoxBody>; | ||
type Error = <$underlying as tower_service::Service<Request<Body>>>::Error; | ||
type Future = <$underlying as tower_service::Service<Request<Body>>>::Future; | ||
|
||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
<$underlying as tower_service::Service<Request<Body>>>::poll_ready( | ||
&mut self.0, | ||
cx, | ||
) | ||
} | ||
|
||
fn call(&mut self, req: Request<Body>) -> Self::Future { | ||
<$underlying as tower_service::Service<Request<Body>>>::call(&mut self.0, req) | ||
} | ||
} | ||
impl<C: CryptoSuite> NamedService for $service<C> { | ||
const NAME: &'static str = <$underlying as tonic::transport::NamedService>::NAME; | ||
} | ||
impl<C: CryptoSuite> Clone for $service<C> { | ||
fn clone(&self) -> Self { | ||
Self(self.0.clone()) | ||
} | ||
} | ||
)+ | ||
}; | ||
} | ||
|
||
derive_service_for_wrapper!(AuthService as AuthServer<auth::Auth<C>>); | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::error::Error; | ||
|
||
use http::{Request, Response}; | ||
use tonic::body::BoxBody; | ||
use tonic::transport::Body; | ||
use tonic::transport::NamedService; | ||
use tower_service::Service; | ||
use trusted_delivery_core::crypto::CryptoSuite; | ||
|
||
fn _ensure_service_is_valid<S>() | ||
where | ||
S: Service<Request<Body>, Response = Response<BoxBody>> | ||
+ NamedService | ||
+ Clone | ||
+ Send | ||
+ 'static, | ||
S::Future: Send + 'static, | ||
S::Error: Into<Box<dyn Error + Send + Sync>> + Send, | ||
{ | ||
} | ||
|
||
/// Compile-time check that services are compliant with [Server::add_service function] | ||
/// | ||
/// [Server::add_service function]: tonic::transport::Server::add_service | ||
fn _ensure_services_are_valid<C: CryptoSuite>() { | ||
_ensure_service_is_valid::<crate::AuthService<C>>(); | ||
} | ||
} |