All explicit errors in requests are now dedicated errors.

master
Philippe Pittoli 2023-06-14 02:07:03 +02:00
parent 71cbb1d519
commit b047a3fd6f
6 changed files with 49 additions and 9 deletions

View File

@ -59,7 +59,7 @@ class AuthD::Request
def handle(authd : AuthD::Service, fd : Int32) def handle(authd : AuthD::Service, fd : Int32)
# Check if there already is a registered user. # Check if there already is a registered user.
if authd.users.to_a.size > 0 if authd.users.to_a.size > 0
return Response::Error.new "already users in the database" return Response::ErrorAlreadyUsersInDB.new
end end
password_hash = authd.hash_password @password password_hash = authd.hash_password @password

View File

@ -63,7 +63,7 @@ class AuthD::Request
if user.password_renew_key == @password_renew_key if user.password_renew_key == @password_renew_key
user.password_hash = authd.hash_password @new_password user.password_hash = authd.hash_password @new_password
else else
return Response::Error.new "renew key not valid" return Response::ErrorInvalidRenewKey.new
end end
user.password_renew_key = nil user.password_renew_key = nil

View File

@ -22,14 +22,19 @@ class AuthD::Request
new_profile_entries = user.profile || Hash(String, JSON::Any).new new_profile_entries = user.profile || Hash(String, JSON::Any).new
invalid_profile_keys = Array(String).new
unless logged_user.admin unless logged_user.admin
authd.configuration.read_only_profile_keys.each do |key| authd.configuration.read_only_profile_keys.each do |key|
if @new_profile_entries.has_key? key if @new_profile_entries.has_key? key
return Response::Error.new "tried to edit read only key" invalid_profile_keys << key
end end
end end
end end
if invalid_profile_keys.size > 0
return Response::ErrorReadOnlyProfileKeys.new invalid_profile_keys
end
@new_profile_entries.each do |key, value| @new_profile_entries.each do |key, value|
new_profile_entries[key] = value new_profile_entries[key] = value
end end

View File

@ -31,9 +31,7 @@ class AuthD::Request
result = grok.parse @email.not_nil! result = grok.parse @email.not_nil!
email = result["email"]? email = result["email"]?
if email.nil? return Response::ErrorInvalidEmailFormat.new if email.nil?
return Response::ErrorInvalidEmailFormat.new
end
end end
# In this case we should not accept its registration. # In this case we should not accept its registration.
@ -72,7 +70,7 @@ class AuthD::Request
end end
rescue e rescue e
Baguette::Log.error "mailer: #{e}" Baguette::Log.error "mailer: #{e}"
return Response::Error.new "cannot contact the user (not registered)" return Response::ErrorCannotContactUser.new
end end
# add the user only if we were able to send the confirmation mail # add the user only if we were able to send the confirmation mail

View File

@ -13,14 +13,14 @@ class AuthD::Request
return Response::ErrorUserNotFound.new if user.nil? return Response::ErrorUserNotFound.new if user.nil?
if user.contact.activation_key.nil? if user.contact.activation_key.nil?
return Response::Error.new "user already validated" return Response::ErrorUserAlreadyValidated.new
end end
# Remove the user contact activation key: the email is validated. # Remove the user contact activation key: the email is validated.
if user.contact.activation_key == @activation_key if user.contact.activation_key == @activation_key
user.contact.activation_key = nil user.contact.activation_key = nil
else else
return Response::Error.new "wrong activation key" return Response::ErrorInvalidActivationKey.new
end end
authd.users_per_uid.update user.uid.to_s, user authd.users_per_uid.update user.uid.to_s, user

View File

@ -65,4 +65,41 @@ class AuthD::Response
end end
end end
AuthD.responses << ErrorInvalidCredentials AuthD.responses << ErrorInvalidCredentials
IPC::JSON.message ErrorReadOnlyProfileKeys, 30 do
property read_only_keys : Array(String)
def initialize(@read_only_keys)
end
end
AuthD.responses << ErrorReadOnlyProfileKeys
IPC::JSON.message ErrorInvalidActivationKey, 31 do
def initialize()
end
end
AuthD.responses << ErrorInvalidActivationKey
IPC::JSON.message ErrorUserAlreadyValidated, 32 do
def initialize()
end
end
AuthD.responses << ErrorUserAlreadyValidated
IPC::JSON.message ErrorCannotContactUser, 33 do
def initialize()
end
end
AuthD.responses << ErrorCannotContactUser
IPC::JSON.message ErrorInvalidRenewKey, 34 do
def initialize()
end
end
AuthD.responses << ErrorInvalidRenewKey
IPC::JSON.message ErrorAlreadyUsersInDB, 35 do
def initialize()
end
end
AuthD.responses << ErrorAlreadyUsersInDB
end end