Client now is simpler (a single function parses responses).

master
Philippe Pittoli 2023-06-14 18:37:16 +02:00
parent da641a6d3d
commit 538faf5004
1 changed files with 33 additions and 140 deletions

View File

@ -22,6 +22,16 @@ module AuthD
m.not_nil! m.not_nil!
end end
# TODO: parse_message should raise exception if response not anticipated
def parse_message(expected_messages, message)
em = Array(IPC::JSON.class).new
expected_messages.each do |e|
em << e
end
em << Response::Error
em.parse_ipc_json message
end
def get_token?(login : String, password : String) : String? def get_token?(login : String, password : String) : String?
send_now Request::Login.new login, password send_now Request::Login.new login, password
@ -68,206 +78,89 @@ module AuthD
write @server_fd, m write @server_fd, m
end end
def decode_token(token)
user, meta = JWT.decode token, @key, JWT::Algorithm::HS256
user = ::AuthD::User::Public.from_json user.to_json
{user, meta}
end
# 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, def add_user(login : String, password : String,
admin : Bool, admin : Bool,
email : String?, email : String?,
profile : Hash(String, ::JSON::Any)?) : ::AuthD::User::Public | Exception profile : Hash(String, ::JSON::Any)?)
send_now Request::AddUser.new login, password, admin, email, profile send_now Request::AddUser.new login, password, admin, email, profile
parse_message [Response::UserAdded], read
response = AuthD.responses.parse_ipc_json read
case response
when Response::UserAdded
response.user
when Response::Error
raise Exception.new response.reason
else
# Should not happen in serialized connections, but…
# itll happen if you run several requests at once.
Exception.new
end
end end
def bootstrap(login : String, def bootstrap(login : String,
password : String, password : String,
email : String, email : String,
profile : Hash(String, ::JSON::Any)? = nil) : ::AuthD::User::Public | Exception profile : Hash(String, ::JSON::Any)? = nil)
send_now Request::BootstrapFirstAdmin.new login, password, email, profile send_now Request::BootstrapFirstAdmin.new login, password, email, profile
parse_message [Response::UserAdded], read
response = AuthD.responses.parse_ipc_json read
case response
when Response::UserAdded
response.user
when Response::Error
raise Exception.new response.reason
else
Exception.new
end
end end
def validate_user(login : String, activation_key : String) : ::AuthD::User::Public | Exception def decode_token(token)
send_now Request::DecodeToken.new token
parse_message [Response::User], read
end
def validate_user(login : String, activation_key : String)
send_now Request::ValidateUser.new login, activation_key send_now Request::ValidateUser.new login, activation_key
parse_message [Response::UserValidated], read
response = AuthD.responses.parse_ipc_json read
case response
when Response::UserValidated
response.user
when Response::Error
raise Exception.new response.reason
else
# Should not happen in serialized connections, but…
# itll happen if you run several requests at once.
Exception.new
end
end end
def ask_password_recovery(uid_or_login : String | Int32) def ask_password_recovery(uid_or_login : String | Int32)
send_now Request::AskPasswordRecovery.new uid_or_login send_now Request::AskPasswordRecovery.new uid_or_login
response = AuthD.responses.parse_ipc_json read parse_message [Response::PasswordRecoverySent], read
case response
when Response::PasswordRecoverySent
when Response::Error
raise Exception.new response.reason
else
Exception.new
end
end end
def change_password(uid_or_login : String | Int32, new_pass : String, renew_key : String) def change_password(uid_or_login : String | Int32, new_pass : String, renew_key : String)
send_now Request::PasswordRecovery.new uid_or_login, renew_key, new_pass send_now Request::PasswordRecovery.new uid_or_login, renew_key, new_pass
response = AuthD.responses.parse_ipc_json read parse_message [Response::PasswordRecovered], read
case response
when Response::PasswordRecovered
when Response::Error
raise Exception.new response.reason
else
Exception.new
end
end end
def register(login : String, def register(login : String,
password : String, password : String,
email : String?, email : String?,
profile : Hash(String, ::JSON::Any)?) : ::AuthD::User::Public? profile : Hash(String, ::JSON::Any)?)
send_now Request::Register.new login, password, email, profile send_now Request::Register.new login, password, email, profile
response = AuthD.responses.parse_ipc_json read parse_message [Response::UserAdded], read
case response
when Response::UserAdded
when Response::Error
raise Exception.new response.reason
end
end end
def mod_user(uid_or_login : Int32 | String, password : String? = nil, email : String? = nil) : Bool | Exception def mod_user(uid_or_login : Int32 | String, password : String? = nil, email : String? = nil)
request = Request::ModUser.new uid_or_login request = Request::ModUser.new uid_or_login
request.password = password if password request.password = password if password
request.email = email if email request.email = email if email
send_now request send_now request
parse_message [Response::UserEdited], read
response = AuthD.responses.parse_ipc_json read
case response
when Response::UserEdited
true
when Response::Error
Exception.new response.reason
else
Exception.new "???"
end
end end
def check_permission(user : Int32, service_name : String, resource_name : String) : User::PermissionLevel def check_permission(user : Int32, service_name : String, resource_name : String)
request = Request::CheckPermission.new user, service_name, resource_name request = Request::CheckPermission.new user, service_name, resource_name
send_now request send_now request
parse_message [Response::PermissionCheck], read
response = AuthD.responses.parse_ipc_json read
case response
when Response::PermissionCheck
response.permission
when Response
raise Exception.new "unexpected response: #{response.type}"
else
raise Exception.new "unexpected response"
end
end end
def set_permission(uid : Int32, service : String, resource : String, permission : User::PermissionLevel) def set_permission(uid : Int32, service : String, resource : String, permission : User::PermissionLevel)
request = Request::SetPermission.new uid, service, resource, permission request = Request::SetPermission.new uid, service, resource, permission
send_now request send_now request
parse_message [Response::PermissionSet], read
response = AuthD.responses.parse_ipc_json read
case response
when Response::PermissionSet
true
when Response
raise Exception.new "unexpected response: #{response.type}"
else
raise Exception.new "unexpected response"
end
end end
def search_user(user_login : String) def search_user(user_login : String)
send_now Request::SearchUser.new user_login send_now Request::SearchUser.new user_login
response = AuthD.responses.parse_ipc_json read parse_message [Response::MatchingUsers], read
case response
when Response::MatchingUsers
response.users
when Response::Error
raise Exception.new response.reason
else
Exception.new
end
end end
def edit_profile_content(user : Int32 | String, new_values) def edit_profile_content(user : Int32 | String, new_values)
send_now Request::EditProfileEntries.new user, new_values send_now Request::EditProfileEntries.new user, new_values
response = AuthD.responses.parse_ipc_json read parse_message [Response::User], read
case response
when Response::User
response.user
when Response::Error
raise Exception.new response.reason
else
raise Exception.new "unexpected response"
end
end end
def delete(user : Int32 | String) def delete(user : Int32 | String)
send_now Request::Delete.new user send_now Request::Delete.new user
delete_ parse_message [Response::UserDeleted], read
end
def delete_
response = AuthD.responses.parse_ipc_json read
case response
when Response::Error
raise Exception.new response.reason
end
response
end end
end end
end end