From a7774444fe0724840dbb877b0d17a48b6ddaebdd Mon Sep 17 00:00:00 2001 From: Bat Date: Sun, 22 Apr 2018 19:13:12 +0100 Subject: [PATCH] User creation --- Cargo.lock | 71 +++++++++++++++++++ Cargo.toml | 13 +++- .../2018-04-22-151330_create_user/down.sql | 2 + .../2018-04-22-151330_create_user/up.sql | 13 ++++ src/main.rs | 7 +- src/models/mod.rs | 1 + src/models/user.rs | 52 ++++++++++++++ src/routes/mod.rs | 1 + src/routes/user.rs | 50 +++++++++++++ src/schema.rs | 22 ++++++ templates/users/new.tera | 25 +++++++ 11 files changed, 253 insertions(+), 4 deletions(-) create mode 100644 migrations/2018-04-22-151330_create_user/down.sql create mode 100644 migrations/2018-04-22-151330_create_user/up.sql create mode 100644 src/models/user.rs create mode 100644 src/routes/user.rs create mode 100644 templates/users/new.tera diff --git a/Cargo.lock b/Cargo.lock index df48f631..6c396f93 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -49,11 +49,55 @@ dependencies = [ "safemem 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "base64" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "safemem 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "bcrypt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "base64 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", + "blowfish 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "bitflags" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "block-cipher-trait" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "blowfish" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "block-cipher-trait 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "opaque-debug 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "byte-tools" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "byteorder" version = "1.2.2" @@ -204,6 +248,14 @@ name = "gcc" version = "0.3.54" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "generic-array" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "glob" version = "0.2.11" @@ -368,6 +420,11 @@ dependencies = [ "libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "opaque-debug" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "ordermap" version = "0.2.13" @@ -401,6 +458,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "plume" version = "0.1.0" dependencies = [ + "bcrypt 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "diesel 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "dotenv 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "rocket 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -712,6 +770,11 @@ name = "typeable" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "typenum" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "ucd-util" version = "0.1.1" @@ -832,7 +895,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum backtrace 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ebbe525f66f42d207968308ee86bc2dd60aa5fab535b22e616323a173d097d8e" "checksum backtrace-sys 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "44585761d6161b0f57afc49482ab6bd067e4edef48c12a152c237eb0203f7661" "checksum base64 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "96434f987501f0ed4eb336a411e0631ecd1afa11574fe148587adc4ff96143c9" +"checksum base64 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "229d032f1a99302697f10b27167ae6d03d49d032e6a8e2550e8d3fc13356d2b4" +"checksum bcrypt 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1a1512813db09170b44a00870b58421876d797b77b085c5205a24db90905f758" "checksum bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b3c30d3802dfb7281680d6285f2ccdaa8c2d8fee41f93805dba5c4cf50dc23cf" +"checksum block-cipher-trait 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d6136d803280ae3532efa36114335255ea94f3d75d735ddedd66b0d7cd30bad3" +"checksum blowfish 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "95ede07672d9f4144c578439aa352604ec5c67a80c940fe8d382ddbeeeb3c6d8" +"checksum byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "560c32574a12a89ecd91f5e742165893f86e3ab98d21f8ea548658eb9eef5f40" "checksum byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "73b5bdfe7ee3ad0b99c9801d58807a9dbc9e09196365b0203853b99889ab3c87" "checksum cc 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)" = "8b9d2900f78631a5876dc5d6c9033ede027253efcd33dd36b1309fc6cab97ee0" "checksum cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c819a1287eb618df47cc647173c5c4c66ba19d888a6e50d605672aed3140de" @@ -851,6 +919,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" "checksum gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)" = "5e33ec290da0d127825013597dbdfc28bee4964690c7ce1166cbc2a7bd08b1bb" +"checksum generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ef25c5683767570c2bbd7deba372926a55eaae9982d7726ee2a1050239d45b9d" "checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" "checksum httparse 1.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "c2f407128745b78abc95c0ffbe4e5d37427fdc0d45470710cfef8c44522a2e37" "checksum humansize 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e" @@ -874,6 +943,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum num-integer 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f8d26da319fb45674985c78f1d1caf99aa4941f785d384a2ae36d0740bc3e2fe" "checksum num-traits 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dee092fcdf725aee04dd7da1d21debff559237d49ef1cb3e69bcb8ece44c7364" "checksum num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a3322e4bca9d212ad9a158a02abc6934d005490c054a2778df73a70aa0a30" +"checksum opaque-debug 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d620c9c26834b34f039489ac0dfdb12c7ac15ccaf818350a64c9b5334a452ad7" "checksum ordermap 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "b81cf3b8cb96aa0e73bbedfcdc9708d09fec2854ba8d474be4e6f666d7379e8b" "checksum pear 0.0.16 (registry+https://github.com/rust-lang/crates.io-index)" = "b9b645aa07cf1010a67e9f67b4b9b96d6c5fb9315eee678a061d6ab58e9cb77f" "checksum pear_codegen 0.0.16 (registry+https://github.com/rust-lang/crates.io-index)" = "ca34109829349aeefe22772916da5404b3f5cd0e63a72c5d91209fc809342265" @@ -913,6 +983,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "a0263c6c02c4db6c8f7681f9fd35e90de799ebd4cfdeab77a38f4ff6b3d8c0d9" "checksum traitobject 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "efd1f82c56340fdf16f2a953d7bda4f8fdffba13d93b00844c25572110b26079" "checksum typeable 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1410f6f91f21d1612654e7cc69193b0334f909dcf2c790c4826254fbb86f8887" +"checksum typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "612d636f949607bdf9b123b4a6f6d966dedf3ff669f7f045890d3a4a73948169" "checksum ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fd2be2d6639d0f8fe6cdda291ad456e23629558d466e2789d2c3e9892bda285d" "checksum unicase 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7f4765f83163b74f957c797ad9253caf97f103fb064d3999aea9568d09fc8a33" "checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" diff --git a/Cargo.toml b/Cargo.toml index 7d993ca0..2421726f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,8 +3,15 @@ authors = ["Bat' "] name = "plume" version = "0.1.0" [dependencies] -diesel = { version = "*", features = [ "postgres", "r2d2" ] } +bcrypt = "0.2" +dotenv = "*" rocket = "*" rocket_codegen = "*" -rocket_contrib = { version = "*", features = [ "tera_templates", "json" ] } -dotenv = "*" + +[dependencies.diesel] +features = ["postgres", "r2d2"] +version = "*" + +[dependencies.rocket_contrib] +features = ["tera_templates", "json"] +version = "*" diff --git a/migrations/2018-04-22-151330_create_user/down.sql b/migrations/2018-04-22-151330_create_user/down.sql new file mode 100644 index 00000000..dc3714bd --- /dev/null +++ b/migrations/2018-04-22-151330_create_user/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE users; diff --git a/migrations/2018-04-22-151330_create_user/up.sql b/migrations/2018-04-22-151330_create_user/up.sql new file mode 100644 index 00000000..6c6114e3 --- /dev/null +++ b/migrations/2018-04-22-151330_create_user/up.sql @@ -0,0 +1,13 @@ +-- Your SQL goes here +CREATE TABLE users ( + id SERIAL PRIMARY KEY, + username VARCHAR NOT NULL, + display_name VARCHAR NOT NULL DEFAULT '', + outbox_url VARCHAR NOT NULL, + inbox_url VARCHAR NOT NULL, + is_admin BOOLEAN NOT NULL DEFAULT 'f', + summary TEXT NOT NULL DEFAULT '', + email TEXT, + hashed_password TEXT, + instance_id INTEGER REFERENCES instances(id) ON DELETE CASCADE NOT NULL +); diff --git a/src/main.rs b/src/main.rs index ae1f07fc..0ce5f6e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ extern crate diesel; extern crate dotenv; extern crate rocket; extern crate rocket_contrib; +extern crate bcrypt; use diesel::pg::PgConnection; use diesel::r2d2::{ConnectionManager, Pool}; @@ -48,7 +49,11 @@ fn main() { rocket::ignite() .mount("/", routes![ routes::instance::configure, - routes::instance::post_config + routes::instance::post_config, + + routes::user::details, + routes::user::new, + routes::user::create, ]) .manage(init_pool()) .attach(Template::fairing()) diff --git a/src/models/mod.rs b/src/models/mod.rs index 1d5ea990..5cd0b566 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1 +1,2 @@ pub mod instance; +pub mod user; diff --git a/src/models/user.rs b/src/models/user.rs new file mode 100644 index 00000000..950c8d2f --- /dev/null +++ b/src/models/user.rs @@ -0,0 +1,52 @@ +use diesel; +use diesel::{ QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection }; +use schema::users; + +#[derive(Queryable, Identifiable)] +pub struct User { + pub id: i32, + pub username: String, + pub display_name: String, + pub outbox_url: String, + pub inbox_url: String, + pub is_admin: bool, + pub summary: String, + pub email: Option, + pub hashed_password: Option, + pub instance_id: i32 +} + +#[derive(Insertable)] +#[table_name = "users"] +pub struct NewUser { + pub username: String, + pub display_name: String, + pub outbox_url: String, + pub inbox_url: String, + pub is_admin: bool, + pub summary: String, + pub email: Option, + pub hashed_password: Option, + pub instance_id: i32 +} + +impl User { + fn grant_admin_rights() {} + + pub fn insert (conn: &PgConnection, new: NewUser) -> User { + diesel::insert_into(users::table) + .values(new) + .get_result(conn) + .expect("Error saving new instance") + } + + pub fn compute_outbox(user: String, hostname: String) -> String { + format!("https://{}/@/{}/outbox", hostname, user) + } + + pub fn compute_inbox(user: String, hostname: String) -> String { + format!("https://{}/@/{}/inbox", hostname, user) + } + + fn get () {} +} diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 1d5ea990..5cd0b566 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -1 +1,2 @@ pub mod instance; +pub mod user; diff --git a/src/routes/user.rs b/src/routes/user.rs new file mode 100644 index 00000000..c8d43fed --- /dev/null +++ b/src/routes/user.rs @@ -0,0 +1,50 @@ +use rocket::request::Form; +use rocket::response::Redirect; +use rocket_contrib::Template; +use std::collections::HashMap; +use bcrypt::{hash, DEFAULT_COST}; + +use db_conn::DbConn; +use models::user::*; +use models::instance::Instance; + + +#[get("/@/")] +fn details(name: String) { + +} + +#[get("/users/new")] +fn new() -> Template { + Template::render("users/new", HashMap::::new()) +} + +#[derive(FromForm)] +struct NewUserForm { + username: String, + email: String, + password: String, + password_confirmation: String +} + +#[post("/users/new", data = "")] +fn create(conn: DbConn, data: Form) -> Redirect { + let inst = Instance::get_local(&*conn).unwrap(); + let form = data.get(); + + if form.password == form.password_confirmation { + User::insert(&*conn, NewUser { + username: form.username.to_string(), + display_name: form.username.to_string(), + outbox_url: User::compute_outbox(form.username.to_string(), inst.public_domain.to_string()), + inbox_url: User::compute_inbox(form.username.to_string(), inst.public_domain.to_string()), + is_admin: !inst.has_admin(&*conn), + summary: String::from(""), + email: Some(form.email.to_string()), + hashed_password: Some(hash(form.password.as_str(), DEFAULT_COST).unwrap()), + instance_id: inst.id + }); + } + + Redirect::to(format!("/@/{}", data.get().username).as_str()) +} diff --git a/src/schema.rs b/src/schema.rs index 5f535de2..9a5abdca 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -8,3 +8,25 @@ table! { blocked -> Bool, } } + +table! { + users (id) { + id -> Int4, + username -> Varchar, + display_name -> Varchar, + outbox_url -> Varchar, + inbox_url -> Varchar, + is_admin -> Bool, + summary -> Text, + email -> Nullable, + hashed_password -> Nullable, + instance_id -> Int4, + } +} + +joinable!(users -> instances (instance_id)); + +allow_tables_to_appear_in_same_query!( + instances, + users, +); diff --git a/templates/users/new.tera b/templates/users/new.tera new file mode 100644 index 00000000..6dd3f791 --- /dev/null +++ b/templates/users/new.tera @@ -0,0 +1,25 @@ + + + + + New account + + +

Create an account

+
+ + + + + + + + + + + + + +
+ +