require 'highline/import' require "interact" require 'paint' require 'fileutils' module App42 module Command class User include App42::Command::UserToken include App42::Base::Util include Interactive # @return # dup argument to get a non-frozen string def initialize(options={} ) @options = options.dup end # list local api and secret key def keys if check_key_file? puts message "#{Message::KEYS}", false, 'green' api_key, secret_key = get_keys puts "API Key = #{api_key}" puts "Secret Key = #{secret_key}" else message "#{Message::KEYS_NOT_FIND}", true, 'red' # TODO, should be dynamic App42::Base::Help.addkeys end end # clear local credentials def clear if App42::Command::Auth.logged_in? then begin ans = ask "Do you want to delete existing keys?", :default => true print_new_line if ans == true key_file = remove_key_file puts message "#{Message::KEYS_CLEARED}", false, 'green' else exit! end rescue message "#{Message::SOMETHING_WRONG}", false, 'red' end else puts message "#{Message::KEYS_NOT_FIND}", false, 'red' end end # Configure app42 credentials def add update_key if App42::Command::Auth.logged_in? api_key, secret_key = collect_app42_credentials if is_api_key_valid? api_key, secret_key configure_app42 api_key, secret_key print Paint["Adding keys...", :yellow] puts Paint["done", :green] else puts Paint["#{Message::WRONG_KEY}", :red] remove_key_file print_new_line exit! end end # update existing key def update_key puts message "#{Message::KEYS_EXIST}", false, 'red' ans = ask "\nDo you want to update existing keys?", :default => true print_new_line ans == true ? (return) : (exit!) end def is_api_key_valid? api_key, secret_key #:nodoc: key_validate_params = { 'apiKey'=> api_key, 'version' => VERSION, 'timeStamp' => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%LZ") } api_key_valid = validate_api_and_secret_key key_validate_params, 'info', 'authenticate', secret_key return api_key_valid['success'] end # validate api and secret key def validate_api_and_secret_key query_params, resource, action, secret_key url = resource_url(resource, action) sign = App42::Client::RestUtil.new.sign(secret_key, query_params ) response = App42::Client::App42RestClient.new.get(sign, url, query_params) end # collect keys for keys:add # Get api key # Get secret key def collect_app42_credentials api_key = @options[:api] if @options[:api] secret_key = @options[:secret] if @options[:secret] message '=== Enter your App42PaaS keys ===', true, 'blue' if api_key.nil? && secret_key.nil? api_key = get_api_key if api_key.nil? secret_key = get_secret_key if secret_key.nil? return api_key, secret_key end # collect api key from client def get_api_key(prompt = Paint["Enter API Key:", :bright]) api_key = @options[:api_key] if @options[:api_key] api_key = ask(prompt) {|q| q.echo = true} if api_key.nil? return api_key.strip end # collect secret key from client def get_secret_key(prompt = Paint["Enter Secret Key:", :bright]) secret_key = @options[:secret_key] if @options[:secret_key] secret_key = ask(prompt) {|q| q.echo = true} if secret_key.nil? return secret_key.strip end private # make sure, have configuration file # configure api and secrete key def configure_app42 api_key, secret_key if api_key && secret_key begin ensure_config_dir local_app42_key api_key, secret_key rescue Exception => e puts e end end end end end end