summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-04-30 11:32:17 +0800
committerruki <[email protected]>2022-04-30 11:32:17 +0800
commit72e9d69c25aaa720baa5191a47f1a8f7a24a7fd4 (patch)
tree37834a3bedf9caf190f36249ea0ee8c825e784c2
parentd88e430d33bd26cb5b5f87b6feba9ebdc1017652 (diff)
improve auth
-rw-r--r--xmake/modules/private/service/config.lua12
-rw-r--r--xmake/modules/private/service/remote_build/server.lua6
-rw-r--r--xmake/modules/private/service/server.lua44
3 files changed, 24 insertions, 38 deletions
diff --git a/xmake/modules/private/service/config.lua b/xmake/modules/private/service/config.lua
index 9bdb98ad7..d11a4f1ea 100644
--- a/xmake/modules/private/service/config.lua
+++ b/xmake/modules/private/service/config.lua
@@ -32,13 +32,11 @@ function _generate_configfile()
server = {
listen = "0.0.0.0:9691",
workdir = path.join(servicedir, "remote_build"),
- users = {
- root = {
- pass = "123456",
- known_hosts = {
- "127.0.0.1"
- }
- }
+ known_hosts = {
+ "127.0.0.1"
+ },
+ auths = {
+ "root:123456"
}
},
client = {
diff --git a/xmake/modules/private/service/remote_build/server.lua b/xmake/modules/private/service/remote_build/server.lua
index de2b05cd0..c6a80fa42 100644
--- a/xmake/modules/private/service/remote_build/server.lua
+++ b/xmake/modules/private/service/remote_build/server.lua
@@ -41,9 +41,9 @@ function remote_build_server:init(daemon)
local address = assert(config.get("remote_build.server.listen"), "config(remote_build.server.listen): not found!")
super.address_set(self, address)
- -- init users
- local users = config.get("remote_build.server.users")
- super.users_set(self, users)
+ -- init authorizations
+ local auths = config.get("remote_build.server.auths")
+ super.auths_set(self, auths)
-- init handler
super.handler_set(self, self._on_handle)
diff --git a/xmake/modules/private/service/server.lua b/xmake/modules/private/service/server.lua
index 92ebdec82..24e6fb644 100644
--- a/xmake/modules/private/service/server.lua
+++ b/xmake/modules/private/service/server.lua
@@ -22,6 +22,7 @@
import("core.base.object")
import("core.base.bytes")
import("core.base.base64")
+import("core.base.hashset")
import("core.base.socket")
import("core.base.scheduler")
import("private.service.message")
@@ -68,21 +69,25 @@ function server:port()
return self._PORT
end
--- set users
-function server:users_set(users)
- self._USERS = users
+-- get authorizations
+function server:auths()
+ return self._AUTHS
end
--- get the user information
-function server:user(name)
- if name and self._USERS then
- return self._USERS[name]
+-- set authorizations
+function server:auths_set(auths)
+ if auths then
+ local result = hashset.new()
+ for _, auth in ipairs(auths) do
+ result:insert(base64.encode(auth))
+ end
+ self._AUTHS = result
end
end
-- we need verify user
function server:need_verfiy()
- return self._USERS ~= nil
+ return self:auths() ~= nil
end
-- verify user
@@ -91,26 +96,9 @@ function server:verify_user(auth)
return false, "client has no authorization, this remote server need user authorization!"
end
- -- decode authorization
- local authstr = base64.decode(auth)
- local splitinfo = authstr:str():split(":")
- if not splitinfo or #splitinfo ~= 2 then
- return false, "invalid authorization!"
- end
-
- -- get client user and password
- local client_user = splitinfo[1]
- local client_pass = splitinfo[2]
- if not client_user or not client_pass then
- return false, "invalid user and password!"
- end
-
- -- get server user and password
- local server_userinfo = self:user(client_user)
- assert(server_userinfo, "user(%s) is unknown!", client_user)
- local server_pass = server_userinfo.pass
- if client_pass ~= server_pass then
- return false, "password is incorrect!"
+ -- check authorization
+ if not self:auths():has(auth) then
+ return false, "user and password are incorrect!"
end
-- TODO check known_hosts