diff options
Diffstat (limited to 'lockchain-http/src/handlers.rs')
-rw-r--r-- | lockchain-http/src/handlers.rs | 486 |
1 files changed, 250 insertions, 236 deletions
diff --git a/lockchain-http/src/handlers.rs b/lockchain-http/src/handlers.rs index 2b220a2..354b4c9 100644 --- a/lockchain-http/src/handlers.rs +++ b/lockchain-http/src/handlers.rs @@ -1,236 +1,250 @@ -//! Definition of the core lockchain API - -use actix_web::{HttpRequest, Json, Responder}; -use lockchain::{ - traits::{Body, Vault}, Record, -}; - -use models::{inputs::*, responses::*}; -use state::ApiState; - -use std::intrinsics; -use std::sync::{Arc, Mutex}; - -type HttpRequestState<T> = HttpRequest<Arc<Mutex<T>>>; - -/// GET /vault -/// -/// Check the documentation for more information about how to provide payloads -pub fn get_vaults<B, V>(req: HttpRequestState<ApiState<B, V>>) -> impl Responder -where - B: Body, - V: Vault<B>, -{ - let state = req.state().lock().unwrap(); - Json(VaultList { - vaults: state.vaults().iter().map(|s| s.to_string()).collect(), - count: state.count(), - }) -} - -/// PUT /vault -/// -/// Check the documentation for more information about how to provide payloads -pub fn create_vault<B, V>( - (item, req): (Json<VaultCreate>, HttpRequestState<ApiState<B, V>>), -) -> impl Responder -where - B: Body, - V: Vault<B>, -{ - let mut state = req.state().lock().unwrap(); - let location = if state.bound_scope { - state.working_dir.clone().join(&item.location) - } else { - (&item.location).into() - }; - - state.add_vault(&item.name, V::new(&item.name, location.to_str().unwrap())); - Json(VaultCreateResponse { - name: item.name.clone(), - created: true, - error: None, - }) -} - -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 - B: Body, - V: Vault<B>, -{ - Json(OperationFailed { - reason: "Not implemented".into(), - code: 255, - }) -} - -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>, -{ - Json(OperationFailed { - reason: "Not implemented".into(), - code: 255, - }) -} - -/// GET /vault/{vault-id}/records/{record-id} -pub fn get_record<B, V>( - (item, req): (Json<VaultCreate>, HttpRequestState<ApiState<B, V>>), -) -> impl Responder -where - B: Body, - V: Vault<B>, -{ - let mut state = req.state().lock().unwrap(); - let vault = state.get_vault(""); - - Json(OperationFailed { - reason: "Not implemented".into(), - code: 255, - }) - - // Ok(Json(CarrierMessage { - // error: Ok(()), - // data: Some(Record::new("name", "category", vec!["test", "foo"])), - // })) -} - -/// 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>, -{ - Json(OperationFailed { - reason: "Not implemented".into(), - code: 255, - }) -} - -/// 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>, -{ - Json(OperationFailed { - reason: "Not implemented".into(), - code: 255, - }) -} - -/// PUT /authenticate -pub fn authenticate<B, V>( - (item, req): (Json<Authenticate>, HttpRequestState<ApiState<B, V>>), -) -> impl Responder -where - B: Body, - V: Vault<B>, -{ - use lockchain::users::*; - let Authenticate { username, password } = item.into_inner(); - - Json(match pam_authenticate(&username, &password) { - Ok(()) => CarrierMessage { - error: Ok(()), - data: Some(TokenMessage { - username, - token: String::new(), - }), - }, - Err(e) => CarrierMessage { - error: Err(e.into()), - data: Some(OperationFailed { - reason: "Meh!".into(), - code: 1, - }), - }, - }) -} - -/// PUT /de-authenticate -pub fn deauthenticate<B, V>( - (item, req): (Json<Deauthenticate>, HttpRequestState<ApiState<B, V>>), -) -> impl Responder -where - B: Body, - V: Vault<B>, -{ - Json(OperationFailed { - reason: "Not implemented".into(), - code: 255, - }) -} - -/// GET /api -/// -/// Check the documentation for more information about how to provide payloads -pub fn api_data<B: Body, V: Vault<B>>(_: HttpRequestState<ApiState<B, V>>) -> impl Responder -where - B: Body, - V: Vault<B>, -{ - Json(ApiInformation { - version: "1.0".into(), - providers: vec![ - unsafe { intrinsics::type_name::<V>() }.into(), - unsafe { intrinsics::type_name::<B>() }.into(), - ], - hostname: None, - supported: "1.0".into(), - }) -} +// //! Definition of the core lockchain API + +// use actix_web::{HttpRequest, Json, Responder}; +// use lockchain::{ +// traits::{Body, Vault}, Record, +// }; + +// use models::{inputs::*, responses::*, NoneError, Response}; +// use state::ApiState; + +// use std::intrinsics; +// use std::sync::{Arc, Mutex}; + +// type HttpRequestState<T> = HttpRequest<Arc<Mutex<T>>>; + +// /// GET /vault +// /// +// /// Check the documentation for more information about how to provide payloads +// pub fn get_vaults<B, V>(req: HttpRequestState<ApiState<B, V>>) -> impl Responder +// where +// B: Body, +// V: Vault<B>, +// { +// let state = req.state().lock().unwrap(); +// Json(VaultList { +// vaults: state.vaults().iter().map(|s| s.to_string()).collect(), +// count: state.count(), +// }) +// } + +// /// PUT /vault +// /// +// /// Check the documentation for more information about how to provide payloads +// pub fn create_vault<B, V>( +// (item, req): (Json<VaultCreate>, HttpRequestState<ApiState<B, V>>), +// ) -> impl Responder +// where +// B: Body, +// V: Vault<B>, +// { +// let mut state = req.state().lock().unwrap(); +// let location = if state.bound_scope { +// state.working_dir.clone().join(&item.location) +// } else { +// (&item.location).into() +// }; + +// state.add_vault(&item.name, V::new(&item.name, location.to_str().unwrap())); +// Json(Response::Success::<NoneError>) +// } + +// pub fn delete_vault<B, V>( +// (item, req): (Json<VaultCreate>, HttpRequestState<ApiState<B, V>>), +// ) -> impl Responder +// where +// B: Body, +// V: Vault<B>, +// { +// use lockchain::errors::Error as LockError; + +// Json(OperationFailed::<LockError> { +// reason: "Not implemented".into(), +// error: LockError::UnknownFailure.into(), +// }) +// } + +// pub fn scope_vault<B, V>( +// (item, req): (Json<VaultCreate>, HttpRequestState<ApiState<B, V>>), +// ) -> impl Responder +// where +// B: Body, +// V: Vault<B>, +// { +// use lockchain::errors::Error as LockError; + +// Json(OperationFailed::<LockError> { +// reason: "Not implemented".into(), +// error: LockError::UnknownFailure.into(), +// }) +// } + +// pub fn unscope_vault<B, V>( +// (item, req): (Json<VaultCreate>, HttpRequestState<ApiState<B, V>>), +// ) -> impl Responder +// where +// B: Body, +// V: Vault<B>, +// { +// use lockchain::errors::Error as LockError; + +// Json(Response::Failure::<LockError>(OperationFailed { +// reason: "Not implemented".into(), +// error: LockError::UnknownFailure.into(), +// })) + +// // Json() +// } + +// /// POST /vault/{vault-id} +// pub fn update_vault<B, V>(_req: HttpRequestState<ApiState<B, V>>) -> impl Responder +// where +// B: Body, +// V: Vault<B>, +// { +// use lockchain::errors::Error as LockError; + +// Json(OperationFailed::<LockError> { +// reason: "Not implemented".into(), +// error: LockError::UnknownFailure.into(), +// }) +// } + +// pub fn get_all_records<B, V>(_req: HttpRequestState<ApiState<B, V>>) -> impl Responder +// where +// B: Body, +// V: Vault<B>, +// { +// use lockchain::errors::Error as LockError; + +// Json(OperationFailed::<LockError> { +// reason: "Not implemented".into(), +// error: LockError::UnknownFailure.into(), +// }) +// } + +// /// PUT /vault/{vault-id}/records +// pub fn create_record<B, V>(_req: HttpRequestState<ApiState<B, V>>) -> impl Responder +// where +// B: Body, +// V: Vault<B>, +// { +// use lockchain::errors::Error as LockError; + +// Json(OperationFailed::<LockError> { +// reason: "Not implemented".into(), +// error: LockError::UnknownFailure.into(), +// }) +// } + +// /// GET /vault/{vault-id}/records/{record-id} +// pub fn get_record<B, V>( +// (item, req): (Json<VaultCreate>, HttpRequestState<ApiState<B, V>>), +// ) -> impl Responder +// where +// B: Body, +// V: Vault<B>, +// { +// let mut state = req.state().lock().unwrap(); +// let vault = state.get_vault(""); + +// use lockchain::errors::Error as LockError; + +// Json(OperationFailed::<LockError> { +// reason: "Not implemented".into(), +// error: LockError::UnknownFailure.into(), +// }) +// } + +// /// 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>, +// { +// use lockchain::errors::Error as LockError; + +// Json(OperationFailed::<LockError> { +// reason: "Not implemented".into(), +// error: LockError::UnknownFailure.into(), +// }) +// } + +// /// 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>, +// { +// use lockchain::errors::Error as LockError; + +// Json(OperationFailed::<LockError> { +// reason: "Not implemented".into(), +// error: LockError::UnknownFailure.into(), +// }) +// } + +// /// PUT /authenticate +// pub fn authenticate<B, V>( +// (item, req): (Json<Authenticate>, HttpRequestState<ApiState<B, V>>), +// ) -> impl Responder +// where +// B: Body, +// V: Vault<B>, +// { +// use lockchain::users::*; +// let Authenticate { username, password } = item.into_inner(); + +// Json(match pam_authenticate(&username, &password) { +// Ok(()) => { +// /* Store the token for auth later */ +// let state = req.state().lock().unwrap(); +// let token = String::new(); +// state.tokens.insert(token.clone()); + +// Response::Token(TokenMessage { +// username, +// token: token, +// }) +// } +// Err(e) => Response::Failure(OperationFailed { +// reason: "Failed to authenticate user".into(), +// error: e.into(), +// }), +// }) +// } + +// /// PUT /de-authenticate +// pub fn deauthenticate<B, V>( +// (item, req): (Json<Deauthenticate>, HttpRequestState<ApiState<B, V>>), +// ) -> impl Responder +// where +// B: Body, +// V: Vault<B>, +// { +// use lockchain::errors::Error as LockError; + +// Json(OperationFailed::<LockError> { +// reason: "Not implemented".into(), +// error: LockError::UnknownFailure.into(), +// }) +// } + +// /// GET /api +// /// +// /// Check the documentation for more information about how to provide payloads +// pub fn api_data<B: Body, V: Vault<B>>(_: HttpRequestState<ApiState<B, V>>) -> impl Responder +// where +// B: Body, +// V: Vault<B>, +// { +// Json(ApiInformation { +// version: "1.0".into(), +// providers: vec![ +// unsafe { intrinsics::type_name::<V>() }.into(), +// unsafe { intrinsics::type_name::<B>() }.into(), +// ], +// hostname: None, +// supported: "1.0".into(), +// }) +// } |