Sha256: b3346c4d5424b83baeed9e37372b008d50100c0039a0b49f03a73664d041cdf8

Contents?: true

Size: 1.57 KB

Versions: 9

Compression:

Stored size: 1.57 KB

Contents

module HammerCLIForeman
  module Api
    class InteractiveBasicAuth < ApipieBindings::Authenticators::BasicAuth
      def authenticate(request, args)
        if HammerCLI.interactive?
          get_user
          get_password
        end
        super
      end

      def error(ex)
        if ex.is_a?(RestClient::Unauthorized)
          self.clear
          message = _("Invalid username or password.")
          begin
            message = JSON.parse(ex.response.body)['error']['message']
          rescue
          end
          return UnauthorizedError.new(message)
        end
      end

      def status
        unless @user.nil? || @password.nil?
          _("Using configured credentials for user '%s'.") % @user
        else
          _("Credentials are not configured.")
        end
      end

      def user(ask=nil)
        @user ||= ask && get_user
      end

      def password(ask=nil)
        @password ||= ask && get_password
      end

      def set_credentials(user, password)
        @user = user
        @password = password
      end

      def clear
        set_credentials(nil, nil)
      end

      private

      def get_user
        @user ||= ask_user(_("[Foreman] Username:%s") % " ")
      end

      def get_password
        @password ||= ask_user(_("[Foreman] Password for %{user}:%{wsp}") % {:user => @user, :wsp => " "}, true)
      end

      def ask_user(prompt, silent=false)
        if silent
          HammerCLI.interactive_output.ask(prompt) { |q| q.echo = false }
        else
          HammerCLI.interactive_output.ask(prompt)
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
hammer_cli_foreman-0.18.2 lib/hammer_cli_foreman/api/interactive_basic_auth.rb
hammer_cli_foreman-0.18.1 lib/hammer_cli_foreman/api/interactive_basic_auth.rb
hammer_cli_foreman-0.17.2 lib/hammer_cli_foreman/api/interactive_basic_auth.rb
hammer_cli_foreman-0.18.0 lib/hammer_cli_foreman/api/interactive_basic_auth.rb
hammer_cli_foreman-0.17.1 lib/hammer_cli_foreman/api/interactive_basic_auth.rb
hammer_cli_foreman-0.17.0 lib/hammer_cli_foreman/api/interactive_basic_auth.rb
hammer_cli_foreman-0.15.2 lib/hammer_cli_foreman/api/interactive_basic_auth.rb
hammer_cli_foreman-0.16.0 lib/hammer_cli_foreman/api/interactive_basic_auth.rb
hammer_cli_foreman-0.15.1 lib/hammer_cli_foreman/api/interactive_basic_auth.rb