Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

removed explicit save on validation to make compatable with mongomapper

  • Loading branch information...
commit 545897e5af98882be85fb84b64fb86e49b864de0 1 parent e4de9e8
bhbryant authored
View
86 generators/devise_oauth2_authenticatable/devise_oauth2_authenticatable_generator.rb
@@ -1,44 +1,44 @@
-# encoding: utf-8
-
-class DeviseOauth2AuthenticatableGenerator < Rails::Generator::Base #:nodoc:
-
- default_options :client_id => "YOUR_APP_API_ID",
- :client_key => "YOUR_APP_SECRET_KEY",
- :auth_server => "https://graph.facebook.com",
- :requested_scope => "email,offline_access,publish_stream"
-
- def manifest
- record do |m|
- # m.dependency 'xd_receiver', [], options.merge(:collision => :skip)
- m.template 'oauth2_config.yml', File.join(*%w[config oauth2_config.yml])
- # m.template 'devise.facebook_connectable.js', File.join(*%w[public javascripts devise.facebook_connectable.js])
- end
- end
-
- protected
-
- def add_options!(opt)
- opt.separator ''
- opt.separator 'Options:'
-
- opt.on('--id CLIENT_ID', "Application API ID.") do |v|
- options[:client_id] = v if v.present?
- end
-
- opt.on('--key SECRET_KEY', "Application Secret key.") do |v|
- options[:client_key] = v if v.present?
- end
- opt.on('--server AUTH_SERVER', "Authentication Server.") do |v|
- options[:auth_server] = v if v.present?
- end
- opt.on('--resources REQUESTED_RESOURCES', "Requested Resources.") do |v|
- options[:requested_scope] = v if v.present?
- end
- end
-
-
- def banner
- "Usage: #{$0} devise_oauth2_authenticatable [--id API_ID] [--key SECRET_KEY] [--server AUTH_SERVER] [--scope REQUESTED RESOURCES]"
- end
-
+# encoding: utf-8
+
+class DeviseOauth2AuthenticatableGenerator < Rails::Generator::Base #:nodoc:
+
+ default_options :client_id => "YOUR_APP_API_ID",
+ :client_key => "YOUR_APP_SECRET_KEY",
+ :auth_server => "https://graph.facebook.com",
+ :requested_scope => "email,offline_access,publish_stream"
+
+ def manifest
+ record do |m|
+ # m.dependency 'xd_receiver', [], options.merge(:collision => :skip)
+ m.template 'oauth2_config.yml', File.join(*%w[config oauth2_config.yml])
+ # m.template 'devise.facebook_connectable.js', File.join(*%w[public javascripts devise.facebook_connectable.js])
+ end
+ end
+
+ protected
+
+ def add_options!(opt)
+ opt.separator ''
+ opt.separator 'Options:'
+
+ opt.on('--id CLIENT_ID', "Application API ID.") do |v|
+ options[:client_id] = v if v.present?
+ end
+
+ opt.on('--key SECRET_KEY', "Application Secret key.") do |v|
+ options[:client_key] = v if v.present?
+ end
+ opt.on('--server AUTH_SERVER', "Authentication Server.") do |v|
+ options[:auth_server] = v if v.present?
+ end
+ opt.on('--resources REQUESTED_RESOURCES', "Requested Resources.") do |v|
+ options[:requested_scope] = v if v.present?
+ end
+ end
+
+
+ def banner
+ "Usage: #{$0} devise_oauth2_authenticatable [--id API_ID] [--key SECRET_KEY] [--server AUTH_SERVER] [--scope REQUESTED RESOURCES]"
+ end
+
end
View
44 generators/devise_oauth2_authenticatable/templates/oauth2_config.yml
@@ -1,23 +1,23 @@
-defaults: &defaults
- # Required.
- client_id: <%= options[:client_id] %>
- client_secret: <%= options[:client_key] %>
- authorization_server: <%= options[:auth_server] %>
- requested_scope: <%= options[:requested_scope] %>
-
-
-
-development:
- <<: *defaults
-
-test: &test
- <<: *defaults
-
-production: &production
- <<: *defaults
-
-# staging:
-# <<: *production
-#
-# cucumber:
+defaults: &defaults
+ # Required.
+ client_id: <%= options[:client_id] %>
+ client_secret: <%= options[:client_key] %>
+ authorization_server: <%= options[:auth_server] %>
+ requested_scope: <%= options[:requested_scope] %>
+
+
+
+development:
+ <<: *defaults
+
+test: &test
+ <<: *defaults
+
+production: &production
+ <<: *defaults
+
+# staging:
+# <<: *production
+#
+# cucumber:
# <<: *test
View
116 lib/devise_oauth2_authenticatable.rb
@@ -1,59 +1,59 @@
-# encoding: utf-8
-require 'devise'
-require 'oauth2'
-
-
-require 'devise_oauth2_authenticatable/model'
-require 'devise_oauth2_authenticatable/strategy'
-require 'devise_oauth2_authenticatable/schema'
-require 'devise_oauth2_authenticatable/routes'
-#require 'devise_oauth2_authenticatable/controller_filters'
-require 'devise_oauth2_authenticatable/view_helpers'
-
-
-module Devise
- # Specifies the name of the database column name used for storing
- # the oauth UID. Useful if this info should be saved in a
- # generic column if different authentication solutions are used.
- mattr_accessor :oauth2_uid_field
- @@oauth2_uid_field = :oauth2_uid
-
- # Specifies the name of the database column name used for storing
- # the user Facebook session key. Useful if this info should be saved in a
- # generic column if different authentication solutions are used.
- mattr_accessor :oauth2_token_field
- @@oauth2_token_field = :oauth2_token
-
- # Specifies if account should be created if no account exists for
- # a specified Facebook UID or not.
- mattr_accessor :oauth2_auto_create_account
- @@oauth2_auto_create_account = true
-
- def self.oauth2_client
- @@oauth2_client ||= OAuth2::Client.new(OAUTH2_CONFIG['client_id'], OAUTH2_CONFIG['client_secret'], :site => OAUTH2_CONFIG['authorization_server'])
- end
-
-
- def self.session_sign_in_url(request, mapping)
- url = URI.parse(request.url)
- url.path = "#{mapping.parsed_path}/#{mapping.path_names[:sign_in]}"
- url.query = nil
- url.to_s
- end
-
- def self.requested_scope
- @@requested_scope ||= OAUTH2_CONFIG['requested_scope']
- end
-
-end
-
-# Load core I18n locales: en
-#
-I18n.load_path.unshift File.join(File.dirname(__FILE__), *%w[devise_oauth2_authenticatable locales en.yml])
-
-# Add +:facebook_connectable+ strategies to defaults.
-#
-Devise.add_module(:oauth2_authenticatable,
- :strategy => true,
- :controller => :sessions,
+# encoding: utf-8
+require 'devise'
+require 'oauth2'
+
+
+require 'devise_oauth2_authenticatable/model'
+require 'devise_oauth2_authenticatable/strategy'
+require 'devise_oauth2_authenticatable/schema'
+require 'devise_oauth2_authenticatable/routes'
+#require 'devise_oauth2_authenticatable/controller_filters'
+require 'devise_oauth2_authenticatable/view_helpers'
+
+
+module Devise
+ # Specifies the name of the database column name used for storing
+ # the oauth UID. Useful if this info should be saved in a
+ # generic column if different authentication solutions are used.
+ mattr_accessor :oauth2_uid_field
+ @@oauth2_uid_field = :oauth2_uid
+
+ # Specifies the name of the database column name used for storing
+ # the user Facebook session key. Useful if this info should be saved in a
+ # generic column if different authentication solutions are used.
+ mattr_accessor :oauth2_token_field
+ @@oauth2_token_field = :oauth2_token
+
+ # Specifies if account should be created if no account exists for
+ # a specified Facebook UID or not.
+ mattr_accessor :oauth2_auto_create_account
+ @@oauth2_auto_create_account = true
+
+ def self.oauth2_client
+ @@oauth2_client ||= OAuth2::Client.new(OAUTH2_CONFIG['client_id'], OAUTH2_CONFIG['client_secret'], :site => OAUTH2_CONFIG['authorization_server'])
+ end
+
+
+ def self.session_sign_in_url(request, mapping)
+ url = URI.parse(request.url)
+ url.path = "#{mapping.parsed_path}/#{mapping.path_names[:sign_in]}"
+ url.query = nil
+ url.to_s
+ end
+
+ def self.requested_scope
+ @@requested_scope ||= OAUTH2_CONFIG['requested_scope']
+ end
+
+end
+
+# Load core I18n locales: en
+#
+I18n.load_path.unshift File.join(File.dirname(__FILE__), *%w[devise_oauth2_authenticatable locales en.yml])
+
+# Add +:facebook_connectable+ strategies to defaults.
+#
+Devise.add_module(:oauth2_authenticatable,
+ :strategy => true,
+ :controller => :sessions,
:model => 'devise_oauth2_authenticatable/model')
View
16 lib/devise_oauth2_authenticatable/locales/en.yml
@@ -1,9 +1,9 @@
-en:
- devise:
- sessions:
- oauth2_invalid: "Could not login. Invalid account."
- oauth2_timeout: "OAuth2 session expired., please sign in again to continue."
- oauth2_authenticity_token: "Something went wrong. For security reasons, please sign in again." # Revise this message =)
- oauth2_actions:
- sign_in: "Sign in" # NOTE: Not used for the default Facebook Connect button.
+en:
+ devise:
+ sessions:
+ oauth2_invalid: "Could not login. Invalid account."
+ oauth2_timeout: "OAuth2 session expired., please sign in again to continue."
+ oauth2_authenticity_token: "Something went wrong. For security reasons, please sign in again." # Revise this message =)
+ oauth2_actions:
+ sign_in: "Sign in" # NOTE: Not used for the default Facebook Connect button.
sign_out: "Sign out"
View
392 lib/devise_oauth2_authenticatable/model.rb
@@ -1,197 +1,197 @@
-# encoding: utf-8
-require 'devise/models'
-
-
-module Devise #:nodoc:
- # module OAuth2Authenticatable #:nodoc:
- module Models #:nodoc:
-
- # OAuth2 Connectable Module, responsible for validating authenticity of a
- # user and storing credentials while signing in using their OAuth2 account.
- #
- # == Configuration:
- #
- # You can overwrite configuration values by setting in globally in Devise (+Devise.setup+),
- # using devise method, or overwriting the respective instance method.
- #
- # +oauth2_uid_field+ - Defines the name of the OAuth2 user UID database attribute/column.
- #
- # +oauth2_token_field+ - Defines the name of the OAuth2 session key database attribute/column.
- #
- # +oauth2_auto_create_account+ - Speifies if account should automatically be created upon connect
- # if not already exists.
- #
- # == Examples:
- #
- # User.oauth2_connect(:uid => '123456789') # returns authenticated user or nil
- # User.find(1).oauth2_connected? # returns true/false
- #
- module Oauth2Authenticatable
-
- def self.included(base) #:nodoc:
- base.class_eval do
- extend ClassMethods
- end
- end
-
- # Store OAuth2 Connect account/session credentials.
- #
- def store_oauth2_credentials!(attributes = {})
- self.send(:"#{self.class.oauth2_uid_field}=", attributes[:uid])
- self.send(:"#{self.class.oauth2_token_field}=", attributes[:token])
-
- # Confirm without e-mail - if confirmable module is loaded.
- self.skip_confirmation! if self.respond_to?(:skip_confirmation!)
-
- # Only populate +email+ field if it's available (e.g. if +authenticable+ module is used).
- self.email = attributes[:email] || '' if self.respond_to?(:email)
-
- # Lazy hack: These database fields are required if +authenticable+/+confirmable+
- # module(s) is used. Could be avoided with :null => true for authenticatable
- # migration, but keeping this to avoid unnecessary problems.
- self.password_salt = '' if self.respond_to?(:password_salt)
- self.encrypted_password = '' if self.respond_to?(:encrypted_password)
- end
-
- # Checks if OAuth2 Connected.
- #
- def oauth2_connected?
- self.send(:"#{self.class.oauth2_uid_field}").present?
- end
- alias :is_oauth2_connected? :oauth2_connected?
-
- # Hook that gets called *before* connect (only at creation). Useful for
- # specifiying additional user info (etc.) from OAuth2.
- #
- # Default: Do nothing.
- #
- # == Examples:
- #
- # # Overridden in OAuth2 Connect:able model, e.g. "User".
- # #
- # def before_oauth2_auto_create(oauth2_user_attributes)
-
- # self.profile.first_name = oauth2_user_attributes.first_name
-
- #
- # end
- #
- # == For more info:
- #
- # * http://oauth2er.pjkh.com/user/populate
- #
- def on_before_oauth2_auto_create(oauth2_user_attributes)
-
- if self.respond_to?(:before_oauth2_auto_create)
- self.send(:before_oauth2_auto_create, oauth2_user_attributes) rescue nil
- end
- end
-
- # Hook that gets called *after* a connection (each time). Useful for
- # fetching additional user info (etc.) from OAuth2.
- #
- # Default: Do nothing.
- #
- # == Example:
- #
- # # Overridden in OAuth2 Connect:able model, e.g. "User".
- # #
- # def after_oauth2_connect(oauth2_user_attributes)
- # # See "on_before_oauth2_connect" example.
- # end
- #
- def on_after_oauth2_connect(oauth2_user_attributes)
-
- if self.respond_to?(:after_oauth2_auto_create)
- self.send(:after_oauth2_auto_create, oauth2_user_attributes) rescue nil
- end
- end
-
- # Optional: Store session key.
- #
- def store_session(using_token)
- if self.token != using_token
- self.update_attribute(self.send(:"#{self.class.oauth2_token_field}"), using_token)
- end
- end
-
- protected
-
- # Passwords are always required if it's a new rechord and no oauth_id exists, or if the password
- # or confirmation are being set somewhere.
- def password_required?
-
- ( new_record? && oauth2_uid.nil? ) || !password.nil? || !password_confirmation.nil?
- end
-
- module ClassMethods
-
- # Configuration params accessible within +Devise.setup+ procedure (in initalizer).
- #
- # == Example:
- #
- # Devise.setup do |config|
- # config.oauth2_uid_field = :oauth2_uid
- # config.oauth2_token_field = :oauth2_token
- # config.oauth2_auto_create_account = true
- # end
- #
- ::Devise::Models.config(self,
- :oauth2_uid_field,
- :oauth2_token_field,
- :oauth2_auto_create_account
- )
-
- # Alias don't work for some reason, so...a more Ruby-ish alias
- # for +oauth2_auto_create_account+.
- #
- def oauth2_auto_create_account?
- self.oauth2_auto_create_account
- end
-
- # Authenticate a user based on OAuth2 UID.
- #
- def authenticate_with_oauth2(oauth2_id, oauth2_token)
-
- # find user and update access token
- returning(self.find_for_oauth2(oauth2_id)) do |user|
- user.update_attributes(:oauth2_token => oauth2_token) unless user.nil?
- end
-
- end
-
- protected
-
-
-
- # Find first record based on conditions given (OAuth2 UID).
- # Overwrite to add customized conditions, create a join, or maybe use a
- # namedscope to filter records while authenticating.
- #
- # == Example:
- #
- # def self.find_for_oauth2(uid, conditions = {})
- # conditions[:active] = true
- # self.find_by_oauth2_uid(uid, :conditions => conditions)
- # end
- #
- def find_for_oauth2(uid, conditions = {})
-
- self.find_by_oauth2_uid(uid, :conditions => conditions)
- end
-
-
-
- # Contains the logic used in authentication. Overwritten by other devise modules.
- # In the OAuth2 Connect case; nothing fancy required.
- #
- def valid_for_oauth2(resource, attributes)
- true
- end
-
- end
-
- end
- end
- # end
+# encoding: utf-8
+require 'devise/models'
+
+
+module Devise #:nodoc:
+ # module OAuth2Authenticatable #:nodoc:
+ module Models #:nodoc:
+
+ # OAuth2 Connectable Module, responsible for validating authenticity of a
+ # user and storing credentials while signing in using their OAuth2 account.
+ #
+ # == Configuration:
+ #
+ # You can overwrite configuration values by setting in globally in Devise (+Devise.setup+),
+ # using devise method, or overwriting the respective instance method.
+ #
+ # +oauth2_uid_field+ - Defines the name of the OAuth2 user UID database attribute/column.
+ #
+ # +oauth2_token_field+ - Defines the name of the OAuth2 session key database attribute/column.
+ #
+ # +oauth2_auto_create_account+ - Speifies if account should automatically be created upon connect
+ # if not already exists.
+ #
+ # == Examples:
+ #
+ # User.oauth2_connect(:uid => '123456789') # returns authenticated user or nil
+ # User.find(1).oauth2_connected? # returns true/false
+ #
+ module Oauth2Authenticatable
+
+ def self.included(base) #:nodoc:
+ base.class_eval do
+ extend ClassMethods
+ end
+ end
+
+ # Store OAuth2 Connect account/session credentials.
+ #
+ def store_oauth2_credentials!(attributes = {})
+ self.send(:"#{self.class.oauth2_uid_field}=", attributes[:uid])
+ self.send(:"#{self.class.oauth2_token_field}=", attributes[:token])
+
+ # Confirm without e-mail - if confirmable module is loaded.
+ self.skip_confirmation! if self.respond_to?(:skip_confirmation!)
+
+ # Only populate +email+ field if it's available (e.g. if +authenticable+ module is used).
+ self.email = attributes[:email] || '' if self.respond_to?(:email)
+
+ # Lazy hack: These database fields are required if +authenticable+/+confirmable+
+ # module(s) is used. Could be avoided with :null => true for authenticatable
+ # migration, but keeping this to avoid unnecessary problems.
+ self.password_salt = '' if self.respond_to?(:password_salt)
+ self.encrypted_password = '' if self.respond_to?(:encrypted_password)
+ end
+
+ # Checks if OAuth2 Connected.
+ #
+ def oauth2_connected?
+ self.send(:"#{self.class.oauth2_uid_field}").present?
+ end
+ alias :is_oauth2_connected? :oauth2_connected?
+
+ # Hook that gets called *before* connect (only at creation). Useful for
+ # specifiying additional user info (etc.) from OAuth2.
+ #
+ # Default: Do nothing.
+ #
+ # == Examples:
+ #
+ # # Overridden in OAuth2 Connect:able model, e.g. "User".
+ # #
+ # def before_oauth2_auto_create(oauth2_user_attributes)
+
+ # self.profile.first_name = oauth2_user_attributes.first_name
+
+ #
+ # end
+ #
+ # == For more info:
+ #
+ # * http://oauth2er.pjkh.com/user/populate
+ #
+ def on_before_oauth2_auto_create(oauth2_user_attributes)
+
+ if self.respond_to?(:before_oauth2_auto_create)
+ self.send(:before_oauth2_auto_create, oauth2_user_attributes) rescue nil
+ end
+ end
+
+ # Hook that gets called *after* a connection (each time). Useful for
+ # fetching additional user info (etc.) from OAuth2.
+ #
+ # Default: Do nothing.
+ #
+ # == Example:
+ #
+ # # Overridden in OAuth2 Connect:able model, e.g. "User".
+ # #
+ # def after_oauth2_connect(oauth2_user_attributes)
+ # # See "on_before_oauth2_connect" example.
+ # end
+ #
+ def on_after_oauth2_connect(oauth2_user_attributes)
+
+ if self.respond_to?(:after_oauth2_auto_create)
+ self.send(:after_oauth2_auto_create, oauth2_user_attributes) rescue nil
+ end
+ end
+
+ # Optional: Store session key.
+ #
+ def store_session(using_token)
+ if self.token != using_token
+ self.update_attribute(self.send(:"#{self.class.oauth2_token_field}"), using_token)
+ end
+ end
+
+ protected
+
+ # Passwords are always required if it's a new rechord and no oauth_id exists, or if the password
+ # or confirmation are being set somewhere.
+ def password_required?
+
+ ( new_record? && oauth2_uid.nil? ) || !password.nil? || !password_confirmation.nil?
+ end
+
+ module ClassMethods
+
+ # Configuration params accessible within +Devise.setup+ procedure (in initalizer).
+ #
+ # == Example:
+ #
+ # Devise.setup do |config|
+ # config.oauth2_uid_field = :oauth2_uid
+ # config.oauth2_token_field = :oauth2_token
+ # config.oauth2_auto_create_account = true
+ # end
+ #
+ ::Devise::Models.config(self,
+ :oauth2_uid_field,
+ :oauth2_token_field,
+ :oauth2_auto_create_account
+ )
+
+ # Alias don't work for some reason, so...a more Ruby-ish alias
+ # for +oauth2_auto_create_account+.
+ #
+ def oauth2_auto_create_account?
+ self.oauth2_auto_create_account
+ end
+
+ # Authenticate a user based on OAuth2 UID.
+ #
+ def authenticate_with_oauth2(oauth2_id, oauth2_token)
+
+ # find user and update access token
+ returning(self.find_for_oauth2(oauth2_id)) do |user|
+ user.update_attributes(:oauth2_token => oauth2_token) unless user.nil?
+ end
+
+ end
+
+ protected
+
+
+
+ # Find first record based on conditions given (OAuth2 UID).
+ # Overwrite to add customized conditions, create a join, or maybe use a
+ # namedscope to filter records while authenticating.
+ #
+ # == Example:
+ #
+ # def self.find_for_oauth2(uid, conditions = {})
+ # conditions[:active] = true
+ # self.find_by_oauth2_uid(uid, :conditions => conditions)
+ # end
+ #
+ def find_for_oauth2(uid, conditions = {})
+
+ self.find_by_oauth2_uid(uid, :conditions => conditions)
+ end
+
+
+
+ # Contains the logic used in authentication. Overwritten by other devise modules.
+ # In the OAuth2 Connect case; nothing fancy required.
+ #
+ def valid_for_oauth2(resource, attributes)
+ true
+ end
+
+ end
+
+ end
+ end
+ # end
end
View
20 lib/devise_oauth2_authenticatable/routes.rb
@@ -1,11 +1,11 @@
-# encoding: utf-8
-
-ActionController::Routing::RouteSet::Mapper.class_eval do
-
- protected
-
- # Setup routes for +OAuth2SessionsController+.
- #
- alias :oauth2_authenticatable :database_authenticatable
-
+# encoding: utf-8
+
+ActionController::Routing::RouteSet::Mapper.class_eval do
+
+ protected
+
+ # Setup routes for +OAuth2SessionsController+.
+ #
+ alias :oauth2_authenticatable :database_authenticatable
+
end
View
42 lib/devise_oauth2_authenticatable/schema.rb
@@ -1,22 +1,22 @@
-# encoding: utf-8
-require 'devise/schema'
-
-module Devise #:nodoc:
- module Oauth2Authenticatable #:nodoc:
-
- module Schema
-
- # Database migration schema for Facebook Connect.
- #
- def oauth2_authenticatable
- apply_schema ::Devise.oauth2_uid_field, Integer, :limit => 8 # BIGINT unsigned / 64-bit int
- apply_schema ::Devise.oauth2_token_field, String, :limit => 149 # [128][1][20] chars
- end
-
- end
- end
-end
-
-Devise::Schema.module_eval do
- include ::Devise::Oauth2Authenticatable::Schema
+# encoding: utf-8
+require 'devise/schema'
+
+module Devise #:nodoc:
+ module Oauth2Authenticatable #:nodoc:
+
+ module Schema
+
+ # Database migration schema for Facebook Connect.
+ #
+ def oauth2_authenticatable
+ apply_schema ::Devise.oauth2_uid_field, Integer, :limit => 8 # BIGINT unsigned / 64-bit int
+ apply_schema ::Devise.oauth2_token_field, String, :limit => 149 # [128][1][20] chars
+ end
+
+ end
+ end
+end
+
+Devise::Schema.module_eval do
+ include ::Devise::Oauth2Authenticatable::Schema
end
View
202 lib/devise_oauth2_authenticatable/strategy.rb
@@ -1,102 +1,102 @@
-# encoding: utf-8
-require 'devise/strategies/base'
-
-
-module Devise #:nodoc:
- module Oauth2Authenticatable #:nodoc:
- module Strategies #:nodoc:
-
- # Default strategy for signing in a user using Facebook Connect (a Facebook account).
- # Redirects to sign_in page if it's not authenticated
- #
- class Oauth2Authenticatable < ::Devise::Strategies::Base
-
-
-
- # Without a oauth session authentication cannot proceed.
- #
- def valid?
-
- valid_controller? && valid_params? && mapping.to.respond_to?('authenticate_with_oauth2')
-
- end
-
- # Authenticate user with OAuth2
- #
- def authenticate!
- klass = mapping.to
- begin
-
-
- # Verify User Auth code and get access token from auth server: will error on failue
- access_token = Devise::oauth2_client.web_server.get_access_token(
- params[:code], :redirect_uri => Devise::session_sign_in_url(request,mapping)
- )
-
- # retrieve user attributes
-
- # Get user details from OAuth2 Service
- # NOTE: Facebook Graph Specific
- # TODO: break this out into separate model or class to handle
- # different oauth2 providers
- oauth2_user_attributes = JSON.parse(access_token.get('/me'))
-
- user = klass.authenticate_with_oauth2(oauth2_user_attributes['id'], access_token.token)
-
-
-
- if user.present?
- user.on_after_oauth2_connect(oauth2_user_attributes)
- success!(user)
- else
- if klass.oauth2_auto_create_account?
-
-
-
- user = returning(klass.new) do |u|
- u.store_oauth2_credentials!(
- :token => access_token.token,
- :uid => oauth2_user_attributes['id']
- )
- u.on_before_oauth2_auto_create(oauth2_user_attributes)
- end
-
- begin
-
-
- user.save(true)
- user.on_after_oauth2_connect(oauth2_user_attributes)
-
-
- success!(user)
- rescue
- fail!(:oauth2_invalid)
- end
- else
- fail!(:oauth2_invalid)
- end
- end
-
- rescue => e
- fail!(e.message)
- end
- end
-
-
-
-
- protected
- def valid_controller?
- params[:controller] == 'sessions'
- end
-
- def valid_params?
- params[:code].present?
- end
-
- end
- end
- end
-end
-
+# encoding: utf-8
+require 'devise/strategies/base'
+
+
+module Devise #:nodoc:
+ module Oauth2Authenticatable #:nodoc:
+ module Strategies #:nodoc:
+
+ # Default strategy for signing in a user using Facebook Connect (a Facebook account).
+ # Redirects to sign_in page if it's not authenticated
+ #
+ class Oauth2Authenticatable < ::Devise::Strategies::Base
+
+
+
+ # Without a oauth session authentication cannot proceed.
+ #
+ def valid?
+
+ valid_controller? && valid_params? && mapping.to.respond_to?('authenticate_with_oauth2')
+
+ end
+
+ # Authenticate user with OAuth2
+ #
+ def authenticate!
+ klass = mapping.to
+ begin
+
+
+ # Verify User Auth code and get access token from auth server: will error on failue
+ access_token = Devise::oauth2_client.web_server.get_access_token(
+ params[:code], :redirect_uri => Devise::session_sign_in_url(request,mapping)
+ )
+
+ # retrieve user attributes
+
+ # Get user details from OAuth2 Service
+ # NOTE: Facebook Graph Specific
+ # TODO: break this out into separate model or class to handle
+ # different oauth2 providers
+ oauth2_user_attributes = JSON.parse(access_token.get('/me'))
+
+ user = klass.authenticate_with_oauth2(oauth2_user_attributes['id'], access_token.token)
+
+
+
+ if user.present?
+ user.on_after_oauth2_connect(oauth2_user_attributes)
+ success!(user)
+ else
+ if klass.oauth2_auto_create_account?
+
+
+
+ user = returning(klass.new) do |u|
+ u.store_oauth2_credentials!(
+ :token => access_token.token,
+ :uid => oauth2_user_attributes['id']
+ )
+ u.on_before_oauth2_auto_create(oauth2_user_attributes)
+ end
+
+ begin
+
+
+ user.save
+ user.on_after_oauth2_connect(oauth2_user_attributes)
+
+
+ success!(user)
+ rescue
+ fail!(:oauth2_invalid)
+ end
+ else
+ fail!(:oauth2_invalid)
+ end
+ end
+
+ rescue => e
+ fail!(e.message)
+ end
+ end
+
+
+
+
+ protected
+ def valid_controller?
+ params[:controller] == 'sessions'
+ end
+
+ def valid_params?
+ params[:code].present?
+ end
+
+ end
+ end
+ end
+end
+
Warden::Strategies.add(:oauth2_authenticatable, Devise::Oauth2Authenticatable::Strategies::Oauth2Authenticatable)
View
57 lib/devise_oauth2_authenticatable/view_helpers.rb
@@ -1,29 +1,30 @@
-# encoding: utf-8
-require 'devise/mapping'
-
-module Devise #:nodoc:
- module Oauth2Authenticatable #:nodoc:
-
- # OAuth2 view helpers to easily add the link to the OAuth2 connection popup and also the necessary JS code.
- #
- module Helpers
-
- # Creates the link to
- def link_to_oauth2(link_text, options={})
-
-
- session_sign_in_url = Devise::session_sign_in_url(request,::Devise.mappings[:user])
-
- link_to link_text, Devise::oauth2_client.web_server.authorize_url(
- :redirect_uri => session_sign_in_url,
- :scope => Devise::requested_scope
- ), options
- end
-
-
-
- end
- end
-end
-
+# encoding: utf-8
+require 'devise/mapping'
+
+module Devise #:nodoc:
+ module Oauth2Authenticatable #:nodoc:
+
+ # OAuth2 view helpers to easily add the link to the OAuth2 connection popup and also the necessary JS code.
+ #
+ module Helpers
+
+ # Creates the link to
+ def link_to_oauth2(link_text, options={})
+
+
+ session_sign_in_url = Devise::session_sign_in_url(request,::Devise.mappings[:user])
+
+ link_to link_text, Devise::oauth2_client.web_server.authorize_url(
+ :redirect_uri => session_sign_in_url,
+ :scope => Devise::requested_scope
+ ), options
+ end
+
+
+
+
+ end
+ end
+end
+
::ActionView::Base.send :include, Devise::Oauth2Authenticatable::Helpers
View
6 rails/init.rb
@@ -1,4 +1,4 @@
-# encoding: utf-8
-Devise::OAUTH2_CONFIG = YAML.load_file(Rails.root.join('config', 'oauth2_config.yml'))[Rails.env]
-
+# encoding: utf-8
+Devise::OAUTH2_CONFIG = YAML.load_file(Rails.root.join('config', 'oauth2_config.yml'))[Rails.env]
+
require 'devise_oauth2_authenticatable'
Please sign in to comment.
Something went wrong with that request. Please try again.