foreverman-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 (62) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.markdown +234 -0
  3. data/Rakefile +85 -0
  4. data/init.rb +1 -0
  5. data/lib/authlogic-connect.rb +39 -0
  6. data/lib/authlogic_connect/access_token.rb +61 -0
  7. data/lib/authlogic_connect/authlogic_connect.rb +46 -0
  8. data/lib/authlogic_connect/callback_filter.rb +19 -0
  9. data/lib/authlogic_connect/common.rb +10 -0
  10. data/lib/authlogic_connect/common/session.rb +30 -0
  11. data/lib/authlogic_connect/common/state.rb +45 -0
  12. data/lib/authlogic_connect/common/user.rb +77 -0
  13. data/lib/authlogic_connect/common/variables.rb +124 -0
  14. data/lib/authlogic_connect/engine.rb +14 -0
  15. data/lib/authlogic_connect/ext.rb +56 -0
  16. data/lib/authlogic_connect/oauth.rb +14 -0
  17. data/lib/authlogic_connect/oauth/helper.rb +20 -0
  18. data/lib/authlogic_connect/oauth/process.rb +75 -0
  19. data/lib/authlogic_connect/oauth/session.rb +62 -0
  20. data/lib/authlogic_connect/oauth/state.rb +60 -0
  21. data/lib/authlogic_connect/oauth/tokens/aol_token.rb +2 -0
  22. data/lib/authlogic_connect/oauth/tokens/facebook_token.rb +11 -0
  23. data/lib/authlogic_connect/oauth/tokens/foursquare_token.rb +15 -0
  24. data/lib/authlogic_connect/oauth/tokens/get_satisfaction_token.rb +9 -0
  25. data/lib/authlogic_connect/oauth/tokens/github_token.rb +14 -0
  26. data/lib/authlogic_connect/oauth/tokens/google_token.rb +41 -0
  27. data/lib/authlogic_connect/oauth/tokens/linked_in_token.rb +19 -0
  28. data/lib/authlogic_connect/oauth/tokens/meetup_token.rb +12 -0
  29. data/lib/authlogic_connect/oauth/tokens/myspace_token.rb +26 -0
  30. data/lib/authlogic_connect/oauth/tokens/netflix_token.rb +10 -0
  31. data/lib/authlogic_connect/oauth/tokens/oauth_token.rb +164 -0
  32. data/lib/authlogic_connect/oauth/tokens/ohloh_token.rb +9 -0
  33. data/lib/authlogic_connect/oauth/tokens/opensocial_token.rb +0 -0
  34. data/lib/authlogic_connect/oauth/tokens/twitter_token.rb +8 -0
  35. data/lib/authlogic_connect/oauth/tokens/vimeo_token.rb +18 -0
  36. data/lib/authlogic_connect/oauth/tokens/yahoo_token.rb +19 -0
  37. data/lib/authlogic_connect/oauth/user.rb +64 -0
  38. data/lib/authlogic_connect/oauth/variables.rb +64 -0
  39. data/lib/authlogic_connect/openid.rb +11 -0
  40. data/lib/authlogic_connect/openid/process.rb +74 -0
  41. data/lib/authlogic_connect/openid/session.rb +56 -0
  42. data/lib/authlogic_connect/openid/state.rb +48 -0
  43. data/lib/authlogic_connect/openid/tokens/aol_token.rb +0 -0
  44. data/lib/authlogic_connect/openid/tokens/blogger_token.rb +0 -0
  45. data/lib/authlogic_connect/openid/tokens/flickr_token.rb +0 -0
  46. data/lib/authlogic_connect/openid/tokens/my_openid_token.rb +3 -0
  47. data/lib/authlogic_connect/openid/tokens/openid_token.rb +9 -0
  48. data/lib/authlogic_connect/openid/user.rb +38 -0
  49. data/lib/authlogic_connect/openid/variables.rb +19 -0
  50. data/lib/authlogic_connect/rack_state.rb +19 -0
  51. data/lib/open_id_authentication.rb +127 -0
  52. data/rails/init.rb +19 -0
  53. data/test/controllers/test_users_controller.rb +21 -0
  54. data/test/database.yml +3 -0
  55. data/test/libs/database.rb +47 -0
  56. data/test/libs/user.rb +7 -0
  57. data/test/libs/user_session.rb +2 -0
  58. data/test/test_helper.rb +178 -0
  59. data/test/test_oauth.rb +178 -0
  60. data/test/test_openid.rb +71 -0
  61. data/test/test_user.rb +85 -0
  62. metadata +244 -0
@@ -0,0 +1,19 @@
1
+ module AuthlogicConnect::Openid::Variables
2
+ include AuthlogicConnect::Openid::State
3
+
4
+ # openid_provider = "blogger", "myopenid", etc.
5
+ # openid_identifier = "viatropos.myopenid.com", etc.
6
+ # openid_key = "viatropos"
7
+ # def openid_attributes
8
+ # [:openid_provider, :openid_identifier, :openid_key]
9
+ # end
10
+
11
+ def openid_identifier
12
+ auth_params[:openid_identifier] if auth_params?
13
+ end
14
+
15
+ def openid_provider
16
+ from_session_or_params(:openid_provider) if auth_controller?
17
+ end
18
+
19
+ end
@@ -0,0 +1,19 @@
1
+ class AuthlogicConnect::RackState
2
+ def initialize(app)
3
+ @app = app
4
+ end
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"
10
+ def call(env)
11
+ if env["rack.session"].nil?
12
+ raise "Make sure you are setting the session in Rack too! Place this in config/application.rb"
13
+ end
14
+ unless env["rack.session"][:auth_callback_method].blank?
15
+ env["REQUEST_METHOD"] = env["rack.session"].delete(:auth_callback_method).to_s.upcase
16
+ end
17
+ @app.call(env)
18
+ end
19
+ end
@@ -0,0 +1,127 @@
1
+ # copied from open_id_authentication plugin on github
2
+ require 'uri'
3
+ require 'openid'
4
+ require 'rack/openid'
5
+
6
+ module OpenIdAuthentication
7
+ def self.new(app)
8
+ store = OpenIdAuthentication.store
9
+ if store.nil?
10
+ Rails.logger.warn "OpenIdAuthentication.store is nil. Using in-memory store."
11
+ end
12
+
13
+ ::Rack::OpenID.new(app, OpenIdAuthentication.store)
14
+ end
15
+
16
+ def self.store
17
+ @@store
18
+ end
19
+
20
+ def self.store=(*store_option)
21
+ store, *parameters = *([ store_option ].flatten)
22
+
23
+ @@store = case store
24
+ when :memory
25
+ require 'openid/store/memory'
26
+ OpenID::Store::Memory.new
27
+ when :file
28
+ require 'openid/store/filesystem'
29
+ OpenID::Store::Filesystem.new(Rails.root.join('tmp/openids'))
30
+ when :memcache
31
+ require 'memcache'
32
+ require 'openid/store/memcache'
33
+ OpenID::Store::Memcache.new(MemCache.new(parameters))
34
+ else
35
+ store
36
+ end
37
+ end
38
+
39
+ self.store = nil
40
+
41
+ class Result
42
+ ERROR_MESSAGES = {
43
+ :missing => "Sorry, the OpenID server couldn't be found",
44
+ :invalid => "Sorry, but this does not appear to be a valid OpenID",
45
+ :canceled => "OpenID verification was canceled",
46
+ :failed => "OpenID verification failed",
47
+ :setup_needed => "OpenID verification needs setup"
48
+ }
49
+
50
+ def self.[](code)
51
+ new(code)
52
+ end
53
+
54
+ def initialize(code)
55
+ @code = code
56
+ end
57
+
58
+ def status
59
+ @code
60
+ end
61
+
62
+ ERROR_MESSAGES.keys.each { |state| define_method("#{state}?") { @code == state } }
63
+
64
+ def successful?
65
+ @code == :successful
66
+ end
67
+
68
+ def unsuccessful?
69
+ ERROR_MESSAGES.keys.include?(@code)
70
+ end
71
+
72
+ def message
73
+ ERROR_MESSAGES[@code]
74
+ end
75
+ end
76
+
77
+ protected
78
+ # The parameter name of "openid_identifier" is used rather than
79
+ # the Rails convention "open_id_identifier" because that's what
80
+ # the specification dictates in order to get browser auto-complete
81
+ # working across sites
82
+ def using_open_id?(identifier = nil) #:doc:
83
+ identifier ||= open_id_identifier
84
+ !identifier.blank? || request.env[Rack::OpenID::RESPONSE]
85
+ end
86
+
87
+ def authenticate_with_open_id(identifier = nil, options = {}, &block) #:doc:
88
+ identifier ||= open_id_identifier
89
+ if request.env[Rack::OpenID::RESPONSE]
90
+ complete_open_id_authentication(&block)
91
+ else
92
+ begin_open_id_authentication(identifier, options, &block)
93
+ end
94
+ end
95
+
96
+ private
97
+ def open_id_identifier
98
+ params[:openid_identifier] || params[:openid_url]
99
+ end
100
+
101
+ def begin_open_id_authentication(identifier, options = {})
102
+ options[:identifier] = identifier
103
+ value = Rack::OpenID.build_header(options)
104
+ response.headers[Rack::OpenID::AUTHENTICATE_HEADER] = value
105
+ head :unauthorized
106
+ end
107
+
108
+ def complete_open_id_authentication
109
+ response = request.env[Rack::OpenID::RESPONSE]
110
+ identifier = response.display_identifier
111
+ case response.status
112
+ when OpenID::Consumer::SUCCESS
113
+ yield Result[:successful], identifier,
114
+ OpenID::SReg::Response.from_success_response(response)
115
+ when :missing
116
+ yield Result[:missing], identifier, nil
117
+ when :invalid
118
+ yield Result[:invalid], identifier, nil
119
+ when OpenID::Consumer::CANCEL
120
+ yield Result[:canceled], identifier, nil
121
+ when OpenID::Consumer::FAILURE
122
+ yield Result[:failed], identifier, nil
123
+ when OpenID::Consumer::SETUP_NEEDED
124
+ yield Result[:setup_needed], response.setup_url, nil
125
+ end
126
+ end
127
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,19 @@
1
+ require "authlogic-connect"
2
+
3
+ # copied from open_id_authentication plugin on github
4
+
5
+ # this is the Rails 2.x equivalent.
6
+ # Rails 3 equivalent is in authlogic_connect/engine.rb
7
+ if Rails.version < '3'
8
+ config.gem 'rack-openid', :lib => 'rack/openid', :version => '>=0.2.1'
9
+ end
10
+
11
+ require 'open_id_authentication'
12
+
13
+ config.middleware.use OpenIdAuthentication
14
+ config.middleware.use AuthlogicConnect::CallbackFilter
15
+
16
+ config.after_initialize do
17
+ OpenID::Util.logger = Rails.logger
18
+ ActionController::Base.send :include, OpenIdAuthentication
19
+ end
@@ -0,0 +1,21 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ class UsersControllerTest < ActionController::TestCase
4
+
5
+ tests UsersController
6
+
7
+ context "when signed out" do
8
+ # setup { sign_out }
9
+
10
+ context "on GET to #new" do
11
+
12
+ setup { get :new }
13
+
14
+ should "do something???" do
15
+ puts "REQUEST: #{@user.inspect}"
16
+ end
17
+
18
+ end
19
+
20
+ end
21
+ end
data/test/database.yml ADDED
@@ -0,0 +1,3 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: ":memory:"
@@ -0,0 +1,47 @@
1
+
2
+ begin
3
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
4
+ rescue ArgumentError
5
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
6
+ end
7
+
8
+ ActiveRecord::Base.configurations = true
9
+
10
+ # this schema was directly copied from
11
+ # http://github.com/viatropos/authlogic-connect-example/blob/master/db/schema.rb
12
+ ActiveRecord::Schema.define(:version => 1) do
13
+
14
+ create_table :sessions, :force => true do |t|
15
+ t.string :session_id, :null => false
16
+ t.text :data
17
+ t.datetime :created_at
18
+ t.datetime :updated_at
19
+ end
20
+
21
+ create_table :access_tokens do |t|
22
+ t.integer :user_id
23
+ t.string :type, :limit => 30
24
+ t.string :key # how we identify the user, in case they logout and log back in
25
+ t.string :token, :limit => 1024 # This has to be huge because of Yahoo's excessively large tokens
26
+ t.string :secret
27
+ t.boolean :active # whether or not it's associated with the account
28
+ t.timestamps
29
+ end
30
+
31
+ create_table :users, :force => true do |t|
32
+ t.datetime :created_at
33
+ t.datetime :updated_at
34
+ t.string :login
35
+ t.string :email
36
+ t.string :crypted_password
37
+ t.string :password_salt
38
+ t.string :persistence_token, :null => false
39
+ t.integer :login_count, :default => 0, :null => false
40
+ t.datetime :last_request_at
41
+ t.datetime :last_login_at
42
+ t.datetime :current_login_at
43
+ t.string :last_login_ip
44
+ t.string :current_login_ip
45
+ end
46
+
47
+ end
data/test/libs/user.rb ADDED
@@ -0,0 +1,7 @@
1
+ class User < ActiveRecord::Base
2
+ acts_as_authentic do |config|
3
+ config.validate_email_field = false
4
+ config.validate_login_field = false
5
+ config.validate_password_field = false
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ class UserSession < Authlogic::Session::Base
2
+ end
@@ -0,0 +1,178 @@
1
+ require "rubygems"
2
+ require "ruby-debug"
3
+ gem 'test-unit'
4
+ require "test/unit"
5
+ require 'active_support'
6
+ require 'active_support/test_case'
7
+ require "active_record"
8
+ require "active_record/fixtures"
9
+ require 'action_controller'
10
+ require 'shoulda'
11
+ require 'mocha'
12
+
13
+ require File.dirname(__FILE__) + '/libs/database'
14
+ require File.dirname(__FILE__) + '/../lib/authlogic-connect' unless defined?(AuthlogicConnect)
15
+ require File.dirname(__FILE__) + '/libs/user'
16
+ require File.dirname(__FILE__) + '/libs/user_session'
17
+ require 'authlogic/test_case'
18
+
19
+ # A temporary fix to bring active record errors up to speed with rails edge.
20
+ # I need to remove this once the new gem is released. This is only here so my tests pass.
21
+ unless defined?(::ActiveModel)
22
+ class ActiveRecord::Errors
23
+ def [](key)
24
+ value = on(key)
25
+ value.is_a?(Array) ? value : [value].compact
26
+ end
27
+ end
28
+ end
29
+
30
+ AuthlogicConnect.config = {
31
+ :default => "twitter",
32
+ :connect => {
33
+ :twitter => {
34
+ :key => "my_key",
35
+ :secret => "my_secret",
36
+ :headers => {
37
+ "User-Agent" => "Safari",
38
+ "MyApp-Version" => "1.2"
39
+ },
40
+ :api_version => 1
41
+ },
42
+ :facebook => {
43
+ :key => "my_key",
44
+ :secret => "my_secret"
45
+ },
46
+ :foursquare => {
47
+ :key => "my_key",
48
+ :secret => "my_secret"
49
+ },
50
+ :google => {
51
+ :key => "my_key",
52
+ :secret => "my_secret"
53
+ },
54
+ :yahoo => {
55
+ :key => "my_key",
56
+ :secret => "my_secret"
57
+ },
58
+ :vimeo => {
59
+
60
+ }
61
+ }
62
+ }
63
+
64
+ # want to add a "method" property!
65
+ Authlogic::TestCase::MockRequest.class_eval do
66
+ def method
67
+ "POST"
68
+ end
69
+ end
70
+
71
+ module ControllerHelpers
72
+ def controller_name
73
+ "users"
74
+ end
75
+
76
+ def action_name
77
+ "create"
78
+ end
79
+
80
+ def url_for(options = {})
81
+ p = []
82
+ options.each do |k,v|
83
+ p << "#{k}=#{v}"
84
+ end
85
+ p = "?#{p.join("&")}"
86
+ url = "http://localhost:3000/users#{p}"
87
+ end
88
+
89
+ def session=(value)
90
+ @session = value
91
+ end
92
+ end
93
+ Authlogic::ControllerAdapters::AbstractAdapter.send(:include, ControllerHelpers)
94
+
95
+ Authlogic::CryptoProviders::AES256.key = "myafdsfddddddddddddddddddddddddddddddddddddddddddddddd"
96
+
97
+ class ActiveSupport::TestCase
98
+ include ActiveRecord::TestFixtures
99
+ self.fixture_path = File.dirname(__FILE__) + "/fixtures"
100
+ self.use_transactional_fixtures = false
101
+ self.use_instantiated_fixtures = false
102
+ self.pre_loaded_fixtures = false
103
+ fixtures :all
104
+ setup :activate_authlogic
105
+
106
+ def create_token
107
+ token = OAuth::RequestToken.new("twitter", "key", "secret")
108
+ token.params = {
109
+ :oauth_callback_confirmed => "true",
110
+ :oauth_token_secret => "secret",
111
+ :oauth_token => "key"
112
+ }
113
+ token.consumer = OAuth::Consumer.new("key", "secret",
114
+ :site => "http://twitter.com",
115
+ :proxy => nil,
116
+ :oauth_version => "1.0",
117
+ :request_token_path => "/oauth/request_token",
118
+ :authorize_path => "/oauth/authorize",
119
+ :scheme => :header,
120
+ :signature_method => "HMAC-SHA1",
121
+ :authorize_url => "http://twitter.com/oauth/authenticate",
122
+ :access_token_path => "/oauth/access_token"
123
+ )
124
+ token
125
+ end
126
+
127
+ private
128
+ def password_for(user)
129
+ case user
130
+ when users(:ben)
131
+ "benrocks"
132
+ when users(:zack)
133
+ "zackrocks"
134
+ end
135
+ end
136
+
137
+ def http_basic_auth_for(user = nil, &block)
138
+ unless user.blank?
139
+ controller.http_user = user.login
140
+ controller.http_password = password_for(user)
141
+ end
142
+ yield
143
+ controller.http_user = controller.http_password = nil
144
+ end
145
+
146
+ def set_cookie_for(user, id = nil)
147
+ controller.cookies["user_credentials"] = {:value => user.persistence_token, :expires => nil}
148
+ end
149
+
150
+ def unset_cookie
151
+ controller.cookies["user_credentials"] = nil
152
+ end
153
+
154
+ def set_params_for(user, id = nil)
155
+ controller.params["user_credentials"] = user.single_access_token
156
+ end
157
+
158
+ def unset_params
159
+ controller.params["user_credentials"] = nil
160
+ end
161
+
162
+ def set_request_content_type(type)
163
+ controller.request_content_type = type
164
+ end
165
+
166
+ def unset_request_content_type
167
+ controller.request_content_type = nil
168
+ end
169
+
170
+ def set_session_for(user, id = nil)
171
+ controller.session["user_credentials"] = user.persistence_token
172
+ controller.session["user_credentials_id"] = user.id
173
+ end
174
+
175
+ def unset_session
176
+ controller.session["user_credentials"] = controller.session["user_credentials_id"] = nil
177
+ end
178
+ end