Coding style.
This commit is contained in:
parent
a7a1c54161
commit
ddb8edacbb
20
src/authd.cr
20
src/authd.cr
@ -8,15 +8,15 @@ require "./group.cr"
|
||||
|
||||
module AuthD
|
||||
enum RequestTypes
|
||||
GET_TOKEN
|
||||
ADD_USER
|
||||
GetToken
|
||||
AddUser
|
||||
end
|
||||
|
||||
enum ResponseTypes
|
||||
OK
|
||||
MALFORMED_REQUEST
|
||||
INVALID_CREDENTIALS
|
||||
INVALID_USER
|
||||
Ok
|
||||
MalformedRequest
|
||||
InvalidCredentials
|
||||
InvalidUser
|
||||
end
|
||||
|
||||
class GetTokenRequest
|
||||
@ -48,14 +48,14 @@ module AuthD
|
||||
end
|
||||
|
||||
def get_token?(login : String, password : String)
|
||||
send RequestTypes::GET_TOKEN.value.to_u8, {
|
||||
send RequestTypes::GetToken.value.to_u8, {
|
||||
:login => login,
|
||||
:password => password
|
||||
}.to_json
|
||||
|
||||
response = read
|
||||
|
||||
if response.type == ResponseTypes::OK.value.to_u8
|
||||
if response.type == ResponseTypes::Ok.value.to_u8
|
||||
response.payload
|
||||
else
|
||||
nil
|
||||
@ -76,7 +76,7 @@ module AuthD
|
||||
|
||||
# FIXME: Extra options may be useful to implement here.
|
||||
def add_user(login : String, password : String) : AuthD::User | Exception
|
||||
send RequestTypes::ADD_USER, {
|
||||
send RequestTypes::AddUser, {
|
||||
:login => login,
|
||||
:password => password
|
||||
}.to_json
|
||||
@ -85,7 +85,7 @@ module AuthD
|
||||
|
||||
pp! response.type
|
||||
case ResponseTypes.new response.type.to_i
|
||||
when ResponseTypes::OK
|
||||
when ResponseTypes::Ok
|
||||
AuthD::User.from_json response.payload
|
||||
else
|
||||
Exception.new response.payload
|
||||
|
16
src/main.cr
16
src/main.cr
@ -56,11 +56,11 @@ IPC::Service.new "auth" do |event|
|
||||
payload = message.payload
|
||||
|
||||
case RequestTypes.new message.type.to_i
|
||||
when RequestTypes::GET_TOKEN
|
||||
when RequestTypes::GetToken
|
||||
begin
|
||||
request = GetTokenRequest.from_json payload
|
||||
rescue e
|
||||
client.send ResponseTypes::MALFORMED_REQUEST.value.to_u8, e.message || ""
|
||||
client.send ResponseTypes::MalformedRequest.value.to_u8, e.message || ""
|
||||
|
||||
next
|
||||
end
|
||||
@ -68,31 +68,31 @@ IPC::Service.new "auth" do |event|
|
||||
user = passwd.get_user request.login, request.password
|
||||
|
||||
if user.nil?
|
||||
client.send ResponseTypes::INVALID_CREDENTIALS.value.to_u8, ""
|
||||
client.send ResponseTypes::InvalidCredentials.value.to_u8, ""
|
||||
|
||||
next
|
||||
end
|
||||
|
||||
client.send ResponseTypes::OK.value.to_u8,
|
||||
client.send ResponseTypes::Ok.value.to_u8,
|
||||
JWT.encode user.to_h, authd_jwt_key, "HS256"
|
||||
when RequestTypes::ADD_USER
|
||||
when RequestTypes::AddUser
|
||||
begin
|
||||
request = AddUserRequest.from_json payload
|
||||
rescue e
|
||||
client.send ResponseTypes::MALFORMED_REQUEST.value.to_u8, e.message || ""
|
||||
client.send ResponseTypes::MalformedRequest.value.to_u8, e.message || ""
|
||||
|
||||
next
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user