-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignup.rs
208 lines (179 loc) · 5.75 KB
/
signup.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use argon2::password_hash::{rand_core::OsRng, SaltString, PasswordHasher};
use axum::{extract::State, response::{Html, IntoResponse, Redirect, Response}, Form};
use chrono::Utc;
use serde::Deserialize;
use sqlx::MySqlPool;
use tera::Context;
use crate::{errors, models::{self, allocated_user::AllocatedUser, authenticated_user_record::AuthenticatedUserRecord, user::User}, state::AppState};
struct HashedPasswordRecord {
hash: String,
salt: String
}
fn hash_incoming_password(
password: String
) -> anyhow::Result<HashedPasswordRecord> {
// TODO: Check parameters with rich errors!
// TODO: Make testable!
// MARK: Password hashing
let salt = SaltString::generate(&mut OsRng);
let argon = argon2::Argon2::default();
let hash_result = argon.hash_password(password.as_bytes(), &salt);
if hash_result.is_err() {
return Err(anyhow::anyhow!("Could not hash password!"))
}
let hash = hash_result.unwrap().to_string();
Ok(
HashedPasswordRecord {
hash,
salt: salt.to_string()
}
)
}
async fn check_user_allocation(
db_pool: &sqlx::MySqlPool,
proposed_user: models::allocated_user::AllocatedUser,
) -> anyhow::Result<Option<AllocatedUser>>{
// Check user is allocated
let fetched_row: Option<AllocatedUser> =
sqlx::query_as!(
AllocatedUser,
"SELECT * FROM AllocatedUser WHERE username = ? AND secret = ?",
proposed_user.username,
proposed_user.secret
)
.fetch_optional(db_pool)
.await?;
Ok(fetched_row)
}
async fn remove_allocated_user(
db_pool: &sqlx::MySqlPool,
allocated_user: AllocatedUser
) -> anyhow::Result<()> {
sqlx::query!(
"DELETE FROM AllocatedUser WHERE username = ? AND secret = ?",
allocated_user.username,
allocated_user.secret
)
.execute(db_pool)
.await?;
Ok(())
}
async fn insert_user_into_db(
user: models::user::User,
db_pool: &sqlx::MySqlPool
) -> anyhow::Result<User> {
sqlx::query!(
"INSERT INTO User(id, username, email, description, created) VALUES (?, ?, ?, ?, ?);",
user.id.to_string(),
user.username,
user.email,
user.description,
user.created
)
.execute(db_pool)
.await?;
Ok(user)
}
async fn insert_user_authentication_details_into_db(
db_pool: &MySqlPool,
authenticated_user_record: AuthenticatedUserRecord
) -> anyhow::Result<()> {
sqlx::query!(
"INSERT INTO Authentication (user_id, pass_hash, salt, stale, updated) VALUES (?, ?, ?, ?, ?);",
authenticated_user_record.user_id.to_string(),
authenticated_user_record.pass_hash,
authenticated_user_record.salt,
authenticated_user_record.stale,
authenticated_user_record.updated
)
.execute(db_pool)
.await?;
Ok(())
}
#[derive(Debug)]
enum SignupFlowErrors {
UserNotAllocated
}
impl std::fmt::Display for SignupFlowErrors {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::UserNotAllocated => write!(f, "You haven't been invited to use hrefsurf yet, or you provided an incorrect username/secret. We cannot specify more for security reasons.")
}
}
}
async fn perform_signup_flow(
db_pool: &sqlx::MySqlPool,
form_details: SignupFormDetails,
) -> anyhow::Result<()> {
// MARK: Check for allocated user
// TODO: Check username uniqueness!
let allocated_user = AllocatedUser {
username: form_details.username.clone(),
secret: form_details.secret
};
let allocated_user
= check_user_allocation(db_pool, allocated_user).await?;
if allocated_user.is_none() {
return Err(anyhow::anyhow!(SignupFlowErrors::UserNotAllocated))
}
let allocated_user = allocated_user.unwrap();
let user = User {
id: uuid::Uuid::new_v4(),
username: form_details.username,
// TODO: Validate email
email: form_details.email,
description: "".to_owned(),
created: Utc::now().naive_utc()
};
let password_record = hash_incoming_password(
form_details.password
)?;
let authenticated_record = AuthenticatedUserRecord {
user_id: user.id.clone(),
pass_hash: password_record.hash,
salt: password_record.salt,
stale: false,
updated: Utc::now().naive_utc()
};
// MARK: Make changes to database
// Begin transaction for subsequent mutable changes to DB
let add_user_transaction = db_pool.begin().await?;
remove_allocated_user(db_pool, allocated_user).await?;
insert_user_into_db(user, db_pool).await?;
insert_user_authentication_details_into_db(db_pool, authenticated_record).await?;
add_user_transaction.commit().await?;
Ok(())
}
#[derive(Deserialize)]
pub struct SignupFormDetails {
pub username: String,
pub password: String,
pub email: String,
pub secret: String,
}
fn render_signup(
state: AppState
) -> Result<Html<String>, errors::HandlerErrors> {
let context = Context::new();
let render = state
.tera
.render("auth/signup.html", &context)?;
Ok(Html(render))
}
#[axum_macros::debug_handler]
pub async fn get(
State(state): State<AppState>
) -> Result<Html<String>, errors::HandlerErrors> {
render_signup(state)
}
#[axum_macros::debug_handler]
pub async fn post(
State(state): State<AppState>,
Form(form_details): Form<SignupFormDetails>
) -> Result<Response, errors::HandlerErrors> {
let result = perform_signup_flow(&state.db_pool, form_details).await;
if result.is_err() {
return Ok(render_signup(state).into_response()); // TODO: Prepopulate form
}
Ok(Redirect::to("/").into_response())
}