class Octoauth::Auth

Authentication object

Public Class Methods

new(params = {}) click to toggle source
# File lib/octoauth/auth.rb, line 23
def initialize(params = {})
  parse_params!(params)
  save if @options[:autosave]
end

Public Instance Methods

save() click to toggle source
# File lib/octoauth/auth.rb, line 28
def save
  raise 'No token to save' unless token
  raise 'No file given for config' unless config.file
  config.token = token
  return unless @token_changed
  config.write
end
token() click to toggle source
# File lib/octoauth/auth.rb, line 36
def token
  @token ||= load_token
end

Private Instance Methods

authenticate(using2fa) click to toggle source
# File lib/octoauth/auth.rb, line 93
def authenticate(using2fa)
  client = Octokit::Client.new(
    @options.subset(:login, :password, :api_endpoint)
  )
  delete_existing_token(client)
  client.create_authorization(
    { note: note }.merge(@options.subset(:scopes, :headers, :fingerprint))
  ).token
rescue Octokit::OneTimePasswordRequired
  raise('Invalid 2fa code') if using2fa
  load_token(true)
end
config() click to toggle source
# File lib/octoauth/auth.rb, line 42
def config
  return @config if @config
  configs = config_files.map do |file|
    ConfigFile.new file: file, note: config_note
  end
  @config = configs.find(&:token) || configs.first
end
config_files() click to toggle source
# File lib/octoauth/auth.rb, line 50
def config_files
  @options[:files] ||= [@options[:file]]
end
config_note() click to toggle source
# File lib/octoauth/auth.rb, line 61
def config_note
  return @options[:note] unless @options[:api_endpoint]
  "#{@options[:note]}--#{@options[:api_endpoint]}"
end
delete_existing_token(client) click to toggle source
# File lib/octoauth/auth.rb, line 106
def delete_existing_token(client)
  headers = @options.subset(:headers)
  client.authorizations(headers)
        .select { |x| x[:note] == note }
        .map { |x| client.delete_authorization(x[:id], headers) }
end
hostname() click to toggle source
# File lib/octoauth/auth.rb, line 70
def hostname
  return @hostname if @hostname
  @hostname ||= ENV['HOSTNAME'] || `hostname`.split.first || 'NULL'
rescue Errno::ENOENT
  @hostname = 'NULL'
end
load_token(needs2fa = false) click to toggle source
# File lib/octoauth/auth.rb, line 86
def load_token(needs2fa = false)
  return config.token if config.token
  @token_changed = true
  prompt!(needs2fa)
  authenticate(needs2fa)
end
note() click to toggle source
# File lib/octoauth/auth.rb, line 66
def note
  @note ||= "#{@options[:note]}/#{hostname}"
end
parse_params!(params) click to toggle source
# File lib/octoauth/auth.rb, line 54
def parse_params!(params)
  @options = params.subset(
    :file, :files, :note, :autosave, :scopes,
    :login, :password, :twofactor, :api_endpoint
  )
end
prompt!(needs2fa = false) click to toggle source
# File lib/octoauth/auth.rb, line 77
def prompt!(needs2fa = false)
  @options[:login] ||= PROMPTS[:login].ask
  @options[:password] ||= PROMPTS[:password].ask
  @options[:scopes] ||= DEFAULT_SCOPES
  return unless needs2fa
  @options[:twofactor] ||= PROMPTS[:twofactor].ask
  @options[:headers] = { 'X-GitHub-OTP' => @options[:twofactor] }
end