authlogic-connect 0.0.1

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 (40) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.markdown +145 -0
  3. data/Rakefile +69 -0
  4. data/init.rb +1 -0
  5. data/lib/authlogic_connect.rb +71 -0
  6. data/lib/authlogic_connect/common.rb +9 -0
  7. data/lib/authlogic_connect/common/session.rb +27 -0
  8. data/lib/authlogic_connect/common/user.rb +40 -0
  9. data/lib/authlogic_connect/common/variables.rb +21 -0
  10. data/lib/authlogic_connect/oauth.rb +11 -0
  11. data/lib/authlogic_connect/oauth/helper.rb +16 -0
  12. data/lib/authlogic_connect/oauth/process.rb +82 -0
  13. data/lib/authlogic_connect/oauth/session.rb +72 -0
  14. data/lib/authlogic_connect/oauth/tokens/delicious_token.rb +12 -0
  15. data/lib/authlogic_connect/oauth/tokens/facebook_token.rb +19 -0
  16. data/lib/authlogic_connect/oauth/tokens/get_satisfaction_token.rb +14 -0
  17. data/lib/authlogic_connect/oauth/tokens/google_token.rb +38 -0
  18. data/lib/authlogic_connect/oauth/tokens/myspace_token.rb +14 -0
  19. data/lib/authlogic_connect/oauth/tokens/oauth_token.rb +48 -0
  20. data/lib/authlogic_connect/oauth/tokens/opensocial_token.rb +0 -0
  21. data/lib/authlogic_connect/oauth/tokens/photobucket_token.rb +13 -0
  22. data/lib/authlogic_connect/oauth/tokens/smug_mug_token.rb +14 -0
  23. data/lib/authlogic_connect/oauth/tokens/twitter_token.rb +11 -0
  24. data/lib/authlogic_connect/oauth/tokens/vimeo_token.rb +13 -0
  25. data/lib/authlogic_connect/oauth/tokens/yahoo_token.rb +20 -0
  26. data/lib/authlogic_connect/oauth/user.rb +81 -0
  27. data/lib/authlogic_connect/oauth/variables.rb +34 -0
  28. data/lib/authlogic_connect/openid.rb +8 -0
  29. data/lib/authlogic_connect/openid/session.rb +125 -0
  30. data/lib/authlogic_connect/openid/tokens/aol_token.rb +0 -0
  31. data/lib/authlogic_connect/openid/tokens/blogger_token.rb +0 -0
  32. data/lib/authlogic_connect/openid/tokens/flickr_token.rb +0 -0
  33. data/lib/authlogic_connect/openid/tokens/my_openid_token.rb +0 -0
  34. data/lib/authlogic_connect/openid/tokens/openid_token.rb +3 -0
  35. data/lib/authlogic_connect/openid/user.rb +93 -0
  36. data/lib/authlogic_connect/openid/variables.rb +5 -0
  37. data/lib/oauth_callback_filter.rb +12 -0
  38. data/lib/token.rb +37 -0
  39. data/rails/init.rb +17 -0
  40. metadata +175 -0
@@ -0,0 +1,34 @@
1
+ module AuthlogicConnect::Oauth
2
+ module Variables
3
+
4
+ # These are just helper variables
5
+ def oauth_response
6
+ auth_params && oauth_key
7
+ end
8
+
9
+ def oauth_key
10
+ return nil unless auth_controller
11
+ oauth_version == 1.0 ? auth_params[:oauth_token] : auth_params[:code]
12
+ end
13
+
14
+ def oauth_version
15
+ oauth_token.oauth_version
16
+ end
17
+
18
+ def oauth_provider
19
+ auth_session[:oauth_provider] || "facebook"
20
+ end
21
+
22
+ def oauth_consumer
23
+ AuthlogicConnect.consumer(oauth_provider)
24
+ end
25
+
26
+ def oauth_client
27
+ oauth_token.client
28
+ end
29
+
30
+ def oauth_token
31
+ AuthlogicConnect.token(oauth_provider)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,8 @@
1
+ module AuthlogicConnect::Openid
2
+ end
3
+
4
+ require File.dirname(__FILE__) + "/openid/user"
5
+ require File.dirname(__FILE__) + "/openid/session"
6
+
7
+ ActiveRecord::Base.send(:include, AuthlogicConnect::Openid::User)
8
+ Authlogic::Session::Base.send(:include, AuthlogicConnect::Openid::Session)
@@ -0,0 +1,125 @@
1
+ module AuthlogicConnect::Openid
2
+ # This module is responsible for adding all of the OpenID goodness to the Authlogic::Session::Base class.
3
+ module Session
4
+ # Add a simple openid_identifier attribute and some validations for the field.
5
+ def self.included(klass)
6
+ klass.extend ClassMethods
7
+ klass.class_eval do
8
+ include InstanceMethods
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ # What method should we call to find a record by the openid_identifier?
14
+ # This is useful if you want to store multiple openid_identifiers for a single record.
15
+ # You could do something like:
16
+ #
17
+ # class User < ActiveRecord::Base
18
+ # def self.find_by_openid_identifier(identifier)
19
+ # user.first(:conditions => {:openid_identifiers => {:identifier => identifier}})
20
+ # end
21
+ # end
22
+ #
23
+ # Obviously the above depends on what you are calling your assocition, etc. But you get the point.
24
+ #
25
+ # * <tt>Default:</tt> :find_by_openid_identifier
26
+ # * <tt>Accepts:</tt> Symbol
27
+ def find_by_openid_identifier_method(value = nil)
28
+ rw_config(:find_by_openid_identifier_method, value, :find_by_openid_identifier)
29
+ end
30
+ alias_method :find_by_openid_identifier_method=, :find_by_openid_identifier_method
31
+
32
+ # Add this in your Session object to Auto Register a new user using openid via sreg
33
+ def auto_register(value=true)
34
+ auto_register_value(value)
35
+ end
36
+
37
+ def auto_register_value(value=nil)
38
+ rw_config(:auto_register,value,false)
39
+ end
40
+
41
+ alias_method :auto_register=,:auto_register
42
+ end
43
+
44
+ module InstanceMethods
45
+ def self.included(klass)
46
+ klass.class_eval do
47
+ attr_reader :openid_identifier
48
+ validate :validate_openid_error
49
+ validate :validate_by_openid, :if => :authenticating_with_openid?
50
+ end
51
+ end
52
+
53
+ # Hooks into credentials so that you can pass an :openid_identifier key.
54
+ def credentials=(value)
55
+ super
56
+ values = value.is_a?(Array) ? value : [value]
57
+ hash = values.first.is_a?(Hash) ? values.first.with_indifferent_access : nil
58
+ self.openid_identifier = hash[:openid_identifier] if !hash.nil? && hash.key?(:openid_identifier)
59
+ end
60
+
61
+ def openid_identifier=(value)
62
+ @openid_identifier = value.blank? ? nil : OpenIdAuthentication.normalize_identifier(value)
63
+ @openid_error = nil
64
+ rescue OpenIdAuthentication::InvalidOpenId => e
65
+ @openid_identifier = nil
66
+ @openid_error = e.message
67
+ end
68
+
69
+ # Cleaers out the block if we are authenticating with OpenID, so that we can redirect without a DoubleRender
70
+ # error.
71
+ def save_with_openid(&block)
72
+ block = nil if !openid_identifier.blank?
73
+ return block.nil?
74
+ end
75
+
76
+ private
77
+ def authenticating_with_openid?
78
+ attempted_record.nil? && errors.empty? && (!openid_identifier.blank? || (controller.params[:open_id_complete] && controller.params[:for_session]))
79
+ end
80
+
81
+ def find_by_openid_identifier_method
82
+ self.class.find_by_openid_identifier_method
83
+ end
84
+
85
+ def find_by_openid_identifier_method
86
+ self.class.find_by_openid_identifier_method
87
+ end
88
+
89
+ def auto_register?
90
+ self.class.auto_register_value
91
+ end
92
+
93
+ def validate_by_openid
94
+ self.remember_me = auth_params[:remember_me] == "true" if auth_params.key?(:remember_me)
95
+ self.attempted_record = klass.send(find_by_openid_identifier_method, openid_identifier)
96
+ if !attempted_record
97
+ if auto_register?
98
+ self.attempted_record = klass.new :openid_identifier => openid_identifier
99
+ attempted_record.save do |result|
100
+ if result
101
+ true
102
+ else
103
+ false
104
+ end
105
+ end
106
+ else
107
+ errors.add(:openid_identifier, "did not match any users in our database, have you set up your account to use OpenID?")
108
+ end
109
+ return
110
+ end
111
+ controller.send(:authenticate_with_open_id, openid_identifier, :return_to => controller.url_for(:for_session => "1", :remember_me => remember_me?)) do |result, openid_identifier|
112
+ if result.unsuccessful?
113
+ errors.add_to_base(result.message)
114
+ return
115
+ end
116
+
117
+ end
118
+ end
119
+
120
+ def validate_openid_error
121
+ errors.add(:openid_identifier, @openid_error) if @openid_error
122
+ end
123
+ end
124
+ end
125
+ end
File without changes
@@ -0,0 +1,3 @@
1
+ class OpenidToken < Token
2
+
3
+ end
@@ -0,0 +1,93 @@
1
+ module AuthlogicConnect::Openid
2
+ module User
3
+ def self.included(base)
4
+ base.class_eval do
5
+ add_acts_as_authentic_module(InstanceMethods, :prepend)
6
+ end
7
+ end
8
+
9
+ module InstanceMethods
10
+
11
+ def self.included(base)
12
+ return if !base.column_names.include?("openid_identifier")
13
+
14
+ base.class_eval do
15
+ validates_uniqueness_of :openid_identifier, :scope => validations_scope, :if => :using_openid?
16
+ validate :validate_openid
17
+ validates_length_of_password_field_options validates_length_of_password_field_options.merge(:if => :validate_password_with_openid?)
18
+ validates_confirmation_of_password_field_options validates_confirmation_of_password_field_options.merge(:if => :validate_password_with_openid?)
19
+ validates_length_of_password_confirmation_field_options validates_length_of_password_confirmation_field_options.merge(:if => :validate_password_with_openid?)
20
+ validates_length_of_login_field_options validates_length_of_login_field_options.merge(:if => :validate_password_with_openid?)
21
+ validates_format_of_login_field_options validates_format_of_login_field_options.merge(:if => :validate_password_with_openid?)
22
+ end
23
+ end
24
+
25
+ def openid_identifier=(value)
26
+ write_attribute(:openid_identifier, value.blank? ? nil : OpenIdAuthentication.normalize_identifier(value))
27
+ reset_persistence_token if openid_identifier_changed?
28
+ rescue OpenIdAuthentication::InvalidOpenId => e
29
+ @openid_error = e.message
30
+ end
31
+
32
+ def save_with_openid(perform_validation = true, &block)
33
+ return false if perform_validation && block_given? && authenticating_with_openid? && !authenticating_with_openid
34
+ return false if new_record? && !openid_complete?
35
+ return true
36
+ end
37
+
38
+ protected
39
+
40
+ def validate_openid
41
+ errors.add(:openid_identifier, "had the following error: #{@openid_error}") if @openid_error
42
+ end
43
+
44
+ def using_openid?
45
+ respond_to?(:openid_identifier) && !openid_identifier.blank?
46
+ end
47
+
48
+ def openid_complete?
49
+ auth_params[:open_id_complete] && auth_params[:for_model]
50
+ end
51
+
52
+ def authenticating_with_openid?
53
+ session_class.activated? && ((using_openid? && openid_identifier_changed?) || openid_complete?)
54
+ end
55
+
56
+ def validate_password_with_openid?
57
+ !using_openid? && require_password?
58
+ end
59
+
60
+ def authenticating_with_openid
61
+ @openid_error = nil
62
+ if !openid_complete?
63
+ auth_session[:openid_attributes] = attributes_to_save
64
+ else
65
+ self.attributes = auth_session[:openid_attributes]
66
+ auth_session[:openid_attributes] = nil
67
+ end
68
+
69
+ options = {}
70
+ options[:return_to] = auth_controller.url_for(:for_model => "1",:controller => "users", :action => "create")
71
+
72
+ auth_controller.send(:authenticate_with_open_id, openid_identifier, options) do |result, openid_identifier, registration|
73
+ if result.unsuccessful?
74
+ @openid_error = result.message
75
+ else
76
+ self.openid_identifier = openid_identifier
77
+ end
78
+ return true
79
+ end
80
+ return false
81
+ end
82
+
83
+ def attributes_to_save
84
+ attrs_to_save = attributes.clone.delete_if do |k, v|
85
+ [:id, :password, crypted_password_field, password_salt_field, :persistence_token, :perishable_token, :single_access_token, :login_count,
86
+ :failed_login_count, :last_request_at, :current_login_at, :last_login_at, :current_login_ip, :last_login_ip, :created_at,
87
+ :updated_at, :lock_version].include?(k.to_sym)
88
+ end
89
+ attrs_to_save.merge!(:password => password, :password_confirmation => password_confirmation)
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,5 @@
1
+ module AuthlogicConnect::Openid
2
+ module Variables
3
+
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ class OauthCallbackFilter
2
+ def initialize(app)
3
+ @app = app
4
+ end
5
+
6
+ def call(env)
7
+ unless env["rack.session"][:oauth_callback_method].blank?
8
+ env["REQUEST_METHOD"] = env["rack.session"].delete(:oauth_callback_method).to_s.upcase
9
+ end
10
+ @app.call(env)
11
+ end
12
+ end
data/lib/token.rb ADDED
@@ -0,0 +1,37 @@
1
+ class Token < ActiveRecord::Base
2
+ belongs_to :user
3
+ validates_presence_of :key, :secret
4
+
5
+ def client
6
+ self.class.client
7
+ end
8
+
9
+ def consumer
10
+ self.class.consumer
11
+ end
12
+
13
+ def service_name
14
+ self.class.service_name
15
+ end
16
+
17
+ class << self
18
+ def service_name
19
+ @service_name ||= self.to_s.underscore.scan(/^(.*?)(_token)?$/)[0][0].to_sym
20
+ end
21
+
22
+ def client
23
+ raise "implement client in subclass"
24
+ end
25
+
26
+ def consumer
27
+ raise "implement consumer in subclass"
28
+ end
29
+
30
+ protected
31
+
32
+ def credentials
33
+ @credentials ||= AuthlogicConnect.credentials(service_name)
34
+ end
35
+ end
36
+
37
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,17 @@
1
+ require "authlogic_connect"
2
+ require "oauth_callback_filter"
3
+
4
+ # Throw callback rack app into the middleware stack
5
+ ActionController::Dispatcher.middleware = ActionController::MiddlewareStack.new do |m|
6
+ ActionController::Dispatcher.middleware.each do |klass|
7
+ m.use klass
8
+ end
9
+ m.use OauthCallbackFilter
10
+ end
11
+
12
+ custom_models = Dir["#{File.dirname(__FILE__)}/../lib/authlogic_connect/oauth/tokens"]
13
+ custom_models +=Dir["#{File.dirname(__FILE__)}/../lib/authlogic_connect/openid/tokens"]
14
+ custom_models.each do |path|
15
+ $LOAD_PATH << path
16
+ ActiveSupport::Dependencies.load_paths << path
17
+ end
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: authlogic-connect
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Lance Pollard
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-10 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activesupport
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 1
30
+ - 2
31
+ version: 2.1.2
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: activerecord
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 2
43
+ - 1
44
+ - 2
45
+ version: 2.1.2
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: authlogic
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ type: :runtime
59
+ version_requirements: *id003
60
+ - !ruby/object:Gem::Dependency
61
+ name: oauth
62
+ prerelease: false
63
+ requirement: &id004 !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ type: :runtime
71
+ version_requirements: *id004
72
+ - !ruby/object:Gem::Dependency
73
+ name: json
74
+ prerelease: false
75
+ requirement: &id005 !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ type: :runtime
83
+ version_requirements: *id005
84
+ - !ruby/object:Gem::Dependency
85
+ name: oauth2
86
+ prerelease: false
87
+ requirement: &id006 !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ type: :runtime
95
+ version_requirements: *id006
96
+ description: Let your app use all of Oauth and OpenID
97
+ email: lancejpollard@gmail.com
98
+ executables: []
99
+
100
+ extensions: []
101
+
102
+ extra_rdoc_files: []
103
+
104
+ files:
105
+ - README.markdown
106
+ - Rakefile
107
+ - init.rb
108
+ - MIT-LICENSE
109
+ - lib/authlogic_connect/common/session.rb
110
+ - lib/authlogic_connect/common/user.rb
111
+ - lib/authlogic_connect/common/variables.rb
112
+ - lib/authlogic_connect/common.rb
113
+ - lib/authlogic_connect/oauth/helper.rb
114
+ - lib/authlogic_connect/oauth/process.rb
115
+ - lib/authlogic_connect/oauth/session.rb
116
+ - lib/authlogic_connect/oauth/tokens/delicious_token.rb
117
+ - lib/authlogic_connect/oauth/tokens/facebook_token.rb
118
+ - lib/authlogic_connect/oauth/tokens/get_satisfaction_token.rb
119
+ - lib/authlogic_connect/oauth/tokens/google_token.rb
120
+ - lib/authlogic_connect/oauth/tokens/myspace_token.rb
121
+ - lib/authlogic_connect/oauth/tokens/oauth_token.rb
122
+ - lib/authlogic_connect/oauth/tokens/opensocial_token.rb
123
+ - lib/authlogic_connect/oauth/tokens/photobucket_token.rb
124
+ - lib/authlogic_connect/oauth/tokens/smug_mug_token.rb
125
+ - lib/authlogic_connect/oauth/tokens/twitter_token.rb
126
+ - lib/authlogic_connect/oauth/tokens/vimeo_token.rb
127
+ - lib/authlogic_connect/oauth/tokens/yahoo_token.rb
128
+ - lib/authlogic_connect/oauth/user.rb
129
+ - lib/authlogic_connect/oauth/variables.rb
130
+ - lib/authlogic_connect/oauth.rb
131
+ - lib/authlogic_connect/openid/session.rb
132
+ - lib/authlogic_connect/openid/tokens/aol_token.rb
133
+ - lib/authlogic_connect/openid/tokens/blogger_token.rb
134
+ - lib/authlogic_connect/openid/tokens/flickr_token.rb
135
+ - lib/authlogic_connect/openid/tokens/my_openid_token.rb
136
+ - lib/authlogic_connect/openid/tokens/openid_token.rb
137
+ - lib/authlogic_connect/openid/user.rb
138
+ - lib/authlogic_connect/openid/variables.rb
139
+ - lib/authlogic_connect/openid.rb
140
+ - lib/authlogic_connect.rb
141
+ - lib/oauth_callback_filter.rb
142
+ - lib/token.rb
143
+ - rails/init.rb
144
+ has_rdoc: true
145
+ homepage: http://github.com/viatropos/authlogic-connect
146
+ licenses: []
147
+
148
+ post_install_message:
149
+ rdoc_options: []
150
+
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ segments:
158
+ - 0
159
+ version: "0"
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ segments:
165
+ - 0
166
+ version: "0"
167
+ requirements: []
168
+
169
+ rubyforge_project: authlogic-connect
170
+ rubygems_version: 1.3.6
171
+ signing_key:
172
+ specification_version: 3
173
+ summary: "Authlogic Connect: Let your app use all of Oauth and OpenID"
174
+ test_files: []
175
+