intridea/omniauth-github — Issue #2: Authenticating Github works but callback shows failure
Author: hjuskewycz
Created: 2011-11-13 13:01:33+00:00
State: open

I am trying to use OmniAuth Github gem for our service. The service needs to connect to Github and access private and public repos (btw it would nice if the readme would explain :scope => 'repo'). The problem I have, is that we can establish the connection to Github, but we receive an error on our backend. So the redirect to Github works, the click on 'Allow' establishes the connection, BUT we receive a failure.

Here is my configuration:

production.rb
```ruby
Rails.application.config.middleware.use OmniAuth::Builder do
provider :github, ENV['GITHUB_CLIENT_ID'], ENV['GITHUB_CLIENT_SECRET'], :scope => "repo"
end
```

routes.rb
```ruby
#omniauth
match '/auth/failure', :to => 'omni_auth#failure'
match '/auth/:provider/callback', :to => 'omni_auth#callback'
```

github_auth_controller.rb
```ruby
class GithubAuthTokensController < AuthorizationController
authorize_resource :project

def new
origin_url = CGI::escape(project_github_auth_tokens_path)
redirect_to "/auth/github?origin=#{origin_url}"
end

def create
if session[:omniauth] == false
flash_error "github_service.create.flash.failure"
else
access_token = session[:omniauth]["credentials"]["token"]
UserServiceData.save_access_token!(current_project, ServiceProvider.github, access_token)
flash_success "github_service.create.flash.success"
end

rescue Exception => e
flash_error e.message
ensure
redirect_to project_service_hooks_path(:user_url => current_project.owner.url, :project_id => current_project.to_param)
end
end
```

```ruby

#this controller is used to redirect to the origin url after oauth authentication
class OmniAuthController < ApplicationController
def callback
session[:omniauth] = auth_hash
redirect_to origin_url_on_success || '/'
end

def failure
puts "params #{params}"
puts "FAILURE FAILURE '#{origin_url_on_failure}'"
session[:omniauth] = false
redirect_to origin_url_on_failure || '/'
end

def auth_hash
request.env['omniauth.auth']
end

def origin_url_on_success
request.env['omniauth.origin']
end

def origin_url_on_failure
request.env['rack.session']['omniauth.origin']
end
end

```

I am really stuck on this problem. I forked OmnitAuth, Oauth, ... just to add some put statements, but I don't figure out the problem. Here is the output from the put statements (I altered of course the credentials):

```bash
2011-11-13T12:57:45+00:00 app[web.1]:
2011-11-13T12:57:47+00:00 app[web.1]: cache: [GET /hemju/linguist/github_auth_tokens/new] miss
2011-11-13T12:57:47+00:00 heroku[router]: GET app.lingui.st/hemju/linguist/github_auth_tokens/new dyno=web.1 queue=0 wait=0ms service=1400ms status=302 bytes=152
2011-11-13T12:57:47+00:00 heroku[nginx]: 127.0.0.1 - - [13/Nov/2011:04:57:47 -0800] "GET /hemju/linguist/github_auth_tokens/new HTTP/1.0" 302 152 "https://app.lingui.st/hemju/linguist/service_hooks" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2" app.lingui.st
2011-11-13T12:57:47+00:00 heroku[nginx]: 10.202.85.124 - - [13/Nov/2011:04:57:47 -0800] "GET /hemju/linguist/github_auth_tokens/new HTTP/1.1" 302 152 "https://app.lingui.st/hemju/linguist/service_hooks" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2" app.lingui.st
2011-11-13T12:57:47+00:00 app[web.1]:
2011-11-13T12:57:47+00:00 app[web.1]:
2011-11-13T12:57:47+00:00 app[web.1]: Started GET "/auth/github?origin=%2Fhemju%2Flinguist%2Fgithub_auth_tokens" for 10.202.85.124 at 2011-11-13 12:57:47 +0000
2011-11-13T12:57:47+00:00 app[web.1]: cache: [GET /auth/github?origin=%2Fhemju%2Flinguist%2Fgithub_auth_tokens] miss
2011-11-13T12:57:47+00:00 heroku[router]: GET app.lingui.st/auth/github dyno=web.1 queue=0 wait=0ms service=91ms status=302 bytes=174
2011-11-13T12:57:47+00:00 heroku[nginx]: 127.0.0.1 - - [13/Nov/2011:04:57:47 -0800] "GET /auth/github?origin=%2Fhemju%2Flinguist%2Fgithub_auth_tokens HTTP/1.0" 302 174 "https://app.lingui.st/hemju/linguist/service_hooks" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2" app.lingui.st
2011-11-13T12:57:47+00:00 heroku[nginx]: 10.202.85.124 - - [13/Nov/2011:04:57:47 -0800] "GET /auth/github?origin=%2Fhemju%2Flinguist%2Fgithub_auth_tokens HTTP/1.1" 302 174 "https://app.lingui.st/hemju/linguist/service_hooks" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2" app.lingui.st
2011-11-13T12:57:52+00:00 app[web.1]:
2011-11-13T12:57:52+00:00 app[web.1]:
2011-11-13T12:57:52+00:00 app[web.1]: Started GET "/auth/github/callback?code=1ef746512988a4d76ad1" for 10.202.85.124 at 2011-11-13 12:57:52 +0000
2011-11-13T12:57:52+00:00 app[web.1]:
2011-11-13T12:57:52+00:00 app[web.1]:
2011-11-13T12:57:52+00:00 app[web.1]:
2011-11-13T12:57:52+00:00 app[web.1]:
2011-11-13T12:57:52+00:00 app[web.1]:
2011-11-13T12:57:52+00:00 app[web.1]:
2011-11-13T12:57:52+00:00 app[web.1]:
2011-11-13T12:57:52+00:00 app[web.1]:
2011-11-13T12:57:52+00:00 app[web.1]:
2011-11-13T12:57:52+00:00 app[web.1]:
2011-11-13T12:57:52+00:00 app[web.1]:
2011-11-13T12:57:52+00:00 app[web.1]:
2011-11-13T12:57:52+00:00 app[web.1]:
2011-11-13T12:57:52+00:00 app[web.1]:
2011-11-13T12:57:52+00:00 app[web.1]: OAUTH2#CALLBACK PHASE
2011-11-13T12:57:52+00:00 app[web.1]: body #
2011-11-13T12:57:52+00:00 app[web.1]: params {"code"=>"1ef746512988a4d76ad1"}
2011-11-13T12:57:52+00:00 app[web.1]: error
2011-11-13T12:57:52+00:00 app[web.1]: reason
2011-11-13T12:57:52+00:00 app[web.1]: 1
2011-11-13T12:57:52+00:00 app[web.1]: CLIENT #GET_TOKEN
2011-11-13T12:57:52+00:00 heroku[router]: GET app.lingui.st/auth/github/callback dyno=web.1 queue=0 wait=0ms service=87ms status=302 bytes=20
2011-11-13T12:57:52+00:00 heroku[nginx]: 127.0.0.1 - - [13/Nov/2011:04:57:52 -0800] "GET /auth/github/callback?code=1ef944212988a4d76ad1 HTTP/1.0" 302 20 "https://github.com/login/oauth/authorize?response_type=code&client_id=af123f0e0b1372e94367&redirect_uri=https%3A%2F%2Fapp.lingui.st%2Fauth%2Fgithub%2Fcallback&scope=repo" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2" app.lingui.st
2011-11-13T12:57:52+00:00 heroku[nginx]: 10.202.85.124 - - [13/Nov/2011:04:57:52 -0800] "GET /auth/github/callback?code=1ef746512988a4d76ad1 HTTP/1.1" 302 20 "https://github.com/login/oauth/authorize?response_type=code&client_id=af023f342a1372e94367&redirect_uri=https%3A%2F%2Fapp.lingui.st%2Fauth%2Fgithub%2Fcallback&scope=repo" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2" app.lingui.st
2011-11-13T12:57:52+00:00 app[web.1]:
2011-11-13T12:57:52+00:00 app[web.1]:
2011-11-13T12:57:52+00:00 app[web.1]: Started GET "/auth/failure?message=invalid_credentials" for 10.202.85.124 at 2011-11-13 12:57:52 +0000
2011-11-13T12:57:52+00:00 app[web.1]: params {"message"=>"invalid_credentials", "controller"=>"omni_auth", "action"=>"failure"}
2011-11-13T12:57:52+00:00 app[web.1]: FAILURE FAILURE ''
2011-11-13T12:57:52+00:00 app[web.1]: cache: [GET /auth/failure?message=invalid_credentials] miss
2011-11-13T12:57:52+00:00 heroku[router]: GET app.lingui.st/auth/failure dyno=web.1 queue=0 wait=0ms service=19ms status=302 bytes=113
2011-11-13T12:57:52+00:00 heroku[nginx]: 127.0.0.1 - - [13/Nov/2011:04:57:52 -0800] "GET /auth/failure?message=invalid_credentials HTTP/1.0" 302 113 "https://github.com/login/oauth/authorize?response_type=code&client_id=af023f0e0b1123e94367&redirect_uri=https%3A%2F%2Fapp.lingui.st%2Fauth%2Fgithub%2Fcallback&scope=repo" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2" app.lingui.st
2011-11-13T12:57:52+00:00 heroku[nginx]: 10.202.85.124 - - [13/Nov/2011:04:57:52 -0800] "GET /auth/failure?message=invalid_credentials HTTP/1.1" 302 113 "https://github.com/login/oauth/authorize?response_type=code&client_id=af023f0e012332e94367&redirect_uri=https%3A%2F%2Fapp.lingui.st%2Fauth%2Fgithub%2Fcallback&scope=repo" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2" app.lingui.st
2011-11-13T12:57:52+00:00 app[web.1]:
2011-11-13T12:57:52+00:00 app[web.1]:
2011-11-13T12:57:52+00:00 app[web.1]: Started GET "/" for 10.202.85.124 at 2011-11-13 12:57:52 +0000
2011-11-13T12:58:00+00:00 heroku[nginx]: 127.0.0.1 - - [13/Nov/2011:04:58:00 -0800] "GET / HTTP/1.0" 200 2952 "https://github.com/login/oauth/authorize?response_type=code&client_id=af023f0e0b23452e94367&redirect_uri=https%3A%2F%2Fapp.lingui.st%2Fauth%2Fgithub%2Fcallback&scope=repo" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2" app.lingui.st
2011-11-13T12:58:00+00:00 heroku[nginx]: 10.202.85.124 - - [13/Nov/2011:04:58:00 -0800] "GET / HTTP/1.1" 200 2952 "https://github.com/login/oauth/authorize?response_type=code&client_id=af023f0e0b243432e94367&redirect_uri=https%3A%2F%2Fapp.lingui.st%2Fauth%2Fgithub%2Fcallback&scope=repo" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2" app.lingui.st
2011-11-13T12:58:00+00:00 app[web.1]: cache: [GET /] miss
```