authlogic-connect 0.0.3.2 → 0.0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +16 -5
- data/Rakefile +1 -1
- data/lib/authlogic-connect.rb +4 -42
- data/lib/authlogic_connect/callback_filter.rb +6 -2
- data/lib/authlogic_connect/common.rb +1 -0
- data/lib/authlogic_connect/common/ext.rb +55 -0
- data/lib/authlogic_connect/oauth/process.rb +2 -1
- data/lib/authlogic_connect/oauth/user.rb +4 -2
- data/lib/authlogic_connect/openid/user.rb +10 -11
- data/test/database.yml +3 -0
- data/test/test_authlogic_connect.rb +13 -0
- data/test/test_helper.rb +53 -0
- metadata +7 -3
data/README.markdown
CHANGED
@@ -160,11 +160,7 @@ That's it! The rest is taken care of for you.
|
|
160
160
|
- Save user
|
161
161
|
- Finish block, render page
|
162
162
|
|
163
|
-
##
|
164
|
-
|
165
|
-
This has no tests! I had to build this in a weekend and am not fluent with Shoulda, which I'd like to use. One of these days when I can breathe.
|
166
|
-
|
167
|
-
## Goals
|
163
|
+
## Project Goals
|
168
164
|
|
169
165
|
1. It should require the end user ONE CLICK to create an account with your site.
|
170
166
|
2. It should not depend on Javascript
|
@@ -172,6 +168,21 @@ This has no tests! I had to build this in a weekend and am not fluent with Shou
|
|
172
168
|
4. You should never have to touch the User/Session model/controller/migration if you are a just looking to get up and running quickly.
|
173
169
|
5. You should be able to plugin ruby libraries that wrap an api, such as TwitterAuth via `@user.twitter`, and LinkedIn via `@user.linked_in`. Just because it's that easy.
|
174
170
|
|
171
|
+
### Tests
|
172
|
+
|
173
|
+
This has no tests! I had to build this in a day and am not fluent with Shoulda, which I'd like to use. It should have lots of tests to make sure all permutations of login and account association work perfectly.
|
174
|
+
|
175
|
+
Goal:
|
176
|
+
|
177
|
+
- Test Framework: [Shoulda](http://github.com/thoughtbot/shoulda)
|
178
|
+
- Autotest with Shoulda
|
179
|
+
- Testing style like [Paperclip Tests](http://github.com/thoughtbot/paperclip/tree/master/test/)
|
180
|
+
- Rails 2.3+ and Rails 3 Compatability
|
181
|
+
|
182
|
+
I have no idea how to get up and running with Autotest and Shoulda right now. If you know, I'd love to get the answer on Stack Overflow:
|
183
|
+
|
184
|
+
[http://stackoverflow.com/questions/2823224/what-test-environment-setup-do-committers-use-in-the-ruby-community](http://stackoverflow.com/questions/2823224/what-test-environment-setup-do-committers-use-in-the-ruby-community)
|
185
|
+
|
175
186
|
## TODO
|
176
187
|
|
177
188
|
- Change `register_with_oauth` and related to `register_method` and `login_method`: oauth, openid, traditional
|
data/Rakefile
CHANGED
@@ -6,7 +6,7 @@ require 'rake/gempackagetask'
|
|
6
6
|
spec = Gem::Specification.new do |s|
|
7
7
|
s.name = "authlogic-connect"
|
8
8
|
s.author = "Lance Pollard"
|
9
|
-
s.version = "0.0.3.
|
9
|
+
s.version = "0.0.3.3"
|
10
10
|
s.summary = "Authlogic Connect: Let your app use all of Oauth and OpenID"
|
11
11
|
s.homepage = "http://github.com/viatropos/authlogic-connect"
|
12
12
|
s.email = "lancejpollard@gmail.com"
|
data/lib/authlogic-connect.rb
CHANGED
@@ -3,49 +3,11 @@ require 'authlogic'
|
|
3
3
|
require 'oauth'
|
4
4
|
require 'oauth2'
|
5
5
|
|
6
|
-
|
7
|
-
# Throw callback rack app into the middleware stack
|
8
|
-
# TODO: Somehow do this for Rails 3?
|
9
|
-
# For now it is in the sample Rails 3 app
|
10
|
-
=begin
|
11
|
-
ActionController::Dispatcher.middleware = ActionController::MiddlewareStack.new do |m|
|
12
|
-
ActionController::Dispatcher.middleware.each do |klass|
|
13
|
-
m.use klass
|
14
|
-
end
|
15
|
-
m.use AuthlogicConnect::CallbackFilter
|
16
|
-
end
|
17
|
-
=end
|
18
6
|
this = File.dirname(__FILE__)
|
19
7
|
library = "#{this}/authlogic_connect"
|
20
8
|
|
21
|
-
class Hash
|
22
|
-
def recursively_symbolize_keys!
|
23
|
-
self.symbolize_keys!
|
24
|
-
self.values.each do |v|
|
25
|
-
if v.is_a? Hash
|
26
|
-
v.recursively_symbolize_keys!
|
27
|
-
elsif v.is_a? Array
|
28
|
-
v.recursively_symbolize_keys!
|
29
|
-
end
|
30
|
-
end
|
31
|
-
self
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
class Array
|
36
|
-
def recursively_symbolize_keys!
|
37
|
-
self.each do |item|
|
38
|
-
if item.is_a? Hash
|
39
|
-
item.recursively_symbolize_keys!
|
40
|
-
elsif item.is_a? Array
|
41
|
-
item.recursively_symbolize_keys!
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
9
|
module AuthlogicConnect
|
48
|
-
|
10
|
+
KEY = "connect"
|
49
11
|
|
50
12
|
class << self
|
51
13
|
|
@@ -63,15 +25,15 @@ module AuthlogicConnect
|
|
63
25
|
end
|
64
26
|
|
65
27
|
def credentials(service)
|
66
|
-
key("
|
28
|
+
key("#{KEY}.#{service.to_s}")
|
67
29
|
end
|
68
30
|
|
69
31
|
def services
|
70
|
-
key(
|
32
|
+
key(KEY)
|
71
33
|
end
|
72
34
|
|
73
35
|
def service_names
|
74
|
-
|
36
|
+
services.keys.collect(&:to_s)
|
75
37
|
end
|
76
38
|
|
77
39
|
def include?(service)
|
@@ -3,12 +3,16 @@ class AuthlogicConnect::CallbackFilter
|
|
3
3
|
@app = app
|
4
4
|
end
|
5
5
|
|
6
|
+
# this intercepts how the browser interprets the url.
|
7
|
+
# so we override it and say,
|
8
|
+
# "if we've stored a variable in the session called :auth_callback_method,
|
9
|
+
# then convert that into a POST call so we re-call the original method"
|
6
10
|
def call(env)
|
7
11
|
if env["rack.session"].nil?
|
8
12
|
raise "Make sure you are setting the session in Rack too! Place this in config/application.rb"
|
9
13
|
end
|
10
|
-
unless env["rack.session"][:
|
11
|
-
env["REQUEST_METHOD"] = env["rack.session"].delete(:
|
14
|
+
unless env["rack.session"][:auth_callback_method].blank?
|
15
|
+
env["REQUEST_METHOD"] = env["rack.session"].delete(:auth_callback_method).to_s.upcase
|
12
16
|
end
|
13
17
|
@app.call(env)
|
14
18
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
class String
|
2
|
+
# normalizes an OpenID according to http://openid.net/specs/openid-authentication-2_0.html#normalization
|
3
|
+
def normalize_identifier
|
4
|
+
# clean up whitespace
|
5
|
+
identifier = self.dup.strip
|
6
|
+
|
7
|
+
# if an XRI has a prefix, strip it.
|
8
|
+
identifier.gsub!(/xri:\/\//i, '')
|
9
|
+
|
10
|
+
# dodge XRIs -- TODO: validate, don't just skip.
|
11
|
+
unless ['=', '@', '+', '$', '!', '('].include?(identifier.at(0))
|
12
|
+
# does it begin with http? if not, add it.
|
13
|
+
identifier = "http://#{identifier}" unless identifier =~ /^http/i
|
14
|
+
|
15
|
+
# strip any fragments
|
16
|
+
identifier.gsub!(/\#(.*)$/, '')
|
17
|
+
|
18
|
+
begin
|
19
|
+
uri = URI.parse(identifier)
|
20
|
+
uri.scheme = uri.scheme.downcase # URI should do this
|
21
|
+
identifier = uri.normalize.to_s
|
22
|
+
rescue URI::InvalidURIError
|
23
|
+
raise InvalidOpenId.new("#{identifier} is not an OpenID identifier")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
return identifier
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class Hash
|
32
|
+
def recursively_symbolize_keys!
|
33
|
+
self.symbolize_keys!
|
34
|
+
self.values.each do |v|
|
35
|
+
if v.is_a? Hash
|
36
|
+
v.recursively_symbolize_keys!
|
37
|
+
elsif v.is_a? Array
|
38
|
+
v.recursively_symbolize_keys!
|
39
|
+
end
|
40
|
+
end
|
41
|
+
self
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class Array
|
46
|
+
def recursively_symbolize_keys!
|
47
|
+
self.each do |item|
|
48
|
+
if item.is_a? Hash
|
49
|
+
item.recursively_symbolize_keys!
|
50
|
+
elsif item.is_a? Array
|
51
|
+
item.recursively_symbolize_keys!
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -34,13 +34,14 @@ module AuthlogicConnect::Oauth
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def save_oauth_callback
|
37
|
+
puts "save_oauth_callback"
|
37
38
|
# Store the class which is redirecting, so we can ensure other classes
|
38
39
|
# don't get confused and attempt to use the response
|
39
40
|
auth_session[:oauth_request_class] = self.class.name
|
40
41
|
auth_session[:oauth_provider] = auth_params[:oauth_provider]
|
41
42
|
|
42
43
|
# Tell our rack callback filter what method the current request is using
|
43
|
-
auth_session[:
|
44
|
+
auth_session[:auth_callback_method] = auth_controller.request.method
|
44
45
|
end
|
45
46
|
|
46
47
|
def save_auth_session(request)
|
@@ -59,9 +59,11 @@ module AuthlogicConnect::Oauth
|
|
59
59
|
def authenticating_with_oauth?
|
60
60
|
return false unless oauth_provider
|
61
61
|
# Initial request when user presses one of the button helpers
|
62
|
-
(auth_params && !auth_params[:register_with_oauth].blank?)
|
62
|
+
initial_request = (auth_params && !auth_params[:register_with_oauth].blank?)
|
63
63
|
# When the oauth provider responds and we made the initial request
|
64
|
-
(oauth_response && auth_session && auth_session[:oauth_request_class] == self.class.name)
|
64
|
+
initial_response = (oauth_response && auth_session && auth_session[:oauth_request_class] == self.class.name)
|
65
|
+
|
66
|
+
return initial_request || initial_response
|
65
67
|
end
|
66
68
|
|
67
69
|
def authenticate_with_oauth
|
@@ -23,15 +23,14 @@ module AuthlogicConnect::Openid
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def openid_identifier=(value)
|
26
|
-
write_attribute(:openid_identifier, value.blank? ? nil :
|
26
|
+
write_attribute(:openid_identifier, value.blank? ? nil : value.to_s.normalize_identifier)
|
27
27
|
reset_persistence_token if openid_identifier_changed?
|
28
|
-
rescue
|
28
|
+
rescue Exception => e
|
29
29
|
@openid_error = e.message
|
30
30
|
end
|
31
31
|
|
32
32
|
def save_with_openid(perform_validation = true, &block)
|
33
33
|
return false if perform_validation && block_given? && authenticating_with_openid? && !authenticating_with_openid
|
34
|
-
return false if new_record? && !openid_complete?
|
35
34
|
return true
|
36
35
|
end
|
37
36
|
|
@@ -42,15 +41,15 @@ module AuthlogicConnect::Openid
|
|
42
41
|
end
|
43
42
|
|
44
43
|
def using_openid?
|
45
|
-
respond_to?(:openid_identifier) && !openid_identifier.blank?
|
44
|
+
respond_to?(:openid_identifier) && !auth_params[:openid_identifier].blank?
|
46
45
|
end
|
47
46
|
|
48
47
|
def openid_complete?
|
49
|
-
|
48
|
+
auth_session[:openid_attributes]
|
50
49
|
end
|
51
50
|
|
52
51
|
def authenticating_with_openid?
|
53
|
-
session_class.activated? && ((using_openid?
|
52
|
+
session_class.activated? && ((using_openid?) || openid_complete?)
|
54
53
|
end
|
55
54
|
|
56
55
|
def validate_password_with_openid?
|
@@ -60,15 +59,15 @@ module AuthlogicConnect::Openid
|
|
60
59
|
def authenticating_with_openid
|
61
60
|
@openid_error = nil
|
62
61
|
if !openid_complete?
|
63
|
-
|
62
|
+
# Tell our rack callback filter what method the current request is using
|
63
|
+
auth_session[:auth_callback_method] = auth_controller.request.method
|
64
|
+
auth_session[:openid_attributes] = attributes_to_save
|
64
65
|
else
|
65
|
-
self.attributes
|
66
|
-
auth_session[:openid_attributes] = nil
|
66
|
+
self.attributes = auth_session.delete(:openid_attributes)
|
67
67
|
end
|
68
68
|
|
69
69
|
options = {}
|
70
|
-
options[:return_to] = auth_controller.url_for(:for_model => "1"
|
71
|
-
|
70
|
+
options[:return_to] = auth_controller.url_for(:for_model => "1", :controller => "users", :action => "create")
|
72
71
|
auth_controller.send(:authenticate_with_open_id, openid_identifier, options) do |result, openid_identifier, registration|
|
73
72
|
if result.unsuccessful?
|
74
73
|
@openid_error = result.message
|
data/test/database.yml
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'test/helper'
|
2
|
+
|
3
|
+
class AuthlogicConnectTest < Test::Unit::TestCase
|
4
|
+
context "AuthlogicConnect.config" do
|
5
|
+
setup do
|
6
|
+
AuthlogicConnect.config = {}
|
7
|
+
end
|
8
|
+
|
9
|
+
should "have an empty configuration hash" do
|
10
|
+
assert_equal true, AuthlogicConnect.config.empty?
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'tempfile'
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
require 'shoulda'
|
6
|
+
gem 'activerecord', '~>3.0.0'
|
7
|
+
gem 'activesupport', '~>3.0.0'
|
8
|
+
gem 'actionpack', '~>3.0.0'
|
9
|
+
require 'active_record'
|
10
|
+
require 'active_record/version'
|
11
|
+
require 'active_support'
|
12
|
+
require 'action_pack'
|
13
|
+
gem "ruby-openid"
|
14
|
+
gem 'rack-openid', '>=0.2.1'
|
15
|
+
gem "authlogic", :git => "git://github.com/odorcicd/authlogic.git", :branch => "rails3"
|
16
|
+
require 'authlogic'
|
17
|
+
gem "oauth"
|
18
|
+
gem "oauth2"
|
19
|
+
|
20
|
+
puts "Testing against version #{ActiveRecord::VERSION::STRING}"
|
21
|
+
|
22
|
+
begin
|
23
|
+
require 'ruby-debug'
|
24
|
+
rescue LoadError => e
|
25
|
+
puts "debugger disabled"
|
26
|
+
end
|
27
|
+
|
28
|
+
ROOT = File.join(File.dirname(__FILE__), '..')
|
29
|
+
|
30
|
+
def silence_warnings
|
31
|
+
old_verbose, $VERBOSE = $VERBOSE, nil
|
32
|
+
yield
|
33
|
+
ensure
|
34
|
+
$VERBOSE = old_verbose
|
35
|
+
end
|
36
|
+
|
37
|
+
class Test::Unit::TestCase
|
38
|
+
def setup
|
39
|
+
silence_warnings do
|
40
|
+
Object.const_set(:Rails, stub('Rails', :root => ROOT, :env => 'test'))
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
$LOAD_PATH << File.join(ROOT, 'lib')
|
46
|
+
$LOAD_PATH << File.join(ROOT, 'lib', 'authlogic-connect')
|
47
|
+
|
48
|
+
require File.join(ROOT, 'lib', 'authlogic-connect.rb')
|
49
|
+
|
50
|
+
FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
|
51
|
+
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
52
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
53
|
+
ActiveRecord::Base.establish_connection(config['test'])
|
metadata
CHANGED
@@ -6,8 +6,8 @@ version: !ruby/object:Gem::Version
|
|
6
6
|
- 0
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.0.3.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Lance Pollard
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-05-
|
18
|
+
date: 2010-05-12 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -109,6 +109,7 @@ files:
|
|
109
109
|
- MIT-LICENSE
|
110
110
|
- lib/authlogic-connect.rb
|
111
111
|
- lib/authlogic_connect/callback_filter.rb
|
112
|
+
- lib/authlogic_connect/common/ext.rb
|
112
113
|
- lib/authlogic_connect/common/session.rb
|
113
114
|
- lib/authlogic_connect/common/user.rb
|
114
115
|
- lib/authlogic_connect/common/variables.rb
|
@@ -140,6 +141,9 @@ files:
|
|
140
141
|
- lib/authlogic_connect/openid.rb
|
141
142
|
- lib/authlogic_connect/token.rb
|
142
143
|
- rails/init.rb
|
144
|
+
- test/database.yml
|
145
|
+
- test/test_authlogic_connect.rb
|
146
|
+
- test/test_helper.rb
|
143
147
|
has_rdoc: true
|
144
148
|
homepage: http://github.com/viatropos/authlogic-connect
|
145
149
|
licenses: []
|