Skip to content

Commit

Permalink
Working version which at least produces @raw_info
Browse files Browse the repository at this point in the history
  • Loading branch information
kisonecat committed Dec 23, 2012
1 parent 6165a90 commit 8231c82
Showing 1 changed file with 61 additions and 3 deletions.
64 changes: 61 additions & 3 deletions lib/omniauth-coursera.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Coursera < OmniAuth::Strategies::OAuth
:site => 'https://authentication.coursera.org',
:request_token_path => '/auth/oauth/api/request_token',
:access_token_path => '/auth/oauth/api/access_token',
:authorize_url => 'https://authentication.coursera.org/auth/oauth/api/'
:authorize_url => nil # this will be set during the request_phase
}

option :fields, ["id"]
Expand All @@ -35,9 +35,67 @@ class Coursera < OmniAuth::Strategies::OAuth
}
end

def request_phase
request_token = consumer.get_request_token({:oauth_callback => callback_url}, options.request_params)
session['oauth'] ||= {}

token = request_token.params['oauth_token']
secret = request_token.params['oauth_secret']

session['oauth'][name.to_s] = {
'callback_confirmed' => request_token.callback_confirmed?,
'request_token' => token,
'request_secret' => secret}

authentication_url = request_token.params['authentication_url']
params = { :oauth_token => request_token.token }

uri = URI.parse(authentication_url)
uri.query = params.map { |k,v| [k, CGI.escape(v)] * "=" } * "&"
Rails.logger.debug "Redirecting to #{uri.to_s}"
redirect uri.to_s

rescue ::Timeout::Error => e
fail!(:timeout, e)
rescue ::Net::HTTPFatalError, ::OpenSSL::SSL::SSLError => e
fail!(:service_unavailable, e)
end


def callback_phase
raise OmniAuth::NoSessionError.new("Session Expired") if session['oauth'].nil?

puts "session['oauth'] = #{session['oauth']}"
request_token = ::OAuth::RequestToken.new(consumer, session['oauth'][name.to_s].delete('request_token'), session['oauth'][name.to_s].delete('request_secret'))

puts "request_token.params.inspect = #{request_token.params.inspect}"
puts "request_token.token = #{request_token.token}"
puts "request_token.secret = #{request_token.secret}"

opts = {}
opts[:oauth_verifier] = request['oauth_verifier']

@access_token = request_token.get_access_token(opts)
puts @access_token.token
puts @access_token.secret

self.env['omniauth.auth'] = auth_hash
call_app!

rescue ::Timeout::Error => e
fail!(:timeout, e)
rescue ::Net::HTTPFatalError, ::OpenSSL::SSL::SSLError => e
fail!(:service_unavailable, e)
rescue ::OAuth::Unauthorized => e
fail!(:invalid_credentials, e)
rescue ::MultiJson::DecodeError => e
fail!(:invalid_response, e)
rescue ::OmniAuth::NoSessionError => e
fail!(:session_expired, e)
end

def raw_info
@raw_info ||= MultiJson.decode(access_token.get('/auth/oauth/api/get_identity')).body
puts "################################################################"
@raw_info ||= JSON.parse(access_token.get('/auth/oauth/api/get_identity').body)
puts "@raw_info = #{@raw_info}"
end
end
Expand Down

0 comments on commit 8231c82

Please sign in to comment.