Sha256: 6330c60b8ea77eaa3b0ee6b4f50618b6fac24425689e0d7a0ec8483796dfc90c

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

module Gemfury::Command::Authorization
  include Gemfury::Platform

private
  def with_authorization(&block)
    # Load up the credentials
    load_credentials!

    # Attempt the operation and prompt user in case of
    # lack of authorization or a 401 response from the server
    begin
      prompt_credentials! if @user_api_key.nil?
      block.call
    rescue Gemfury::Unauthorized
      shell.say "Oops! Authentication failure.", :red
      @user_api_key = nil
      retry
    end
  end

  def prompt_credentials!
    # Prompt credentials
    highline = HighLine.new
    highline.say 'Please enter your Gemfury credentials.'
    email = highline.ask('Email: ')
    passw = highline.ask('Password: ') { |q| q.echo = false }

    # Request and save the API access token
    if !email.empty? && !passw.empty?
      @user_api_key = client.get_access_token(email, passw)
      write_credentials!
    end
  end

  def load_credentials!
    conf = read_config_file
    @user_api_key = conf[:gemfury_api_key] if conf[:gemfury_api_key]
  end

  def write_credentials!
    config = read_config_file.merge(:gemfury_api_key => @user_api_key)
    File.open(config_path, 'w') { |f| f.write(YAML.dump(config)) }
  end

  def read_config_file
    File.exist?(config_path) ? YAML.load_file(config_path) : {}
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gemfury-0.2.0.beta2 lib/gemfury/command/authorization.rb
gemfury-0.2.0.beta1 lib/gemfury/command/authorization.rb