nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
accounts.cc
Go to the documentation of this file.
1#include "server/accounts.h"
2
3#include <algorithm>
4#include <cctype>
5#include <string>
6#include <vector>
7
8#include "absl/strings/numbers.h"
9#include "protocol/messages.h"
10
11namespace nanommo {
12
13absl::Status CheckUsername(std::string_view username) {
14 if (username.size() < 3 || username.size() > 16) {
15 return Refusal(absl::StatusCode::kInvalidArgument,
16 Msg("Names are 3 to 16 characters long."));
17 }
18 const auto allowed = [](char c) {
19 return std::isalnum(static_cast<unsigned char>(c)) || c == '_';
20 };
21 if (!std::all_of(username.begin(), username.end(), allowed)) {
22 return Refusal(absl::StatusCode::kInvalidArgument,
23 Msg("Names are letters, digits and underscores."));
24 }
25 return absl::OkStatus();
26}
27
28absl::Status CheckPassword(std::string_view password) {
29 if (password.size() < 8) {
30 return Refusal(absl::StatusCode::kInvalidArgument,
31 Msg("Passwords are at least 8 characters long."));
32 }
33 if (password.size() > 128) {
34 return Refusal(absl::StatusCode::kInvalidArgument,
35 Msg("Passwords are at most 128 characters long."));
36 }
37 return absl::OkStatus();
38}
39
40absl::StatusOr<uint32_t> Accounts::Create(const std::string& username,
41 const std::string& password_hash) {
42 // The unique key on the name decides, with an AlreadyExists error. Checking
43 // first and inserting after would let two people take one name in between.
44 const absl::StatusOr<Result> result = database_.Run(
45 "INSERT INTO accounts (username, password_hash) VALUES (?, ?)",
46 {username, password_hash});
47 if (!result.ok()) {
48 return result.status();
49 }
50 return static_cast<uint32_t>(result->insert_id);
51}
52
53absl::StatusOr<Account> Accounts::Find(const std::string& username) {
54 const absl::StatusOr<Result> result = database_.Run(
55 "SELECT id, password_hash FROM accounts WHERE username = ?", {username});
56 if (!result.ok()) {
57 return result.status();
58 }
59 if (result->rows.empty()) {
60 return absl::NotFoundError("no such account");
61 }
62 const std::vector<std::string>& row = result->rows.front();
63 Account account{.password_hash = row[1]};
64 if (!absl::SimpleAtoi(row[0], &account.id)) {
65 return absl::InternalError("an account id that isn't a number");
66 }
67 return account;
68}
69
70} // namespace nanommo
absl::StatusOr< Account > Find(const std::string &username)
Returns a NotFound error if there's no account by that name.
Definition accounts.cc:53
absl::StatusOr< uint32_t > Create(const std::string &username, const std::string &password_hash)
Returns an AlreadyExists error if the name is taken.
Definition accounts.cc:40
Definition chat.cc:9
absl::Status CheckUsername(std::string_view username)
An InvalidArgument error says what's wrong, in words for the player.
Definition accounts.cc:13
absl::Status Refusal(absl::StatusCode code, const Sentence &why)
Definition messages.cc:218
Sentence Msg(std::string_view text, Parts... parts)
Definition messages.h:92
absl::Status CheckPassword(std::string_view password)
Definition accounts.cc:28
uint32_t id
Definition accounts.h:20