From 1f85aab84c1239f9b1eece75a4f8112a8513cd27 Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Tue, 8 Jan 2019 01:04:20 +0900 Subject: [PATCH] GetUser requests. --- src/authd.cr | 22 +++++++++++++++++++++- src/main.cr | 15 +++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/authd.cr b/src/authd.cr index 132d0ee..c2fdd3e 100644 --- a/src/authd.cr +++ b/src/authd.cr @@ -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 diff --git a/src/main.cr b/src/main.cr index 3bca688..bedd9de 100644 --- a/src/main.cr +++ b/src/main.cr @@ -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