authd/utils/authc.cr

185 lines
4.7 KiB
Crystal
Raw Normal View History

2020-10-11 15:51:26 +02:00
require "option_parser"
2020-10-09 18:13:58 +02:00
require "ipc"
require "yaml"
require "baguette-crystal-base"
require "../src/authd.cr"
# require "./altideal-client.cr"
# require "./yaml_dates.cr" # from "3 days - 2 hours" to YAML-compliant dates
# require "./yaml_uuid.cr" # YAML UUID parser
# require "./authd_api.cr" # Authd interface functions
class Context
class_property simulation = false # do not perform the action
class_property authd_login = "undef" # undef authd user
class_property authd_pass = "undef" # undef authd user password
class_property shared_key = "undef" # undef authd user password
# # Properties to select what to display when printing a deal.
# class_property print_title = true
# class_property print_description = true
# class_property print_owner = true
# class_property print_nb_comments = true
class_property user_profile : Hash(String,JSON::Any)?
class_property command = "not-implemented"
# TODO: inner arguments.
# Will be parsed later, with a specific parser.
class_property args : Array(String)? = nil
end
2020-10-11 20:52:04 +02:00
# require "./parse-me"
require "./better-parser"
2020-10-09 18:13:58 +02:00
class Actions
def self.ask_password
STDOUT << "password: "
STDOUT << `stty -echo`
STDOUT.flush
password = STDIN.gets.try &.chomp
STDOUT << '\n'
STDOUT << `stty echo`
password
end
property the_call = {} of String => Proc(Nil)
property authd : AuthD::Client
def initialize(@authd)
# Admin section.
@the_call["user-add"] = ->user_add
@the_call["user-mod"] = ->user_mod
@the_call["set-permissions"] = ->set_permissions
@the_call["check-permissions"] = ->check_permissions
# User.
@the_call["registration"] = ->user_registration
@the_call["delete"] = ->user_deletion
end
def user_registration
2020-10-11 15:51:26 +02:00
# pp! authd.register login, password.not_nil!, email, phone, profile: profile
rescue e : AuthD::Exception
puts "error: #{e.message}"
2020-10-09 18:13:58 +02:00
end
def user_deletion
end
def user_add
args = Context.args
if args.nil? || args.size < 3
Baguette::Log.warning "subcommand usage: user email phone"
Baguette::Log.warning " example: 1002 test@example.com 0690290516"
Baguette::Log.warning ""
Baguette::Log.warning "User profile opt: -P | --profile"
return
end
profile = Context.user_profile
password = Actions.ask_password
exit 1 unless password
2020-10-11 15:51:26 +02:00
# pp! authd.add_user login, password.not_nil!, email, phone, profile: profile
rescue e : AuthD::Exception
puts "error: #{e.message}"
2020-10-09 18:13:58 +02:00
end
def user_mod
end
def check_permissions
args = Context.args
if args.nil? || args.size < 3
Baguette::Log.warning "subcommand usage: user application resource"
Baguette::Log.warning " example: 1002 my-application chat "
Baguette::Log.warning ""
Baguette::Log.warning "permission list: none read edit admin"
return
end
user, application, resource = args[0..2]
pp! user, application, resource
pp! @authd.check_permission user.to_i, application, resource
end
2020-10-11 15:51:26 +02:00
# TODO
def validate
# pp! r = authd.validate_user login.not_nil!, activation_key.not_nil!
end
def search
# pp! r = authd.search_user login.not_nil!
end
def get
# pp! authd.get_user? login
end
def ask_password_recovery
# pp! authd.ask_password_recovery login
end
2020-10-09 18:13:58 +02:00
def set_permissions
args = Context.args
if args.nil? || args.size < 4
Baguette::Log.warning "subcommand usage: user application resource permission"
Baguette::Log.warning " example: 1002 my-application chat read"
Baguette::Log.warning ""
Baguette::Log.warning "permission list: none read edit admin"
return
end
user, application, resource, permission = args[0..3]
pp! user, application, resource, permission
perm = AuthD::User::PermissionLevel.parse(permission)
pp! @authd.set_permission user.to_i, application, resource, perm
end
end
def main
# Authd connection.
authd = AuthD::Client.new
authd.key = Context.shared_key if Context.shared_key != "undef"
# Authd token.
# FIXME: not sure about getting the token, it seems not used elsewhere.
# If login == pass == "undef": do not even try.
#unless Context.authd_login == Context.authd_pass && Context.authd_login == "undef"
# login = Context.authd_login
# pass = Context.authd_pass
# token = authd.get_token? login, pass
# raise "cannot get a token" if token.nil?
# # authd.login token
#end
actions = Actions.new authd
# Now we did read the intent, we should proceed doing what was asked.
begin
actions.the_call[Context.command].call
rescue e
Baguette::Log.info "The command is not recognized (or implemented)."
end
# authd disconnection
authd.close
rescue e
Baguette::Log.info "Exception: #{e}"
end
# Command line:
# tool [options] command [options-for-command]
main