Sha256: 8f0b212d28ab3ecd8db17abcc381c0a5affbe336699a2db2bb533f19fce36bfa

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

Warden::Strategies.add(:github) do
  # Need to make sure that we have a pure representation of the query string.
  # Rails adds an "action" parameter which causes the openid gem to error
  def params
    @params ||= Rack::Utils.parse_query(request.query_string)
  end

  def authenticate!
    if params['code']
      begin
        access_token = access_token_for(params['code'])
        user = JSON.parse(access_token.get('/api/v2/json/user/show'))
        success!(Warden::Github::Oauth::User.new(user['user'], access_token.token))
      rescue OAuth2::Error
        %(<p>Outdated ?code=#{params['code']}:</p><p>#{$!}</p><p><a href="/auth/github">Retry</a></p>)
      end
    else
      env['rack.session']['return_to'] = env['REQUEST_URI']
      throw(:warden, [ 302, {'Location' => authorize_url}, [ ]])
    end
  end

  private

  def oauth_client
    oauth_proxy.client
  end

  def authorize_url
    oauth_proxy.authorize_url
  end

  def access_token_for(code)
    oauth_proxy.access_token_for(code)
  end

  def oauth_proxy
    @oauth_proxy ||= Warden::Github::Oauth::Proxy.new(env['warden'].config[:github_client_id],
                                                      env['warden'].config[:github_secret],
                                                      env['warden'].config[:github_scopes],
                                                      callback_url)
  end

  def callback_url
    absolute_url(request, env['warden'].config[:github_callback_url])
  end

  def absolute_url(request, suffix = nil)
    port_part = case request.scheme
                when "http"
                  request.port == 80 ? "" : ":#{request.port}"
                when "https"
                  request.port == 443 ? "" : ":#{request.port}"
                end
    "#{request.scheme}://#{request.host}#{port_part}#{suffix}"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
warden-github-0.2.1 lib/warden-github/strategy.rb