From e29d0883a853c93e4eca1e502213dc603a9e6b93 Mon Sep 17 00:00:00 2001 From: Peter Cai Date: Fri, 21 Feb 2020 15:43:08 +0800 Subject: [PATCH] add user uuid and return it in auth response to match official impl why why why --- Cargo.lock | 83 +++++++++++++++++++ Cargo.toml | 3 +- .../2020-02-20-085623_create_user/up.sql | 1 + src/api.rs | 16 +++- src/main.rs | 1 + src/schema.rs | 1 + src/user.rs | 28 +++++-- 7 files changed, 124 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b6a7a09..6cbff15 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -93,6 +93,15 @@ version = "1.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" +[[package]] +name = "c2-chacha" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "214238caa1bf3a496ec3392968969cab8549f96ff30652c9e56885329315f6bb" +dependencies = [ + "ppv-lite86", +] + [[package]] name = "cc" version = "1.0.50" @@ -306,6 +315,17 @@ dependencies = [ "typenum", ] +[[package]] +name = "getrandom" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "hermit-abi" version = "0.1.7" @@ -709,6 +729,12 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677" +[[package]] +name = "ppv-lite86" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b" + [[package]] name = "proc-macro2" version = "0.4.30" @@ -792,6 +818,29 @@ dependencies = [ "winapi 0.3.8", ] +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom", + "libc", + "rand_chacha", + "rand_core 0.5.1", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03a2a90da8c7523f554344f921aa97283eadf6ac484a6d2a7d0212fa7f8d6853" +dependencies = [ + "c2-chacha", + "rand_core 0.5.1", +] + [[package]] name = "rand_core" version = "0.3.1" @@ -807,6 +856,24 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core 0.5.1", +] + [[package]] name = "rdrand" version = "0.4.0" @@ -1088,6 +1155,7 @@ dependencies = [ "rust-crypto", "scrypt", "serde", + "uuid", ] [[package]] @@ -1300,6 +1368,15 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4ae116fef2b7fea257ed6440d3cfcff7f190865f170cdad00bb6465bf18ecba" +[[package]] +name = "uuid" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11" +dependencies = [ + "rand 0.7.3", +] + [[package]] name = "vcpkg" version = "0.2.8" @@ -1329,6 +1406,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + [[package]] name = "winapi" version = "0.2.8" diff --git a/Cargo.toml b/Cargo.toml index c145035..8f02183 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,4 +16,5 @@ lazy_static = "1.4.0" serde = { version = "1.0.104", features = ["derive"] } scrypt = "0.2.0" rust-crypto = "0.2.36" -chrono = { version = "0.4", features = ["serde"] } \ No newline at end of file +chrono = { version = "0.4", features = ["serde"] } +uuid = { version = "0.8", features = ["v4"] } \ No newline at end of file diff --git a/migrations/2020-02-20-085623_create_user/up.sql b/migrations/2020-02-20-085623_create_user/up.sql index 4dd58f9..8507903 100644 --- a/migrations/2020-02-20-085623_create_user/up.sql +++ b/migrations/2020-02-20-085623_create_user/up.sql @@ -1,5 +1,6 @@ CREATE TABLE users ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + uuid VARCHAR NOT NULL, email VARCHAR NOT NULL, password VARCHAR NOT NULL, pw_cost INTEGER NOT NULL, diff --git a/src/api.rs b/src/api.rs index b0d83fc..4b4c20b 100644 --- a/src/api.rs +++ b/src/api.rs @@ -40,8 +40,15 @@ fn error_resp(status: Status, errors: Vec) -> Custom) -> Custom Custom> { // Try to find the user first let res = user::User::find_user_by_email(&db, mail) - .and_then(|u| u.create_token(passwd)); + .and_then(|u| u.create_token(passwd) + .map(|x| (u.uuid, u.email, x))); match res { - Ok(token) => success_resp(AuthResult { + Ok((uuid, email, token)) => success_resp(AuthResult { + user: AuthResultUser { + uuid, + email + }, token }), Err(user::UserOpError(e)) => diff --git a/src/main.rs b/src/main.rs index 9fcf701..df51e42 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ extern crate crypto; extern crate scrypt; #[macro_use] extern crate lazy_static; +extern crate uuid; mod schema; mod api; diff --git a/src/schema.rs b/src/schema.rs index 21ef538..b682c8e 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -15,6 +15,7 @@ table! { table! { users (id) { id -> Integer, + uuid -> Text, email -> Text, password -> Text, pw_cost -> Integer, diff --git a/src/user.rs b/src/user.rs index 3c78a55..7523dd6 100644 --- a/src/user.rs +++ b/src/user.rs @@ -1,6 +1,7 @@ use crate::schema::users; use crate::schema::users::dsl::*; use crate::{lock_db_write, lock_db_read}; +use crate::uuid::Uuid; use diesel::prelude::*; use diesel::sqlite::SqliteConnection; use rocket::request; @@ -64,6 +65,7 @@ impl Into for Password { #[derive(Queryable)] struct UserQuery { pub id: i32, + pub uuid: String, pub email: String, pub password: String, pub pw_cost: i32, @@ -75,6 +77,7 @@ impl Into for UserQuery { fn into(self) -> User { User { id: self.id, + uuid: self.uuid, email: self.email, // We can directly construct Password here // because it's already the hashed value from db @@ -89,6 +92,7 @@ impl Into for UserQuery { #[derive(Debug)] pub struct User { pub id: i32, + pub uuid: String, pub email: String, pub password: Password, pub pw_cost: i32, @@ -96,8 +100,7 @@ pub struct User { pub version: String } -#[derive(Insertable, Deserialize)] -#[table_name="users"] +#[derive(Deserialize)] pub struct NewUser { pub email: String, pub password: String, @@ -106,14 +109,27 @@ pub struct NewUser { pub version: String } +#[derive(Insertable)] +#[table_name="users"] +struct NewUserInsert { + uuid: String, + email: String, + password: String, + pw_cost: i32, + pw_nonce: String, + version: String +} + impl User { - pub fn create(db: &SqliteConnection, new_user: &NewUser) -> Result<(), UserOpError> { - let user_hashed = NewUser { + pub fn create(db: &SqliteConnection, new_user: &NewUser) -> Result { + let uid = Uuid::new_v4().to_hyphenated().to_string(); + let user_hashed = NewUserInsert { + uuid: uid.clone(), email: new_user.email.clone(), password: Password::new(&new_user.password).into(), pw_cost: new_user.pw_cost.clone(), pw_nonce: new_user.pw_nonce.clone(), - version: new_user.version.clone() + version: new_user.version.clone(), }; match Self::find_user_by_email(db, &new_user.email) { @@ -122,7 +138,7 @@ impl User { .and_then(|_| diesel::insert_into(users::table) .values(user_hashed) .execute(db) - .map(|_| ()) + .map(|_| uid) .map_err(|_| UserOpError::new("Database error"))) } }