3 Copyright (C) 2018 bendeutsch, Ben Deutsch <ben@bendeutsch.de>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "lua_api/l_auth.h"
21 #include "lua_api/l_internal.h"
22 #include "common/c_converter.h"
23 #include "common/c_content.h"
24 #include "cpp_api/s_base.h"
26 #include "environment.h"
27 #include "database/database.h"
30 // common start: ensure auth db
31 AuthDatabase *ModApiAuth::getAuthDb(lua_State *L)
33 ServerEnvironment *server_environment =
34 dynamic_cast<ServerEnvironment *>(getEnv(L));
35 if (!server_environment)
37 return server_environment->getAuthDatabase();
40 void ModApiAuth::pushAuthEntry(lua_State *L, const AuthEntry &authEntry)
43 int table = lua_gettop(L);
45 lua_pushnumber(L, authEntry.id);
46 lua_setfield(L, table, "id");
48 lua_pushstring(L, authEntry.name.c_str());
49 lua_setfield(L, table, "name");
51 lua_pushstring(L, authEntry.password.c_str());
52 lua_setfield(L, table, "password");
55 int privtable = lua_gettop(L);
56 for (const std::string &privs : authEntry.privileges) {
57 lua_pushboolean(L, true);
58 lua_setfield(L, privtable, privs.c_str());
60 lua_setfield(L, table, "privileges");
62 lua_pushnumber(L, authEntry.last_login);
63 lua_setfield(L, table, "last_login");
65 lua_pushvalue(L, table);
69 int ModApiAuth::l_auth_read(lua_State *L)
72 AuthDatabase *auth_db = getAuthDb(L);
76 const char *name = luaL_checkstring(L, 1);
77 bool success = auth_db->getAuth(std::string(name), authEntry);
81 pushAuthEntry(L, authEntry);
86 int ModApiAuth::l_auth_save(lua_State *L)
89 AuthDatabase *auth_db = getAuthDb(L);
92 luaL_checktype(L, 1, LUA_TTABLE);
96 success = getintfield(L, table, "id", authEntry.id);
97 success = success && getstringfield(L, table, "name", authEntry.name);
98 success = success && getstringfield(L, table, "password", authEntry.password);
99 lua_getfield(L, table, "privileges");
100 if (lua_istable(L, -1)) {
102 while (lua_next(L, -2)) {
103 authEntry.privileges.emplace_back(
104 lua_tostring(L, -2)); // the key, not the value
110 lua_pop(L, 1); // the table
111 success = success && getintfield(L, table, "last_login", authEntry.last_login);
114 lua_pushboolean(L, false);
118 lua_pushboolean(L, auth_db->saveAuth(authEntry));
122 // auth_create(table)
123 int ModApiAuth::l_auth_create(lua_State *L)
125 NO_MAP_LOCK_REQUIRED;
126 AuthDatabase *auth_db = getAuthDb(L);
129 luaL_checktype(L, 1, LUA_TTABLE);
133 // no meaningful id field, we assume
134 success = getstringfield(L, table, "name", authEntry.name);
135 success = success && getstringfield(L, table, "password", authEntry.password);
136 lua_getfield(L, table, "privileges");
137 if (lua_istable(L, -1)) {
139 while (lua_next(L, -2)) {
140 authEntry.privileges.emplace_back(
141 lua_tostring(L, -2)); // the key, not the value
147 lua_pop(L, 1); // the table
148 success = success && getintfield(L, table, "last_login", authEntry.last_login);
153 if (auth_db->createAuth(authEntry)) {
154 pushAuthEntry(L, authEntry);
162 int ModApiAuth::l_auth_delete(lua_State *L)
164 NO_MAP_LOCK_REQUIRED;
165 AuthDatabase *auth_db = getAuthDb(L);
168 std::string name(luaL_checkstring(L, 1));
169 lua_pushboolean(L, auth_db->deleteAuth(name));
174 int ModApiAuth::l_auth_list_names(lua_State *L)
176 NO_MAP_LOCK_REQUIRED;
177 AuthDatabase *auth_db = getAuthDb(L);
180 std::vector<std::string> names;
181 auth_db->listNames(names);
182 lua_createtable(L, names.size(), 0);
183 int table = lua_gettop(L);
185 for (const std::string &name : names) {
186 lua_pushstring(L, name.c_str());
187 lua_rawseti(L, table, i++);
193 int ModApiAuth::l_auth_reload(lua_State *L)
195 NO_MAP_LOCK_REQUIRED;
196 AuthDatabase *auth_db = getAuthDb(L);
202 void ModApiAuth::Initialize(lua_State *L, int top)
206 int auth_top = lua_gettop(L);
208 registerFunction(L, "read", l_auth_read, auth_top);
209 registerFunction(L, "save", l_auth_save, auth_top);
210 registerFunction(L, "create", l_auth_create, auth_top);
211 registerFunction(L, "delete", l_auth_delete, auth_top);
212 registerFunction(L, "list_names", l_auth_list_names, auth_top);
213 registerFunction(L, "reload", l_auth_reload, auth_top);
215 lua_setfield(L, top, "auth");