Class: Boxen::Preflight::Creds

Inherits:
Boxen::Preflight show all
Defined in:
lib/boxen/preflight/creds.rb

Instance Attribute Summary collapse

Attributes inherited from Check

#config

Instance Method Summary collapse

Methods inherited from Check

#abort, checks, #initialize, register, run, #warn

Constructor Details

This class inherits a constructor from Boxen::Check

Instance Attribute Details

#otpObject (readonly)

Returns the value of attribute otp.



11
12
13
# File 'lib/boxen/preflight/creds.rb', line 11

def otp
  @otp
end

#passwordObject (readonly)

Returns the value of attribute password.



12
13
14
# File 'lib/boxen/preflight/creds.rb', line 12

def password
  @password
end

Instance Method Details

#get_otpObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/boxen/preflight/creds.rb', line 33

def get_otp
  console = HighLine.new

  # junk API call to send OTP until we implement PUT
  tmp_api.create_authorization rescue nil

  @otp = console.ask "One time password (via SMS or device):" do |q|
    q.echo = '*'
  end
end

#get_tokensObject

Attempt to use the username+password to get a list of the user’s OAuth authorizations from the API. If it fails because of 2FA, ask the user for her OTP and try again.

Returns a list of authorizations



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/boxen/preflight/creds.rb', line 49

def get_tokens
  begin
    tmp_api.authorizations(:headers => headers)
  rescue Octokit::Unauthorized
    abort "Sorry, I can't auth you on GitHub.",
      "Please check your credentials and teams and give it another try."
  rescue Octokit::OneTimePasswordRequired
    puts
    if otp.nil?
      warn "It looks like you have two-factor auth enabled."
    else
      warn "That one time password didn't work. Let's try again."
    end
    get_otp
    get_tokens
  end
end

#headersObject



29
30
31
# File 'lib/boxen/preflight/creds.rb', line 29

def headers
  otp.nil? ? {} : {"X-GitHub-OTP" => otp}
end

#ok?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
# File 'lib/boxen/preflight/creds.rb', line 14

def ok?
  if config.token && config.api.user
    # There was a period of time when login wasn't geting set on first run.
    # This should correct that.
    config. = config.api.user.
    true
  end
rescue
  nil
end

#runObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/boxen/preflight/creds.rb', line 67

def run
  
  tokens = get_tokens

  unless auth = tokens.detect { |a| a.note == "Boxen" }
    auth = tmp_api.create_authorization \
      :note => "Boxen",
      :scopes => %w(repo user),
      :headers => headers
  end

  config.token = auth.token

  unless ok?
    puts
    abort "Something went terribly wrong.",
      "I was able to get your OAuth token, but was unable to use it."
  end
end

#tmp_apiObject



25
26
27
# File 'lib/boxen/preflight/creds.rb', line 25

def tmp_api
  @tmp_api ||= Octokit::Client.new :login => config., :password => password, :auto_paginate => true
end