Skip to content

Instantly share code, notes, and snippets.

@nov
Created September 8, 2010 04:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nov/569650 to your computer and use it in GitHub Desktop.
Save nov/569650 to your computer and use it in GitHub Desktop.
## OpenID/OAuth Hybrid Sample using rack-openid
require "rubygems"
require "sinatra"
use Rack::Session::Cookie
require "rack/openid"
use Rack::OpenID
require "oauth"
get '/login/with_oauth/google' do
oauth_options = {
:consumer_key => "your_google_oauth_consumer_key",
:consumer_secret => "your_google_oauth_consumer_key",
:scope => ["https://www.google.com/m8/feeds/", "https://mail.google.com/mail/feed/atom/"]
}
if resp = request.env["rack.openid.response"]
if resp.status == :success
oauth_response = OpenID::OAuth::Response.from_success_response(resp)
p oauth_response
if oauth_response.request_token
consumer = OAuth::Consumer.new(
oauth_options[:consumer_key],
oauth_options[:consumer_secret],
:site => 'https://www.google.com',
:access_token_path => '/accounts/OAuthGetAccessToken'
)
request_token = OAuth::RequestToken.new(
consumer,
oauth_response.request_token,
"" # OAuth request token secret is also blank in OpenID/OAuth Hybrid
)
access_token = request_token.get_access_token
<<-RESPONSE
<dl>
<dt>Identifier:</dt>
<dd>#{resp.display_identifier}</dd>
<dt>Authorized Request Token:</dt>
<dd>#{request_token.token}</dd>
<dt>Access Token</dt>
<dd>#{access_token.token}</dd>
<dt>Access Token Secret</dt>
<dd>#{access_token.secret}</dd>
</dl>
RESPONSE
else
<<-RESPONSE
<dl>
<dt>Identifier:</dt>
<dd>#{resp.display_identifier}</dd>
<dt>Authorized Request Token:</dt>
<dd>access denied..</dd>
</dl>
RESPONSE
end
else
"Error: #{resp.status}"
end
else
headers 'WWW-Authenticate' => Rack::OpenID.build_header(
:identifier => "https://www.google.com/accounts/o8/id",
:"oauth[consumer]" => oauth_options[:consumer_key],
:"oauth[scope]" => oauth_options[:scope]
)
throw :halt, [401, 'got openid?']
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment