Grooming.

ipc07
Luka Vandervelden 2019-11-22 23:13:34 +01:00
parent 2e5135656c
commit 565ee93c9a
2 changed files with 20 additions and 24 deletions

View File

@ -13,6 +13,9 @@ class AuthD::Response
end end
class_getter type = -1 class_getter type = -1
def type
@@type
end
macro inherited macro inherited
def self.type def self.type

View File

@ -53,61 +53,50 @@ IPC::Service.new "auth" do |event|
case event case event
when IPC::Event::Message when IPC::Event::Message
client = event.connection request = Request.from_ipc event.message
message = event.message response = case request
payload = message.payload
request = Request.from_ipc message
case request
when Request::GetToken when Request::GetToken
user = passwd.get_user request.login, request.password user = passwd.get_user request.login, request.password
if user.nil? if user.nil?
client.send Response::Error.new "invalid credentials" next Response::Error.new "invalid credentials"
next
end end
token = JWT.encode user.to_h, authd_jwt_key, JWT::Algorithm::HS256 token = JWT.encode user.to_h, authd_jwt_key, JWT::Algorithm::HS256
client.send Response::Token.new token Response::Token.new token
when Request::AddUser when Request::AddUser
if request.shared_key != authd_jwt_key if request.shared_key != authd_jwt_key
client.send Response::Error.new "invalid authentication key" next Response::Error.new "invalid authentication key"
next
end end
if passwd.user_exists? request.login if passwd.user_exists? request.login
client.send Response::Error.new "login already used" next Response::Error.new "login already used"
next
end end
user = passwd.add_user request.login, request.password user = passwd.add_user request.login, request.password
client.send Response::UserAdded.new user Response::UserAdded.new user
when Request::GetUserByCredentials when Request::GetUserByCredentials
user = passwd.get_user request.login, request.password user = passwd.get_user request.login, request.password
if user if user
client.send Response::User.new user Response::User.new user
else else
client.send Response::Error.new "user not found" Response::Error.new "user not found"
end end
when Request::GetUser when Request::GetUser
user = passwd.get_user request.uid user = passwd.get_user request.uid
if user if user
client.send Response::User.new user Response::User.new user
else else
client.send Response::Error.new "user not found" Response::Error.new "user not found"
end end
when Request::ModUser when Request::ModUser
if request.shared_key != authd_jwt_key if request.shared_key != authd_jwt_key
client.send Response::Error.new "invalid authentication key" next Response::Error.new "invalid authentication key"
next
end end
password_hash = request.password.try do |s| password_hash = request.password.try do |s|
@ -116,8 +105,12 @@ IPC::Service.new "auth" do |event|
passwd.mod_user request.uid, password_hash: password_hash passwd.mod_user request.uid, password_hash: password_hash
client.send Response::UserEdited.new request.uid Response::UserEdited.new request.uid
else
Response::Error.new "unhandled request type"
end end
event.connection.send response
end end
end end