Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/robonode-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ uuid = { version = "0.8", features = ["v4"] }
warp = "0.3"

[dev-dependencies]
mockall = "0.10"
serde_json = "1"
tracing-test = "0.1"

[features]
Expand Down
62 changes: 40 additions & 22 deletions crates/robonode-server/src/http/filters.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
//! Filters, essentially how [`warp`] implements routes and middlewares.

use std::{convert::TryFrom, sync::Arc};
use std::sync::Arc;

use serde::Serialize;
use warp::Filter;

use crate::{
http::handlers,
logic::{op_authenticate, op_enroll, Logic, Signer, Verifier},
logic::{
op_authenticate, op_enroll, op_get_facetec_device_sdk_params, op_get_facetec_session_token,
LogicOp,
},
};

/// Pass the [`Arc`] to the handler.
Expand All @@ -30,12 +34,23 @@ where
}

/// The root mount point with all the routes.
pub fn root<S, PK>(
logic: Arc<Logic<S, PK>>,
pub fn root<L>(
logic: Arc<L>,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone
where
S: Signer<Vec<u8>> + Send + Sync + 'static,
PK: Send + Sync + for<'a> TryFrom<&'a [u8]> + AsRef<[u8]> + Verifier<Vec<u8>> + Into<Vec<u8>>,
L: LogicOp<op_authenticate::Request>
+ LogicOp<op_enroll::Request>
+ LogicOp<op_get_facetec_device_sdk_params::Request>
+ LogicOp<op_get_facetec_session_token::Request>
+ Send
+ Sync,
<L as LogicOp<op_enroll::Request>>::Error: warp::reject::Reject,
<L as LogicOp<op_authenticate::Request>>::Error: warp::reject::Reject,
<L as LogicOp<op_authenticate::Request>>::Response: Serialize,
<L as LogicOp<op_get_facetec_device_sdk_params::Request>>::Error: warp::reject::Reject,
<L as LogicOp<op_get_facetec_device_sdk_params::Request>>::Response: Serialize,
<L as LogicOp<op_get_facetec_session_token::Request>>::Error: warp::reject::Reject,
<L as LogicOp<op_get_facetec_session_token::Request>>::Response: Serialize,
{
enroll(Arc::clone(&logic))
.or(authenticate(Arc::clone(&logic)))
Expand All @@ -44,12 +59,12 @@ where
}

/// POST /enroll with JSON body.
fn enroll<S, PK>(
logic: Arc<Logic<S, PK>>,
fn enroll<L>(
logic: Arc<L>,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone
where
S: Signer<Vec<u8>> + Send + 'static,
PK: Send + for<'a> TryFrom<&'a [u8]> + AsRef<[u8]>,
L: LogicOp<op_enroll::Request> + Send + Sync,
L::Error: warp::reject::Reject,
{
warp::path!("enroll")
.and(warp::post())
Expand All @@ -59,12 +74,13 @@ where
}

/// POST /authenticate with JSON body.
fn authenticate<S, PK>(
logic: Arc<Logic<S, PK>>,
fn authenticate<L>(
logic: Arc<L>,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone
where
S: Signer<Vec<u8>> + Send + Sync + 'static,
PK: Send + Sync + for<'a> TryFrom<&'a [u8]> + AsRef<[u8]> + Verifier<Vec<u8>> + Into<Vec<u8>>,
L: LogicOp<op_authenticate::Request> + Send + Sync,
L::Error: warp::reject::Reject,
L::Response: Serialize,
{
warp::path!("authenticate")
.and(warp::post())
Expand All @@ -74,12 +90,13 @@ where
}

/// GET /facetec-session-token.
fn get_facetec_session_token<S, PK>(
logic: Arc<Logic<S, PK>>,
fn get_facetec_session_token<L>(
logic: Arc<L>,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone
where
S: Signer<Vec<u8>> + Send + 'static,
PK: Send + for<'a> TryFrom<&'a [u8]>,
L: LogicOp<op_get_facetec_session_token::Request> + Send + Sync,
L::Error: warp::reject::Reject,
L::Response: Serialize,
{
warp::path!("facetec-session-token")
.and(warp::get())
Expand All @@ -88,12 +105,13 @@ where
}

/// GET /facetec-device-sdk-params.
fn get_facetec_device_sdk_params<S, PK>(
logic: Arc<Logic<S, PK>>,
fn get_facetec_device_sdk_params<L>(
logic: Arc<L>,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone
where
S: Signer<Vec<u8>> + Send + 'static,
PK: Send + for<'a> TryFrom<&'a [u8]>,
L: LogicOp<op_get_facetec_device_sdk_params::Request> + Send + Sync,
L::Error: warp::reject::Reject,
L::Response: Serialize,
{
warp::path!("facetec-device-sdk-params")
.and(warp::get())
Expand Down
51 changes: 26 additions & 25 deletions crates/robonode-server/src/http/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
//! Handlers, the HTTP transport coupling for the internal logic.

use std::{convert::TryFrom, sync::Arc};
use serde::Serialize;
use std::sync::Arc;
use warp::hyper::StatusCode;
use warp::Reply;

use crate::logic::{
op_authenticate, op_enroll, op_get_facetec_device_sdk_params, op_get_facetec_session_token,
Logic, Signer, Verifier,
LogicOp,
};

/// Enroll operation HTTP transport coupling.
pub async fn enroll<S, PK>(
logic: Arc<Logic<S, PK>>,
pub async fn enroll<L>(
logic: Arc<L>,
input: op_enroll::Request,
) -> Result<impl warp::Reply, warp::Rejection>
where
S: Signer<Vec<u8>> + Send + 'static,
PK: Send + for<'a> TryFrom<&'a [u8]> + AsRef<[u8]>,
L: LogicOp<op_enroll::Request>,
L::Error: warp::reject::Reject,
{
logic.enroll(input).await.map_err(warp::reject::custom)?;
logic.call(input).await.map_err(warp::reject::custom)?;
Ok(StatusCode::CREATED)
}

/// Authenticate operation HTTP transport coupling.
pub async fn authenticate<S, PK>(
logic: Arc<Logic<S, PK>>,
pub async fn authenticate<L>(
logic: Arc<L>,
input: op_authenticate::Request,
) -> Result<impl warp::Reply, warp::Rejection>
where
S: Signer<Vec<u8>> + Send + 'static,
PK: Send + Sync + for<'a> TryFrom<&'a [u8]> + Verifier<Vec<u8>> + Into<Vec<u8>>,
L: LogicOp<op_authenticate::Request>,
L::Error: warp::reject::Reject,
L::Response: Serialize,
{
let res = logic
.authenticate(input)
.await
.map_err(warp::reject::custom)?;
let res = logic.call(input).await.map_err(warp::reject::custom)?;

let reply = warp::reply::json(&res);
let reply = warp::reply::with_status(reply, StatusCode::OK);
Ok(reply.into_response())
}

/// Get FaceTec Session Token operation HTTP transport coupling.
pub async fn get_facetec_session_token<S, PK>(
logic: Arc<Logic<S, PK>>,
pub async fn get_facetec_session_token<L>(
logic: Arc<L>,
) -> Result<impl warp::Reply, warp::Rejection>
where
S: Signer<Vec<u8>> + Send + 'static,
PK: Send + for<'a> TryFrom<&'a [u8]>,
L: LogicOp<op_get_facetec_session_token::Request>,
L::Error: warp::reject::Reject,
L::Response: Serialize,
{
let res = logic
.get_facetec_session_token()
.call(op_get_facetec_session_token::Request {})
.await
.map_err(warp::reject::custom)?;

Expand All @@ -60,15 +60,16 @@ where
}

/// Get FaceTec Device SDK Params operation HTTP transport coupling.
pub async fn get_facetec_device_sdk_params<S, PK>(
logic: Arc<Logic<S, PK>>,
pub async fn get_facetec_device_sdk_params<L>(
logic: Arc<L>,
) -> Result<impl warp::Reply, warp::Rejection>
where
S: Signer<Vec<u8>> + Send + 'static,
PK: Send + for<'a> TryFrom<&'a [u8]>,
L: LogicOp<op_get_facetec_device_sdk_params::Request>,
L::Error: warp::reject::Reject,
L::Response: Serialize,
{
let res = logic
.get_facetec_device_sdk_params()
.call(op_get_facetec_device_sdk_params::Request {})
.await
.map_err(warp::reject::custom)?;

Expand Down
3 changes: 3 additions & 0 deletions crates/robonode-server/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
mod filters;
mod handlers;

#[cfg(test)]
mod tests;

pub use filters::root;
Loading