Skip to content

Commit

Permalink
fix two factor authentication when creating a new token
Browse files Browse the repository at this point in the history
Enable to create a new OAuth token even when user enabled two factor
authentication.

References #399
  • Loading branch information
yasuoza authored and mislav committed Oct 1, 2013
1 parent 1412e50 commit f45c1e0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
40 changes: 39 additions & 1 deletion features/authentication.feature
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,45 @@ Feature: OAuth authentication
And the exit status should be 1
And the file "../home/.config/hub" should not exist

Scenario: Two-factor authentication
Scenario: Two-factor authentication, create authorization
Given the GitHub API server:
"""
require 'rack/auth/basic'
get('/authorizations') {
auth = Rack::Auth::Basic::Request.new(env)
halt 401 unless auth.credentials == %w[mislav kitty]
if request.env['HTTP_X_GITHUB_OTP'] != "112233"
response.headers['X-GitHub-OTP'] = "required;application"
halt 401
end
json [ ]
}
post('/authorizations') {
auth = Rack::Auth::Basic::Request.new(env)
halt 401 unless auth.credentials == %w[mislav kitty]
if request.env['HTTP_X_GITHUB_OTP'] != "112233"
response.headers['X-GitHub-OTP'] = "required;application"
halt 401
end
json :token => 'OTOKEN'
}
get('/user') {
json :login => 'mislav'
}
post('/user/repos') {
json :full_name => 'mislav/dotfiles'
}
"""
When I run `hub create` interactively
When I type "mislav"
And I type "kitty"
And I type "112233"
Then the output should contain "github.com password for mislav (never stored):"
Then the output should contain "two-factor authentication code:"
And the exit status should be 0
And the file "../home/.config/hub" should contain "oauth_token: OTOKEN"

Scenario: Two-factor authentication, re-use existing authorization
Given the GitHub API server:
"""
require 'rack/auth/basic'
Expand Down
4 changes: 3 additions & 1 deletion lib/hub/github_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ def obtain_oauth_token host, user, two_factor_code = nil
else
# create a new authorization
res = post "https://#{user}@#{host}/authorizations",
:scopes => %w[repo], :note => 'hub', :note_url => oauth_app_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
Expand Down

0 comments on commit f45c1e0

Please sign in to comment.