Coding style.

ipc07
Luka Vandervelden 2018-12-19 21:57:48 +09:00
parent a7a1c54161
commit ddb8edacbb
2 changed files with 18 additions and 18 deletions

View File

@ -8,15 +8,15 @@ require "./group.cr"
module AuthD module AuthD
enum RequestTypes enum RequestTypes
GET_TOKEN GetToken
ADD_USER AddUser
end end
enum ResponseTypes enum ResponseTypes
OK Ok
MALFORMED_REQUEST MalformedRequest
INVALID_CREDENTIALS InvalidCredentials
INVALID_USER InvalidUser
end end
class GetTokenRequest class GetTokenRequest
@ -48,14 +48,14 @@ module AuthD
end end
def get_token?(login : String, password : String) def get_token?(login : String, password : String)
send RequestTypes::GET_TOKEN.value.to_u8, { send RequestTypes::GetToken.value.to_u8, {
:login => login, :login => login,
:password => password :password => password
}.to_json }.to_json
response = read response = read
if response.type == ResponseTypes::OK.value.to_u8 if response.type == ResponseTypes::Ok.value.to_u8
response.payload response.payload
else else
nil nil
@ -76,7 +76,7 @@ module AuthD
# FIXME: Extra options may be useful to implement here. # FIXME: Extra options may be useful to implement here.
def add_user(login : String, password : String) : AuthD::User | Exception def add_user(login : String, password : String) : AuthD::User | Exception
send RequestTypes::ADD_USER, { send RequestTypes::AddUser, {
:login => login, :login => login,
:password => password :password => password
}.to_json }.to_json
@ -85,7 +85,7 @@ module AuthD
pp! response.type pp! response.type
case ResponseTypes.new response.type.to_i case ResponseTypes.new response.type.to_i
when ResponseTypes::OK when ResponseTypes::Ok
AuthD::User.from_json response.payload AuthD::User.from_json response.payload
else else
Exception.new response.payload Exception.new response.payload

View File

@ -56,11 +56,11 @@ IPC::Service.new "auth" do |event|
payload = message.payload payload = message.payload
case RequestTypes.new message.type.to_i case RequestTypes.new message.type.to_i
when RequestTypes::GET_TOKEN when RequestTypes::GetToken
begin begin
request = GetTokenRequest.from_json payload request = GetTokenRequest.from_json payload
rescue e rescue e
client.send ResponseTypes::MALFORMED_REQUEST.value.to_u8, e.message || "" client.send ResponseTypes::MalformedRequest.value.to_u8, e.message || ""
next next
end end
@ -68,31 +68,31 @@ IPC::Service.new "auth" do |event|
user = passwd.get_user request.login, request.password user = passwd.get_user request.login, request.password
if user.nil? if user.nil?
client.send ResponseTypes::INVALID_CREDENTIALS.value.to_u8, "" client.send ResponseTypes::InvalidCredentials.value.to_u8, ""
next next
end end
client.send ResponseTypes::OK.value.to_u8, client.send ResponseTypes::Ok.value.to_u8,
JWT.encode user.to_h, authd_jwt_key, "HS256" JWT.encode user.to_h, authd_jwt_key, "HS256"
when RequestTypes::ADD_USER when RequestTypes::AddUser
begin begin
request = AddUserRequest.from_json payload request = AddUserRequest.from_json payload
rescue e rescue e
client.send ResponseTypes::MALFORMED_REQUEST.value.to_u8, e.message || "" client.send ResponseTypes::MalformedRequest.value.to_u8, e.message || ""
next next
end end
if passwd.user_exists? request.login if passwd.user_exists? request.login
client.send ResponseTypes::INVALID_USER, "Another user with the same login already exists." client.send ResponseTypes::InvalidUser, "Another user with the same login already exists."
next next
end end
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
end end
end end
end end