class LabelMaker::User

Attributes

github_client[R]

Public Class Methods

new() click to toggle source
# File lib/label_maker/user.rb, line 8
def initialize
  # Uses credentials in .netrc to authenticate
  @github_client = Octokit::Client.new(netrc: true)
end

Public Instance Methods

authenticate(logged_in = false) click to toggle source
# File lib/label_maker/user.rb, line 13
def authenticate(logged_in = false)
  if logged_in?
    return true
  else
    puts "Authentication failed or you are a first time user!"
    get_credentials
  end
end
get_credentials() click to toggle source
# File lib/label_maker/user.rb, line 22
def get_credentials
  puts "Please login with your GitHub account:"
  puts "GitHub Username:"
  username = gets.chomp
  puts "Password (hidden):"
  password = STDIN.noecho(&:gets).chomp

  client = Octokit::Client.new(login: username, password: password)
  puts client.inspect

  puts 'Do you use Two Factor Auth? (y/n)'
  agree = gets.chomp
  if agree == 'y'
    puts 'Enter your 2FA token:'
    two_factor = gets.chomp
    oauth = oauth_authorization(client, headers: {"X-GitHub-OTP" => two_factor})
  else
    oauth = oauth_authorization(client)
  end

  save_to_netrc(username, oauth.token)
  authenticate(true)
end

Private Instance Methods

logged_in?() click to toggle source
# File lib/label_maker/user.rb, line 59
def logged_in?
  netrc = Netrc.read
  netrc['api.github.com'] && Octokit::Client.new(netrc: true).login
end
oauth_authorization(client, headers = {}) click to toggle source
# File lib/label_maker/user.rb, line 48
def oauth_authorization(client, headers = {})
  client.create_authorization(scopes: ['user','repo'], note: "LabelMaker gem 0.0.1- #{Time.now.strftime("%Y-%m-%d")}!", headers: headers) 
end
save_to_netrc(user, token) click to toggle source
# File lib/label_maker/user.rb, line 52
def save_to_netrc(user, token)
  netrc = Netrc.read
  netrc.new_item_prefix = "# This entry was added by the LabelMaker gem\n"
  netrc['api.github.com'] = user, token
  netrc.save
end