aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatharina Fey <kookie@spacekookie.de>2018-07-01 00:19:01 +0200
committerKatharina Fey <kookie@spacekookie.de>2018-07-01 00:19:01 +0200
commitd99d8bb4d9fb695f15256a8fe9a85136959e556b (patch)
tree4fed8771ed23ca1268da43a7804dd10c6af3f6c5
parentd6a5d6500cd0e2b94931eab1091274784ebc3007 (diff)
Refactoring models module, adding more inputs
-rw-r--r--lockchain-core/src/users.rs13
-rw-r--r--lockchain-http/src/handlers.rs4
-rw-r--r--lockchain-http/src/lib.rs2
-rw-r--r--lockchain-http/src/models/inputs.rs85
-rw-r--r--lockchain-http/src/models/mod.rs4
-rw-r--r--lockchain-http/src/models/responses.rs (renamed from lockchain-http/src/model.rs)18
6 files changed, 106 insertions, 20 deletions
diff --git a/lockchain-core/src/users.rs b/lockchain-core/src/users.rs
index 880c7f4..ab40c43 100644
--- a/lockchain-core/src/users.rs
+++ b/lockchain-core/src/users.rs
@@ -16,7 +16,7 @@
mod auth;
pub use self::auth::Token;
-use crypto::{encoding, hashing};
+use crypto::{encoding, hashing, random};
use std::collections::HashMap;
use {meta::MetaDomain, traits::AutoEncoder};
@@ -57,6 +57,7 @@ pub struct User {
name: String,
pw_hash: String,
rights: Vec<(Access, Role)>,
+ token: Option<String>,
}
impl User {
@@ -66,12 +67,22 @@ impl User {
name: name.into(),
pw_hash: encoding::base64_encode(&hashing::blake2(pw, name).to_vec()),
rights: Vec::new(),
+ token: None,
}
}
/// Verify a user password input
pub fn verify(&self, pw: &str) -> bool {
self.pw_hash == encoding::base64_encode(&hashing::blake2(pw, &self.name).to_vec())
}
+
+ /// Generate a token unique to this user (or return the existing one)
+ pub fn token(&mut self) -> String {
+ if self.token.is_none() {
+ self.token = Some(encoding::base64_encode(&random::bytes(256)));
+ }
+
+ self.token.as_ref().unwrap().clone()
+ }
}
impl AutoEncoder for User {}
diff --git a/lockchain-http/src/handlers.rs b/lockchain-http/src/handlers.rs
index 3f01fe7..369f888 100644
--- a/lockchain-http/src/handlers.rs
+++ b/lockchain-http/src/handlers.rs
@@ -1,11 +1,11 @@
//! Definition of the core lockchain API
-use actix_web::{HttpRequest, Json, Responder, Result};
+use actix_web::{HttpRequest, Json, Responder};
use lockchain::{
traits::{Body, Vault}, Record,
};
-use model::*;
+use models::{inputs::*, responses::*};
use state::ApiState;
use std::intrinsics;
diff --git a/lockchain-http/src/lib.rs b/lockchain-http/src/lib.rs
index 08e88d2..95d27d2 100644
--- a/lockchain-http/src/lib.rs
+++ b/lockchain-http/src/lib.rs
@@ -25,7 +25,7 @@ extern crate actix_web;
extern crate lockchain_core as lockchain;
mod handlers;
-pub mod model;
+pub mod models;
pub mod state;
use actix_web::{http, server, App};
diff --git a/lockchain-http/src/models/inputs.rs b/lockchain-http/src/models/inputs.rs
new file mode 100644
index 0000000..172b204
--- /dev/null
+++ b/lockchain-http/src/models/inputs.rs
@@ -0,0 +1,85 @@
+use lockchain::users::Role;
+
+/// Fields provided when creating a new vault
+#[derive(Serialize, Deserialize)]
+pub struct VaultCreate {
+ pub name: String,
+ pub location: String,
+ pub token: String,
+}
+
+/// Fields provided when deleting a vault
+#[derive(Serialize, Deserialize)]
+pub struct VaultDelete {
+ pub name: String,
+ pub location: String,
+ pub token: String,
+}
+
+/// Add a vault to the API scope
+#[derive(Serialize, Deserialize)]
+pub struct ScopeVault {
+ pub name: String,
+ pub location: String,
+ pub token: String,
+}
+
+/// Remove a vault from the API scope
+#[derive(Serialize, Deserialize)]
+pub struct UnscopeVault {
+ pub name: String,
+ pub location: String,
+ pub token: String,
+}
+
+#[derive(Serialize, Deserialize)]
+pub struct UpdateVault {
+ pub name: String,
+ pub token: String,
+}
+
+#[derive(Serialize, Deserialize)]
+pub struct CreateRecord {
+ pub vault: String,
+ pub token: String,
+}
+
+/// Query to get a record
+#[derive(Serialize, Deserialize)]
+pub struct GetRecord {
+ pub name: String,
+ pub range: Option<(u32, u32)>,
+ pub token: String,
+}
+
+#[derive(Serialize, Deserialize)]
+pub struct UpdateRecord {
+ pub vault: String,
+ pub record: String,
+ pub token: String,
+}
+
+#[derive(Serialize, Deserialize)]
+pub struct DeleteRecord {
+ pub vault: String,
+ pub record: String,
+ pub token: String,
+}
+
+#[derive(Serialize, Deserialize)]
+pub struct Authenticate {
+ pub username: String,
+ pub password: String,
+}
+
+#[derive(Serialize, Deserialize)]
+pub struct Deauthenticate {
+ pub token: String,
+}
+
+#[derive(Serialize, Deserialize)]
+pub struct Register {
+ pub username: String,
+ pub password: String,
+ pub requested_role: Option<Role>,
+}
diff --git a/lockchain-http/src/models/mod.rs b/lockchain-http/src/models/mod.rs
new file mode 100644
index 0000000..3e22197
--- /dev/null
+++ b/lockchain-http/src/models/mod.rs
@@ -0,0 +1,4 @@
+//! Data models specific to the lockchain API
+
+pub mod inputs;
+pub mod responses; \ No newline at end of file
diff --git a/lockchain-http/src/model.rs b/lockchain-http/src/models/responses.rs
index 8ab79f4..ac4e74b 100644
--- a/lockchain-http/src/model.rs
+++ b/lockchain-http/src/models/responses.rs
@@ -1,4 +1,4 @@
-//! Data models specific to the lockchain API
+
use lockchain::errors::Error as LockError;
use serde::{de::DeserializeOwned, Serialize};
@@ -38,24 +38,10 @@ pub struct VaultList {
pub count: usize,
}
-/// Fields provided when creating a new vault
-#[derive(Serialize, Deserialize)]
-pub struct VaultCreate {
- pub name: String,
- pub location: String,
-}
-
-/// Query to get a record
-#[derive(Serialize, Deserialize)]
-pub struct GetRecord {
- pub name: String,
- pub range: Option<(u32, u32)>,
-}
-
/// Response to creating a new vault
#[derive(Serialize, Deserialize)]
pub struct VaultCreateResponse {
pub name: String,
pub created: bool,
pub error: Option<String>,
-}
+} \ No newline at end of file