authlogic-connect-andrewacove 0.5.0 → 0.5.2

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.
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require 'rake/gempackagetask'
6
6
  spec = Gem::Specification.new do |s|
7
7
  s.name = "authlogic-connect-andrewacove"
8
8
  s.authors = ['Lance Pollard', 'Andrew Cove']
9
- s.version = '0.5.0'
9
+ s.version = '0.5.2'
10
10
  s.summary = "Personal fork of Authlogic Connect: Oauth and OpenID made dead simple"
11
11
  s.homepage = "http://github.com/andrewacove/authlogic-connect"
12
12
  s.email = "andrewacove@gmail.com"
@@ -17,14 +17,19 @@ class OauthToken < AccessToken
17
17
  end
18
18
 
19
19
  def get(path, options = {})
20
- config_options = self.class.send(:credentials)[:options]
20
+ credentials = self.class.send(:credentials)
21
+ puts "DEBUG: credentials #{credentials.to_yaml}"
22
+ config_options = credentials[:options] unless credentials.nil?
23
+ puts "DEBUG: config_options #{config_options.to_yaml}"
21
24
  api_version = config_options[:api_version] unless config_options.nil?
25
+ puts "DEBUG: api_version #{api_version.to_yaml}"
22
26
  path.insert(0, "/#{api_version}") unless api_version.nil?
23
27
  client.get(path, options)
24
28
  end
25
29
 
26
30
  def post(path, body='', headers ={})
27
- config_options = self.class.send(:credentials)[:options]
31
+ credentials = self.class.send(:credentials)
32
+ config_options = credentials[:options] unless credentials.nil?
28
33
  api_version = config_options[:api_version] unless config_options.nil?
29
34
  path.insert(0, "/#{api_version}") unless api_version.nil?
30
35
  client.post(path, body, headers)
@@ -0,0 +1,152 @@
1
+ class OauthToken < AccessToken
2
+
3
+ def client
4
+ unless @client
5
+ if oauth_version == 1.0
6
+ @client = OAuth::AccessToken.new(self.consumer, self.token, self.secret)
7
+ else
8
+ @client = OAuth2::AccessToken.new(self.consumer, self.token)
9
+ end
10
+ end
11
+
12
+ @client
13
+ end
14
+
15
+ def oauth_version
16
+ self.class.oauth_version
17
+ end
18
+
19
+ def get(path, options = {})
20
+ credentials = self.class.send(:credentials)
21
+ config_options = credentials[:options] unless credentials.nil?
22
+ api_version = config_options[:api_version] unless config_options.nil?
23
+ path.insert(0, "/#{api_version}") unless api_version.nil?
24
+ client.get(path, options)
25
+ end
26
+
27
+ def post(path, body='', headers ={})
28
+ credentials = self.class.send(:credentials)
29
+ config_options = credentials[:options] unless credentials.nil?
30
+ api_version = config_options[:api_version] unless config_options.nil?
31
+ path.insert(0, "/#{api_version}") unless api_version.nil?
32
+ client.post(path, body, headers)
33
+ end
34
+
35
+ class << self
36
+
37
+ # oauth version, 1.0 or 2.0
38
+ def version(value)
39
+ @oauth_version = value
40
+ end
41
+
42
+ def oauth_version
43
+ @oauth_version ||= 1.0
44
+ end
45
+
46
+ # unique key that we will use from the AccessToken response
47
+ # to identify the user by.
48
+ # in Twitter, its "user_id". Twitter has "screen_name", but that's
49
+ # more subject to change than user_id. Pick whatever is least likely to change
50
+ def key(value = nil, &block)
51
+ if block_given?
52
+ @oauth_key = block
53
+ else
54
+ @oauth_key = value.is_a?(Symbol) ? value : value.to_sym
55
+ end
56
+ end
57
+
58
+ def oauth_key
59
+ @oauth_key
60
+ end
61
+
62
+ def reset_consumer!
63
+ if oauth_version == 1.0
64
+ @consumer = OAuth::Consumer.new(credentials[:key], credentials[:secret], config.merge(credentials[:options] || {}))
65
+ else
66
+ @consumer = OAuth2::Client.new(credentials[:key], credentials[:secret], config.merge(credentials[:options] || {}))
67
+ end
68
+ end
69
+
70
+ def consumer
71
+ @consumer || reset_consumer!
72
+ end
73
+
74
+ # if we're lucky we can find it by the token.
75
+ def find_by_key_or_token(key, token, options = {})
76
+ result = self.find_by_key(key, options) unless key.nil?
77
+ unless result
78
+ if !token.blank? && self.respond_to?(:find_by_token)
79
+ result = self.find_by_token(token, options)
80
+ end
81
+ end
82
+ result
83
+ end
84
+
85
+ # this is a wrapper around oauth 1 and 2.
86
+ # it looks obscure, but from the api point of view
87
+ # you won't have to worry about it's implementation.
88
+ # in oauth 1.0, key = oauth_token, secret = oauth_secret
89
+ # in oauth 2.0, key = code, secret = access_token
90
+ def get_token_and_secret(options = {})
91
+ oauth_verifier = options[:oauth_verifier]
92
+ redirect_uri = options[:redirect_uri]
93
+ token = options[:token]
94
+ secret = options[:secret]
95
+
96
+ if oauth_version == 1.0
97
+ access = request_token(token, secret).get_access_token(:oauth_verifier => oauth_verifier)
98
+ result = {:token => access.token, :secret => access.secret, :key => nil}
99
+ if self.oauth_key
100
+ if oauth_key.is_a?(Proc)
101
+ result[:key] = oauth_key.call(access)
102
+ else
103
+ result[:key] = access.params[self.oauth_key] || access.params[self.oauth_key.to_s] # try both
104
+ end
105
+ else
106
+ puts "Access Token: #{access.inspect}"
107
+ raise "please set an oauth key for #{service_name.to_s}"
108
+ end
109
+ else
110
+ access = consumer.web_server.get_access_token(secret, :redirect_uri => redirect_uri)
111
+ result = {:token => access.token, :secret => secret, :key => nil}
112
+ end
113
+
114
+ result
115
+ end
116
+
117
+ # this is a cleaner method so we can access the authorize_url
118
+ # from oauth 1 or 2
119
+ def authorize_url(callback_url, &block)
120
+ if oauth_version == 1.0
121
+ request = get_request_token(callback_url)
122
+ yield request if block_given?
123
+ return request.authorize_url
124
+ else
125
+ options = {:redirect_uri => callback_url}
126
+
127
+ unless consumer.nil? || consumer.options.empty? || consumer.options[:scope].nil?
128
+ options[:scope] = consumer.options[:scope]
129
+ else
130
+ options[:scope] = self.config[:scope] unless self.config[:scope].blank?
131
+ end
132
+ return consumer.web_server.authorize_url(options)
133
+ end
134
+ end
135
+
136
+ def request_token(token, secret)
137
+ OAuth::RequestToken.new(consumer, token, secret)
138
+ end
139
+
140
+ # if you pass a hash as the second parameter to consumer.get_request_token,
141
+ # ruby oauth will think this is a form and all sorts of bad things happen
142
+ def get_request_token(callback_url)
143
+ options = {:scope => config[:scope]} if config[:scope]
144
+ consumer.get_request_token({:oauth_callback => callback_url}, options)
145
+ end
146
+
147
+ def get_access_token(oauth_verifier)
148
+ request_token.get_access_token(:oauth_verifier => oauth_verifier)
149
+ end
150
+ end
151
+
152
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: authlogic-connect-andrewacove
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 5
9
- - 0
10
- version: 0.5.0
9
+ - 2
10
+ version: 0.5.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Lance Pollard
@@ -175,6 +175,7 @@ files:
175
175
  - lib/authlogic_connect/oauth/tokens/myspace_token.rb
176
176
  - lib/authlogic_connect/oauth/tokens/netflix_token.rb
177
177
  - lib/authlogic_connect/oauth/tokens/oauth_token.rb
178
+ - lib/authlogic_connect/oauth/tokens/oauth_token.rb~
178
179
  - lib/authlogic_connect/oauth/tokens/ohloh_token.rb
179
180
  - lib/authlogic_connect/oauth/tokens/opensocial_token.rb
180
181
  - lib/authlogic_connect/oauth/tokens/twitter_token.rb