def obtain_oauth_token host, user, two_factor_code = nil
auth_url = URI.parse("https://%s@%s/authorizations" % [CGI.escape(user), host])
post(auth_url) if !two_factor_code
res = get_all(auth_url) do |req|
req['X-GitHub-OTP'] = two_factor_code if two_factor_code
end
unless res.success?
if !two_factor_code && res['X-GitHub-OTP'].to_s.include?('required')
two_factor_code = config.prompt_auth_code
return obtain_oauth_token(host, user, two_factor_code)
else
res.error!
end
end
if found = res.data.find {|auth| auth['note'] == 'hub' || auth['note_url'] == oauth_app_url }
found['token']
else
res = post auth_url,
:scopes => %w[repo], :note => 'hub', :note_url => oauth_app_url do |req|
req['X-GitHub-OTP'] = two_factor_code if two_factor_code
end
res.error! unless res.success?
res.data['token']
end
end