Permalink
Browse files

added oauth2 support to the consumer

  • Loading branch information...
1 parent 6d60a45 commit c5fb872d4807ad58f0374648fd3caba05d60464c @pelle committed Mar 22, 2011
@@ -6,13 +6,22 @@
# OAUTH_CREDENTIALS={
# :twitter=>{
# :key=>"",
-# :secret=>""
+# :secret=>"",
+# :allow_login => true # Use :allow_login => true to allow user to login to account
# },
# :google=>{
# :key=>"",
# :secret=>"",
# :scope=>"" # see http://code.google.com/apis/gdata/faq.html#AuthScopes
# },
+# :github=>{
+# :key => "",
+# :secret => ""
+# },
+# :facebook=>{
+# :key => "",
+# :secret => ""
+# },
# :agree2=>{
# :key=>"",
# :secret=>""
@@ -21,6 +30,14 @@
# :key=>"",
# :secret=>""
# },
+# :oauth2_server => {
+# :key=>"",
+# :secret=>"",
+# :oauth_version => 2
+# :options=>{ # OAuth::Consumer options
+# :site=>"http://hourfeed.com" # Remember to add a site for a generic OAuth site
+# }
+# },
# :hour_feed=>{
# :key=>"",
# :secret=>"",
@@ -20,17 +20,19 @@ class ConsumerToken
embedded_in :user, :inverse_of => :consumer_tokens
def self.find_or_create_from_access_token(user,access_token)
+ secret = access_token.respond_to?(:secret) ? access_token.secret : nil
+
if user
user.consumer_tokens.first(:conditions=>{:_type=>self.to_s,:token=>access_token.token}) ||
- user.consumer_tokens.create!(:_type=>self.to_s,:token=>access_token.token, :secret=>access_token.secret)
+ self.create!(:_type=>self.to_s,:token=>access_token.token, :secret=>secret, :user=>user)
else
# Is there a better way of doing this in mongoid?
user = User.first(:conditions=>{"consumer_tokens._type"=>self.to_s,"consumer_tokens.token"=>access_token.token})
if user
user.consumer_tokens.detect{|t| t.token==access_token.token && t.is_a?(self)}
else
user = User.new
- user.consumer_tokens.create!(:_type=>self.to_s,:token=>access_token.token, :secret=>access_token.secret)
+ self.create!(:_type=>self.to_s,:token=>access_token.token, :secret=>secret, :user=>user)
user.save!
user.consumer_tokens.last
end
@@ -19,7 +19,10 @@ def copy_controller
def add_route
route <<-ROUTE.strip
resources :oauth_consumers do
- get :callback, :on => :member
+ member do
+ get :callback
+ get :callback2
+ end
end
ROUTE
end
@@ -14,6 +14,14 @@
# :secret=>"",
# :scope=>"" # see http://code.google.com/apis/gdata/faq.html#AuthScopes
# },
+# :github=>{
+# :key => "",
+# :secret => ""
+# },
+# :facebook=>{
+# :key => "",
+# :secret => ""
+# },
# :agree2=>{
# :key=>"",
# :secret=>""
@@ -22,6 +30,14 @@
# :key=>"",
# :secret=>""
# },
+# :oauth2_server => {
+# :key=>"",
+# :secret=>"",
+# :oauth_version => 2
+# :options=>{ # OAuth::Consumer options
+# :site=>"http://hourfeed.com" # Remember to add a site for a generic OAuth site
+# }
+# },
# :hour_feed=>{
# :key=>"",
# :secret=>"",
@@ -1,5 +1,5 @@
module Oauth
module Plugin
- VERSION = "0.4.0.pre4"
+ VERSION = "0.4.0.pre5"
end
end
@@ -18,15 +18,38 @@ def index
# If user is already connected it displays a page with an option to disconnect and redo
def show
unless @token
- @request_token=@consumer.get_request_token(callback_oauth_consumer_url(params[:id]))
- session[@request_token.token]=@request_token.secret
- if @request_token.callback_confirmed?
- redirect_to @request_token.authorize_url
+ if @consumer.ancestors.include?(Oauth2Token)
+ redirect_to @consumer.authorize_url(callback2_oauth_consumer_url(params[:id]))
else
- redirect_to(@request_token.authorize_url + "&oauth_callback=#{callback_oauth_consumer_url(params[:id])}")
+ @request_token=@consumer.get_request_token(callback_oauth_consumer_url(params[:id]))
+ session[@request_token.token]=@request_token.secret
+ if @request_token.callback_confirmed?
+ redirect_to @request_token.authorize_url
+ else
+ redirect_to(@request_token.authorize_url + "&oauth_callback=#{callback_oauth_consumer_url(params[:id])}")
+ end
end
end
end
+
+ def callback2
+ @token = @consumer.access_token(current_user,params[:code], callback2_oauth_consumer_url(params[:id]))
+ logger.info @token.inspect
+ if @token
+ # Log user in
+ if logged_in?
+ flash[:notice] = "#{params[:id].humanize} was successfully connected to your account"
+ else
+ self.current_user = @token.user
+ flash[:notice] = "You logged in with #{params[:id].humanize}"
+ end
+ go_back
+ else
+ flash[:error] = "An error happened, please try connecting again"
+ redirect_to oauth_consumer_url(params[:id])
+ end
+
+ end
def callback
logger.info "CALLBACK"
@@ -6,6 +6,8 @@
# So an entry called "my_service" will create a class MyServiceToken which you can
# connect with has_one to your user model.
if defined? ConsumerToken && defined? OAUTH_CREDENTIALS
+ require File.join(File.dirname(__FILE__), 'services', 'oauth2_token')
+
OAUTH_CREDENTIALS.each do |key, value|
class_name=value[:class_name]||"#{key.to_s.classify}Token"
unless Object.const_defined?(class_name.to_sym)
@@ -17,7 +19,7 @@
# Let Rails auto-load from the models folder
eval class_name
rescue NameError
- super_class = value[:super_class]||"ConsumerToken"
+ super_class = value[:super_class]||value[:oauth_version].to_i>=2 ? "Oauth2Token" : "ConsumerToken"
eval "class #{class_name} < #{super_class} ;end"
end
end
@@ -0,0 +1,27 @@
+require 'oauth2'
+class Oauth2Token < ConsumerToken
+
+ def self.consumer
+ @consumer||=create_consumer
+ end
+
+ def self.create_consumer(options={})
+ @consumer||=OAuth2::Client.new credentials[:key],credentials[:secret],credentials[:options]
+ end
+
+ def self.authorize_url(callback_url)
+ options = {:redirect_uri=>callback_url}
+ options[:scope] = credentials[:scope] if credentials[:scope].present?
+ consumer.web_server.authorize_url(options)
+ end
+
+ def self.access_token(user, code, redirect_uri)
+ access_token = consumer.web_server.get_access_token(code, :redirect_uri => redirect_uri)
+ find_or_create_from_access_token user, access_token
+ end
+
+ def client
+ @client ||= OAuth2::AccessToken.new self.class.consumer, token
+ end
+
+end
@@ -7,7 +7,7 @@ module Consumers
module Token
def self.included(model)
model.class_eval do
- validates_presence_of :user, :token, :secret
+ validates_presence_of :user, :token
end
model.send(:include, InstanceMethods)
@@ -26,7 +26,6 @@ def consumer
end
def get_request_token(callback_url)
- Rails.logger.info "OAUTH_CONSUMER #{consumer.inspect}"
consumer.get_request_token(:oauth_callback=>callback_url)
end
@@ -39,12 +38,13 @@ def find_or_create_from_request_token(user,token,secret,oauth_verifier)
end
def find_or_create_from_access_token(user,access_token)
+ secret = access_token.respond_to?(:secret) ? access_token.secret : nil
if user
user.consumer_tokens.first(:conditions=>{:type=>self.to_s,:token=>access_token.token}) ||
- user.consumer_tokens.create!(:type=>self.to_s,:token=>access_token.token, :secret=>access_token.secret)
+ self.create!(:user => user,:token=>access_token.token, :secret=>secret)
else
ConsumerToken.first( :conditions =>{ :token=>access_token.token,:type=>self.to_s}) ||
- create(:type=>self.to_s,:token=>access_token.token, :secret=>access_token.secret)
+ self.create!(:type=>self.to_s,:token=>access_token.token, :secret=>secret)
end
end
View
@@ -31,8 +31,10 @@ Gem::Specification.new do |s|
s.add_development_dependency "guard-rspec"
s.add_development_dependency "growl"
s.add_development_dependency "rack-test"
+
s.add_dependency "multi_json"
s.add_dependency("oauth", ["~> 0.4.4"])
s.add_dependency("rack")
+ s.add_dependency("oauth2")
end

0 comments on commit c5fb872

Please sign in to comment.