class Fue::Auth

Public Class Methods

instance() click to toggle source
# File lib/fue/auth.rb, line 5
def self.instance
  @instance ||= new
end

Public Instance Methods

token() click to toggle source
# File lib/fue/auth.rb, line 9
def token
  stored_options = { username: username, server: 'github.com', label: 'fue' }
  stored_token = Fue::Security.get(stored_options)
  unless stored_token
    stored_token = github_token
    Fue::Security.store!(stored_options.merge(password: stored_token))
    puts 'Token saved to keychain.'
  end
  stored_token
end
username() click to toggle source
# File lib/fue/auth.rb, line 20
def username
  @username ||= begin
    username = get_git_username&.chomp
    username = get_username if username.nil? || username.empty?
    username
  end
end

Private Instance Methods

get_code() click to toggle source
# File lib/fue/auth.rb, line 83
def get_code
  print 'Enter GitHub 2FA code: '
  get_secure
end
get_git_username() click to toggle source
# File lib/fue/auth.rb, line 30
def get_git_username
  Fue::Shell.system!('git config user.name')
rescue RuntimeError
  nil
end
get_password() click to toggle source
# File lib/fue/auth.rb, line 78
def get_password
  print "Enter #{username}'s GitHub password (never stored): "
  get_secure
end
get_secure() click to toggle source
# File lib/fue/auth.rb, line 88
def get_secure
  current_tty = `stty -g`
  system 'stty raw -echo -icanon isig' if $CHILD_STATUS.success?
  input = String.new
  while (char = $stdin.getbyte) && !((char == 13) || (char == 10))
    if (char == 127) || (char == 8)
      input[-1, 1] = '' unless input.empty?
    else
      $stdout.write '*'
      input << char.chr
    end
  end
  print "\r\n"
  input
rescue Interrupt => e
  raise e, 'ctrl + c'
ensure
  system "stty #{current_tty}" unless current_tty.empty?
end
get_username() click to toggle source
# File lib/fue/auth.rb, line 70
def get_username
  print 'Enter GitHub username: '
  username = $stdin.gets
  username&.chomp
rescue Interrupt => e
  raise e, 'ctrl + c'
end
github(code = nil) click to toggle source
# File lib/fue/auth.rb, line 57
def github(code = nil)
  Github.new do |config|
    config.basic_auth = [username, password].join(':')
    if code
      config.connection_options = {
        headers: {
          'X-GitHub-OTP' => code
        }
      }
    end
  end
end
github_token(code = nil) click to toggle source
# File lib/fue/auth.rb, line 36
def github_token(code = nil)
  github(code).auth.create(scopes: ['public_repo'], note: note).token
rescue Github::Error::Unauthorized => e
  case e.response_headers['X-GitHub-OTP']
  when /required/
    github_token(get_code)
  else
    raise e
  end
rescue Github::Error::UnprocessableEntity => e
  raise e, 'A fue token already exists! Please revoke all previously-generated fue personal access tokens at https://github.com/settings/tokens.'
end
note() click to toggle source
# File lib/fue/auth.rb, line 53
def note
  "Fui (https://github.com/dblock/fue) on #{Socket.gethostname}"
end
password() click to toggle source
# File lib/fue/auth.rb, line 49
def password
  @password ||= get_password
end