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 (40) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.markdown +145 -0
  3. data/Rakefile +69 -0
  4. data/init.rb +1 -0
  5. data/lib/authlogic_connect.rb +71 -0
  6. data/lib/authlogic_connect/common.rb +9 -0
  7. data/lib/authlogic_connect/common/session.rb +27 -0
  8. data/lib/authlogic_connect/common/user.rb +40 -0
  9. data/lib/authlogic_connect/common/variables.rb +21 -0
  10. data/lib/authlogic_connect/oauth.rb +11 -0
  11. data/lib/authlogic_connect/oauth/helper.rb +16 -0
  12. data/lib/authlogic_connect/oauth/process.rb +82 -0
  13. data/lib/authlogic_connect/oauth/session.rb +72 -0
  14. data/lib/authlogic_connect/oauth/tokens/delicious_token.rb +12 -0
  15. data/lib/authlogic_connect/oauth/tokens/facebook_token.rb +19 -0
  16. data/lib/authlogic_connect/oauth/tokens/get_satisfaction_token.rb +14 -0
  17. data/lib/authlogic_connect/oauth/tokens/google_token.rb +38 -0
  18. data/lib/authlogic_connect/oauth/tokens/myspace_token.rb +14 -0
  19. data/lib/authlogic_connect/oauth/tokens/oauth_token.rb +48 -0
  20. data/lib/authlogic_connect/oauth/tokens/opensocial_token.rb +0 -0
  21. data/lib/authlogic_connect/oauth/tokens/photobucket_token.rb +13 -0
  22. data/lib/authlogic_connect/oauth/tokens/smug_mug_token.rb +14 -0
  23. data/lib/authlogic_connect/oauth/tokens/twitter_token.rb +11 -0
  24. data/lib/authlogic_connect/oauth/tokens/vimeo_token.rb +13 -0
  25. data/lib/authlogic_connect/oauth/tokens/yahoo_token.rb +20 -0
  26. data/lib/authlogic_connect/oauth/user.rb +81 -0
  27. data/lib/authlogic_connect/oauth/variables.rb +34 -0
  28. data/lib/authlogic_connect/openid.rb +8 -0
  29. data/lib/authlogic_connect/openid/session.rb +125 -0
  30. data/lib/authlogic_connect/openid/tokens/aol_token.rb +0 -0
  31. data/lib/authlogic_connect/openid/tokens/blogger_token.rb +0 -0
  32. data/lib/authlogic_connect/openid/tokens/flickr_token.rb +0 -0
  33. data/lib/authlogic_connect/openid/tokens/my_openid_token.rb +0 -0
  34. data/lib/authlogic_connect/openid/tokens/openid_token.rb +3 -0
  35. data/lib/authlogic_connect/openid/user.rb +93 -0
  36. data/lib/authlogic_connect/openid/variables.rb +5 -0
  37. data/lib/oauth_callback_filter.rb +12 -0
  38. data/lib/token.rb +37 -0
  39. data/rails/init.rb +17 -0
  40. metadata +175 -0
@@ -0,0 +1,82 @@
1
+ module AuthlogicConnect::Oauth
2
+ module Process
3
+
4
+ private
5
+ include Variables
6
+
7
+ def validate_by_oauth
8
+ validate_email_field = false
9
+
10
+ if oauth_response.blank?
11
+ redirect_to_oauth
12
+ else
13
+ authenticate_with_oauth
14
+ end
15
+ end
16
+
17
+ def redirecting_to_oauth_server?
18
+ authenticating_with_oauth? && oauth_response.blank?
19
+ end
20
+
21
+ def redirect_to_oauth
22
+ save_oauth_callback
23
+
24
+ if oauth_version == 1.0
25
+ request = oauth_token.get_request_token(oauth_callback_url)
26
+ save_auth_session(request)
27
+ auth_controller.redirect_to request.authorize_url
28
+ else
29
+ auth_controller.redirect_to oauth_client.web_server.authorize_url(
30
+ :redirect_uri => oauth_callback_url,
31
+ :scope => oauth_token.settings[:scope]
32
+ )
33
+ end
34
+ end
35
+
36
+ def save_oauth_callback
37
+ # Store the class which is redirecting, so we can ensure other classes
38
+ # don't get confused and attempt to use the response
39
+ auth_session[:oauth_request_class] = self.class.name
40
+ auth_session[:oauth_provider] = auth_params[:oauth_provider]
41
+
42
+ # Tell our rack callback filter what method the current request is using
43
+ auth_session[:oauth_callback_method] = auth_controller.request.method
44
+ end
45
+
46
+ def save_auth_session(request)
47
+ # store token and secret
48
+ auth_session[:oauth_request_token] = request.token
49
+ auth_session[:oauth_request_token_secret] = request.secret
50
+ end
51
+
52
+ def oauth_callback_url
53
+ auth_controller.url_for :controller => auth_controller.controller_name, :action => auth_controller.action_name
54
+ end
55
+
56
+ def request_token
57
+ oauth_token.request_token(auth_session[:oauth_request_token], auth_session[:oauth_request_token_secret])
58
+ end
59
+
60
+ # in oauth 1.0, key = oauth_token, secret = oauth_secret
61
+ # in oauth 2.0, key = code, secret = access_token
62
+ def oauth_key_and_secret
63
+ if oauth_version == 1.0
64
+ result = request_token.get_access_token(:oauth_verifier => auth_params[:oauth_verifier])
65
+ result = {:key => result.token, :secret => result.secret}
66
+ else
67
+ result = oauth_client.web_server.get_access_token(oauth_key, :redirect_uri => oauth_callback_url)
68
+ result = {:key => result.token, :secret => oauth_key}
69
+ end
70
+ result
71
+ end
72
+
73
+ def generate_access_token
74
+ if oauth_version == 1.0
75
+ request_token.get_access_token(:oauth_verifier => auth_params[:oauth_verifier])
76
+ else
77
+ oauth_client.web_server.get_access_token(oauth_key, :redirect_uri => oauth_callback_url)
78
+ end
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,72 @@
1
+ module AuthlogicConnect::Oauth
2
+ # This module is responsible for adding oauth
3
+ # to the Authlogic::Session::Base class.
4
+ module Session
5
+ def self.included(base)
6
+ puts "included Oauth in Session"
7
+ base.class_eval do
8
+ include InstanceMethods
9
+ end
10
+ end
11
+
12
+ module InstanceMethods
13
+ include Process
14
+
15
+ def self.included(klass)
16
+ klass.class_eval do
17
+ validate :validate_by_oauth, :if => :authenticating_with_oauth?
18
+ end
19
+ end
20
+
21
+ # Hooks into credentials so that you can pass a user who has already has an oauth access token.
22
+ def credentials=(value)
23
+ super
24
+ values = value.is_a?(Array) ? value : [value]
25
+ hash = values.first.is_a?(Hash) ? values.first.with_indifferent_access : nil
26
+ self.record = hash[:priority_record] if !hash.nil? && hash.key?(:priority_record)
27
+ end
28
+
29
+ def record=(record)
30
+ @record = record
31
+ end
32
+
33
+ private
34
+ # Clears out the block if we are authenticating with oauth,
35
+ # so that we can redirect without a DoubleRender error.
36
+ def save_with_oauth(&block)
37
+ puts "SAVE SESSION WITH OAUTH"
38
+ puts "redirecting_to_oauth_server? #{redirecting_to_oauth_server?.to_s}"
39
+ block = nil if redirecting_to_oauth_server?
40
+ return block.nil?
41
+ end
42
+
43
+ def authenticating_with_oauth?
44
+ return false unless oauth_provider
45
+
46
+ # Initial request when user presses one of the button helpers
47
+ initial_request = (controller.params && !controller.params[:login_with_oauth].blank?)
48
+ # When the oauth provider responds and we made the initial request
49
+ initial_response = (oauth_response && auth_session && auth_session[:oauth_request_class] == self.class.name)
50
+
51
+ return initial_request || initial_response
52
+ end
53
+
54
+ def authenticate_with_oauth
55
+ if @record
56
+ self.attempted_record = record
57
+ else
58
+ # this generated token is always the same for a user!
59
+ # this is searching with User.find ...
60
+ # attempted_record is part of AuthLogic
61
+ key = oauth_key_and_secret[:key]
62
+ token = oauth_token.find_by_key(key, :include => [:user]) # some weird error if I leave out the include
63
+ self.attempted_record = token.user
64
+ end
65
+
66
+ if !attempted_record
67
+ errors.add_to_base("Could not find user in our database, have you registered with your oauth account?")
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,12 @@
1
+ class DeliciousToken < OauthToken
2
+
3
+ class << self
4
+ def settings
5
+ @settings ||= {
6
+ :site => "http://api.del.icio.us",
7
+ :realm => "yahooapis.com"
8
+ }
9
+ end
10
+ end
11
+
12
+ end
@@ -0,0 +1,19 @@
1
+ # http://www.facebook.com/developers/apps.php
2
+ # http://developers.facebook.com/setup/
3
+ class FacebookToken < OauthToken
4
+
5
+ class << self
6
+ def settings
7
+ @settings ||= {
8
+ :site => "https://graph.facebook.com",
9
+ :authorize_url => "https://graph.facebook.com/oauth/authorize",
10
+ :oauth_version => "2.0",
11
+ :scope => "email, offline_access"
12
+ }
13
+ end
14
+
15
+ def oauth_version
16
+ 2.0
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ # http://getsatisfaction.com/developers/oauth
2
+ class GetSatisfactionToken < OauthToken
3
+
4
+ class << self
5
+ def settings
6
+ @settings ||= {
7
+ :site => "http://getsatisfaction.com",
8
+ :request_token_path => "/api/request_token",
9
+ :authorize_url => "/api/authorize",
10
+ :access_token_path => "/api/access_token"
11
+ }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,38 @@
1
+ # http://code.google.com/apis/accounts/docs/OAuth.html
2
+ # http://code.google.com/apis/accounts/docs/RegistrationForWebAppsAuto.html
3
+ # http://googlecodesamples.com/oauth_playground/
4
+ # Scopes:
5
+ # Analytics https://www.google.com/analytics/feeds/
6
+ # Google Base http://www.google.com/base/feeds/
7
+ # Book Search http://www.google.com/books/feeds/
8
+ # Blogger http://www.blogger.com/feeds/
9
+ # Calendar http://www.google.com/calendar/feeds/
10
+ # Contacts http://www.google.com/m8/feeds/
11
+ # Documents List http://docs.google.com/feeds/
12
+ # Finance http://finance.google.com/finance/feeds/
13
+ # GMail https://mail.google.com/mail/feed/atom
14
+ # Health https://www.google.com/health/feeds/
15
+ # H9 https://www.google.com/h9/feeds/
16
+ # Maps http://maps.google.com/maps/feeds/
17
+ # OpenSocial http://www-opensocial.googleusercontent.com/api/people/
18
+ # orkut http://www.orkut.com/social/rest
19
+ # Picasa Web http://picasaweb.google.com/data/
20
+ # Sidewiki http://www.google.com/sidewiki/feeds/
21
+ # Sites http://sites.google.com/feeds/
22
+ # Spreadsheets http://spreadsheets.google.com/feeds/
23
+ # Webmaster Tools http://www.google.com/webmasters/tools/feeds/
24
+ # YouTube http://gdata.youtube.com
25
+ class GoogleToken < OauthToken
26
+
27
+ class << self
28
+ def settings
29
+ @settings ||= {
30
+ :site => "https://www.google.com",
31
+ :request_token_path => "/accounts/OAuthGetRequestToken",
32
+ :authorize_path => "/accounts/OAuthAuthorizeToken",
33
+ :access_token_path => "/accounts/OAuthGetAccessToken",
34
+ :scope => "https://www.google.com/m8/feeds/"
35
+ }
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,14 @@
1
+ # http://wiki.developer.myspace.com/index.php?title=Category:MySpaceID
2
+ class MyspaceToken < OauthToken
3
+
4
+ class << self
5
+ def settings
6
+ @settings ||= {
7
+ :site => "https://www.google.com",
8
+ :request_token_path => "/accounts/OAuthGetRequestToken",
9
+ :authorize_path => "/accounts/OAuthAuthorizeToken",
10
+ :access_token_path => "/accounts/OAuthGetAccessToken"
11
+ }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,48 @@
1
+ class OauthToken < Token
2
+
3
+ # Main client for interfacing with remote service. Override this to use
4
+ # preexisting library eg. Twitter gem.
5
+ def client
6
+ @client ||= OAuth::AccessToken.new(self.class.consumer, token, secret)
7
+ end
8
+
9
+ def simple_client
10
+ @simple_client ||= SimpleClient.new(OAuth::AccessToken.new(self.class.consumer, token, secret))
11
+ end
12
+
13
+ def oauth_version
14
+ self.class.oauth_version
15
+ end
16
+
17
+ class << self
18
+
19
+ def oauth_version
20
+ 1.0
21
+ end
22
+
23
+ def settings
24
+ @settings ||= {}
25
+ end
26
+
27
+ def consumer
28
+ @consumer ||= OAuth::Consumer.new(credentials[:key], credentials[:secret], settings.merge(credentials[:options] || {}))
29
+ end
30
+
31
+ def client
32
+ OAuth2::Client.new(credentials[:key], credentials[:secret], settings)
33
+ end
34
+
35
+ def request_token(token, secret)
36
+ OAuth::RequestToken.new(consumer, token, secret)
37
+ end
38
+
39
+ def get_request_token(callback_url)
40
+ consumer.get_request_token({:oauth_callback => callback_url}, settings)
41
+ end
42
+
43
+ def get_access_token(oauth_verifier)
44
+ request_token.get_access_token(:oauth_verifier => oauth_verifier)
45
+ end
46
+ end
47
+
48
+ end
@@ -0,0 +1,13 @@
1
+ # http://photobucket.com/developer/
2
+ # https://login.photobucket.com/developer/register
3
+ class PhotobucketToken < OauthToken
4
+
5
+ class << self
6
+ def settings
7
+ @settings ||= {
8
+ :site => "http://twitter.com",
9
+ :authorize_url => "http://twitter.com/oauth/authenticate"
10
+ }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ # http://wiki.smugmug.net/display/API/OAuth
2
+ class SmugMugToken < OauthToken
3
+
4
+ class << self
5
+ def settings
6
+ @settings ||= {
7
+ :site => "http://api.smugmug.com",
8
+ :request_token_path => "/services/oauth/getRequestToken.mg",
9
+ :authorize_url => "/services/oauth/authorize.mg",
10
+ :access_token_path => "/services/oauth/getAccessToken.mg"
11
+ }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ class TwitterToken < OauthToken
2
+
3
+ class << self
4
+ def settings
5
+ @settings ||= {
6
+ :site => "http://twitter.com",
7
+ :authorize_url => "http://twitter.com/oauth/authenticate"
8
+ }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ # http://www.vimeo.com/api/docs/oauth
2
+ # http://www.vimeo.com/api/applications/new
3
+ class VimeoToken < OauthToken
4
+
5
+ class << self
6
+ def settings
7
+ @settings ||= {
8
+ :site => "http://vimeo.com",
9
+ :authorize_url => "http://vimeo.com/oauth/authorize"
10
+ }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ # https://developer.apps.yahoo.com/dashboard/createKey.html
2
+ # http://developer.yahoo.com/oauth/guide/oauth-accesstoken.html
3
+ class YahooToken < OauthToken
4
+
5
+ class << self
6
+ def settings
7
+ @settings ||= {
8
+ :site => "https://api.login.yahoo.com",
9
+ :request_token_path => '/oauth/v2/get_request_token',
10
+ :access_token_path => '/oauth/v2/get_token',
11
+ :authorize_path => '/oauth/v2/request_auth'
12
+ }
13
+ end
14
+
15
+ def oauth_version
16
+ 2.0
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,81 @@
1
+ module AuthlogicConnect::Oauth
2
+ module User
3
+ def self.included(base)
4
+ base.class_eval do
5
+ add_acts_as_authentic_module(InstanceMethods, :prepend)
6
+ end
7
+ end
8
+
9
+ module InstanceMethods
10
+ include Process
11
+ # Set up some simple validations
12
+ def self.included(base)
13
+ base.class_eval do
14
+ has_many :tokens, :class_name => "Token", :dependent => :destroy
15
+ belongs_to :active_token, :class_name => "Token", :dependent => :destroy
16
+ accepts_nested_attributes_for :tokens, :active_token
17
+
18
+ validate :validate_by_oauth, :if => :authenticating_with_oauth?
19
+
20
+ # need these validation options if you don't want it to choke
21
+ # on password length, which you don't need if you're using oauth
22
+ validates_length_of_password_field_options validates_length_of_password_field_options.merge(:if => :validate_password_with_oauth?)
23
+ validates_confirmation_of_password_field_options validates_confirmation_of_password_field_options.merge(:if => :validate_password_with_oauth?)
24
+ validates_length_of_password_confirmation_field_options validates_length_of_password_confirmation_field_options.merge(:if => :validate_password_with_oauth?)
25
+ validates_length_of_login_field_options validates_length_of_login_field_options.merge(:if => :validate_password_with_oauth?)
26
+ validates_format_of_login_field_options validates_format_of_login_field_options.merge(:if => :validate_password_with_oauth?)
27
+ end
28
+
29
+ # email needs to be optional for oauth
30
+ base.validate_email_field = false
31
+ end
32
+
33
+ def update_attributes(attributes, &block)
34
+ self.attributes = attributes
35
+ save(true, &block)
36
+ end
37
+
38
+ # NEED TO GIVE A BLOCK
39
+ def save_with_oauth(perform_validation = true, &block)
40
+ if perform_validation && block_given? && redirecting_to_oauth_server?
41
+ # Save attributes so they aren't lost during the authentication with the oauth server
42
+ auth_session[:authlogic_oauth_attributes] = attributes.reject!{|k, v| v.blank?}
43
+ redirect_to_oauth
44
+ return false
45
+ end
46
+ return true
47
+ end
48
+
49
+ protected
50
+
51
+ def using_oauth?
52
+ !oauth_token.blank?
53
+ end
54
+
55
+ def validate_password_with_oauth?
56
+ !using_oauth? && require_password?
57
+ end
58
+
59
+ def authenticating_with_oauth?
60
+ return false unless oauth_provider
61
+ # Initial request when user presses one of the button helpers
62
+ (auth_params && !auth_params[:register_with_oauth].blank?) ||
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)
65
+ end
66
+
67
+ def authenticate_with_oauth
68
+ # Restore any attributes which were saved before redirecting to the oauth server
69
+ self.attributes = auth_session.delete(:authlogic_oauth_attributes)
70
+ token = AuthlogicConnect.token(oauth_provider).new(oauth_key_and_secret)
71
+ if Token.find_by_key(token.key)
72
+ self.errors.add("you have already created an account using your #{oauth_token.service_name} account, so it")
73
+ else
74
+ self.tokens << token
75
+ self.active_token = token
76
+ end
77
+ end
78
+
79
+ end
80
+ end
81
+ end