add user uuid and return it in auth response to match official impl

why why why
This commit is contained in:
Peter Cai 2020-02-21 15:43:08 +08:00
parent 6729959580
commit e29d0883a8
No known key found for this signature in database
GPG key ID: 71F5FB4E4F3FD54F
7 changed files with 124 additions and 9 deletions

83
Cargo.lock generated
View file

@ -93,6 +93,15 @@ version = "1.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" 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]] [[package]]
name = "cc" name = "cc"
version = "1.0.50" version = "1.0.50"
@ -306,6 +315,17 @@ dependencies = [
"typenum", "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]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.1.7" version = "0.1.7"
@ -709,6 +729,12 @@ version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677" checksum = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677"
[[package]]
name = "ppv-lite86"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "0.4.30" version = "0.4.30"
@ -792,6 +818,29 @@ dependencies = [
"winapi 0.3.8", "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]] [[package]]
name = "rand_core" name = "rand_core"
version = "0.3.1" version = "0.3.1"
@ -807,6 +856,24 @@ version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" 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]] [[package]]
name = "rdrand" name = "rdrand"
version = "0.4.0" version = "0.4.0"
@ -1088,6 +1155,7 @@ dependencies = [
"rust-crypto", "rust-crypto",
"scrypt", "scrypt",
"serde", "serde",
"uuid",
] ]
[[package]] [[package]]
@ -1300,6 +1368,15 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4ae116fef2b7fea257ed6440d3cfcff7f190865f170cdad00bb6465bf18ecba" 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]] [[package]]
name = "vcpkg" name = "vcpkg"
version = "0.2.8" version = "0.2.8"
@ -1329,6 +1406,12 @@ dependencies = [
"winapi-util", "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]] [[package]]
name = "winapi" name = "winapi"
version = "0.2.8" version = "0.2.8"

View file

@ -16,4 +16,5 @@ lazy_static = "1.4.0"
serde = { version = "1.0.104", features = ["derive"] } serde = { version = "1.0.104", features = ["derive"] }
scrypt = "0.2.0" scrypt = "0.2.0"
rust-crypto = "0.2.36" rust-crypto = "0.2.36"
chrono = { version = "0.4", features = ["serde"] } chrono = { version = "0.4", features = ["serde"] }
uuid = { version = "0.8", features = ["v4"] }

View file

@ -1,5 +1,6 @@
CREATE TABLE users ( CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
uuid VARCHAR NOT NULL,
email VARCHAR NOT NULL, email VARCHAR NOT NULL,
password VARCHAR NOT NULL, password VARCHAR NOT NULL,
pw_cost INTEGER NOT NULL, pw_cost INTEGER NOT NULL,

View file

@ -40,8 +40,15 @@ fn error_resp<T: Serialize>(status: Status, errors: Vec<String>) -> Custom<JsonR
})) }))
} }
#[derive(Serialize)]
struct AuthResultUser {
email: String,
uuid: String
}
#[derive(Serialize)] #[derive(Serialize)]
struct AuthResult { struct AuthResult {
user: AuthResultUser,
token: String token: String
} }
@ -69,9 +76,14 @@ fn auth_sign_in(db: DbConn, params: Json<SignInParams>) -> Custom<JsonResp<AuthR
fn _sign_in(db: DbConn, mail: &str, passwd: &str) -> Custom<JsonResp<AuthResult>> { fn _sign_in(db: DbConn, mail: &str, passwd: &str) -> Custom<JsonResp<AuthResult>> {
// Try to find the user first // Try to find the user first
let res = user::User::find_user_by_email(&db, mail) 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 { match res {
Ok(token) => success_resp(AuthResult { Ok((uuid, email, token)) => success_resp(AuthResult {
user: AuthResultUser {
uuid,
email
},
token token
}), }),
Err(user::UserOpError(e)) => Err(user::UserOpError(e)) =>

View file

@ -16,6 +16,7 @@ extern crate crypto;
extern crate scrypt; extern crate scrypt;
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
extern crate uuid;
mod schema; mod schema;
mod api; mod api;

View file

@ -15,6 +15,7 @@ table! {
table! { table! {
users (id) { users (id) {
id -> Integer, id -> Integer,
uuid -> Text,
email -> Text, email -> Text,
password -> Text, password -> Text,
pw_cost -> Integer, pw_cost -> Integer,

View file

@ -1,6 +1,7 @@
use crate::schema::users; use crate::schema::users;
use crate::schema::users::dsl::*; use crate::schema::users::dsl::*;
use crate::{lock_db_write, lock_db_read}; use crate::{lock_db_write, lock_db_read};
use crate::uuid::Uuid;
use diesel::prelude::*; use diesel::prelude::*;
use diesel::sqlite::SqliteConnection; use diesel::sqlite::SqliteConnection;
use rocket::request; use rocket::request;
@ -64,6 +65,7 @@ impl Into<String> for Password {
#[derive(Queryable)] #[derive(Queryable)]
struct UserQuery { struct UserQuery {
pub id: i32, pub id: i32,
pub uuid: String,
pub email: String, pub email: String,
pub password: String, pub password: String,
pub pw_cost: i32, pub pw_cost: i32,
@ -75,6 +77,7 @@ impl Into<User> for UserQuery {
fn into(self) -> User { fn into(self) -> User {
User { User {
id: self.id, id: self.id,
uuid: self.uuid,
email: self.email, email: self.email,
// We can directly construct Password here // We can directly construct Password here
// because it's already the hashed value from db // because it's already the hashed value from db
@ -89,6 +92,7 @@ impl Into<User> for UserQuery {
#[derive(Debug)] #[derive(Debug)]
pub struct User { pub struct User {
pub id: i32, pub id: i32,
pub uuid: String,
pub email: String, pub email: String,
pub password: Password, pub password: Password,
pub pw_cost: i32, pub pw_cost: i32,
@ -96,8 +100,7 @@ pub struct User {
pub version: String pub version: String
} }
#[derive(Insertable, Deserialize)] #[derive(Deserialize)]
#[table_name="users"]
pub struct NewUser { pub struct NewUser {
pub email: String, pub email: String,
pub password: String, pub password: String,
@ -106,14 +109,27 @@ pub struct NewUser {
pub version: String 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 { impl User {
pub fn create(db: &SqliteConnection, new_user: &NewUser) -> Result<(), UserOpError> { pub fn create(db: &SqliteConnection, new_user: &NewUser) -> Result<String, UserOpError> {
let user_hashed = NewUser { let uid = Uuid::new_v4().to_hyphenated().to_string();
let user_hashed = NewUserInsert {
uuid: uid.clone(),
email: new_user.email.clone(), email: new_user.email.clone(),
password: Password::new(&new_user.password).into(), password: Password::new(&new_user.password).into(),
pw_cost: new_user.pw_cost.clone(), pw_cost: new_user.pw_cost.clone(),
pw_nonce: new_user.pw_nonce.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) { match Self::find_user_by_email(db, &new_user.email) {
@ -122,7 +138,7 @@ impl User {
.and_then(|_| diesel::insert_into(users::table) .and_then(|_| diesel::insert_into(users::table)
.values(user_hashed) .values(user_hashed)
.execute(db) .execute(db)
.map(|_| ()) .map(|_| uid)
.map_err(|_| UserOpError::new("Database error"))) .map_err(|_| UserOpError::new("Database error")))
} }
} }