diff --git a/PROTOCOL.md b/PROTOCOL.md
index e4cf543..6b88c0b 100644
--- a/PROTOCOL.md
+++ b/PROTOCOL.md
@@ -188,20 +188,9 @@ Communication between the master server and clients will be done over a WebSocke
2 |
Secret |
- Bytes (16) |
+ Bytes (128) |
R1 |
-
- 3 |
- Server Address |
- IPv4 String |
- R1 |
-
-
- 4 |
- Server Port |
- Packed Unsigned Short |
- R1 |
diff --git a/src/server/db/_init_sql.hpp b/src/server/db/_init_sql.hpp
index aaa64d0..9902a82 100644
--- a/src/server/db/_init_sql.hpp
+++ b/src/server/db/_init_sql.hpp
@@ -14,15 +14,11 @@ const char* _mem_db_sql =
"`MAX_USERS` INTEGER NOT NULL DEFAULT 0"
");\n"
- "CREATE UNIQUE INDEX `UIX_SERVER_LICENSES` ON `SERVER_LICENSES` ("
- "`KEY_ID`, `SECRET`"
- ");\n"
-
"CREATE TABLE `USER_KEYS` ("
"`ID` INTEGER,"
"`SECRET` BLOB NOT NULL UNIQUE,"
"PRIMARY KEY(`ID`)"
- ");";
+ ");\n";
const char* _hard_db_init_migration_sql =
"CREATE TABLE `MIGRATIONS` ("
@@ -35,11 +31,15 @@ const char* _hard_db_init_migration_sql =
const std::vector _hard_db_sql = {
/** START MIGRATION 0 **/
"CREATE TABLE `SERVER_LICENSES` ("
- "`KEY_ID` TEXT NOT NULL UNIQUE,"
- "`SECRET` BLOB NOT NULL UNIQUE,"
+ "`KEY_ID` TEXT NOT NULL PRIMARY KEY AUTOINCREMENT,"
+ "`SECRET` BLOB NOT NULL,"
"`ALLOWANCE` INTEGER NOT NULL DEFAULT 0"
");\n"
+ "CREATE UNIQUE INDEX `UIX_SERVER_LICENSES` ON `SERVER_LICENSES` ("
+ "`KEY_ID`, `SECRET`"
+ ");\n"
+
"CREATE TABLE `USERS` ("
"`ID` INTEGER PRIMARY KEY AUTOINCREMENT,"
"`USERNAME` TEXT NOT NULL,"
diff --git a/src/server/hosts/master.hpp b/src/server/hosts/master.hpp
index d11bcf9..7b83302 100644
--- a/src/server/hosts/master.hpp
+++ b/src/server/hosts/master.hpp
@@ -17,9 +17,25 @@ namespace sosc {
class MasterClient {
public:
-
+ explicit MasterClient(const ScapeConnection& client);
+ bool Process(const Queries* queries);
+
+ bool Close();
+ bool Close(const Packet& message);
private:
+ enum MasterToClientId {
+
+ };
+
+ enum ClientToMasterId {
+
+ };
+
ScapeConnection sock;
+
+ bool authed;
+ int auth_attempts;
+ const int MAX_AUTH_ATTEMPTS = 5;
};
class MasterClientPool : public Pool {
@@ -30,8 +46,7 @@ protected:
ctx::MasterClientContext* context,
const Queries* queries) override
{
- // TODO implement
- return true;
+ return client.Process(queries);
}
};
diff --git a/src/server/hosts/master_client.cpp b/src/server/hosts/master_client.cpp
index a200e0f..08e020b 100644
--- a/src/server/hosts/master_client.cpp
+++ b/src/server/hosts/master_client.cpp
@@ -7,8 +7,42 @@ static struct {
/** MASTERCLIENTPOOL CODE **/
void sosc::MasterClientPool::SetupQueries(Queries *queries) {
-#define QRY_USER_REGISTER 0
-
+#define QRY_USER_REG_CHECK 0
+ queries->push_back(new db::Query(
+ "SELECT COUNT(*) FROM `USERS` "
+ "WHERE `USERNAME` = ? OR `EMAIL` = ?"
+ ));
+
+#define QRY_USER_REGISTER 1
+ queries->push_back(new db::Query(
+ "INSERT INTO `USERS` "
+ "(`USERNAME`, `PASS_HASH`, `EMAIL`, `ACTIVATED`, `JOINED`) "
+ "VALUES (?, ?, ?, 0, CURRENT_TIMESTAMP)"
+ ));
+
+#define QRY_USER_GET_PWD_HASH 2
+ queries->push_back(new db::Query(
+ "SELECT `ID`, `PASS_HASH` FROM `USERS` "
+ "WHERE `USERNAME` = ?"
+ ));
+
+#define QRY_USER_GENERATE_KEY 3
+ queries->push_back(new db::Query(
+ "INSERT OR IGNORE INTO `USER_KEYS` "
+ "(`ID`, `SECRET`) VALUES (?, RANDOMBLOB(128))"
+ ));
+
+#define QRY_USER_GET_KEY 4
+ queries->push_back(new db::Query(
+ "SELECT `SECRET` FROM `USER_KEYS` "
+ "WHERE `ID` = ?"
+ ));
+
+#define QRY_USER_CHECK_KEY 5
+ queries->push_back(new db::Query(
+ "SELECT COUNT(*) FROM `USER_KEYS` "
+ "WHERE `ID` = ? AND `SECRET` = ?"
+ ));
}
/** MASTERCLIENT CODE **/
\ No newline at end of file