diff options
author | Katharina Fey <kookie@spacekookie.de> | 2018-06-30 23:45:43 +0200 |
---|---|---|
committer | Katharina Fey <kookie@spacekookie.de> | 2018-06-30 23:45:43 +0200 |
commit | d6a5d6500cd0e2b94931eab1091274784ebc3007 (patch) | |
tree | d117fa71943eb1f02d41f42b04e8ead2a9a3d4ba | |
parent | e3548334f8e5697bd43e5b12959d976711bf5307 (diff) |
Fleshing out REST API fully and adding function stubs
-rw-r--r-- | lockchain-http/src/handlers.rs | 84 | ||||
-rw-r--r-- | lockchain-http/src/lib.rs | 67 | ||||
-rw-r--r-- | lockchain-http/src/state.rs | 6 |
3 files changed, 130 insertions, 27 deletions
diff --git a/lockchain-http/src/handlers.rs b/lockchain-http/src/handlers.rs index e7d0a11..3f01fe7 100644 --- a/lockchain-http/src/handlers.rs +++ b/lockchain-http/src/handlers.rs @@ -53,6 +53,45 @@ where }) } +pub fn delete_vault<B, V>( + (item, req): (Json<VaultCreate>, HttpRequestState<ApiState<B, V>>), +) -> impl Responder +where + B: Body, + V: Vault<B>, +{ + Json(OperationFailed { + reason: "Not implemented".into(), + code: 255, + }) +} + +pub fn scope_vault<B, V>( + (item, req): (Json<VaultCreate>, HttpRequestState<ApiState<B, V>>), +) -> impl Responder +where + B: Body, + V: Vault<B>, +{ + Json(OperationFailed { + reason: "Not implemented".into(), + code: 255, + }) +} + +pub fn unscope_vault<B, V>( + (item, req): (Json<VaultCreate>, HttpRequestState<ApiState<B, V>>), +) -> impl Responder +where + B: Body, + V: Vault<B>, +{ + Json(OperationFailed { + reason: "Not implemented".into(), + code: 255, + }) +} + /// POST /vault/{vault-id} pub fn update_vault<B, V>(_req: HttpRequestState<ApiState<B, V>>) -> impl Responder where @@ -65,8 +104,19 @@ where }) } -/// DELETE /vault/{vault-id} -pub fn delete_vault<B, V>(_req: HttpRequestState<ApiState<B, V>>) -> impl Responder +pub fn get_all_records<B, V>(_req: HttpRequestState<ApiState<B, V>>) -> impl Responder +where + B: Body, + V: Vault<B>, +{ + Json(OperationFailed { + reason: "Not implemented".into(), + code: 255, + }) +} + +/// PUT /vault/{vault-id}/records +pub fn create_record<B, V>(_req: HttpRequestState<ApiState<B, V>>) -> impl Responder where B: Body, V: Vault<B>, @@ -99,8 +149,8 @@ where // })) } -/// PUT /vault/{vault-id}/records -pub fn create_record<B, V>(_req: HttpRequestState<ApiState<B, V>>) -> impl Responder +/// POST /vault/{vault-id}/records/{record-id} +pub fn update_record<B, V>(_req: HttpRequestState<ApiState<B, V>>) -> impl Responder where B: Body, V: Vault<B>, @@ -111,8 +161,8 @@ where }) } -/// POST /vault/{vault-id}/records/{record-id} -pub fn update_record<B, V>(_req: HttpRequestState<ApiState<B, V>>) -> impl Responder +/// DELETE /vault/{vault-id}/records/{record-id} +pub fn delete_record<B, V>(_req: HttpRequestState<ApiState<B, V>>) -> impl Responder where B: Body, V: Vault<B>, @@ -123,8 +173,8 @@ where }) } -/// DELETE /vault/{vault-id}/records/{record-id} -pub fn delete_record<B, V>(_req: HttpRequestState<ApiState<B, V>>) -> impl Responder +/// PUT /authenticate +pub fn authenticate<B, V>(_req: HttpRequestState<ApiState<B, V>>) -> impl Responder where B: Body, V: Vault<B>, @@ -135,8 +185,8 @@ where }) } -/// PUT /authenticate -pub fn authenticate<B, V>(_req: HttpRequestState<ApiState<B, V>>) -> impl Responder +/// PUT /de-authenticate +pub fn deauthenticate<B, V>(_req: HttpRequestState<ApiState<B, V>>) -> impl Responder where B: Body, V: Vault<B>, @@ -148,7 +198,19 @@ where } /// PUT /de-authenticate -pub fn deauthenticate<B, V>(_req: HttpRequestState<ApiState<B, V>>) -> impl Responder +pub fn register<B, V>(_req: HttpRequestState<ApiState<B, V>>) -> impl Responder +where + B: Body, + V: Vault<B>, +{ + Json(OperationFailed { + reason: "Not implemented".into(), + code: 255, + }) +} + +/// PUT /de-authenticate +pub fn get_all_users<B, V>(_req: HttpRequestState<ApiState<B, V>>) -> impl Responder where B: Body, V: Vault<B>, diff --git a/lockchain-http/src/lib.rs b/lockchain-http/src/lib.rs index c9ea213..08e88d2 100644 --- a/lockchain-http/src/lib.rs +++ b/lockchain-http/src/lib.rs @@ -31,6 +31,7 @@ pub mod state; use actix_web::{http, server, App}; use lockchain::traits::{Body, Vault}; use state::ApiState; +use std::error::Error; use std::sync::{Arc, Mutex}; /// A simple rename of the long generic types that are returned for a new server @@ -57,7 +58,11 @@ pub type HttpApi<V> = server::HttpServer<App<Arc<Mutex<V>>>>; /// DataVault::<EncryptedBody>::new("name", "some-location"), /// ).run(); /// ``` -pub fn create_server<B, V>(bind: &str, port: &str, state: ApiState<B, V>) -> HttpApi<ApiState<B, V>> +pub fn create_server<B, V>( + bind: &str, + port: &str, + state: ApiState<B, V>, +) -> Result<HttpApi<ApiState<B, V>>, Box<Error>> where B: Body + 'static, V: Vault<B> + 'static, @@ -68,27 +73,59 @@ where vec![ App::with_state(Arc::clone(&state)) .resource("/vaults", |r| { - r.method(http::Method::GET).with(handlers::get_vaults); - r.method(http::Method::PUT).with(handlers::create_vault); + // Get existing vaults + r.method(http::Method::GET).with(handlers::get_vaults); + + // Create new vault (if authorised) + r.method(http::Method::PUT).with(handlers::create_vault); + + // Delete entire vault (if authorised) + r.method(http::Method::DELETE).with(handlers::delete_vault); }) - .resource("/vaults/{vaultid}", |r| r.f(handlers::update_vault)) - .resource("/vaults/{vaultid}", |r| r.f(handlers::delete_vault)) - .resource("/vaults/{vaultid}/records/{recordid}", |r| { - r.method(http::Method::GET).with(handlers::get_record); + .resource("/vaults/scope", |r| { + // Bring an existing vault into scope (if authorised) + r.method(http::Method::PUT).with(handlers::scope_vault); + // Remove an existing vault from API scope (if authorised) + r.method(http::Method::DELETE).with(handlers::unscope_vault); + }) + .resource("/vaults/{vaultid}", |r| { + // Update vault metadata (access rights, users, indices, etc) + r.method(http::Method::POST).with(handlers::update_vault) }) .resource("/vaults/{vaultid}/records", |r| { - r.f(handlers::create_record) + // Get the vault record index (omits records without access) + r.method(http::Method::GET).with(handlers::get_all_records); + // Create a new record (if authorised) in the vault + r.method(http::Method::PUT).with(handlers::create_record); }) .resource("/vaults/{vaultid}/records/{recordid}", |r| { - r.f(handlers::update_record) + // Get a specific record from a vault + r.method(http::Method::GET).with(handlers::get_record); + // Update a specific record + r.method(http::Method::POST).with(handlers::update_record); + // Delete a specific record from a vault + r.method(http::Method::DELETE).with(handlers::delete_record); }) - .resource("/vaults/{vaultid}/records/{recordid}", |r| { - r.f(handlers::delete_record) + .resource("/users/login", |r| { + // Request a new auth token + r.method(http::Method::POST).with(handlers::authenticate) + }) + .resource("/users/logout", |r| { + // Hand-in active auth token + r.method(http::Method::POST).with(handlers::deauthenticate) + }) + .resource("/users/register", |r| { + // Register a new user (if allowed) + r.method(http::Method::POST).with(handlers::register); + }) + .resource("/users/", |r| { + // Get all available users + r.method(http::Method::GET).with(handlers::get_all_users); }) - .resource("/authenticate", |r| r.f(handlers::authenticate)) - .resource("/deauthenticate", |r| r.f(handlers::deauthenticate)) - .resource("/api", |r| r.f(handlers::api_data)), + .resource("/api", |r| { + r.method(http::Method::GET).with(handlers::api_data); + }), ] }).bind(format!("{}:{}", bind, port)) - .expect("Oh no!") + .map_err(|e| e.into()) } diff --git a/lockchain-http/src/state.rs b/lockchain-http/src/state.rs index 423e674..9a5b4ba 100644 --- a/lockchain-http/src/state.rs +++ b/lockchain-http/src/state.rs @@ -1,4 +1,4 @@ -use lockchain::traits::{AutoEncoder, Body, Vault}; +use lockchain::traits::{AutoEncoder, Body, FileIO, Vault}; use lockchain::users::{User, UserStore}; use std::collections::HashMap; @@ -82,10 +82,13 @@ where B: Body, V: Vault<B>, { + #[allow(unconditional_recursion)] fn default() -> Self { Self { _phantom: PhantomData, bound_scope: true, + vaults: HashMap::new(), + users: Default::default(), ..Default::default() } } @@ -98,6 +101,7 @@ struct SerializedState { } impl AutoEncoder for SerializedState {} +impl FileIO for SerializedState {} /// Implements the transform from in-memory to on-disk impl<'state, B, V> From<&'state ApiState<B, V>> for SerializedState |