GetUser requests.
parent
ddb8edacbb
commit
1f85aab84c
22
src/authd.cr
22
src/authd.cr
|
@ -10,6 +10,7 @@ module AuthD
|
||||||
enum RequestTypes
|
enum RequestTypes
|
||||||
GetToken
|
GetToken
|
||||||
AddUser
|
AddUser
|
||||||
|
GetUser
|
||||||
end
|
end
|
||||||
|
|
||||||
enum ResponseTypes
|
enum ResponseTypes
|
||||||
|
@ -17,6 +18,7 @@ module AuthD
|
||||||
MalformedRequest
|
MalformedRequest
|
||||||
InvalidCredentials
|
InvalidCredentials
|
||||||
InvalidUser
|
InvalidUser
|
||||||
|
UserNotFound # For UID-based GetUser requests.
|
||||||
end
|
end
|
||||||
|
|
||||||
class GetTokenRequest
|
class GetTokenRequest
|
||||||
|
@ -38,6 +40,12 @@ module AuthD
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class GetUserRequest
|
||||||
|
JSON.mapping({
|
||||||
|
uid: Int32
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
class Client < IPC::Client
|
class Client < IPC::Client
|
||||||
property key : String
|
property key : String
|
||||||
|
|
||||||
|
@ -48,7 +56,7 @@ module AuthD
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_token?(login : String, password : String)
|
def get_token?(login : String, password : String)
|
||||||
send RequestTypes::GetToken.value.to_u8, {
|
send RequestTypes::GetToken, {
|
||||||
:login => login,
|
:login => login,
|
||||||
:password => password
|
:password => password
|
||||||
}.to_json
|
}.to_json
|
||||||
|
@ -62,6 +70,18 @@ module AuthD
|
||||||
end
|
end
|
||||||
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)
|
def send(type : RequestTypes, payload)
|
||||||
send type.value.to_u8, payload
|
send type.value.to_u8, payload
|
||||||
end
|
end
|
||||||
|
|
15
src/main.cr
15
src/main.cr
|
@ -93,6 +93,21 @@ IPC::Service.new "auth" do |event|
|
||||||
user = passwd.add_user request.login, request.password
|
user = passwd.add_user request.login, request.password
|
||||||
|
|
||||||
client.send ResponseTypes::Ok, user.to_json
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue