| @@ -4,13 +4,31 @@ |
| 4 | 4 | |
| 5 | 5 | import std/bytes |
| 6 | 6 | import std/http.{type Request, type Response, Response} |
| 7 | | −import std/list |
| 7 | +import std/option |
| 8 | +import std/result |
| 8 | 9 | import std/string |
| 9 | 10 | import argon2 |
| 10 | 11 | import views |
| 11 | 12 | |
| 12 | 13 | pub type User { User(name: String, password_hash: String) } |
| 13 | 14 | |
| 15 | +/// Why a registration failed. |
| 16 | +pub type RegisterError { |
| 17 | + /// The name has fewer than 2 characters, or the password fewer than 8. |
| 18 | + TooShort |
| 19 | + NameTaken |
| 20 | + /// The password could not be hashed: the reason. |
| 21 | + HashFailed(String) |
| 22 | +} |
| 23 | + |
| 24 | +/// Why a login failed. |
| 25 | +pub type LoginError { |
| 26 | + UnknownUser |
| 27 | + WrongPassword |
| 28 | + /// The stored hash cannot be checked: the reason. |
| 29 | + BadHash(String) |
| 30 | +} |
| 31 | + |
| 14 | 32 | /// Logging. |
| 15 | 33 | pub effect log { |
| 16 | 34 | fn log(message: String) -> () |
| @@ -19,8 +37,8 @@ pub effect log { |
| 19 | 37 | /// Users and sessions. |
| 20 | 38 | pub effect users { |
| 21 | 39 | fn find_user(name: String) -> Option<User> |
| 22 | | − /// False if the name is taken. |
| 23 | | − fn add_user(user: User) -> Bool |
| 40 | + /// `Error(NameTaken)` if the name is taken. |
| 41 | + fn add_user(user: User) -> Result<(), RegisterError> |
| 24 | 42 | /// A new session for `name`; its token. |
| 25 | 43 | fn new_session(name: String) -> String |
| 26 | 44 | fn session_user(token: String) -> Option<String> |
| @@ -50,36 +68,48 @@ pub fn route(req: Request) -> Response / <log, users, ffi> { |
| 50 | 68 | |
| 51 | 69 | fn login(fields) { |
| 52 | 70 | let name = field(fields, "name") |
| 53 | | − let password = field(fields, "password") |
| 54 | | − let valid = match find_user(name) { |
| 55 | | − Some(user) -> argon2.verify(password, user.password_hash), |
| 56 | | − None -> False, |
| 57 | | − } |
| 58 | | − if valid { |
| 59 | | − log("login " <> name) |
| 60 | | − start_session(name) |
| 61 | | − } else { |
| 62 | | − log("failed login " <> name) |
| 63 | | − with_status(401, http.html(views.login("Wrong name or password."))) |
| 71 | + match check_password(name, field(fields, "password")) { |
| 72 | + Ok(()) -> { |
| 73 | + log("login " <> name) |
| 74 | + start_session(name) |
| 75 | + }, |
| 76 | + Error(BadHash(reason)) -> { |
| 77 | + log("cannot check the password of " <> name <> ": " <> reason) |
| 78 | + http.text(500, "cannot check the password") |
| 79 | + }, |
| 80 | + // The same answer for both, so it does not tell which names exist. |
| 81 | + Error(UnknownUser) | Error(WrongPassword) -> { |
| 82 | + log("failed login " <> name) |
| 83 | + with_status(401, http.html(views.login("Wrong name or password."))) |
| 84 | + }, |
| 64 | 85 | } |
| 65 | 86 | } |
| 66 | 87 | |
| 88 | +fn check_password(name: String, password: String) -> Result<(), LoginError> / <users, ffi> { |
| 89 | + use user <- result.try(find_user(name) |> option.to_result(UnknownUser)) |
| 90 | + use matches <- result.try(argon2.verify(password, user.password_hash) |> result.map_error(BadHash)) |
| 91 | + if matches { Ok(()) } else { Error(WrongPassword) } |
| 92 | +} |
| 93 | + |
| 67 | 94 | fn register(fields) { |
| 68 | 95 | let name = field(fields, "name") |
| 69 | | − let password = field(fields, "password") |
| 96 | + match new_user(name, field(fields, "password")) { |
| 97 | + Ok(()) -> { |
| 98 | + log("registered " <> name) |
| 99 | + start_session(name) |
| 100 | + }, |
| 101 | + Error(TooShort) -> with_status(400, http.html(views.login("Names need 2 characters and passwords 8."))), |
| 102 | + Error(NameTaken) -> with_status(409, http.html(views.login("That name is taken."))), |
| 103 | + Error(HashFailed(reason)) -> http.text(500, reason), |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +fn new_user(name: String, password: String) -> Result<(), RegisterError> / <users, ffi> { |
| 70 | 108 | if string.length(name) < 2 || string.length(password) < 8 { |
| 71 | | − with_status(400, http.html(views.login("Names need 2 characters and passwords 8."))) |
| 109 | + Error(TooShort) |
| 72 | 110 | } else { |
| 73 | | − match argon2.hash_password(password, hashing()) { |
| 74 | | − Ok(hash) -> |
| 75 | | − if add_user(User(name, hash)) { |
| 76 | | − log("registered " <> name) |
| 77 | | − start_session(name) |
| 78 | | − } else { |
| 79 | | − with_status(409, http.html(views.login("That name is taken."))) |
| 80 | | − }, |
| 81 | | − Error(e) -> http.text(500, e), |
| 82 | | − } |
| 111 | + use hash <- result.try(argon2.hash_password(password, hashing()) |> result.map_error(HashFailed)) |
| 112 | + add_user(User(name, hash)) |
| 83 | 113 | } |
| 84 | 114 | } |
| 85 | 115 | |
| @@ -107,10 +137,7 @@ fn session_of(cookies: List<String>) -> Option<String> / <users> { |
| 107 | 137 | } |
| 108 | 138 | |
| 109 | 139 | fn field(fields, name) { |
| 110 | | − match http.field(fields, name) { |
| 111 | | − Some(v) -> v, |
| 112 | | − None -> "", |
| 113 | | − } |
| 140 | + http.field(fields, name) |> option.unwrap_or("") |
| 114 | 141 | } |
| 115 | 142 | |
| 116 | 143 | fn redirect(to: String, cookie: String) -> Response { |