lsdr-authlogic-connect 0.0.3.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.markdown +240 -0
  3. data/Rakefile +71 -0
  4. data/init.rb +1 -0
  5. data/lib/authlogic-connect.rb +27 -0
  6. data/lib/authlogic_connect/authlogic_connect.rb +46 -0
  7. data/lib/authlogic_connect/callback_filter.rb +19 -0
  8. data/lib/authlogic_connect/common.rb +10 -0
  9. data/lib/authlogic_connect/common/session.rb +27 -0
  10. data/lib/authlogic_connect/common/state.rb +16 -0
  11. data/lib/authlogic_connect/common/user.rb +115 -0
  12. data/lib/authlogic_connect/common/variables.rb +77 -0
  13. data/lib/authlogic_connect/engine.rb +14 -0
  14. data/lib/authlogic_connect/ext.rb +56 -0
  15. data/lib/authlogic_connect/oauth.rb +14 -0
  16. data/lib/authlogic_connect/oauth/helper.rb +20 -0
  17. data/lib/authlogic_connect/oauth/process.rb +68 -0
  18. data/lib/authlogic_connect/oauth/session.rb +58 -0
  19. data/lib/authlogic_connect/oauth/state.rb +54 -0
  20. data/lib/authlogic_connect/oauth/tokens/facebook_token.rb +11 -0
  21. data/lib/authlogic_connect/oauth/tokens/get_satisfaction_token.rb +9 -0
  22. data/lib/authlogic_connect/oauth/tokens/google_token.rb +41 -0
  23. data/lib/authlogic_connect/oauth/tokens/linked_in_token.rb +19 -0
  24. data/lib/authlogic_connect/oauth/tokens/myspace_token.rb +26 -0
  25. data/lib/authlogic_connect/oauth/tokens/oauth_token.rb +131 -0
  26. data/lib/authlogic_connect/oauth/tokens/opensocial_token.rb +0 -0
  27. data/lib/authlogic_connect/oauth/tokens/twitter_token.rb +8 -0
  28. data/lib/authlogic_connect/oauth/tokens/vimeo_token.rb +18 -0
  29. data/lib/authlogic_connect/oauth/tokens/yahoo_token.rb +19 -0
  30. data/lib/authlogic_connect/oauth/user.rb +68 -0
  31. data/lib/authlogic_connect/oauth/variables.rb +55 -0
  32. data/lib/authlogic_connect/openid.rb +11 -0
  33. data/lib/authlogic_connect/openid/process.rb +30 -0
  34. data/lib/authlogic_connect/openid/session.rb +78 -0
  35. data/lib/authlogic_connect/openid/state.rb +47 -0
  36. data/lib/authlogic_connect/openid/tokens/aol_token.rb +0 -0
  37. data/lib/authlogic_connect/openid/tokens/blogger_token.rb +0 -0
  38. data/lib/authlogic_connect/openid/tokens/flickr_token.rb +0 -0
  39. data/lib/authlogic_connect/openid/tokens/my_openid_token.rb +3 -0
  40. data/lib/authlogic_connect/openid/tokens/openid_token.rb +9 -0
  41. data/lib/authlogic_connect/openid/user.rb +62 -0
  42. data/lib/authlogic_connect/openid/variables.rb +19 -0
  43. data/lib/authlogic_connect/token.rb +53 -0
  44. data/lib/open_id_authentication.rb +128 -0
  45. data/rails/init.rb +19 -0
  46. data/test/controllers/test_users_controller.rb +21 -0
  47. data/test/libs/database.rb +48 -0
  48. data/test/libs/user.rb +3 -0
  49. data/test/libs/user_session.rb +2 -0
  50. data/test/old.rb +53 -0
  51. data/test/test_authlogic_connect.rb +13 -0
  52. data/test/test_helper.rb +153 -0
  53. data/test/test_user.rb +255 -0
  54. metadata +247 -0
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Lance Pollard (lancejpollard@gmail.com)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,240 @@
1
+ # AuthlogicConnect
2
+
3
+ AuthlogicConnect is an extension of the Authlogic library to add extensive Oauth and OpenID support.
4
+
5
+ It allows you to login through any of the 30+ Oauth and OpenID providers on the Internet.
6
+
7
+ That makes life easy and gives you a lot of power.
8
+
9
+ Here's a **[live example](http://authlogic-connect.heroku.com)** on Heroku ([with source](http://github.com/viatropos/authlogic-connect-example)).
10
+
11
+ ## Supported Providers
12
+
13
+ AuthlogicConnect currently allows you to login with 7 Oauth providers and all the OpenID providers. Feel free to add support for more as you need them.
14
+
15
+ ### Oauth
16
+
17
+ - Twitter
18
+ - Facebook
19
+ - Google
20
+ - LinkedIn
21
+ - MySpace
22
+ - Vimeo
23
+ - Yahoo
24
+
25
+ ### OpenID
26
+
27
+ - MyOpenID
28
+
29
+ ### Lists of all known providers here:
30
+
31
+ - [Oauth Providers](http://wiki.oauth.net/ServiceProviders)
32
+ - [OpenID Providers](http://en.wikipedia.org/wiki/List_of_OpenID_providers)
33
+ - [More OpenID](http://openid.net/get-an-openid/)
34
+
35
+ ## Install and use
36
+
37
+ ### 1. Install AuthlogicConnect
38
+
39
+ sudo gem install authlogic-connect
40
+
41
+ ### 2. Add the gem dependencies in your config:
42
+
43
+ Rails 2.3.x: `config/environment.rb`
44
+
45
+ config.gem "json"
46
+ config.gem "authlogic"
47
+ config.gem "oauth"
48
+ config.gem "oauth2"
49
+ config.gem "authlogic-connect"
50
+
51
+ Rails 3: `Gemfile`
52
+
53
+ gem "ruby-openid"
54
+ gem "rack-openid", ">=0.2.1", :require => "rack/openid"
55
+ gem "authlogic", :git => "git://github.com/odorcicd/authlogic.git", :branch => "rails3"
56
+ gem "oauth"
57
+ gem "oauth2"
58
+ gem "authlogic-connect"
59
+
60
+ ### 3. Add the `OpenIdAuthentication.store`
61
+
62
+ Do to [some strange problem](http://github.com/openid/ruby-openid/issues#issue/1) I have yet to really understand, Rails 2.3.5 doesn't like when `OpenIdAuthentication.store` is null, which means it uses the "in memory" store and for some reason fails.
63
+
64
+ So as a fix, if you are using Rails < 3, add these at the end of your `config/environment.rb` files:
65
+
66
+ In development mode:
67
+
68
+ OpenIdAuthentication.store = :file
69
+
70
+ In production (on Heroku primarily)
71
+
72
+ OpenIdAuthentication.store = :memcache
73
+
74
+ ### 4. Add the Migrations
75
+
76
+ See the [Rails 2 Example](http://github.com/viatropos/authlogic-connect-example-rails2) and [Rails 3 Example](http://github.com/viatropos/authlogic-connect-example) projects to see what you need. Will add a generator sometime.
77
+
78
+ Files needed are:
79
+
80
+ - models: User, UserSession
81
+ - controllers: UsersController, UserSessionsController, ApplicationController
82
+ - migrations: create\_users, create\_sessions, create\_tokens
83
+ - initializers: config/authlogic.example.yml, config/initializers/authlogic_connect_config.rb
84
+ - routes
85
+
86
+ ### 5. Configure your keys
87
+
88
+ In `config/authlogic.yml`, write your keys and secrets for each service you would like to support. You have to manually go to the websites and register with the service provider (list of those links coming soon, in token classes for now).
89
+
90
+ connect:
91
+ twitter:
92
+ key: "my_key"
93
+ secret: "my_secret"
94
+ label: "Twitter"
95
+ facebook:
96
+ key: "my_key"
97
+ secret: "my_secret"
98
+ label: "Facebook"
99
+ google:
100
+ key: "my_key"
101
+ secret: "my_secret"
102
+ label: "Google"
103
+ yahoo:
104
+ key: "my_key"
105
+ secret: "my_secret"
106
+ label: "Yahoo"
107
+ myspace:
108
+ key: "my_key"
109
+ secret: "my_secret"
110
+ vimeo:
111
+ key: "my_key"
112
+ secret: "my_secret"
113
+ linked_in:
114
+ key: "my_key"
115
+ secret: "my_secret"
116
+
117
+ These are then loaded via the initializer script in `config/initializers/authlogic_connect_config.rb`:
118
+
119
+ AuthlogicConnect.config = YAML.load_file("config/authlogic.yml")
120
+
121
+ ### 6. Make sure you save your objects properly
122
+
123
+ Because of the redirects involved in Oauth and OpenID, you MUST pass a block to the `save` method in your UsersController and UserSessionsController:
124
+
125
+ @user_session.save do |result|
126
+ if result
127
+ flash[:notice] # "Login successful!"
128
+ redirect_back_or_default account_url
129
+ else
130
+ render :action => :new
131
+ end
132
+ end
133
+
134
+ If you don't use the block, we will get a DoubleRender error. We need the block to jump out of the rendering while redirecting.
135
+
136
+ ### 7. Add Parameters to Forms in your Views
137
+
138
+ There are 3 things to include in your views.
139
+
140
+ First, you must specify whether this is for _registration_ or _login_. This is stored in the `authentication_type` key with a value of `user` for registration and `session` for login:
141
+
142
+ %input{:type => :hidden, :name => :authentication_type, :value => :user}
143
+
144
+ Second, if you are using Oauth, you must include an input with name `oauth_provider` and value `twitter` or whatever other provider you might want (see example apps for dynamic example).
145
+
146
+ %input{:type => :radio, :id => :twitter_oauth_provider, :name => :oauth_provider, :value => :twitter}
147
+
148
+ Finally, if you are using OpenID, you must include an input with name `openid_identifier`, which is a text field with the value the user types in for their address:
149
+
150
+ %input.nice{:type => :text, :name => :openid_identifier}
151
+
152
+ Those are passed as parameters to Authlogic, and the complicated details are abstracted away.
153
+
154
+ ## Overview of the User Experience
155
+
156
+ There are 3 ways you a user can login with AuthlogicConnect:
157
+
158
+ 1. Clicking an Oauth Provider
159
+ 2. Clicking an OpenID Provider and entering in their username
160
+ 3. Manually typing in a full OpenID address
161
+
162
+ Oauth is very different from OpenID, but this aims to make them work the same.
163
+
164
+ ## Examples
165
+
166
+ These are examples of what you can get from a User. Code is placed in controller for demo purposes, it should be abstracted into the model.
167
+
168
+ ### API
169
+
170
+ User model has the following public accessors and methods. This example assumes:
171
+
172
+ - You've associated your Google, OpenID, and Twitter accounts with this app.
173
+ - You're currently logged in via Google.
174
+
175
+ Inside the `show` method in a controller...
176
+
177
+ def show
178
+ @user = @current_user
179
+
180
+ puts @user.tokens #=> [
181
+ #<OpenidToken id: 12, user_id: 9, type: "OpenidToken", key: "http://my-openid-login.myopenid.com/", token: nil, secret: nil, active: nil, created_at: "2010-05-24 14:52:19", updated_at: "2010-05-24 14:52:19">,
182
+ #<TwitterToken id: 13, user_id: 9, type: "TwitterToken", key: "my-twitter-id-123", token: "twitter-token", secret: "twitter-secret", active: nil, created_at: "2010-05-24 15:03:05", updated_at: "2010-05-24 15:03:05">,
183
+ #<GoogleToken id: 14, user_id: 9, type: "GoogleToken", key: "my-email@gmail.com", token: "google-token", secret: "google-secret", active: nil, created_at: "2010-05-24 15:09:04", updated_at: "2010-05-24 15:09:04">]
184
+
185
+ puts @user.tokens.length #=> 3
186
+
187
+ # currently logged in with...
188
+ puts @user.active_token #=> #<GoogleToken id: 14, user_id: 9, type: "GoogleToken", key: "my-email@gmail.com", token: "google-token", secret: "google-secret", active: nil, created_at: "2010-05-24 15:09:04", updated_at: "2010-05-24 15:09:04">
189
+
190
+ puts @user.authenticated_with #=> ["twitter", "openid", "google"]
191
+ puts @user.authenticated_with?(:twitter) #=> true
192
+ puts @user.authenticated_with?(:facebook) #=> false
193
+
194
+ puts @user.has_token?(:google) #=> true
195
+
196
+ puts @user.get_token(:google) #=> #<GoogleToken id: 14, user_id: 9, type: "GoogleToken", key: "my-email@gmail.com", token: "google-token", secret: "google-secret", active: nil, created_at: "2010-05-24 15:09:04", updated_at: "2010-05-24 15:09:04">
197
+
198
+ # change active_token
199
+ @user.active_token = @user.get_token(:twitter)
200
+ puts @user.active_token #=> #<TwitterToken id: 13, user_id: 9, type: "TwitterToken", key: "my-twitter-id-123", token: "twitter-token", secret: "twitter-secret", active: nil, created_at: "2010-05-24 15:03:05", updated_at: "2010-05-24 15:03:05">
201
+
202
+ # access oauth api
203
+ @twitter = @user.active_token
204
+ @twitter_profile = JSON.parse(@twitter.get("/account/verify_credentials.json").body) #=> twitter api stuff
205
+ # ...
206
+ end
207
+
208
+ ### Get Facebook Data
209
+
210
+ If they've associated their Facebook account with your site, you can access Facebook data.
211
+
212
+ def show
213
+ @user = @current_user
214
+ token = @user.active_token # assuming this is FacebookToken
215
+ facebook = JSON.parse(token.get("/me"))
216
+ @profile = {
217
+ :id => facebook["id"],
218
+ :name => facebook["name"],
219
+ :photo => "https://graph.facebook.com/#{facebook["id"]}/picture",
220
+ :link => facebook["link"],
221
+ :title => "Facebook"
222
+ }
223
+ @profile = @user.profile
224
+ end
225
+
226
+ ## Helpful links
227
+
228
+ * **Authlogic:** [http://github.com/binarylogic/authlogic](http://github.com/binarylogic/authlogic)
229
+ * **AuthlogicConnect Example Project:** [http://github.com/viatropos/authlogic-connect-example](http://github.com/viatropos/authlogic-connect-example)
230
+ * **Live example with Twitter and Facebook using Rails 3:** [http://authlogic-connect.heroku.com](http://authlogic-connect.heroku.com)
231
+ * **Rails 2.3.5 Example:** [http://github.com/viatropos/authlogic-connect-example-rails2](http://github.com/viatropos/authlogic-connect-example-rails2)
232
+ * **Rubygems Repository:** [http://rubygems.org/gems/authlogic-connect](http://rubygems.org/gems/authlogic-connect)
233
+
234
+ ## Rest...
235
+
236
+ Thanks for the people that are already extending the project, all the input making things move much faster.
237
+
238
+ Feel free to add to the wiki if you figure things out or make new distinctions.
239
+
240
+ https://rpxnow.com/features
@@ -0,0 +1,71 @@
1
+ require 'rake'
2
+ require "rake/rdoctask"
3
+ require 'rake/gempackagetask'
4
+
5
+ # http://docs.rubygems.org/read/chapter/20
6
+ spec = Gem::Specification.new do |s|
7
+ s.name = "lsdr-authlogic-connect"
8
+ s.author = "Lance Pollard"
9
+ s.version = "0.0.3.9"
10
+ s.summary = "Authlogic Connect: Let your app use all of Oauth and OpenID"
11
+ s.homepage = "http://github.com/lsdr/authlogic-connect"
12
+ s.email = "lsdrocha@gmail.com"
13
+ s.description = "authlogic-connect fork to solve a few nagging issues, while an update is not (business-)viable"
14
+ s.has_rdoc = true
15
+ s.rubyforge_project = "lsdr-authlogic-connect"
16
+ s.platform = Gem::Platform::RUBY
17
+ s.files = %w(README.markdown Rakefile init.rb MIT-LICENSE) + Dir["{lib,rails,test}/**/*"] - Dir["test/tmp"]
18
+ s.require_path = "lib"
19
+ s.add_dependency("activesupport", "2.3.5")
20
+ s.add_dependency("activerecord", "2.3.5")
21
+ s.add_dependency("json", "1.4.3")
22
+ s.add_dependency("ruby-openid", "2.1.7")
23
+ s.add_dependency("rack-openid", "1.0.3")
24
+ s.add_dependency("oauth", "0.4.0")
25
+ s.add_dependency("oauth2", "0.0.8")
26
+ s.add_dependency("authlogic", "2.1.3")
27
+ end
28
+
29
+ desc "Create .gemspec file (useful for github)"
30
+ task :gemspec do
31
+ File.open("pkg/#{spec.name}.gemspec", "w") do |f|
32
+ f.puts spec.to_ruby
33
+ end
34
+ end
35
+
36
+ desc "Build the gem into the current directory"
37
+ task :gem => :gemspec do
38
+ `gem build pkg/#{spec.name}.gemspec`
39
+ end
40
+
41
+ desc "Publish gem to rubygems"
42
+ task :publish => [:package] do
43
+ %x[gem push pkg/#{spec.name}-#{spec.version}.gem]
44
+ end
45
+
46
+ desc "Print a list of the files to be put into the gem"
47
+ task :manifest do
48
+ File.open("Manifest", "w") do |f|
49
+ spec.files.each do |file|
50
+ f.puts file
51
+ end
52
+ end
53
+ end
54
+
55
+ Rake::GemPackageTask.new(spec) do |pkg|
56
+ pkg.gem_spec = spec
57
+ pkg.package_dir = "pkg"
58
+ end
59
+
60
+ desc "Install the gem locally"
61
+ task :install => [:package] do
62
+ sh %{sudo gem install pkg/#{spec.name}-#{spec.version} --no-ri --no-rdoc}
63
+ end
64
+
65
+ desc "Generate the rdoc"
66
+ Rake::RDocTask.new do |rdoc|
67
+ files = ["README.markdown", "lib/**/*.rb"]
68
+ rdoc.rdoc_files.add(files)
69
+ rdoc.main = "README.markdown"
70
+ rdoc.title = spec.summary
71
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ File.dirname(__FILE__) + "/rails/init.rb"
@@ -0,0 +1,27 @@
1
+ require 'active_record'
2
+ require "rubygems"
3
+ require 'authlogic'
4
+ require 'oauth'
5
+ require 'oauth2'
6
+
7
+ this = File.dirname(__FILE__)
8
+ library = "#{this}/authlogic_connect"
9
+
10
+ require "#{this}/open_id_authentication"
11
+ require "#{library}/ext"
12
+ require "#{library}/authlogic_connect"
13
+ require "#{library}/callback_filter"
14
+ require "#{library}/token"
15
+ require "#{library}/openid"
16
+ require "#{library}/oauth"
17
+ require "#{library}/common"
18
+ require "#{library}/engine" if defined?(Rails) && Rails::VERSION::MAJOR == 3
19
+
20
+ custom_models = ["#{library}/token"]
21
+ custom_models += Dir["#{library}/oauth/tokens"]
22
+ custom_models += Dir["#{library}/openid/tokens"]
23
+
24
+ custom_models.each do |path|
25
+ $LOAD_PATH << path
26
+ ActiveSupport::Dependencies.load_paths << path
27
+ end
@@ -0,0 +1,46 @@
1
+ module AuthlogicConnect
2
+ KEY = "connect" unless defined?(KEY)
3
+ OAUTH = "oauth" unless defined?(OAUTH)
4
+ OPEN_ID = "open_id" unless defined?(OPEN_ID)
5
+
6
+ class << self
7
+
8
+ attr_accessor :config
9
+
10
+ def config=(value)
11
+ value.recursively_symbolize_keys!
12
+ @config = value
13
+ end
14
+
15
+ def key(path)
16
+ result = self.config
17
+ path.to_s.split(".").each { |node| result = result[node.to_sym] if result }
18
+ result
19
+ end
20
+
21
+ def credentials(service)
22
+ key("#{KEY}.#{service.to_s}")
23
+ end
24
+
25
+ def services
26
+ key(KEY)
27
+ end
28
+
29
+ def service_names
30
+ services.keys.collect(&:to_s)
31
+ end
32
+
33
+ def include?(service)
34
+ !credentials(service).nil?
35
+ end
36
+
37
+ def token(key)
38
+ raise "can't find key '#{key.to_s}' in AuthlogicConnect.config" unless AuthlogicConnect.include?(key) and !key.to_s.empty?
39
+ "#{key.to_s.camelcase}Token".constantize
40
+ end
41
+
42
+ def consumer(key)
43
+ token(key).consumer
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,19 @@
1
+ class AuthlogicConnect::CallbackFilter
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,10 @@
1
+ module AuthlogicConnect::Common
2
+ end
3
+
4
+ require File.dirname(__FILE__) + "/common/state"
5
+ require File.dirname(__FILE__) + "/common/variables"
6
+ require File.dirname(__FILE__) + "/common/user"
7
+ require File.dirname(__FILE__) + "/common/session"
8
+
9
+ ActiveRecord::Base.send(:include, AuthlogicConnect::Common::User)
10
+ Authlogic::Session::Base.send(:include, AuthlogicConnect::Common::Session)
@@ -0,0 +1,27 @@
1
+ module AuthlogicConnect::Common
2
+ module Session
3
+
4
+ def self.included(base)
5
+ base.class_eval do
6
+ include Variables
7
+ include InstanceMethods
8
+ end
9
+ end
10
+
11
+ module InstanceMethods
12
+
13
+ # core save method coordinating how to save the session
14
+ def save(&block)
15
+ block_destroyed = false
16
+ if authenticating_with_openid?
17
+ block_destroyed = save_with_openid(&block)
18
+ elsif authenticating_with_oauth?
19
+ block_destroyed = save_with_oauth(&block)
20
+ end
21
+ block = nil if block_destroyed
22
+ super(&block)
23
+ end
24
+ end
25
+
26
+ end
27
+ end