Can now authenticated from the client.

master
Philippe Pittoli 2023-06-14 20:16:42 +02:00
parent 2024325092
commit 9c75522d01
3 changed files with 33 additions and 44 deletions

View File

@ -3,12 +3,10 @@ require "json"
module AuthD module AuthD
class Client < IPC class Client < IPC
property key : String
property server_fd : Int32 = -1 property server_fd : Int32 = -1
def initialize def initialize
super super
@key = ""
fd = self.connect "auth" fd = self.connect "auth"
if fd.nil? if fd.nil?
raise "couldn't connect to 'auth' IPC service" raise "couldn't connect to 'auth' IPC service"
@ -28,44 +26,23 @@ module AuthD
expected_messages.each do |e| expected_messages.each do |e|
em << e em << e
end end
em << Response::Error # response = AuthD.responses.parse_ipc_json read
em.parse_ipc_json message em.parse_ipc_json message
end end
def get_token?(login : String, password : String) : String? def login?(login : String, password : String)
send_now Request::Login.new login, password send_now Request::Login.new login, password
parse_message [Response::Login], read
response = AuthD.responses.parse_ipc_json read
if response.is_a?(Response::Login)
response.token
else
nil
end
end end
def get_user?(login : String, password : String) : AuthD::User::Public? def get_user?(login : String, password : String)
send_now Request::GetUserByCredentials.new login, password send_now Request::GetUserByCredentials.new login, password
parse_message [Response::User], read
response = AuthD.responses.parse_ipc_json read
if response.is_a? Response::User
response.user
else
nil
end
end end
def get_user?(uid_or_login : Int32 | String) : ::AuthD::User::Public? def get_user?(uid_or_login : Int32 | String)
send_now Request::GetUser.new uid_or_login send_now Request::GetUser.new uid_or_login
parse_message [Response::User], read
response = AuthD.responses.parse_ipc_json read
if response.is_a? Response::User
response.user
else
nil
end
end end
def send_now(msg : IPC::JSON) def send_now(msg : IPC::JSON)

View File

@ -86,6 +86,7 @@ parser = OptionParser.new do |parser|
parser.banner = "usage: user add login email [-P profile] [opt]" parser.banner = "usage: user add login email [-P profile] [opt]"
Baguette::Log.info "Adding a user to the DB." Baguette::Log.info "Adding a user to the DB."
Context.command = "user-add" Context.command = "user-add"
opt_authd_login.call parser
opt_profile.call parser opt_profile.call parser
opt_help.call parser opt_help.call parser
# login email # login email
@ -96,6 +97,7 @@ parser = OptionParser.new do |parser|
parser.banner = "Usage: user mod userid [-e email|-P profile] [opt]" parser.banner = "Usage: user mod userid [-e email|-P profile] [opt]"
Baguette::Log.info "Modify a user account." Baguette::Log.info "Modify a user account."
Context.command = "user-mod" Context.command = "user-mod"
opt_authd_login.call parser
opt_email.call parser opt_email.call parser
opt_profile.call parser opt_profile.call parser
opt_help.call parser opt_help.call parser
@ -128,7 +130,7 @@ parser = OptionParser.new do |parser|
parser.banner = "Usage: user get login [opt]" parser.banner = "Usage: user get login [opt]"
Baguette::Log.info "Get user info." Baguette::Log.info "Get user info."
Context.command = "user-get" Context.command = "user-get"
# No need to be authenticated. opt_authd_login.call parser
opt_help.call parser opt_help.call parser
# login # login
unrecognized_args_to_context_args.call parser, 1 unrecognized_args_to_context_args.call parser, 1
@ -138,7 +140,7 @@ parser = OptionParser.new do |parser|
parser.banner = "Usage: user recover login [opt]" parser.banner = "Usage: user recover login [opt]"
Baguette::Log.info "Search user." Baguette::Log.info "Search user."
Context.command = "user-search" Context.command = "user-search"
# No need to be authenticated. opt_authd_login.call parser
opt_help.call parser opt_help.call parser
# login # login
unrecognized_args_to_context_args.call parser, 1 unrecognized_args_to_context_args.call parser, 1
@ -178,6 +180,7 @@ permission list: none read edit admin
END END
Baguette::Log.info "Set permissions." Baguette::Log.info "Set permissions."
Context.command = "permission-set" Context.command = "permission-set"
opt_authd_login.call parser
opt_help.call parser opt_help.call parser
# userid application resource permission # userid application resource permission
unrecognized_args_to_context_args.call parser, 4 unrecognized_args_to_context_args.call parser, 4
@ -192,6 +195,7 @@ permission list: none read edit admin
END END
Baguette::Log.info "Check permissions." Baguette::Log.info "Check permissions."
Context.command = "permission-check" Context.command = "permission-check"
opt_authd_login.call parser
opt_help.call parser opt_help.call parser
# userid application resource # userid application resource
unrecognized_args_to_context_args.call parser, 3 unrecognized_args_to_context_args.call parser, 3

View File

@ -5,8 +5,8 @@ require "./authd.cr"
class Context class Context
class_property simulation = false # do not perform the action class_property simulation = false # do not perform the action
class_property authd_login = "undef" # undef authd user class_property authd_login : String? = nil
class_property authd_pass = "undef" # undef authd user password class_property authd_pass : String? = nil
# # Properties to select what to display when printing a deal. # # Properties to select what to display when printing a deal.
# class_property print_title = true # class_property print_title = true
@ -207,16 +207,24 @@ def main
# Authd connection. # Authd connection.
authd = AuthD::Client.new authd = AuthD::Client.new
# Authd token. if login = Context.authd_login
# FIXME: not sure about getting the token, it seems not used elsewhere. pass = if p = Context.authd_pass
# If login == pass == "undef": do not even try. p
#unless Context.authd_login == Context.authd_pass && Context.authd_login == "undef" else
# login = Context.authd_login password = Actions.ask_password
# pass = Context.authd_pass raise "cannot get a password" unless password
# token = authd.get_token? login, pass password
# raise "cannot get a token" if token.nil? end
# # authd.login token response = authd.login? login, pass
#end case response
when Response::Login
uid = response.uid
token = response.token
Baguette::Log.info "Authenticated as #{login} #{uid}, token: #{token}"
else
raise "Cannot authenticate to authd with login #{login}: #{response}."
end
end
actions = Actions.new authd actions = Actions.new authd