jspooner-authlogic-connect 0.0.19

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) 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/session.rb +30 -0
  10. data/lib/authlogic_connect/common/state.rb +45 -0
  11. data/lib/authlogic_connect/common/user.rb +77 -0
  12. data/lib/authlogic_connect/common/variables.rb +124 -0
  13. data/lib/authlogic_connect/common.rb +10 -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/helper.rb +20 -0
  17. data/lib/authlogic_connect/oauth/process.rb +77 -0
  18. data/lib/authlogic_connect/oauth/session.rb +90 -0
  19. data/lib/authlogic_connect/oauth/state.rb +60 -0
  20. data/lib/authlogic_connect/oauth/tokens/aol_token.rb +2 -0
  21. data/lib/authlogic_connect/oauth/tokens/facebook_token.rb +11 -0
  22. data/lib/authlogic_connect/oauth/tokens/foursquare_token.rb +15 -0
  23. data/lib/authlogic_connect/oauth/tokens/get_satisfaction_token.rb +9 -0
  24. data/lib/authlogic_connect/oauth/tokens/github_token.rb +14 -0
  25. data/lib/authlogic_connect/oauth/tokens/google_token.rb +41 -0
  26. data/lib/authlogic_connect/oauth/tokens/linked_in_token.rb +19 -0
  27. data/lib/authlogic_connect/oauth/tokens/meetup_token.rb +12 -0
  28. data/lib/authlogic_connect/oauth/tokens/myspace_token.rb +26 -0
  29. data/lib/authlogic_connect/oauth/tokens/netflix_token.rb +10 -0
  30. data/lib/authlogic_connect/oauth/tokens/oauth_token.rb +164 -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 +64 -0
  37. data/lib/authlogic_connect/oauth/variables.rb +64 -0
  38. data/lib/authlogic_connect/oauth.rb +14 -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/authlogic_connect/openid.rb +11 -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/libs/database.rb +47 -0
  55. data/test/libs/user.rb +7 -0
  56. data/test/libs/user_session.rb +2 -0
  57. data/test/test_helper.rb +178 -0
  58. data/test/test_oauth.rb +178 -0
  59. data/test/test_openid.rb +71 -0
  60. data/test/test_user.rb +85 -0
  61. metadata +243 -0
data/MIT-LICENSE ADDED
@@ -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.
data/README.markdown ADDED
@@ -0,0 +1,234 @@
1
+ # AuthlogicConnect
2
+
3
+ > Instant Oauth and OpenID support for your Rails and Sinatra Apps
4
+
5
+ AuthlogicConnect is an extension of the Authlogic library that adds complete Oauth and OpenID support to your application. It provides a single interface to Oauth 1.0 and Oauth 2.0.
6
+
7
+ It currently allows you to login with Github, Facebook, Twitter, Google, LinkedIn, MySpace, Vimeo, and Yahoo Oauth providers, and all the OpenID providers. Feel free to add support for more as you need them.
8
+
9
+ Here's a **[live example](http://authlogic-connect.heroku.com)** on Heroku ([with source](http://github.com/viatropos/authlogic-connect-example)). [Here's the Docs](http://rdoc.info/projects/viatropos/authlogic-connect)
10
+
11
+ ### Lists of known providers:
12
+
13
+ - [Oauth Providers](http://wiki.oauth.net/ServiceProviders)
14
+ - [OpenID Providers](http://en.wikipedia.org/wiki/List_of_OpenID_providers)
15
+ - [More OpenID](http://openid.net/get-an-openid/)
16
+
17
+ ## Install
18
+
19
+ ### 1. Install AuthlogicConnect
20
+
21
+ sudo gem install authlogic-connect
22
+
23
+ ### 2. Add the gem dependencies in your config:
24
+
25
+ Rails 2.3.x: `config/environment.rb`
26
+
27
+ config.gem "json"
28
+ config.gem "authlogic"
29
+ config.gem "oauth"
30
+ config.gem "oauth2"
31
+ config.gem "authlogic-connect"
32
+
33
+ Rails 3: `Gemfile`
34
+
35
+ gem "ruby-openid"
36
+ gem "rack-openid", ">=0.2.1", :require => "rack/openid"
37
+ gem "authlogic", :git => "git://github.com/odorcicd/authlogic.git", :branch => "rails3"
38
+ gem "oauth"
39
+ gem "oauth2"
40
+ gem "authlogic-connect"
41
+
42
+ ### 3. Add the OpenIdAuthentication.store
43
+
44
+ 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.
45
+
46
+ So as a fix, if you are using Rails < 3, add these at the end of your `config/environment.rb` files:
47
+
48
+ In development mode:
49
+
50
+ OpenIdAuthentication.store = :file
51
+
52
+ In production (on Heroku primarily)
53
+
54
+ OpenIdAuthentication.store = :memcache
55
+
56
+ ### 4. Add the Migrations
57
+
58
+ 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.
59
+
60
+ Files needed are:
61
+
62
+ - models: User, UserSession
63
+ - controllers: UsersController, UserSessionsController, ApplicationController
64
+ - migrations: create\_users, create\_sessions, create\_tokens
65
+ - initializers: config/authlogic.example.yml, config/initializers/authlogic_connect_config.rb
66
+ - routes
67
+
68
+ ### 5. Configure your keys
69
+
70
+ 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).
71
+
72
+ connect:
73
+ twitter:
74
+ key: "my_key"
75
+ secret: "my_secret"
76
+ label: "Twitter"
77
+ facebook:
78
+ key: "my_key"
79
+ secret: "my_secret"
80
+ label: "Facebook"
81
+ google:
82
+ key: "my_key"
83
+ secret: "my_secret"
84
+ label: "Google"
85
+ yahoo:
86
+ key: "my_key"
87
+ secret: "my_secret"
88
+ label: "Yahoo"
89
+ myspace:
90
+ key: "my_key"
91
+ secret: "my_secret"
92
+ vimeo:
93
+ key: "my_key"
94
+ secret: "my_secret"
95
+ linked_in:
96
+ key: "my_key"
97
+ secret: "my_secret"
98
+
99
+ These are then loaded via the initializer script in `config/initializers/authlogic_connect_config.rb`:
100
+
101
+ AuthlogicConnect.config = YAML.load_file("config/authlogic.yml")
102
+
103
+ ### 6. Make sure you save your objects properly
104
+
105
+ Because of the redirects involved in Oauth and OpenID, you MUST pass a block to the `save` method in your UsersController and UserSessionsController:
106
+
107
+ @user_session.save do |result|
108
+ if result
109
+ flash[:notice] # "Login successful!"
110
+ redirect_back_or_default account_url
111
+ else
112
+ render :action => :new
113
+ end
114
+ end
115
+
116
+ 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.
117
+
118
+ ### 7. Add Parameters to Forms in your Views
119
+
120
+ There are 3 things to include in your views.
121
+
122
+ 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:
123
+
124
+ %input{:type => :hidden, :name => :authentication_type, :value => :user}
125
+
126
+ 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).
127
+
128
+ %input{:type => :radio, :id => :twitter_oauth_provider, :name => :oauth_provider, :value => :twitter}
129
+
130
+ 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:
131
+
132
+ %input.nice{:type => :text, :name => :openid_identifier}
133
+
134
+ Those are passed as parameters to Authlogic, and the complicated details are abstracted away.
135
+
136
+ ## Overview of the User Experience
137
+
138
+ There are 3 ways you a user can login with AuthlogicConnect:
139
+
140
+ 1. Clicking an Oauth Provider
141
+ 2. Clicking an OpenID Provider and entering in their username
142
+ 3. Manually typing in a full OpenID address
143
+
144
+ Oauth is very different from OpenID, but this aims to make them work the same.
145
+
146
+ ## Examples
147
+
148
+ 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.
149
+
150
+ ### API
151
+
152
+ User model has the following public accessors and methods. This example assumes:
153
+
154
+ - You've associated your Google, OpenID, and Twitter accounts with this app.
155
+ - You're currently logged in via Google.
156
+
157
+ Inside the `show` method in a controller...
158
+
159
+ def show
160
+ @user = @current_user
161
+
162
+ puts @user.tokens #=> [
163
+ #<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">,
164
+ #<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">,
165
+ #<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">]
166
+
167
+ puts @user.tokens.length #=> 3
168
+
169
+ # currently logged in with...
170
+ 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">
171
+
172
+ puts @user.authenticated_with #=> ["twitter", "openid", "google"]
173
+ puts @user.authenticated_with?(:twitter) #=> true
174
+ puts @user.authenticated_with?(:facebook) #=> false
175
+
176
+ puts @user.has_token?(:google) #=> true
177
+
178
+ 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">
179
+
180
+ # change active_token
181
+ @user.active_token = @user.get_token(:twitter)
182
+ 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">
183
+
184
+ # access oauth api
185
+ @twitter = @user.active_token
186
+ @twitter_profile = JSON.parse(@twitter.get("/account/verify_credentials.json").body) #=> twitter api stuff
187
+ # ...
188
+ end
189
+
190
+ ### Get Facebook Data
191
+
192
+ If they've associated their Facebook account with your site, you can access Facebook data.
193
+
194
+ def show
195
+ @user = @current_user
196
+ token = @user.active_token # assuming this is FacebookToken
197
+ facebook = JSON.parse(token.get("/me"))
198
+ @profile = {
199
+ :id => facebook["id"],
200
+ :name => facebook["name"],
201
+ :photo => "https://graph.facebook.com/#{facebook["id"]}/picture",
202
+ :link => facebook["link"],
203
+ :title => "Facebook"
204
+ }
205
+ @profile = @user.profile
206
+ end
207
+
208
+ ## Helpful links
209
+
210
+ * **Authlogic:** [http://github.com/binarylogic/authlogic](http://github.com/binarylogic/authlogic)
211
+ * **AuthlogicConnect Example Project:** [http://github.com/viatropos/authlogic-connect-example](http://github.com/viatropos/authlogic-connect-example)
212
+ * **Live example with Twitter and Facebook using Rails 3:** [http://authlogic-connect.heroku.com](http://authlogic-connect.heroku.com)
213
+ * **Rails 2.3.5 Example:** [http://github.com/viatropos/authlogic-connect-example-rails2](http://github.com/viatropos/authlogic-connect-example-rails2)
214
+ * **Rubygems Repository:** [http://rubygems.org/gems/authlogic-connect](http://rubygems.org/gems/authlogic-connect)
215
+
216
+ ## Rest...
217
+
218
+ Thanks for the people that are already extending the project, all the input making things move much faster. [Andrew Cove](http://github.com/andrewacove) and [Daf](http://github.com/daf) have helped me quite a bit, thanks guys.
219
+
220
+ Feel free to add to the wiki if you figure things out or make new distinctions.
221
+
222
+ ## Flow
223
+
224
+ - Try to create a session
225
+ - Session logs into provider
226
+ - On success, if no user, redirect to User#create
227
+
228
+ #### Notes
229
+
230
+ - Build mechanize tool to automatically create applications with service providers.
231
+
232
+ ### Todo
233
+
234
+ - Add [Andrew Cove's](http://github.com/andrewacove) idea of a "Merge Code". So if user creates Facebook account logs out, and create Twitter account, a code they can use to pass to facebook account so it knows it's associated with Twitter.
data/Rakefile ADDED
@@ -0,0 +1,85 @@
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 = "jspooner-authlogic-connect"
8
+ s.author = "Lance Pollard"
9
+ s.version = "0.0.19"
10
+ s.summary = "Authlogic Connect: Oauth and OpenID made dead simple"
11
+ s.homepage = "http://github.com/jspooner/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 = "jspooner-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 %{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
76
+
77
+ desc 'run unit tests'
78
+ task :test do
79
+ Dir["test/**/*"].each do |file|
80
+ next unless File.extname(file) == ".rb"
81
+ next unless File.basename(file) =~ /test_/
82
+ next if File.basename(file) =~ /test_helper/
83
+ system "ruby #{file}"
84
+ end
85
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ File.dirname(__FILE__) + "/rails/init.rb"
@@ -0,0 +1,39 @@
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
+ # Rails 3/2 config
25
+ load_path_method = ActiveSupport::Dependencies.respond_to?(:autoload_paths) ? :autoload_paths : :load_paths
26
+
27
+ custom_models.each do |path|
28
+ $LOAD_PATH << path
29
+ ActiveSupport::Dependencies.send(load_path_method) << path
30
+ end
31
+
32
+ # Rails 3beta4 backport
33
+ if defined?(ActiveSupport::HashWithIndifferentAccess)
34
+ ActiveSupport::HashWithIndifferentAccess.class_eval do
35
+ def symbolize_keys!
36
+ symbolize_keys
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,61 @@
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 config
21
+ self.class.config
22
+ end
23
+
24
+ def get(path)
25
+
26
+ end
27
+
28
+ class << self
29
+ def service_name
30
+ @service_name ||= self.to_s.underscore.scan(/^(.*?)(_token)?$/)[0][0].to_sym
31
+ end
32
+
33
+ def client
34
+ raise "implement client in subclass"
35
+ end
36
+
37
+ def consumer
38
+ raise "implement consumer in subclass"
39
+ end
40
+
41
+ def settings(site, hash = {})
42
+ @settings = hash.merge(:site => site)
43
+ end
44
+
45
+ def config
46
+ @settings ||= {}
47
+ @settings.dup
48
+ end
49
+
50
+ def api_version(value = nil)
51
+ config[:api]
52
+ end
53
+
54
+ protected
55
+
56
+ def credentials
57
+ @credentials ||= AuthlogicConnect.credentials(service_name)
58
+ end
59
+ end
60
+
61
+ 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,30 @@
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
+ # want to destroy the block if we redirect to a remote service, that's it.
15
+ # otherwise the block contains the render methods we wan to use
16
+ def save(&block)
17
+ self.errors.clear
18
+ # log_state
19
+ authenticate_via_protocol(block_given?) do |redirecting|
20
+ block = nil if redirecting
21
+ result = super(&block)
22
+ cleanup_authentication_session unless block.nil?
23
+ result
24
+ end
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,45 @@
1
+ # This class holds query/state variables common to oauth and openid
2
+ module AuthlogicConnect::Common::State
3
+
4
+ def auth_controller?
5
+ !auth_controller.blank?
6
+ end
7
+
8
+ def auth_params?
9
+ auth_controller? && !auth_params.blank?
10
+ end
11
+
12
+ def auth_session?
13
+ !auth_session.blank?
14
+ end
15
+
16
+ def is_auth_session?
17
+ self.is_a?(Authlogic::Session::Base)
18
+ end
19
+
20
+ def start_authentication?
21
+ start_oauth? || start_openid?
22
+ end
23
+
24
+ def validate_password_with_oauth?
25
+ !using_openid? && super
26
+ end
27
+
28
+ def validate_password_with_openid?
29
+ !using_oauth? && super
30
+ end
31
+
32
+ # because user and session are so closely tied together, I am still
33
+ # uncertain as to how they are saved. So this makes sure if we are
34
+ # logging in, it must be saving the session, otherwise the user.
35
+ def correct_request_class?
36
+ return false unless auth_params?
37
+
38
+ if is_auth_session?
39
+ auth_type.to_s == "session"
40
+ else
41
+ auth_type.to_s == "user"
42
+ end
43
+ end
44
+
45
+ end
@@ -0,0 +1,77 @@
1
+ # This class is the main api for the user.
2
+ # It is also required to properly sequence the save methods
3
+ # for the different authentication types (oauth and openid)
4
+ module AuthlogicConnect::Common::User
5
+
6
+ def self.included(base)
7
+ base.class_eval do
8
+ add_acts_as_authentic_module(InstanceMethods, :append)
9
+ add_acts_as_authentic_module(AuthlogicConnect::Common::Variables, :prepend)
10
+ end
11
+ end
12
+
13
+ module InstanceMethods
14
+
15
+ def self.included(base)
16
+ base.class_eval do
17
+ has_many :access_tokens, :class_name => "AccessToken", :dependent => :destroy
18
+ belongs_to :active_token, :class_name => "AccessToken", :dependent => :destroy
19
+ accepts_nested_attributes_for :access_tokens, :active_token
20
+ end
21
+ end
22
+
23
+ def authenticated_with
24
+ @authenticated_with ||= self.access_tokens.collect{|t| t.service_name.to_s}
25
+ end
26
+
27
+ def authenticated_with?(service)
28
+ self.access_tokens.detect{|t| t.service_name.to_s == service.to_s}
29
+ end
30
+
31
+ def update_attributes(attributes, &block)
32
+ self.attributes = attributes
33
+ save(:validate => true, &block)
34
+ end
35
+
36
+ def has_token?(service_name)
37
+ !get_token(service_name).nil?
38
+ end
39
+
40
+ def get_token(service_name)
41
+ self.access_tokens.detect {|i| i.service_name.to_s == service_name.to_s}
42
+ end
43
+
44
+ # core save method coordinating how to save the user.
45
+ # we dont' want to ru validations based on the
46
+ # authentication mission we are trying to accomplish.
47
+ # instead, we just return save as false.
48
+ # the next time around, when we recieve the callback,
49
+ # we will run the validations.
50
+ # when you call 'current_user_session' in ApplicationController,
51
+ # it leads to calling 'save' on this User object via "session.record.save",
52
+ # from the 'persisting?' method. So we don't want any of this to occur
53
+ # when that save is called, and the only way to check currently is
54
+ # to check if there is a block_given?
55
+ def save(options = {}, &block)
56
+ self.errors.clear
57
+ # log_state
58
+ options = {} if options == false
59
+ options[:validate] = true unless options.has_key?(:validate)
60
+ save_options = ActiveRecord::VERSION::MAJOR < 3 ? options[:validate] : options
61
+
62
+ # kill the block if we're starting authentication
63
+ authenticate_via_protocol(block_given?, options) do |start_authentication|
64
+ block = nil if start_authentication # redirecting
65
+ # forces you to validate, only if a block is given
66
+ result = super(save_options) # validate!
67
+ unless block.nil?
68
+ cleanup_authentication_session(options)
69
+ yield(result)
70
+ end
71
+ result
72
+ end
73
+ end
74
+
75
+ end
76
+
77
+ end