diff --git a/src/authd/client.cr b/src/authd/client.cr index ccb3b18..70dcefc 100644 --- a/src/authd/client.cr +++ b/src/authd/client.cr @@ -22,6 +22,16 @@ module AuthD m.not_nil! 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? send_now Request::Login.new login, password @@ -68,206 +78,89 @@ module AuthD write @server_fd, m 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. def add_user(login : String, password : String, admin : Bool, 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 - - 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… - # it’ll happen if you run several requests at once. - Exception.new - end + parse_message [Response::UserAdded], read end def bootstrap(login : String, password : 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 - - 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 + parse_message [Response::UserAdded], read 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 - - 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… - # it’ll happen if you run several requests at once. - Exception.new - end + parse_message [Response::UserValidated], read end def ask_password_recovery(uid_or_login : String | Int32) send_now Request::AskPasswordRecovery.new uid_or_login - response = AuthD.responses.parse_ipc_json read - - case response - when Response::PasswordRecoverySent - when Response::Error - raise Exception.new response.reason - else - Exception.new - end + parse_message [Response::PasswordRecoverySent], read end 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 - response = AuthD.responses.parse_ipc_json read - - case response - when Response::PasswordRecovered - when Response::Error - raise Exception.new response.reason - else - Exception.new - end + parse_message [Response::PasswordRecovered], read end def register(login : String, password : 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 - response = AuthD.responses.parse_ipc_json read - - case response - when Response::UserAdded - when Response::Error - raise Exception.new response.reason - end + parse_message [Response::UserAdded], read 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.password = password if password request.email = email if email send_now request - - response = AuthD.responses.parse_ipc_json read - - case response - when Response::UserEdited - true - when Response::Error - Exception.new response.reason - else - Exception.new "???" - end + parse_message [Response::UserEdited], read 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 - send_now request - - 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 + parse_message [Response::PermissionCheck], read end def set_permission(uid : Int32, service : String, resource : String, permission : User::PermissionLevel) request = Request::SetPermission.new uid, service, resource, permission - send_now request - - 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 + parse_message [Response::PermissionSet], read end def search_user(user_login : String) send_now Request::SearchUser.new user_login - response = AuthD.responses.parse_ipc_json read - - case response - when Response::MatchingUsers - response.users - when Response::Error - raise Exception.new response.reason - else - Exception.new - end + parse_message [Response::MatchingUsers], read end def edit_profile_content(user : Int32 | String, new_values) send_now Request::EditProfileEntries.new user, new_values - response = AuthD.responses.parse_ipc_json read - - case response - when Response::User - response.user - when Response::Error - raise Exception.new response.reason - else - raise Exception.new "unexpected response" - end + parse_message [Response::User], read end def delete(user : Int32 | String) send_now Request::Delete.new user - delete_ - end - def delete_ - response = AuthD.responses.parse_ipc_json read - case response - when Response::Error - raise Exception.new response.reason - end - response + parse_message [Response::UserDeleted], read end end end