diff --git a/module/CFunctions.cpp b/module/CFunctions.cpp index ce950c7..271e9bf 100644 --- a/module/CFunctions.cpp +++ b/module/CFunctions.cpp @@ -29,7 +29,7 @@ int CFunctions::sign_jwt_token(lua_State* lua_vm) // Read other arguments const auto claims = Utils::parse_named_table(lua_vm, 2); - const auto algorithm = jwt_algorithm(reinterpret_cast(lua_touserdata(lua_vm, 3))); + const auto algorithm = jwt_algorithm(reinterpret_cast(lua_touserdata(lua_vm, 3))); const auto private_key_path = lua_tostring(lua_vm, 4); std::string private_key = private_key_path; diff --git a/module/Utils.h b/module/Utils.h index 629b060..3bec42d 100644 --- a/module/Utils.h +++ b/module/Utils.h @@ -1,95 +1,95 @@ -#pragma once -#include -#include -#include - -#include "ILuaModuleManager.h" - -static const int index_value = -1; -static const int index_key = -2; - -#ifdef _DEBUG -#define DEBUG_LOG( msg ) std::cout << "[ml_jwt] " << __FILE__ << ":" << __LINE__ << ": " << msg << "\n" -#else -#define DEBUG_LOG( msg ) -#endif - -enum jwt_algorithm : uint32_t -{ - jwt_algorithm_none = 0x123456789ABCDEFui32, - jwt_algorithm_hs256 = jwt_algorithm_none << 1, - jwt_algorithm_hs384 = jwt_algorithm_none << 2, - jwt_algorithm_hs512 = jwt_algorithm_none << 3, - jwt_algorithm_rs256 = jwt_algorithm_none << 4, - jwt_algorithm_rs384 = jwt_algorithm_none << 5, - jwt_algorithm_rs512 = jwt_algorithm_none << 6 -}; - -class Utils -{ -public: - static inline std::unordered_map parse_named_table(lua_State* lua_vm, const int index) - { - unordered_map result; - - for(lua_pushnil(lua_vm); lua_next(lua_vm, index) != 0; lua_pop(lua_vm, 1)) - { - const auto type_key = lua_type(lua_vm, index_key); - const auto type_value = lua_type(lua_vm, index_value); - if (type_key != LUA_TSTRING && type_key != LUA_TNUMBER) - { - stringstream ss; - ss << "got invalid table key, expected string or number got " << lua_typename(lua_vm, type_key); - throw runtime_error(ss.str()); - } - - if (type_value != LUA_TSTRING && type_value != LUA_TNUMBER) - { - stringstream ss; - ss << "got invalid table value, expected string or number got " << lua_typename(lua_vm, type_value); - throw runtime_error(ss.str()); - } - - auto to_string = [lua_vm](const int index) -> const std::string - { - const auto type = lua_type(lua_vm, index); - if (type == LUA_TSTRING) - { - return lua_tostring(lua_vm, index); - } - - if (type == LUA_TNUMBER) - { - return std::to_string(lua_tointeger(lua_vm, index)); - } - return {}; - }; - - result[to_string(index_key)] = to_string(index_value); - } - - return result; - } - - static inline void get_real_path(lua_State* lua_vm, const std::string& relative_path, std::string& real_path) - { - char buf[300]; - if (!pModuleManager->GetResourceFilePath(lua_vm, relative_path.c_str(), buf, sizeof(buf))) - { - stringstream ss; - ss << "invalid path: " << relative_path; - throw runtime_error(ss.str()); - } - - // Check if path is valid - const std::string path{ buf }; - if (path.find("..") != std::string::npos) - { - stringstream ss; - ss << "path is illegal: " << relative_path; - throw runtime_error(ss.str()); - } - - real_path.assign(path); - } -}; +#pragma once +#include +#include +#include + +#include "ILuaModuleManager.h" + +static const int index_value = -1; +static const int index_key = -2; + +#ifdef _DEBUG +#define DEBUG_LOG( msg ) std::cout << "[ml_jwt] " << __FILE__ << ":" << __LINE__ << ": " << msg << "\n" +#else +#define DEBUG_LOG( msg ) +#endif + +enum jwt_algorithm : uint64_t +{ + jwt_algorithm_none = 0x123456789ABCDEF, + jwt_algorithm_hs256 = jwt_algorithm_none << 1, + jwt_algorithm_hs384 = jwt_algorithm_none << 2, + jwt_algorithm_hs512 = jwt_algorithm_none << 3, + jwt_algorithm_rs256 = jwt_algorithm_none << 4, + jwt_algorithm_rs384 = jwt_algorithm_none << 5, + jwt_algorithm_rs512 = jwt_algorithm_none << 6 +}; + +class Utils +{ +public: + static inline std::unordered_map parse_named_table(lua_State* lua_vm, const int index) + { + unordered_map result; + + for(lua_pushnil(lua_vm); lua_next(lua_vm, index) != 0; lua_pop(lua_vm, 1)) + { + const auto type_key = lua_type(lua_vm, index_key); + const auto type_value = lua_type(lua_vm, index_value); + if (type_key != LUA_TSTRING && type_key != LUA_TNUMBER) + { + stringstream ss; + ss << "got invalid table key, expected string or number got " << lua_typename(lua_vm, type_key); + throw runtime_error(ss.str()); + } + + if (type_value != LUA_TSTRING && type_value != LUA_TNUMBER) + { + stringstream ss; + ss << "got invalid table value, expected string or number got " << lua_typename(lua_vm, type_value); + throw runtime_error(ss.str()); + } + + auto to_string = [lua_vm](const int index) -> const std::string + { + const auto type = lua_type(lua_vm, index); + if (type == LUA_TSTRING) + { + return lua_tostring(lua_vm, index); + } + + if (type == LUA_TNUMBER) + { + return std::to_string(lua_tointeger(lua_vm, index)); + } + return {}; + }; + + result[to_string(index_key)] = to_string(index_value); + } + + return result; + } + + static inline void get_real_path(lua_State* lua_vm, const std::string& relative_path, std::string& real_path) + { + char buf[300]; + if (!pModuleManager->GetResourceFilePath(lua_vm, relative_path.c_str(), buf, sizeof(buf))) + { + stringstream ss; + ss << "invalid path: " << relative_path; + throw runtime_error(ss.str()); + } + + // Check if path is valid + const std::string path{ buf }; + if (path.find("..") != std::string::npos) + { + stringstream ss; + ss << "path is illegal: " << relative_path; + throw runtime_error(ss.str()); + } + + real_path.assign(path); + } +};