<commit_before>require 'security'

module IosDeployKit
  class PasswordManager
    attr_accessor :username, :password

    HOST = "itunesconnect.apple.com"

    def initialize

      self.username ||= ENV["IOS_DEPLOY_KIT_USER"] || self.load_from_keychain[0]
      self.password ||= ENV["IOS_DEPLOY_KIT_PASSWORD"] || self.load_from_keychain[1]

    end

  
    def load_from_keychain
      pass = Security::InternetPassword.find(:server => IosDeployKit::PasswordManager::HOST)
      
      return [pass.attributes['acct'], pass.password] if pass
      return [nil, nil]
    end
  end
end<commit_msg>Added asking and storing of login information when none was given<commit_after>require 'security'
require 'highline/import' # to hide the entered password

module IosDeployKit
  class PasswordManager
    attr_accessor :username, :password

    HOST = "itunesconnect.apple.com"

    def initialize

      self.username ||= ENV["IOS_DEPLOY_KIT_USER"] || self.load_from_keychain[0]
      self.password ||= ENV["<PASSWORD>"] || self.load_from_keychain[1]

      if (self.username || '').length == 0 or (self.password || '').length == 0
        ask_for_login
      end
    end

    def ask_for_login
      puts "No username or password given. You can use environment variables"
      puts "IOS_DEPLOY_KIT_USER, IOS_DEPLOY_KIT_PASSWORD"
      puts "The login information will be stored in your keychain"

      while (self.username || '').length == 0
        self.username = ask("Username: ")
      end

      while (self.password || '').length == 0
        self.password = ask("Password: ") { |q| q.echo = "*" }
      end

      # Now we store this information in the keychain
      # Example usage taken from https://github.com/nomad/cupertino/blob/master/lib/cupertino/provisioning_portal/commands/login.rb
      if Security::InternetPassword.add(IosDeployKit::PasswordManager::HOST, self.username, self.password)
        return true
      else
        Helper.log.error "Could not store password in keychain"
        return false
      end
    end
  
    def load_from_keychain
      pass = Security::InternetPassword.find(:server => IosDeployKit::PasswordManager::HOST)
      
      return [pass.attributes['acct'], pass.password] if pass
      return [nil, nil]
    end
  end
end
{'fraction_non_alphanumeric': 0.10456942003514938, 'fraction_numerical': 0.0035149384885764497, 'mean_word_length': 3.1856617647058822, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4539442', 'n_tokens_mistral': 720, 'n_tokens_neox': 692, 'n_words': 171}