mislav / hub

A command-line tool that makes git easier to use with GitHub.
https://hub.github.com/
MIT License
22.78k stars 2.2k forks source link

OAuth 422 Unprocessable Entity when I have already another authentication #496

Closed waterlink closed 10 years ago

waterlink commented 10 years ago

I had hub running ok on another laptop three months ago.

Now I have installed hub again on new laptop from gem (Ubuntu 13.10, rvm, ruby 2.0.0-p353).

No command related to github is working. Just unable to authenticate. For example

hub ci-status

outputs following stack trace:

/home/user/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/net/http/response.rb:119:in `error!': 422 "Unprocessable Entity" (Net::HTTPServerException)
    from /home/user/.rvm/gems/ruby-2.0.0-p353/gems/hub-1.11.2/lib/hub/github_api.rb:338:in `obtain_oauth_token'
    from /home/user/.rvm/gems/ruby-2.0.0-p353/gems/hub-1.11.2/lib/hub/github_api.rb:301:in `block in apply_authentication'
    from /home/user/.rvm/gems/ruby-2.0.0-p353/gems/hub-1.11.2/lib/hub/github_api.rb:476:in `block in oauth_token'
    from /home/user/.rvm/gems/ruby-2.0.0-p353/gems/hub-1.11.2/lib/hub/github_api.rb:372:in `block in fetch_value'
    from /home/user/.rvm/gems/ruby-2.0.0-p353/gems/hub-1.11.2/lib/hub/github_api.rb:371:in `fetch'
    from /home/user/.rvm/gems/ruby-2.0.0-p353/gems/hub-1.11.2/lib/hub/github_api.rb:371:in `fetch_value'
    from /home/user/.rvm/gems/ruby-2.0.0-p353/gems/hub-1.11.2/lib/hub/github_api.rb:475:in `oauth_token'
    from /home/user/.rvm/gems/ruby-2.0.0-p353/gems/hub-1.11.2/lib/hub/github_api.rb:300:in `apply_authentication'
    from /home/user/.rvm/gems/ruby-2.0.0-p353/gems/hub-1.11.2/lib/hub/github_api.rb:346:in `apply_authentication'
    from /home/user/.rvm/gems/ruby-2.0.0-p353/gems/hub-1.11.2/lib/hub/github_api.rb:230:in `perform_request'
    from /home/user/.rvm/gems/ruby-2.0.0-p353/gems/hub-1.11.2/lib/hub/github_api.rb:193:in `get'
    from /home/user/.rvm/gems/ruby-2.0.0-p353/gems/hub-1.11.2/lib/hub/github_api.rb:155:in `statuses'
    from /home/user/.rvm/gems/ruby-2.0.0-p353/gems/hub-1.11.2/lib/hub/commands.rb:91:in `ci_status'
    from /home/user/.rvm/gems/ruby-2.0.0-p353/gems/hub-1.11.2/lib/hub/commands.rb:61:in `run'
    from /home/user/.rvm/gems/ruby-2.0.0-p353/gems/hub-1.11.2/lib/hub/runner.rb:12:in `initialize'
    from /home/user/.rvm/gems/ruby-2.0.0-p353/gems/hub-1.11.2/lib/hub/runner.rb:17:in `new'
    from /home/user/.rvm/gems/ruby-2.0.0-p353/gems/hub-1.11.2/lib/hub/runner.rb:17:in `execute'
    from /home/user/.rvm/gems/ruby-2.0.0-p353/gems/hub-1.11.2/bin/hub:7:in `<top (required)>'
    from /home/user/.rvm/gems/ruby-2.0.0-p353/bin/hub:23:in `load'
    from /home/user/.rvm/gems/ruby-2.0.0-p353/bin/hub:23:in `<main>'
    from /home/user/.rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `eval'
    from /home/user/.rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `<main>'

A bit of inspecting ruby files and adding debug output before exception raised resulted to pinpointing problem. Response body from github oauth is following:

{"message":"Validation Failed","documentation_url":"http://developer.github.com/v3/oauth_authorizations/#create-a-new-authorization","errors":[{"resource":"OauthAccess","code":"already_exists","field":"description"}]}

So, "code": "already_exists". I had a guess to change 'note' sent to github oauth to something another in GitHubAPI::HttpMethods::OAuth#obtain_oauth_token method:

          res = post auth_url,
-            :scopes => %w[repo], :note => 'hub', :note_url => oauth_app_url do |req|
+           :scopes => %w[repo], :note => 'my-hub', :note_url => oauth_app_url do |req|
              req['X-GitHub-OTP'] = two_factor_code if two_factor_code
            end

And it worked out for me. And obtained new authentication.

sethbro commented 10 years ago

I also encountered this issue when using hub on a second laptop. As @waterlink points out, it seems to be the duplicate OAuth token description (note in the API call) that causes the problem. Rather than make a change in code, I went to the Github website & changed the token description used by my other computer (under Account Settings > Applications).

I assume that registering hub with a unique token (e.g. hug-xx123) on first authentication would solve the problem.

e0da commented 10 years ago

:+1: This has been biting me, too.

docwhat commented 10 years ago

:+1: Thanks for explaining it. I just renamed them to "hub-".

Ideally, it would be creating something like that. If there was a collision, then it should present the link to the page where you can rename them or offer to rename them itself.

Ciao!

waterlink commented 10 years ago

Is it possible to generate something like "hub-{long-random-unique-hash-here-from-your-laptop-metadata}" ?

e0da commented 10 years ago

@docwhat thanks for the tip. Renaming my existing hub allowed me to register a new one. Now I'll just name them hub-#{hostname} manually at https://github.com/settings/applications.

waterlink commented 10 years ago

How about something like that?

res = post auth_url,
           :scopes => %w[repo], :note => "hub-#{get_unique_host_id}", :note_url => oauth_app_url do |req|
              req['X-GitHub-OTP'] = two_factor_code if two_factor_code
            end

where get_unique_host_id may be something like that (for Linux):

def get_unique_host_id
  if OS::linux?
    `hostid`
  elsif OS::windows?
    # do similar thing for windows
  elsif OS::macosx?
    # do similiar thing for macos
  end
end

Or we can just generate this unique id and store it somewhere in the system, for example together with oauth tokens.

mislav commented 10 years ago

I've already fixed this, pending push and release. No unique identifiers necessary.

waterlink commented 10 years ago

@mislav thanks!

mislav commented 10 years ago

This fix is released. Now if there's any token named "hub", we reuse that as well. This will avoid trying to create another token with the same name "hub" and hitting the validation error.

You can delete other tokens for hub that you've renamed and just keep the one that has "hub" for description.

Raynos commented 10 years ago

I ran into this issue today. I installed hub:

I had to rename all my existing hub tokens to hub-{noise}.

$ hub --version
git version 1.9.1
hub version 1.12.0-10-g67728bc
mislav commented 10 years ago

@Raynos I'm sorry to hear that. Version 1.12.0 definitely includes this fix, and there's a test for this behavior. Is it possible that you had an older version of hub at the time you tried to re-auth, or that you might have two versions of hub on your system? where -a hub

Anyways, now that you worked aroun this, you can delete the old hub-{noise} tokens now; they're of no use to anything anymore.