Module: GHI::Authorization

Extended by:
Formatting
Defined in:
lib/ghi/authorization.rb

Defined Under Namespace

Classes: Required

Constant Summary

Constants included from Formatting

Formatting::CURSOR, Formatting::THROBBERS

Constants included from Formatting::Colors

Formatting::Colors::ANSI, Formatting::Colors::WEB

Instance Attribute Summary

Attributes included from Formatting

#paging

Class Method Summary collapse

Methods included from Formatting

columns, dimensions, format_comment, format_comment_editor, format_comments_and_events, format_date, format_editor, format_event, format_event_type, format_issue, format_issues, format_issues_header, format_labels, format_markdown, format_milestone, format_milestone_editor, format_milestones, format_number, format_state, format_tag, format_username, indent, page, paginate?, paging?, past_due?, percent, puts, throb, truncate

Methods included from Formatting::Colors

#bg, #blink, #bright, #colorize?, colorize?, #fg, #highlight, #inverse, #no_color, #to_hex, #underline

Class Method Details

.authorize!(user = username, pass = password, local = true) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ghi/authorization.rb', line 17

def authorize! user = username, pass = password, local = true
  return false unless user && pass
  code ||= nil # 2fa
  args = code ? [] : [54, "✔\r"]
  note = %w[ghi]
  note << "(#{GHI.repo})" if local
  note << "on #{Socket.gethostname}"
  res = throb(*args) {
    headers = {}
    headers['X-GitHub-OTP'] = code if code
    body = {
      :scopes   => %w(public_repo repo),
      :note     => note.join(' '),
      :note_url => 'https://github.com/stephencelis/ghi'
    }
    Client.new(user, pass).post(
      '/authorizations', body, :headers => headers
    )
  }
  @token = res.body['token']

  unless username
    system "git config#{' --global' unless local} github.user #{user}"
  end

  store_token! username, token, local
rescue Client::Error => e
  if e.response['X-GitHub-OTP'] =~ /required/
    puts "Bad code." if code
    print "Two-factor authentication code: "
    trap('INT') { abort }
    code = gets
    code = '' and puts "\n" unless code
    retry
  end

  if e.errors.any? { |err| err['code'] == 'already_exists' }
    message = <<EOF.chomp
A ghi token already exists!

Please revoke all previously-generated ghi personal access tokens here:

  https://github.com/settings/applications
EOF
  else
    message = e.message
  end
  abort "#{message}#{CURSOR[:column][0]}"
end

.passwordObject



72
73
74
75
# File 'lib/ghi/authorization.rb', line 72

def password
  return @password if defined? @password
  @password = GHI.config 'github.password'
end

.tokenObject



12
13
14
15
# File 'lib/ghi/authorization.rb', line 12

def token
  return @token if defined? @token
  @token = GHI.config 'ghi.token'
end

.usernameObject



67
68
69
70
# File 'lib/ghi/authorization.rb', line 67

def username
  return @username if defined? @username
  @username = GHI.config 'github.user'
end