GetUser requests.

ipc07
Luka Vandervelden 2019-01-08 01:04:20 +09:00
parent ddb8edacbb
commit 1f85aab84c
2 changed files with 36 additions and 1 deletions

View File

@ -10,6 +10,7 @@ module AuthD
enum RequestTypes
GetToken
AddUser
GetUser
end
enum ResponseTypes
@ -17,6 +18,7 @@ module AuthD
MalformedRequest
InvalidCredentials
InvalidUser
UserNotFound # For UID-based GetUser requests.
end
class GetTokenRequest
@ -38,6 +40,12 @@ module AuthD
})
end
class GetUserRequest
JSON.mapping({
uid: Int32
})
end
class Client < IPC::Client
property key : String
@ -48,7 +56,7 @@ module AuthD
end
def get_token?(login : String, password : String)
send RequestTypes::GetToken.value.to_u8, {
send RequestTypes::GetToken, {
:login => login,
:password => password
}.to_json
@ -62,6 +70,18 @@ module AuthD
end
end
def get_user?(uid : Int32)
send RequestTypes::GetUser, {:uid => uid}.to_json
response = read
if response.type == ResponseTypes::Ok.value.to_u8
User.from_json response.payload
else
nil
end
end
def send(type : RequestTypes, payload)
send type.value.to_u8, payload
end

View File

@ -93,6 +93,21 @@ IPC::Service.new "auth" do |event|
user = passwd.add_user request.login, request.password
client.send ResponseTypes::Ok, user.to_json
when RequestTypes::GetUser
begin
request = GetUserRequest.from_json payload
rescue e
client.send ResponseTypes::MalformedRequest, e.message || ""
next
end
user = passwd.get_user request.uid
if user
client.send ResponseTypes::Ok, user.to_json
else
client.send ResponseTypes::UserNotFound, ""
end
end
end
end