~vpzom/bracketmonster

ref: bd165fcdff7d65503132b761848379e5df767c7e bracketmonster/brackend/src/routes/v1/users.rs -rw-r--r-- 9.3 KiB
bd165fcdColin Reeder Apply username restrictions for new accounts, too 2 years ago
                                                                                
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
use crate::{report_parse_error, require_login, simple_response, to_200, to_204, BracketID, BracketType, DbPool, Error, LoginError, UserID};

#[derive(Debug, Clone, Copy)]
enum UserIDOrMe {
    Me,
    UserID(UserID),
}

impl UserIDOrMe {
    pub fn resolve(self, me: UserID) -> UserID {
        match self {
            UserIDOrMe::Me => me,
            UserIDOrMe::UserID(id) => id,
        }
    }
}

impl std::str::FromStr for UserIDOrMe {
    type Err = <UserID as std::str::FromStr>::Err;

    fn from_str(src: &str) -> Result<Self, Self::Err> {
        if src == "~me" {
            Ok(UserIDOrMe::Me)
        } else {
            src.parse().map(UserIDOrMe::UserID)
        }
    }
}

async fn require_me(user: UserIDOrMe, me: UserID) -> Result<(), Error> {
    match user {
        UserIDOrMe::Me => Ok(()),
        UserIDOrMe::UserID(user) => {
            if user == me {
                Ok(())
            } else {
                Err(Error::UserError(simple_response(hyper::StatusCode::FORBIDDEN, "You are not authorized to do this")))
            }
        }
    }
}

fn is_allowed_username(username: &str) -> bool {
    username
        .chars()
        .all(|chr| chr.is_ascii_alphabetic() || chr.is_ascii_digit())
}

pub fn route_users() -> crate::RouteNode<()> {
    let route_brackets = crate::RouteNode::new()
        .with_handler_async("GET", |(user,), ctx, req| async move {
            route_users_brackets_list_fn(ctx.db_pool.clone(), req, user)
                .await
                .map(|v| to_200(&v))
        });

    let route_user =
        crate::RouteNode::new().with_handler_async("PATCH", |(user,), ctx, req| async move {
            route_users_edit_fn(ctx.db_pool.clone(), req, user)
                .await
                .map(to_204)
        })
        .with_handler_async("GET", |(user,), ctx, req| async move {
            route_users_get_fn(ctx.db_pool.clone(), req, user)
                .await
                .map(|v| to_200(&v))
        })
        .with_child("brackets", route_brackets);

    crate::RouteNode::new()
        .with_handler_async("POST", |_, ctx, req| async move {
            route_users_create_fn(ctx.db_pool.clone(), req)
                .await
                .map(|v| to_200(&v))
        })
        .with_child_parse::<UserIDOrMe, _>(route_user)
}

async fn route_users_create_fn(
    db_pool: DbPool,
    req: hyper::Request<hyper::Body>,
) -> Result<serde_json::Value, Error> {
    #[derive(serde_derive::Deserialize, Debug, Default)]
    struct UserCreateBody {
        username: Option<String>,
        password: Option<String>,
    }

    let body = hyper::body::to_bytes(req.into_body()).await?;

    let body: UserCreateBody = if body.is_empty() {
        Default::default()
    } else {
        serde_json::from_slice(&body).map_err(report_parse_error)?
    };

    let client = db_pool.get().await?;

    let (user_id, username): (i32, _) = match if let Some(username) = body.username {
        if !is_allowed_username(&username) {
            return Err(Error::UserError(crate::simple_response(hyper::StatusCode::BAD_REQUEST, "Invalid characters in username")));
        }

        if let Some(password) = body.password {
            let password_hash =
                tokio::task::spawn_blocking(|| bcrypt::hash(password, bcrypt::DEFAULT_COST))
                    .await??;
            let stmt = client
                .prepare("INSERT INTO users (username, password_hash) VALUES ($1, $2) RETURNING id")
                .await?;
            Some((
                client
                    .query_one(&stmt, &[&username, &password_hash])
                    .await?,
                Some(username),
            ))
        } else {
            None
        }
    } else if body.password.is_some() {
        None
    } else {
        let stmt = client
            .prepare("INSERT INTO users DEFAULT VALUES RETURNING id")
            .await?;
        Some((client.query_one(&stmt, &[]).await?, None))
    } {
        Some((row, username)) => (row.get(0), username),
        None => {
            return Err(Error::UserError({
                let mut res =
                    hyper::Response::new("username and password cannot be used separately".into());
                *res.status_mut() = hyper::StatusCode::BAD_REQUEST;
                res
            }));
        }
    };

    let token = uuid::Uuid::new_v4();
    {
        let stmt = client
            .prepare("INSERT INTO logins (token, user_id) VALUES ($1, $2)")
            .await?;
        client.execute(&stmt, &[&token, &user_id]).await?;
    }

    Ok(serde_json::json!({
        "token": token.to_string(),
        "user": {
            "id": user_id,
            "username": username,
        }
    }))
}

async fn route_users_get_fn(
    db_pool: DbPool,
    req: hyper::Request<hyper::Body>,
    user: UserIDOrMe,
) -> Result<serde_json::Value, Error> {
    let user: i32 = match user {
        UserIDOrMe::Me => {
            let user = crate::get_login(&req, &db_pool)
                .await
                .map_err(LoginError::to_user)?;

            match user {
                None => {
                    Err(Error::UserError({
                        let mut res = hyper::Response::new("You are not logged in".into());
                        *res.status_mut() = hyper::StatusCode::UNAUTHORIZED;
                        res
                    }))
                },
                Some(user) => Ok(user),
            }?
        },
        UserIDOrMe::UserID(user) => user,
    };

    let client = db_pool.get().await?;

    let row = client.query_opt("SELECT username FROM users WHERE id=$1", &[&user]).await?;

    match row {
        None => Err(Error::UserError({
            let mut res = hyper::Response::new("No such user".into());
            *res.status_mut() = hyper::StatusCode::NOT_FOUND;
            res
        })),
        Some(row) => {
            let username: Option<String> = row.get(0);

            Ok(serde_json::json!({
                "username": username,
                "id": user
            }))
        }
    }
}

async fn route_users_edit_fn(
    db_pool: DbPool,
    req: hyper::Request<hyper::Body>,
    user: UserIDOrMe,
) -> Result<(), Error> {
    #[derive(serde_derive::Deserialize, Debug)]
    struct UserEditBody {
        username: Option<String>,
        password: Option<String>,
        email: Option<String>,
    }

    let login_user = require_login(&req, &db_pool).await?;
    let user = user.resolve(login_user);

    let body: UserEditBody = serde_json::from_slice(&hyper::body::to_bytes(req.into_body()).await?)
        .map_err(report_parse_error)?;

    if user != login_user {
        return Err(Error::UserError({
            let mut res =
                hyper::Response::new("You don't have permission to modify this user".into());
            *res.status_mut() = hyper::StatusCode::FORBIDDEN;
            res
        }));
    }

    let client = db_pool.get().await?;

    let mut values = Vec::with_capacity(3);
    let mut columns = Vec::with_capacity(3);

    if let Some(username) = body.username {
        if !is_allowed_username(&username) {
            return Err(Error::UserError({
                let mut res = hyper::Response::new("Invalid characters in username".into());
                *res.status_mut() = hyper::StatusCode::BAD_REQUEST;
                res
            }));
        }

        values.push(username);
        columns.push("username");
    }

    if let Some(password) = body.password {
        let hash =
            tokio::task::spawn_blocking(|| bcrypt::hash(password, bcrypt::DEFAULT_COST)).await??;

        values.push(hash);
        columns.push("password_hash");
    }

    if let Some(email) = body.email {
        if !email.contains('@') {
            return Err(Error::UserError({
                let mut res = hyper::Response::new("Invalid email address".into());
                *res.status_mut() = hyper::StatusCode::BAD_REQUEST;
                res
            }));
        }

        values.push(email);
        columns.push("email");
    }

    let mut values: Vec<&(dyn tokio_postgres::types::ToSql + Sync)> =
        values.iter().map(|x| x as _).collect();
    values.push(&user);

    let sql: &str = &format!(
        "UPDATE users SET {} WHERE id=${}",
        columns
            .into_iter()
            .enumerate()
            .map(|(idx, col)| format!("{} = ${}", col, idx + 1))
            .collect::<Vec<_>>()
            .join(", "),
        values.len()
    );

    client.execute(sql, &values[..]).await?;

    Ok(())
}

async fn route_users_brackets_list_fn(
    db_pool: DbPool,
    req: hyper::Request<hyper::Body>,
    user: UserIDOrMe,
) -> Result<serde_json::Value, Error> {
    let login = require_login(&req, &db_pool).await?;
    require_me(user, login).await?;
    let user = login;

    let client = db_pool.get().await?;

    let stmt = client.prepare("SELECT id, name, type FROM brackets, bracket_access WHERE bracket_access.bracket=brackets.id AND is_admin=TRUE AND user_id=$1").await?;
    let rows = client.query(&stmt, &[&user]).await?;

    Ok(serde_json::Value::Array(
        rows.into_iter()
            .map(|row| {
                serde_json::json!({
                    "id": BracketID::from_internal(row.get(0)).to_external(),
                    "name": row.get::<_, String>(1),
                    "type": BracketType::from_internal(&row.get::<_, String>(2)),
                })
            })
            .collect(),
    ))
}