class GitHubAuth

Constants

CREDENTIALS
NOTE

Change NOTE, SCOPES and CREDENTIALS to match your app’s needs.

SCOPES

Public Class Methods

client() click to toggle source
# File lib/auth/github.rb, line 14
def self.client
  new.client
end

Public Instance Methods

client() click to toggle source
# File lib/auth/github.rb, line 18
def client
  @client ||= lambda do
    unless File.exist?(CREDENTIALS)
      authenticate
    end
    Octokit::Client.new(YAML.load_file(CREDENTIALS))
  end.call
end

Private Instance Methods

ask_login() click to toggle source
# File lib/auth/github.rb, line 43
def ask_login
  ask("Enter you GitHub username: ")
end
ask_password() click to toggle source
# File lib/auth/github.rb, line 47
def ask_password
  ask("Enter your GitHub password (this will NOT be stored): ") { |q| q.echo = '*' }
end
authenticate() click to toggle source
# File lib/auth/github.rb, line 28
def authenticate
  login = password = token = ""
  login = `git config github.user`.chomp
  login = ask_login if login.empty?
  password = ask_password
  auth_client = Octokit::Client.new(:login => login, :password => password)
  auth = auth_client.authorizations.detect { |a| a.note == NOTE }
  unless auth
    auth = auth_client.create_authorization(:scopes => SCOPES, :note => NOTE)
  end
  File.open(CREDENTIALS, 'w') do |f|
    f.puts({ :login => login, :access_token => auth.token }.to_yaml)
  end
end