reflex 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. data/.document +5 -0
  2. data/.gitignore +24 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +193 -0
  5. data/Rakefile +146 -0
  6. data/VERSION +1 -0
  7. data/cucumber.yml +7 -0
  8. data/features/connect_to_provider.feature +18 -0
  9. data/features/social_authentication.feature +50 -0
  10. data/features/step_definitions/reflex_steps.rb +93 -0
  11. data/features/step_definitions/web_steps.rb +192 -0
  12. data/features/support/env.rb +57 -0
  13. data/features/support/mocha.rb +16 -0
  14. data/features/support/mock_controller/oauth_authorize.html.erb +15 -0
  15. data/features/support/mocks.rb +21 -0
  16. data/features/support/paths.rb +39 -0
  17. data/features/support/rails_root/.gitignore +8 -0
  18. data/features/support/rails_root/Rakefile +10 -0
  19. data/features/support/rails_root/app/controllers/application_controller.rb +20 -0
  20. data/features/support/rails_root/app/controllers/user_sessions_controller.rb +24 -0
  21. data/features/support/rails_root/app/controllers/users_controller.rb +46 -0
  22. data/features/support/rails_root/app/helpers/application_helper.rb +3 -0
  23. data/features/support/rails_root/app/helpers/user_sessions_helper.rb +2 -0
  24. data/features/support/rails_root/app/helpers/users_helper.rb +2 -0
  25. data/features/support/rails_root/app/models/user.rb +7 -0
  26. data/features/support/rails_root/app/models/user_session.rb +2 -0
  27. data/features/support/rails_root/app/views/layouts/application.html.erb +23 -0
  28. data/features/support/rails_root/app/views/user_sessions/new.html.erb +23 -0
  29. data/features/support/rails_root/app/views/users/_form.html.erb +31 -0
  30. data/features/support/rails_root/app/views/users/_user.html.erb +1 -0
  31. data/features/support/rails_root/app/views/users/edit.html.erb +2 -0
  32. data/features/support/rails_root/app/views/users/index.html.erb +8 -0
  33. data/features/support/rails_root/app/views/users/new.html.erb +2 -0
  34. data/features/support/rails_root/app/views/users/show.html.erb +20 -0
  35. data/features/support/rails_root/config/boot.rb +110 -0
  36. data/features/support/rails_root/config/database.yml +9 -0
  37. data/features/support/rails_root/config/environment.rb +22 -0
  38. data/features/support/rails_root/config/environments/development.rb +0 -0
  39. data/features/support/rails_root/config/environments/test.rb +0 -0
  40. data/features/support/rails_root/config/initializers/backtrace_silencers.rb +7 -0
  41. data/features/support/rails_root/config/initializers/inflections.rb +10 -0
  42. data/features/support/rails_root/config/initializers/mime_types.rb +5 -0
  43. data/features/support/rails_root/config/initializers/new_rails_defaults.rb +21 -0
  44. data/features/support/rails_root/config/initializers/session_store.rb +15 -0
  45. data/features/support/rails_root/config/routes.rb +9 -0
  46. data/features/support/rails_root/db/migrate/001_create_users.rb +16 -0
  47. data/features/support/rails_root/db/migrate/002_create_reflex_connections.rb +17 -0
  48. data/features/support/rails_root/db/schema.rb +35 -0
  49. data/features/support/rails_root/vendor/plugins/reflex/rails/init.rb +1 -0
  50. data/features/traditional_registration_and_authentication.feature +39 -0
  51. data/init.rb +5 -0
  52. data/lib/reflex/authlogic/account.rb +55 -0
  53. data/lib/reflex/authlogic/acts_as_authentic.rb +19 -0
  54. data/lib/reflex/authlogic/authentication_process.rb +40 -0
  55. data/lib/reflex/authlogic/callback_filter.rb +26 -0
  56. data/lib/reflex/authlogic/connectable.rb +87 -0
  57. data/lib/reflex/authlogic/connection.rb +18 -0
  58. data/lib/reflex/authlogic/session.rb +84 -0
  59. data/lib/reflex/base.rb +37 -0
  60. data/lib/reflex/configuration.rb +38 -0
  61. data/lib/reflex/oauth_server.rb +47 -0
  62. data/lib/reflex/system.rb +25 -0
  63. data/lib/reflex.rb +14 -0
  64. data/rails/init.rb +22 -0
  65. data/rails_generators/reflex_connection_migration/reflex_connection_migration_generator.rb +12 -0
  66. data/rails_generators/reflex_connection_migration/templates/create_reflex_connections.rb +17 -0
  67. data/reflex.gemspec +164 -0
  68. data/spec/fakeweb/OAuthServer.getProviders +5 -0
  69. data/spec/fakeweb/OAuthServer.sessionGetProfile +5 -0
  70. data/spec/fakeweb/OAuthServer.tokenAccess +4 -0
  71. data/spec/fakeweb/OAuthServer.tokenRequest +5 -0
  72. data/spec/fakeweb/OAuthServer.tokenSetUserId +5 -0
  73. data/spec/fakeweb/OAuthServer.userGetProfile +5 -0
  74. data/spec/fakeweb/OAuthServer.userGetProviders +4 -0
  75. data/spec/fakeweb/OAuthServer.userRemoveProvider +5 -0
  76. data/spec/fakeweb/System.listMethods +5 -0
  77. data/spec/fakeweb/System.methodDescription +5 -0
  78. data/spec/fakeweb/System.methodSignature +5 -0
  79. data/spec/reflex/authlogic/connection_spec.rb +22 -0
  80. data/spec/reflex/base_spec.rb +29 -0
  81. data/spec/reflex/configuration_spec.rb +71 -0
  82. data/spec/reflex/oauth_server_spec.rb +219 -0
  83. data/spec/reflex/system_spec.rb +83 -0
  84. data/spec/reflex_spec.rb +5 -0
  85. data/spec/schema.rb +15 -0
  86. data/spec/spec.opts +1 -0
  87. data/spec/spec_helper.rb +66 -0
  88. metadata +294 -0
@@ -0,0 +1,110 @@
1
+ # Don't change this file!
2
+ # Configure your app in config/environment.rb and config/environments/*.rb
3
+
4
+ RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
5
+
6
+ module Rails
7
+ class << self
8
+ def boot!
9
+ unless booted?
10
+ preinitialize
11
+ pick_boot.run
12
+ end
13
+ end
14
+
15
+ def booted?
16
+ defined? Rails::Initializer
17
+ end
18
+
19
+ def pick_boot
20
+ (vendor_rails? ? VendorBoot : GemBoot).new
21
+ end
22
+
23
+ def vendor_rails?
24
+ File.exist?("#{RAILS_ROOT}/vendor/rails")
25
+ end
26
+
27
+ def preinitialize
28
+ load(preinitializer_path) if File.exist?(preinitializer_path)
29
+ end
30
+
31
+ def preinitializer_path
32
+ "#{RAILS_ROOT}/config/preinitializer.rb"
33
+ end
34
+ end
35
+
36
+ class Boot
37
+ def run
38
+ load_initializer
39
+ Rails::Initializer.run(:set_load_path)
40
+ end
41
+ end
42
+
43
+ class VendorBoot < Boot
44
+ def load_initializer
45
+ require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
46
+ Rails::Initializer.run(:install_gem_spec_stubs)
47
+ Rails::GemDependency.add_frozen_gem_path
48
+ end
49
+ end
50
+
51
+ class GemBoot < Boot
52
+ def load_initializer
53
+ self.class.load_rubygems
54
+ load_rails_gem
55
+ require 'initializer'
56
+ end
57
+
58
+ def load_rails_gem
59
+ if version = self.class.gem_version
60
+ gem 'rails', version
61
+ else
62
+ gem 'rails'
63
+ end
64
+ rescue Gem::LoadError => load_error
65
+ $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
66
+ exit 1
67
+ end
68
+
69
+ class << self
70
+ def rubygems_version
71
+ Gem::RubyGemsVersion rescue nil
72
+ end
73
+
74
+ def gem_version
75
+ if defined? RAILS_GEM_VERSION
76
+ RAILS_GEM_VERSION
77
+ elsif ENV.include?('RAILS_GEM_VERSION')
78
+ ENV['RAILS_GEM_VERSION']
79
+ else
80
+ parse_gem_version(read_environment_rb)
81
+ end
82
+ end
83
+
84
+ def load_rubygems
85
+ min_version = '1.3.2'
86
+ require 'rubygems'
87
+ unless rubygems_version >= min_version
88
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
89
+ exit 1
90
+ end
91
+
92
+ rescue LoadError
93
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
94
+ exit 1
95
+ end
96
+
97
+ def parse_gem_version(text)
98
+ $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
99
+ end
100
+
101
+ private
102
+ def read_environment_rb
103
+ File.read("#{RAILS_ROOT}/config/environment.rb")
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ # All that for this:
110
+ Rails.boot!
@@ -0,0 +1,9 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: db/cucumber.sqlite3
4
+ timeout: 5000
5
+
6
+ development:
7
+ adapter: sqlite3
8
+ database: db/cucumber.sqlite3
9
+ timeout: 5000
@@ -0,0 +1,22 @@
1
+ # Be sure to restart your server when you modify this file
2
+
3
+ # Specifies gem version of Rails to use when vendor/rails is not present
4
+ RAILS_GEM_VERSION = '2.3.5' unless defined? RAILS_GEM_VERSION
5
+
6
+ # Bootstrap the Rails environment, frameworks, and default configuration
7
+ require File.join(File.dirname(__FILE__), 'boot')
8
+
9
+ Rails::Initializer.run do |config|
10
+ config.gem 'authlogic'
11
+ config.gem 'cucumber'
12
+ config.gem 'cucumber-rails', :lib => false
13
+ config.gem 'database_cleaner', :lib => false
14
+ config.gem 'capybara', :lib => false
15
+ config.time_zone = 'UTC'
16
+ config.cache_classes = true
17
+ config.whiny_nils = true
18
+ config.action_controller.consider_all_requests_local = true
19
+ config.action_controller.perform_caching = false
20
+ config.action_controller.allow_forgery_protection = false
21
+ config.action_mailer.delivery_method = :test
22
+ end
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4
+ # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5
+
6
+ # You can also remove all the silencers if you're trying do debug a problem that might steem from framework code.
7
+ Rails.backtrace_cleaner.remove_silencers!
@@ -0,0 +1,10 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new inflection rules using the following format
4
+ # (all these examples are active by default):
5
+ # ActiveSupport::Inflector.inflections do |inflect|
6
+ # inflect.plural /^(ox)$/i, '\1en'
7
+ # inflect.singular /^(ox)en/i, '\1'
8
+ # inflect.irregular 'person', 'people'
9
+ # inflect.uncountable %w( fish sheep )
10
+ # end
@@ -0,0 +1,5 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new mime types for use in respond_to blocks:
4
+ # Mime::Type.register "text/richtext", :rtf
5
+ # Mime::Type.register_alias "text/html", :iphone
@@ -0,0 +1,21 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # These settings change the behavior of Rails 2 apps and will be defaults
4
+ # for Rails 3. You can remove this initializer when Rails 3 is released.
5
+
6
+ if defined?(ActiveRecord)
7
+ # Include Active Record class name as root for JSON serialized output.
8
+ ActiveRecord::Base.include_root_in_json = true
9
+
10
+ # Store the full class name (including module namespace) in STI type column.
11
+ ActiveRecord::Base.store_full_sti_class = true
12
+ end
13
+
14
+ ActionController::Routing.generate_best_match = false
15
+
16
+ # Use ISO 8601 format for JSON serialized times and dates.
17
+ ActiveSupport.use_standard_json_time_format = true
18
+
19
+ # Don't escape HTML entities in JSON, leave that for the #json_escape helper.
20
+ # if you're including raw json in an HTML page.
21
+ ActiveSupport.escape_html_entities_in_json = false
@@ -0,0 +1,15 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key for verifying cookie session data integrity.
4
+ # If you change this key, all old sessions will become invalid!
5
+ # Make sure the secret is at least 30 characters and all random,
6
+ # no regular words or you'll be exposed to dictionary attacks.
7
+ ActionController::Base.session = {
8
+ :key => '_rails_root_session',
9
+ :secret => '92b9078469e5a7ab43000eeb320486bf19b96757cf7fe19d01437a742a5e66890defa0d50cdad33895ed5b5d87d7fff9ea44fc8e820e95ba004b350b54a3db5d'
10
+ }
11
+
12
+ # Use the database for sessions instead of the cookie-based default,
13
+ # which shouldn't be used to store highly confidential information
14
+ # (create the session table with "rake db:sessions:create")
15
+ # ActionController::Base.session_store = :active_record_store
@@ -0,0 +1,9 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+ map.root :controller => :users
3
+ map.resources :users
4
+ map.resources :user_sessions
5
+ map.with_options :controller => :user_sessions do |session|
6
+ session.login '/login', :action => :new
7
+ session.logout '/logout', :action => :destroy
8
+ end
9
+ end
@@ -0,0 +1,16 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :users do |t|
4
+ t.string :name, :null => true
5
+ t.string :login, :null => true
6
+ t.string :crypted_password, :null => true
7
+ t.string :password_salt, :null => true
8
+ t.string :persistence_token, :null => true
9
+ t.timestamps
10
+ end
11
+ end
12
+
13
+ def self.down
14
+ drop_table :users
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ class CreateReflexConnections < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :reflex_connections do |t|
4
+ t.string :provider, :null => false
5
+ t.string :authorizable_type, :null => false
6
+ t.integer :authorizable_id, :null => false
7
+ t.string :uuid, :null => false
8
+ t.timestamps
9
+ end
10
+
11
+ add_index :reflex_connections, [:authorizable_type, :authorizable_id]
12
+ end
13
+
14
+ def self.down
15
+ drop_table :reflex_connections
16
+ end
17
+ end
@@ -0,0 +1,35 @@
1
+ # This file is auto-generated from the current state of the database. Instead of editing this file,
2
+ # please use the migrations feature of Active Record to incrementally modify your database, and
3
+ # then regenerate this schema definition.
4
+ #
5
+ # Note that this schema.rb definition is the authoritative source for your database schema. If you need
6
+ # to create the application database on another system, you should be using db:schema:load, not running
7
+ # all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
8
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
9
+ #
10
+ # It's strongly recommended to check this file into your version control system.
11
+
12
+ ActiveRecord::Schema.define(:version => 2) do
13
+
14
+ create_table "reflex_connections", :force => true do |t|
15
+ t.string "provider", :null => false
16
+ t.string "authorizable_type", :null => false
17
+ t.integer "authorizable_id", :null => false
18
+ t.string "uuid", :null => false
19
+ t.datetime "created_at", :null => false
20
+ t.datetime "updated_at", :null => false
21
+ end
22
+
23
+ add_index "reflex_connections", ["authorizable_type", "authorizable_id"], :name => "index_reflex_connections_on_authorizable_type_and_authorizable_id", :unique => true
24
+
25
+ create_table "users", :force => true do |t|
26
+ t.string "name", :null => false
27
+ t.string "login", :null => true
28
+ t.string "crypted_password", :null => true
29
+ t.string "password_salt", :null => true
30
+ t.string "persistence_token", :null => false
31
+ t.datetime "created_at", :null => false
32
+ t.datetime "updated_at", :null => false
33
+ end
34
+
35
+ end
@@ -0,0 +1 @@
1
+ require File.join(Rails.root, '..', '..', '..', '..', 'reflex/rails/init')
@@ -0,0 +1,39 @@
1
+ Feature: Traditional registration
2
+ In order to allow visitors without an account on one of our providers to create a profile
3
+ As a visitor
4
+ I want to register and login
5
+
6
+ Scenario: Registration
7
+ Given I am on the homepage
8
+ And I follow "Register"
9
+
10
+ When I fill in "Login" with "barney"
11
+ And I fill in "Name" with "Barney Stinson"
12
+ And I fill in "Password" with "awesome"
13
+ And I fill in "user_password_confirmation" with "awesome"
14
+ And I press "Save"
15
+
16
+ Then I should be on the users page
17
+ And I should see "Barney Stinson"
18
+
19
+ Scenario: Logging in
20
+ Given a registered user exists with login: "barney", password: "awesome"
21
+ And I am on the homepage
22
+
23
+ When I follow "Login"
24
+ And I fill in "Login" with "barney"
25
+ And I fill in "Password" with "awesome"
26
+ And I press "Login"
27
+
28
+ Then I should be on the user page for "barney"
29
+ And I should be logged in
30
+
31
+ Scenario: Logging Out
32
+ Given a registered user exists with login: "barney", password: "awesome"
33
+ And I am logged in as "barney" with password "awesome"
34
+ And I am on the homepage
35
+
36
+ When I follow "Logout"
37
+ Then I should be on the homepage
38
+ And I should be logged out
39
+
data/init.rb ADDED
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+
3
+ require 'lib/reflex'
4
+
5
+ $LOAD_PATH.shift
@@ -0,0 +1,55 @@
1
+ module Reflex
2
+ module Authlogic
3
+ module Account
4
+ def self.included(base)
5
+ base.send(:extend, ClassMethods)
6
+ base.send(:include, InstanceMethods)
7
+
8
+ base.send(:has_many, :reflex_connections, :as => :authorizable, :autosave => true,
9
+ :class_name => 'Reflex::Authlogic::Connection')
10
+
11
+ # Overwrite authlogic validation options so they are skipped when saving a react account:
12
+ [:validates_uniqueness_of_login_field_options,
13
+ :validates_length_of_password_field_options,
14
+ :validates_confirmation_of_password_field_options,
15
+ :validates_length_of_password_confirmation_field_options,
16
+ :validates_length_of_login_field_options,
17
+ :validates_format_of_login_field_options].each do |validate_options|
18
+ current_options = base.send(validate_options)
19
+
20
+ base.cattr_accessor "original_#{validate_options}"
21
+ base.send("original_#{validate_options}=", current_options.dup)
22
+
23
+ new_options = current_options.merge(:if => nil, :unless => :saving_for_react?)
24
+
25
+ base.send("#{validate_options}=", new_options)
26
+ end
27
+ end
28
+
29
+ module InstanceMethods
30
+ def saving_for_react?
31
+ reflex_connections.present?
32
+ end
33
+ end
34
+
35
+ module ClassMethods
36
+ def find_by_react_user_id(react_id)
37
+ connection = Reflex::Authlogic::Connection.find_by_authorizable_type_and_uuid(class_name, react_id)
38
+ connection && connection.authorizable
39
+ end
40
+
41
+ def create_for_react(provider, react_profile = nil)
42
+ record = new()
43
+ connection = record.reflex_connections.build(:provider => provider)
44
+
45
+ if react_profile
46
+ record.react_profile = react_profile if record.respond_to?(:react_profile=)
47
+ end
48
+
49
+ record.save_without_session_maintenance
50
+ [record, connection]
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,19 @@
1
+ require 'reflex/authlogic/connection'
2
+ require 'reflex/authlogic/account'
3
+ require 'reflex/authlogic/connectable'
4
+
5
+ module Reflex
6
+ module Authlogic
7
+ module ActsAsAuthentic
8
+ # Adds in the neccesary modules for acts_as_authentic to include.
9
+ def self.included(base)
10
+ base.class_eval do
11
+ add_acts_as_authentic_module(Reflex::Authlogic::Account, :prepend)
12
+ add_acts_as_authentic_module(Reflex::Authlogic::Connectable, :prepend)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ ActiveRecord::Base.send(:include, Reflex::Authlogic::ActsAsAuthentic)
@@ -0,0 +1,40 @@
1
+ module Reflex
2
+ module Authlogic
3
+ module AuthenticationProcess
4
+ private
5
+
6
+ def redirect_to_oauth_server(options = {})
7
+ # Request the URL to redirect to
8
+ result = Reflex::OAuthServer.token_request(react_provider)
9
+
10
+ # Set the request method (POST) in the session, so our
11
+ # middleware can detect it and use it on the callback URL:
12
+ reflex_controller.session[:react_callback_method] = reflex_controller.request.method
13
+ reflex_controller.session[:react_callback_location] = options[:callback_location]
14
+
15
+ # Redirect the visitor to the given URL
16
+ reflex_controller.redirect_to(result['redirectUrl'])
17
+ end
18
+
19
+ def react_provider
20
+ reflex_controller.params && reflex_controller.params['react_provider']
21
+ end
22
+
23
+ def react_oauth_session
24
+ reflex_controller.params && reflex_controller.params['ReactOAuthSession']
25
+ end
26
+
27
+ def redirecting_to_oauth_server?
28
+ reflex_controller && authenticating_via_oauth_server? && !react_oauth_session
29
+ end
30
+
31
+ def authenticating_via_oauth_server?
32
+ reflex_controller && (react_provider.present? || react_oauth_session.present?) # || did_not_allow_access
33
+ end
34
+
35
+ def reflex_controller
36
+ self.is_a?(::Authlogic::Session::Base) ? controller : session_class.controller
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,26 @@
1
+ module Reflex
2
+ module Authlogic
3
+ class CallbackFilter
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(env)
9
+ # if :react_callback_method is available in the session and the requested URL's query
10
+ # contains ReactOAuthSession, change the REQUEST_METHOD to :react_callback_method
11
+ if env["rack.session"][:react_callback_method].present? && env["QUERY_STRING"] =~ /ReactOAuthSession/
12
+ env["REQUEST_METHOD"] = env["rack.session"].delete(:react_callback_method).to_s.upcase
13
+
14
+ if env["rack.session"][:react_callback_location].present?
15
+ location = env["rack.session"].delete(:react_callback_location)
16
+
17
+ env["REQUEST_URI"] = location
18
+ env["PATH_INFO"] = location.split('?').first
19
+ end
20
+ end
21
+
22
+ @app.call(env)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,87 @@
1
+ require 'reflex/authlogic/authentication_process'
2
+
3
+ module Reflex
4
+ module Authlogic
5
+ module Connectable
6
+ def self.included(base)
7
+ base.send(:extend, ClassMethods)
8
+ base.send(:include, InstanceMethods)
9
+
10
+ base.validate :authenticate_oauth_session,
11
+ :if => lambda { |account| !account.new_record? && account.send(:authenticating_via_oauth_server?) }
12
+ end
13
+
14
+ module ClassMethods
15
+ end
16
+
17
+ module InstanceMethods
18
+ include Reflex::Authlogic::AuthenticationProcess
19
+
20
+ def save(perform_validation = true, &block)
21
+ if perform_validation && block_given? && connecting_to_provider?
22
+ # Save attributes in session so we can use them again after authentication
23
+ reflex_controller.session[:authlogic_reflex_attributes] = attributes.reject!{|k, v| v.blank?}
24
+
25
+ # Redirect to the OAuth Server, but set a callback location so we return here
26
+ redirect_to_oauth_server(:callback_location => reflex_controller.request.path)
27
+
28
+ return false
29
+ end
30
+
31
+ result = super
32
+
33
+ yield(result) if block_given?
34
+ result
35
+ end
36
+
37
+ private
38
+
39
+ def connect_to_provider!
40
+ if redirecting_to_oauth_server?
41
+ redirect_to_oauth_server
42
+
43
+ elsif react_oauth_session
44
+ authenticate_oauth_session
45
+
46
+ else
47
+ # User did not grant access. Deal with it!
48
+ errors.add(:reflex, :invalid)
49
+ end
50
+ end
51
+
52
+ def authenticate_oauth_session(allow_retry = true)
53
+ # Request the React Session
54
+ oauth_session = Reflex::OAuthServer.token_access(reflex_controller.params)
55
+ react_provider = oauth_session['connectedWithProvider']
56
+ react_user_id = oauth_session['applicationUserId']
57
+
58
+ if react_user_id
59
+ # applicationUserId is not longer valid, so remove it's provider remotely:
60
+ Reflex::OAuthServer.user_remove_provider(react_user_id, react_provider)
61
+ end
62
+
63
+ # Create a new connection of this provider:
64
+ connection = reflex_connections.create(:provider => react_provider)
65
+
66
+ # Set the user id on react's side:
67
+ Reflex::OAuthServer.token_set_user_id(connection.uuid, react_oauth_session)
68
+
69
+ # Set the attributes that were set pre redirection
70
+ if session_attributes = reflex_controller.session.delete(:authlogic_reflex_attributes)
71
+ self.attributes = session_attributes
72
+ end
73
+
74
+ true
75
+ end
76
+
77
+ def connecting_to_provider?
78
+ !new_record? && react_provider
79
+ end
80
+
81
+ def saving_for_react?
82
+ connecting_to_provider? || super
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,18 @@
1
+ module Reflex
2
+ module Authlogic
3
+ class Connection < ActiveRecord::Base
4
+ set_table_name 'reflex_connections'
5
+
6
+ belongs_to :authorizable, :polymorphic => true
7
+ validates_presence_of :uuid
8
+
9
+ before_validation_on_create :generate_uuid
10
+
11
+ private
12
+
13
+ def generate_uuid
14
+ self.uuid = UUIDTools::UUID.random_create.to_s
15
+ end
16
+ end
17
+ end
18
+ end