authlogic-connect-x 0.0.4.05x

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.markdown +248 -0
  3. data/Rakefile +75 -0
  4. data/init.rb +1 -0
  5. data/lib/authlogic-connect.rb +27 -0
  6. data/lib/authlogic_connect/access_token.rb +53 -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 +32 -0
  12. data/lib/authlogic_connect/common/user.rb +77 -0
  13. data/lib/authlogic_connect/common/variables.rb +137 -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/get_satisfaction_token.rb +9 -0
  24. data/lib/authlogic_connect/oauth/tokens/google_token.rb +41 -0
  25. data/lib/authlogic_connect/oauth/tokens/linked_in_token.rb +19 -0
  26. data/lib/authlogic_connect/oauth/tokens/meetup_token.rb +12 -0
  27. data/lib/authlogic_connect/oauth/tokens/myspace_token.rb +26 -0
  28. data/lib/authlogic_connect/oauth/tokens/netflix_token.rb +10 -0
  29. data/lib/authlogic_connect/oauth/tokens/oauth_token.rb +144 -0
  30. data/lib/authlogic_connect/oauth/tokens/oauth_token.rb~ +140 -0
  31. data/lib/authlogic_connect/oauth/tokens/ohloh_token.rb +9 -0
  32. data/lib/authlogic_connect/oauth/tokens/opensocial_token.rb +0 -0
  33. data/lib/authlogic_connect/oauth/tokens/twitter_token.rb +8 -0
  34. data/lib/authlogic_connect/oauth/tokens/vimeo_token.rb +18 -0
  35. data/lib/authlogic_connect/oauth/tokens/yahoo_token.rb +19 -0
  36. data/lib/authlogic_connect/oauth/user.rb +63 -0
  37. data/lib/authlogic_connect/oauth/variables.rb +64 -0
  38. data/lib/authlogic_connect/openid.rb +11 -0
  39. data/lib/authlogic_connect/openid/process.rb +74 -0
  40. data/lib/authlogic_connect/openid/session.rb +56 -0
  41. data/lib/authlogic_connect/openid/state.rb +48 -0
  42. data/lib/authlogic_connect/openid/tokens/aol_token.rb +0 -0
  43. data/lib/authlogic_connect/openid/tokens/blogger_token.rb +0 -0
  44. data/lib/authlogic_connect/openid/tokens/flickr_token.rb +0 -0
  45. data/lib/authlogic_connect/openid/tokens/my_openid_token.rb +3 -0
  46. data/lib/authlogic_connect/openid/tokens/openid_token.rb +9 -0
  47. data/lib/authlogic_connect/openid/user.rb +38 -0
  48. data/lib/authlogic_connect/openid/variables.rb +19 -0
  49. data/lib/open_id_authentication.rb +127 -0
  50. data/rails/init.rb +19 -0
  51. data/test/controllers/test_users_controller.rb +21 -0
  52. data/test/libs/database.rb +48 -0
  53. data/test/libs/user.rb +7 -0
  54. data/test/libs/user_session.rb +2 -0
  55. data/test/old.rb +53 -0
  56. data/test/test_authlogic_connect.rb +13 -0
  57. data/test/test_helper.rb +153 -0
  58. data/test/test_user.rb +194 -0
  59. metadata +242 -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,248 @@
1
+ # AuthlogicConnect
2
+
3
+ AuthlogicConnect is an extension of the Authlogic library that aims to add complete and seamless Oauth and OpenID support to your application.
4
+
5
+ It allows you to login through any of the 30+ Oauth and OpenID providers on the Internet without having to write any of the logic yourself.
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
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
+ ## Flow
241
+
242
+ - Try to create a session
243
+ - Session logs into provider
244
+ - On success, if no user, redirect to User#create
245
+
246
+ #### Notes
247
+
248
+ - Build mechanize tool to automatically create applications with service providers.
@@ -0,0 +1,75 @@
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 = "authlogic-connect"
8
+ s.author = "Lance Pollard"
9
+ s.version = "0.0.4.05"
10
+ s.summary = "Authlogic Connect: Oauth and OpenID made dead simple"
11
+ s.homepage = "http://github.com/viatropos/authlogic-connect"
12
+ s.email = "lancejpollard@gmail.com"
13
+ s.description = "Oauth and OpenID made dead simple"
14
+ s.has_rdoc = true
15
+ s.rubyforge_project = "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.1.2")
20
+ s.add_dependency("activerecord", ">= 2.1.2")
21
+ s.add_dependency("json")
22
+ s.add_dependency("ruby-openid")
23
+ s.add_dependency("rack-openid", ">=0.2.1")
24
+ s.add_dependency("oauth")
25
+ s.add_dependency("oauth2")
26
+ s.add_dependency("authlogic")
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
72
+
73
+ task :yank do
74
+ `gem yank #{spec.name} -v #{spec.version}`
75
+ 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}/access_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}/access_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,53 @@
1
+ class AccessToken < ActiveRecord::Base
2
+ belongs_to :user
3
+
4
+ def client
5
+ self.class.client
6
+ end
7
+
8
+ def consumer
9
+ self.class.consumer
10
+ end
11
+
12
+ def service_name
13
+ self.class.service_name
14
+ end
15
+
16
+ def settings
17
+ self.class.settings
18
+ end
19
+
20
+ def get(path)
21
+
22
+ end
23
+
24
+ class << self
25
+ def service_name
26
+ @service_name ||= self.to_s.underscore.scan(/^(.*?)(_token)?$/)[0][0].to_sym
27
+ end
28
+
29
+ def client
30
+ raise "implement client in subclass"
31
+ end
32
+
33
+ def consumer
34
+ raise "implement consumer in subclass"
35
+ end
36
+
37
+ def settings(site, hash = {})
38
+ @settings = hash.merge(:site => site)
39
+ end
40
+
41
+ def config
42
+ @settings ||= {}
43
+ @settings.dup
44
+ end
45
+
46
+ protected
47
+
48
+ def credentials
49
+ @credentials ||= AuthlogicConnect.credentials(service_name)
50
+ end
51
+ end
52
+
53
+ 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