From e88c19c892f0df534bfc86ab51dae74020af15c1 Mon Sep 17 00:00:00 2001 From: Karchnu Date: Sun, 11 Oct 2020 20:52:04 +0200 Subject: [PATCH] removing all kind of stuff --- utils/authc.cr | 3 +- utils/authd-user-add.cr | 95 ------------------- utils/authd-user-allow.cr | 70 -------------- utils/authd-user-ask-for-new-password.cr | 43 --------- utils/authd-user-get.cr | 40 -------- utils/authd-user-mod.cr | 88 ----------------- utils/authd-user-perms.cr | 67 ------------- utils/authd-user-search.cr | 38 -------- utils/authd-user-validate.cr | 38 -------- utils/better-parser.cr | 114 ++++++++++++++++++----- 10 files changed, 91 insertions(+), 505 deletions(-) delete mode 100644 utils/authd-user-add.cr delete mode 100644 utils/authd-user-allow.cr delete mode 100644 utils/authd-user-ask-for-new-password.cr delete mode 100644 utils/authd-user-get.cr delete mode 100644 utils/authd-user-mod.cr delete mode 100644 utils/authd-user-perms.cr delete mode 100644 utils/authd-user-search.cr delete mode 100644 utils/authd-user-validate.cr diff --git a/utils/authc.cr b/utils/authc.cr index de148bb..14e91ba 100644 --- a/utils/authc.cr +++ b/utils/authc.cr @@ -34,7 +34,8 @@ class Context class_property args : Array(String)? = nil end -require "./parse-me" +# require "./parse-me" +require "./better-parser" class Actions diff --git a/utils/authd-user-add.cr b/utils/authd-user-add.cr deleted file mode 100644 index 9ecf8b3..0000000 --- a/utils/authd-user-add.cr +++ /dev/null @@ -1,95 +0,0 @@ -require "option_parser" - -require "../src/authd.cr" - -key_file : String? = nil -cli_login : String? = nil -profile_file : String? = nil -register = false -email = nil -phone = nil -password : String? = nil - -OptionParser.parse do |parser| - parser.unknown_args do |args| - if args.size != 3 - puts "usage: #{PROGRAM_NAME} [options]" - puts parser - exit 1 - end - - cli_login, email, phone = args[0..2] - end - - parser.on "-p file", "--profile file", "Read the user profile from a file." do |file| - profile_file = file - end - - parser.on "-X user-password", "--user-password pass", "Read the new user password." do |pass| - password = pass - end - - 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 - puts "usage: #{PROGRAM_NAME} [options]" - 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 - JSON.parse(File.read file).as_h - rescue e - STDERR.puts e.message - exit 1 - end -end - -if password.nil? - STDOUT << "password: " - STDOUT << `stty -echo` - STDOUT.flush - password = STDIN.gets.try &.chomp - - STDOUT << '\n' - STDOUT << `stty echo` -end - -exit 1 unless password - -authd = AuthD::Client.new - -email = nil if email == "" -phone = nil if phone == "" - -begin - if register - pp! authd.register login, password.not_nil!, email, phone, profile: profile - else - key_file.try do |file| # FIXME: fail if missing? - authd.key = File.read(file).chomp - end - - pp! authd.add_user login, password.not_nil!, email, phone, profile: profile - end -rescue e : AuthD::Exception - puts "error: #{e.message}" -end - -authd.close - diff --git a/utils/authd-user-allow.cr b/utils/authd-user-allow.cr deleted file mode 100644 index 9f083bb..0000000 --- a/utils/authd-user-allow.cr +++ /dev/null @@ -1,70 +0,0 @@ -require "option_parser" - -require "../src/authd.cr" - -key_file : String? = nil -login : String? = nil -service : String? = nil -resource : String? = nil -register = false -level = AuthD::User::PermissionLevel::Read - -OptionParser.parse do |parser| - parser.unknown_args do |args| - if args.size != 3 - puts "usage: #{PROGRAM_NAME} [options]" - puts parser - exit 1 - end - - login, service, resource = args - end - - parser.on "-K file", "--key-file file", "Read the authd shared key from a file." do |file| - key_file = file - end - - parser.on "-L level", "--level level", "Sets the permission level to give the user." do |l| - begin - level = AuthD::User::PermissionLevel.parse l - rescue - STDERR.puts "Could not parse permission level '#{l}'" - exit 1 - end - 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 - puts "usage: #{PROGRAM_NAME} [options]" - puts parser - exit 0 - end -end - -if key_file.nil? - STDERR.puts "you need to provide the shared key" - exit 1 -end - -authd = AuthD::Client.new - -authd.key = File.read(key_file.not_nil!).chomp - -begin - user = authd.get_user? login.not_nil! - - if user.nil? - raise AuthD::Exception.new "#{login}: no such user" - end - - # FIXME: make a “disallow” variant. - authd.set_permission user.uid, service.not_nil!, resource.not_nil!, level -rescue e : AuthD::Exception - puts "error: #{e.message}" -end - -authd.close - diff --git a/utils/authd-user-ask-for-new-password.cr b/utils/authd-user-ask-for-new-password.cr deleted file mode 100644 index ebae596..0000000 --- a/utils/authd-user-ask-for-new-password.cr +++ /dev/null @@ -1,43 +0,0 @@ -require "option_parser" - -require "../src/authd.cr" - -key_file : String? = nil -cli_login : String? = nil - -OptionParser.parse do |parser| - parser.unknown_args do |args| - if args.size != 1 - puts "usage: #{PROGRAM_NAME} [options]" - puts parser - exit 1 - end - - cli_login = args[0] - end - - parser.on "-K file", "--key-file file", "Read the authd shared key from a file." do |file| - key_file = file - end - - parser.on "-h", "--help", "Prints this help message." do - puts "usage: #{PROGRAM_NAME} [options]" - puts parser - exit 0 - end -end - -begin - authd = AuthD::Client.new - authd.key = File.read(key_file.not_nil!).chomp - - login = cli_login.not_nil! - - # AskPasswordRecovery => PasswordRecoverySent - # PasswordRecovery => - - pp! authd.ask_password_recovery login -rescue e - puts "Error: #{e}" - exit 1 -end diff --git a/utils/authd-user-get.cr b/utils/authd-user-get.cr deleted file mode 100644 index 436f378..0000000 --- a/utils/authd-user-get.cr +++ /dev/null @@ -1,40 +0,0 @@ -require "option_parser" - -require "../src/authd.cr" - -key_file : String? = nil -cli_login : String? = nil - -OptionParser.parse do |parser| - parser.unknown_args do |args| - if args.size != 1 - puts "usage: #{PROGRAM_NAME} [options]" - puts parser - exit 1 - end - - cli_login = args[0] - end - - parser.on "-K file", "--key-file file", "Read the authd shared key from a file." do |file| - key_file = file - end - - parser.on "-h", "--help", "Prints this help message." do - puts "usage: #{PROGRAM_NAME} [options]" - puts parser - exit 0 - end -end - -begin - authd = AuthD::Client.new - authd.key = File.read(key_file.not_nil!).chomp - - login = cli_login.not_nil! - - pp! authd.get_user? login -rescue e - puts "Error: #{e}" - exit 1 -end diff --git a/utils/authd-user-mod.cr b/utils/authd-user-mod.cr deleted file mode 100644 index 8f5405d..0000000 --- a/utils/authd-user-mod.cr +++ /dev/null @@ -1,88 +0,0 @@ -require "option_parser" - -require "../src/authd.cr" - -key_file : String? = nil -cli_login : String? = nil -profile_file : String? = nil -register = false -email = nil -phone = nil - -OptionParser.parse do |parser| - parser.unknown_args do |args| - if args.size != 3 - puts "usage: #{PROGRAM_NAME} [options]" - puts parser - exit 1 - end - - cli_login, email, phone = args[0..2] - end - - parser.on "-p file", "--profile file", "Read the user profile from a file." do |file| - profile_file = file - end - - 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 - puts "usage: #{PROGRAM_NAME} [options]" - 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 - JSON.parse File.read file - rescue e - STDERR.puts e.message - exit 1 - end -end - -STDOUT << "password: " -STDOUT << `stty -echo` -STDOUT.flush -password = STDIN.gets.try &.chomp - -STDOUT << '\n' -STDOUT << `stty echo` - -exit 1 unless password - -authd = AuthD::Client.new - -email = nil if email == "" -phone = nil if phone == "" - -begin - if register - pp! authd.register login, password, email, phone, profile: profile - else - key_file.try do |file| # FIXME: fail if missing? - authd.key = File.read(file).chomp - end - - pp! authd.add_user login, password, email, phone, profile: profile - end -rescue e : AuthD::Exception - puts "error: #{e.message}" -end - -authd.close - diff --git a/utils/authd-user-perms.cr b/utils/authd-user-perms.cr deleted file mode 100644 index bf36565..0000000 --- a/utils/authd-user-perms.cr +++ /dev/null @@ -1,67 +0,0 @@ -require "option_parser" - -require "../src/authd.cr" - -key_file : String? = nil -cli_login : String? = nil -cli_service : String? = nil -cli_resource : String? = nil -cli_permlvl : String? = nil - -OptionParser.parse do |parser| - parser.unknown_args do |args| - if 3 < args.size > 4 - puts "usage: #{PROGRAM_NAME} [options]" - puts parser - exit 1 - end - - cli_login = args[0] - cli_service = args[1] - cli_resource = args[2] if args.size > 2 - cli_permlvl = args[3] if args.size > 3 - end - - parser.on "-K file", "--key-file file", "Read the authd shared key from a file." do |file| - key_file = file - end - - parser.on "-h", "--help", "Prints this help message." do - puts "usage: #{PROGRAM_NAME} [permission] [options]" - puts "example: #{PROGRAM_NAME} 1002 my-application chat read" - puts - puts "permission list: none read edit admin" - puts parser - exit 0 - end -end - -if cli_login.nil? - STDERR.puts "no login provided" - exit 1 -end - -login = cli_login.not_nil!.to_i # not_nil!? O RLY? -service = cli_service.not_nil! # not_nil! -resource = cli_resource.not_nil! # not_nil! - -authd = AuthD::Client.new - -begin - key_file.try do |file| # FIXME: fail if missing? - authd.key = File.read(file).chomp - end - - if cli_permlvl.nil? - pp! authd.check_permission login, service, resource - else - permlvl = cli_permlvl.not_nil! - perm = AuthD::User::PermissionLevel.parse(permlvl) - pp! authd.set_permission login, service, resource, perm - end -rescue e : AuthD::Exception - puts "error: #{e.message}" -end - -authd.close - diff --git a/utils/authd-user-search.cr b/utils/authd-user-search.cr deleted file mode 100644 index 42dc698..0000000 --- a/utils/authd-user-search.cr +++ /dev/null @@ -1,38 +0,0 @@ -require "option_parser" - -require "../src/authd.cr" - -# key_file : String? = nil -login : String? = nil -activation_key : String? = nil - -OptionParser.parse do |parser| - parser.unknown_args do |args| - if args.size != 1 - puts "usage: #{PROGRAM_NAME} login-to-search [options]" - exit 1 - end - - login = args[0] - end - - #parser.on "-K file", "--key-file file", "Read the authd shared key from a file." do |file| - # key_file = file - #end - - parser.on "-h", "--help", "Prints this help message." do - puts "usage: #{PROGRAM_NAME} login-to-search [options]" - puts parser - exit 0 - end -end - -begin - authd = AuthD::Client.new - # authd.key = File.read(key_file.not_nil!).chomp - - pp! r = authd.search_user login.not_nil! -rescue e - puts "Error: #{e}" - exit 1 -end diff --git a/utils/authd-user-validate.cr b/utils/authd-user-validate.cr deleted file mode 100644 index 039fa06..0000000 --- a/utils/authd-user-validate.cr +++ /dev/null @@ -1,38 +0,0 @@ -require "option_parser" - -require "../src/authd.cr" - -key_file : String? = nil -login : String? = nil -activation_key : String? = nil - -OptionParser.parse do |parser| - parser.unknown_args do |args| - if args.size != 2 - puts "usage: #{PROGRAM_NAME} login activation_key [options]" - exit 1 - end - - login, activation_key = args[0..1] - end - - parser.on "-K file", "--key-file file", "Read the authd shared key from a file." do |file| - key_file = file - end - - parser.on "-h", "--help", "Prints this help message." do - puts "usage: #{PROGRAM_NAME} [options]" - puts parser - exit 0 - end -end - -begin - authd = AuthD::Client.new - authd.key = File.read(key_file.not_nil!).chomp - - pp! r = authd.validate_user login.not_nil!, activation_key.not_nil! -rescue e - puts "Error: #{e}" - exit 1 -end diff --git a/utils/better-parser.cr b/utils/better-parser.cr index 42ab202..abbf241 100644 --- a/utils/better-parser.cr +++ b/utils/better-parser.cr @@ -1,36 +1,100 @@ require "option_parser" -OptionParser.parse do |parser| - parser.unknown_args do |args| - if args.size != 3 - puts "usage: #{PROGRAM_NAME} [options]" - puts parser - exit 1 - end - - cli_login, email, phone = args[0..2] +opt_authd_admin = -> (parser : OptionParser) { + parser.on "-k file", "--key-file file", "Read the authd shared key from a file." do |file| + Context.shared_key = File.read(file).chomp + Baguette::Log.info "Key for admin operations: #{Context.shared_key}." end +} - parser.on "-p file", "--profile file", "Read the user profile from a file." do |file| - profile_file = file + +parser = OptionParser.new do |parser| + parser.banner = "usage: #{PROGRAM_NAME} command help" + parser.on "-v verbosity", "--verbosity v", "Verbosity. From 0 to 4 (debug)." do |v| + Baguette::Context.verbosity = v.to_i + Baguette::Log.info "verbosity = #{v}" end - - parser.on "-X user-password", "--user-password pass", "Read the new user password." do |pass| - password = pass - end - - 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 - puts "usage: #{PROGRAM_NAME} [options]" + puts "usage: #{PROGRAM_NAME} command help" puts parser exit 0 end + + parser.on "user", "user" do + parser.banner = "Usage: user [add | mod | delete]" + parser.on "add", "add" do + Baguette::Log.info "user-add" + Context.command = "user-add" + # parser_user_add.call parser + # opt_profile.call parser + opt_authd_admin.call parser + parser.banner = "Usage: user-add user-id -e email -p phone [opt]" + + parser.on "help", "Prints this help message." do + puts "usage: #{PROGRAM_NAME} user-add THINGS [options]" + puts parser + exit 0 + end + end + + parser.on "mod", "mod" do + Baguette::Log.info "user-mod" + Context.command = "user-mod" + # opt_profile.call parser + opt_authd_admin.call parser + end + + parser.on "delete", "Remove user." do + Baguette::Log.info "Remove user." + Context.command = "delete" + opt_authd_admin.call parser + end + end + + parser.on "permissions", "Permissions." do + parser.banner = "Usage: permissions [check | set]" + parser.on "set", "Set permissions." do + Baguette::Log.info "Set permissions." + Context.command = "set-permissions" + opt_authd_admin.call parser + end + + parser.on "check", "Check permissions." do + Baguette::Log.info "Check permissions." + Context.command = "check-permissions" + opt_authd_admin.call parser + end + end + + # Do not require to be admin. + parser.on "registration", "Register a user." do + Baguette::Log.info "Register a user." + Context.command = "registration" + # opt_profile.call parser + # opt_authd_login.call parser + end + parser.unknown_args do |args| + if args.size > 0 + Baguette::Log.warning "Unknown args: #{args}" + end + end + +# parser.on "-p file", "--profile file", "Read the user profile from a file." do |file| +# profile_file = file +# end +# +# parser.on "-X user-password", "--user-password pass", "Read the new user password." do |pass| +# password = pass +# end +# +# 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 end + +parser.parse