2019-12-17 16:17:25 +01:00
|
|
|
require "option_parser"
|
|
|
|
|
|
|
|
require "../src/authd.cr"
|
|
|
|
|
|
|
|
key_file : String? = nil
|
|
|
|
cli_login : String? = nil
|
|
|
|
profile_file : String? = nil
|
|
|
|
register = false
|
2020-01-27 14:48:26 +01:00
|
|
|
email = nil
|
|
|
|
phone = nil
|
2020-04-17 02:45:30 +02:00
|
|
|
password : String? = nil
|
2019-12-17 16:17:25 +01:00
|
|
|
|
|
|
|
OptionParser.parse do |parser|
|
|
|
|
parser.unknown_args do |args|
|
2020-01-27 23:15:14 +01:00
|
|
|
if args.size != 3
|
2020-01-27 14:48:26 +01:00
|
|
|
puts "usage: #{PROGRAM_NAME} <login> <email> <phone> [options]"
|
2019-12-17 16:17:25 +01:00
|
|
|
puts parser
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
2020-01-27 14:48:26 +01:00
|
|
|
cli_login, email, phone = args[0..2]
|
2019-12-17 16:17:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
parser.on "-p file", "--profile file", "Read the user profile from a file." do |file|
|
|
|
|
profile_file = file
|
|
|
|
end
|
|
|
|
|
2020-04-17 02:45:30 +02:00
|
|
|
parser.on "-X user-password", "--user-password pass", "Read the new user password." do |pass|
|
|
|
|
password = pass
|
|
|
|
end
|
|
|
|
|
2019-12-17 16:17:25 +01:00
|
|
|
parser.on "-K file", "--key-file file", "Read the authd shared key from a file." do |file|
|
|
|
|
key_file = file
|
|
|
|
end
|
|
|
|
|
|
|
|
parser.on "-R", "--register", "Use a registration request instead of a add-user one." do
|
|
|
|
register = true
|
|
|
|
end
|
|
|
|
|
|
|
|
parser.on "-h", "--help", "Prints this help message." do
|
2020-01-27 14:48:26 +01:00
|
|
|
puts "usage: #{PROGRAM_NAME} <login> <email> <phone> [options]"
|
2019-12-17 16:17:25 +01:00
|
|
|
puts parser
|
|
|
|
exit 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if cli_login.nil?
|
|
|
|
STDERR.puts "no login provided"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
login = cli_login.not_nil! # not_nil!? O RLY?
|
|
|
|
|
|
|
|
profile = profile_file.try do |file|
|
|
|
|
begin
|
2020-09-26 14:27:47 +02:00
|
|
|
JSON.parse(File.read file).as_h
|
2019-12-17 16:17:25 +01:00
|
|
|
rescue e
|
|
|
|
STDERR.puts e.message
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-17 02:45:30 +02:00
|
|
|
if password.nil?
|
|
|
|
STDOUT << "password: "
|
|
|
|
STDOUT << `stty -echo`
|
|
|
|
STDOUT.flush
|
|
|
|
password = STDIN.gets.try &.chomp
|
2019-12-17 16:17:25 +01:00
|
|
|
|
2020-04-17 02:45:30 +02:00
|
|
|
STDOUT << '\n'
|
|
|
|
STDOUT << `stty echo`
|
|
|
|
end
|
2019-12-17 16:17:25 +01:00
|
|
|
|
|
|
|
exit 1 unless password
|
|
|
|
|
|
|
|
authd = AuthD::Client.new
|
|
|
|
|
2020-01-27 14:48:26 +01:00
|
|
|
email = nil if email == ""
|
|
|
|
phone = nil if phone == ""
|
|
|
|
|
2019-12-17 16:17:25 +01:00
|
|
|
begin
|
|
|
|
if register
|
2020-04-17 02:45:30 +02:00
|
|
|
pp! authd.register login, password.not_nil!, email, phone, profile: profile
|
2019-12-17 16:17:25 +01:00
|
|
|
else
|
|
|
|
key_file.try do |file| # FIXME: fail if missing?
|
|
|
|
authd.key = File.read(file).chomp
|
|
|
|
end
|
|
|
|
|
2020-04-17 02:45:30 +02:00
|
|
|
pp! authd.add_user login, password.not_nil!, email, phone, profile: profile
|
2019-12-17 16:17:25 +01:00
|
|
|
end
|
|
|
|
rescue e : AuthD::Exception
|
|
|
|
puts "error: #{e.message}"
|
|
|
|
end
|
|
|
|
|
|
|
|
authd.close
|
|
|
|
|