databasedotcom-oauth2 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,6 +15,21 @@ end
15
15
 
16
16
  module Databasedotcom
17
17
 
18
+ def self.parse_domain(url = nil)
19
+ unless url.nil?
20
+ url = "https://" + url if (url =~ /http[s]?:\/\//).nil?
21
+ begin
22
+ url = Addressable::URI.parse(url)
23
+ rescue Addressable::URI::InvalidURIError
24
+ url = nil
25
+ end
26
+ url = url.host unless url.nil?
27
+ url.strip! unless url.nil?
28
+ end
29
+ url = nil if url && url.strip.empty?
30
+ url
31
+ end
32
+
18
33
  class Client
19
34
  def self.from_token(token, api_version)
20
35
  client = nil
@@ -22,7 +37,7 @@ module Databasedotcom
22
37
  client = self.new({
23
38
  :client_id => token.client.id,
24
39
  :client_secret => token.client.secret,
25
- :host => token.client.site
40
+ :host => Databasedotcom.parse_domain(token.client.site)
26
41
  })
27
42
  m = token["id"].match(/\/id\/([^\/]+)\/([^\/]+)$/)
28
43
  client.org_id = m[1] rescue nil
@@ -35,6 +50,11 @@ module Databasedotcom
35
50
  client
36
51
  end
37
52
 
53
+ #def set_org_and_user_id(orgid, userid)
54
+ # @org_id = orgid
55
+ # @user_id = userid
56
+ #end
57
+
38
58
  def org_id=(val)
39
59
  @org_id = val
40
60
  end
@@ -88,6 +108,7 @@ module Databasedotcom
88
108
  @display_override = options[:display_override] || false
89
109
  @immediate_override = options[:immediate_override] || false
90
110
  @api_version = options[:api_version] || "24.0"
111
+ @debugging = options[:debugging] || false
91
112
  end
92
113
 
93
114
  fail "\n\ndatabasedotcom-oauth2 initialization error! :endpoints parameter " \
@@ -139,6 +160,7 @@ module Databasedotcom
139
160
  end
140
161
 
141
162
  def authorize_call
163
+ puts "==================\nauthorize phase\n==================\n" if @debugging
142
164
  #determine endpoint via param; but if blank, use default
143
165
  endpoint = request.params["endpoint"] #get endpoint from http param
144
166
  keys = @endpoints[endpoint] #if endpoint not found, default will be used
@@ -150,11 +172,13 @@ module Databasedotcom
150
172
  state = Addressable::URI.parse(request.params["state"])
151
173
  state.query_values={} unless state.query_values
152
174
  state.query_values= state.query_values.merge({:endpoint => endpoint})
175
+
176
+ puts "endpoint: #{endpoint}\nmydomain: #{mydomain}\nstate: #{state.to_str}" if @debugging
153
177
 
154
178
  #build params hash to be passed to ouath2 authorize redirect url
155
179
  auth_params = {
156
180
  :redirect_uri => "#{full_host}#{@path_prefix}/callback",
157
- :state => state.to_s
181
+ :state => state.to_str
158
182
  }
159
183
  auth_params[:scope] = @scope unless @scope.nil? || @scope.strip.empty?
160
184
  auth_params[:display] = @display unless @display.nil?
@@ -171,7 +195,9 @@ module Databasedotcom
171
195
  auth_params.merge!(overrides)
172
196
 
173
197
  #do redirect
174
- redirect client(mydomain || endpoint, keys[:key], keys[:secret]).auth_code.authorize_url(auth_params)
198
+ redirect_url = client(mydomain || endpoint, keys[:key], keys[:secret]).auth_code.authorize_url(auth_params)
199
+ puts "redirecting to #{redirect_url}..." if @debugging
200
+ redirect redirect_url
175
201
  end
176
202
 
177
203
  def on_callback_path?
@@ -179,6 +205,7 @@ module Databasedotcom
179
205
  end
180
206
 
181
207
  def callback_call
208
+ puts "==================\ncallback phase\n==================\n" if @debugging
182
209
  #check for error
183
210
  callback_error = request.params["error"]
184
211
  callback_error_details = request.params["error_description"]
@@ -194,35 +221,54 @@ module Databasedotcom
194
221
  state_params = state.query_values.dup
195
222
  endpoint = state_params.delete("endpoint")
196
223
  keys = @endpoints[endpoint]
224
+ puts "endpoint #{endpoint}"
225
+ puts "keys #{keys}"
197
226
  state.query_values= state_params
198
227
  state = state.to_s
199
228
  state.sub!(/\?$/,"") unless state.nil?
229
+ puts "endpoint: #{endpoint}\nstate: #{state.to_str}\nretrieving token" if @debugging
200
230
 
201
231
  #do callout to retrieve token
202
232
  access_token = client(endpoint, keys[:key], keys[:secret]).auth_code.get_token(code,
203
233
  :redirect_uri => "#{full_host}#{@path_prefix}/callback")
234
+ puts "access_token immediatly post get token call #{access_token.inspect}" if @debugging
204
235
  access_token.options[:mode] = :query
205
236
  access_token.options[:param_name] = :oauth_token
206
237
  access_token.options[:endpoint] = endpoint
207
238
  access_token.client = nil
239
+ puts "access_token pre marshal-encrypt-cookiewrite #{access_token.inspect}" if @debugging
208
240
 
209
241
  #populate session with serialized, encrypted token
210
242
  #will be used later to materialize actual token and databasedotcom client handle
211
243
  set_session_token(encrypt(access_token))
244
+ puts "session_token \n#{session_token}" if @debugging
212
245
  redirect state.to_str
213
246
  end
214
247
 
215
248
  def materialize_token_and_client_from_session_if_present
216
- access_token = decrypt(session_token) unless session_token.nil? rescue nil
249
+ puts "==========================\nmaterialize intercept\n==========================\n" if @debugging
250
+ access_token = nil
251
+ puts "session_token \n#{session_token}" if @debugging
252
+ begin
253
+ access_token = decrypt(session_token) unless session_token.nil?
254
+ rescue Exception => e
255
+ puts "Exception FYI"
256
+ self.class._log_exception(e)
257
+ end
217
258
  unless access_token.nil?
259
+ puts "access_token post cookieread-decrypt-marshal #{access_token.inspect}" if @debugging
218
260
  instance_url = access_token.params["instance_url"]
219
261
  endpoint = access_token.options[:endpoint]
220
262
  keys = @endpoints[endpoint]
263
+ puts "endpoint #{endpoint}\nkeys #{keys}" if @debugging
221
264
  access_token.client = client(instance_url, keys[:key], keys[:secret])
222
265
  unless keys.nil?
223
- @env[TOKEN_KEY] = access_token
266
+ @env[TOKEN_KEY] = access_token #::OAuth2::AccessToken.from_hash(client(instance_url, keys[:key], keys[:secret]),access_token_hash.dup)
224
267
  @env[CLIENT_KEY] = ::Databasedotcom::Client.from_token(@env[TOKEN_KEY],@api_version)
268
+ @env[CLIENT_KEY].debugging = @debugging
225
269
  end
270
+ puts "materialized token: #{@env[TOKEN_KEY].inspect}" if @debugging
271
+ puts "materialized client: #{@env[CLIENT_KEY].inspect}" if @debugging
226
272
  end
227
273
  end
228
274
 
@@ -284,7 +330,7 @@ module Databasedotcom
284
330
  ::OAuth2::Client.new(
285
331
  client_id,
286
332
  client_secret,
287
- :site => "https://#{self.class.parse_domain(site)}",
333
+ :site => "https://#{Databasedotcom.parse_domain(site)}",
288
334
  :authorize_url => '/services/oauth2/authorize',
289
335
  :token_url => '/services/oauth2/token'
290
336
  )
@@ -306,7 +352,7 @@ module Databasedotcom
306
352
  end
307
353
 
308
354
  def sanitize_mydomain(mydomain)
309
- mydomain = parse_domain(mydomain)
355
+ mydomain = Databasedotcom.parse_domain(mydomain)
310
356
  mydomain = nil unless mydomain.nil? || !mydomain.strip.empty?
311
357
  mydomain = mydomain.split(/\.my\.salesforce\.com/).first + ".my.salesforce.com" unless mydomain.nil?
312
358
  mydomain
@@ -331,21 +377,6 @@ module Databasedotcom
331
377
  endpoints
332
378
  end
333
379
 
334
- def parse_domain(url = nil)
335
- unless url.nil?
336
- url = "https://" + url if (url =~ /http[s]?:\/\//).nil?
337
- begin
338
- url = Addressable::URI.parse(url)
339
- rescue Addressable::URI::InvalidURIError
340
- url = nil
341
- end
342
- url = url.host unless url.nil?
343
- url.strip! unless url.nil?
344
- end
345
- url = nil if url && url.strip.empty?
346
- url
347
- end
348
-
349
380
  def param_repeated(url = nil, param_name = nil)
350
381
  return_value = nil
351
382
  unless url.nil? || url.strip.empty? || param_name.nil?
@@ -1,5 +1,5 @@
1
1
  module Databasedotcom
2
2
  module OAuth2
3
- VERSION = "0.1.7"
3
+ VERSION = "0.1.8"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: databasedotcom-oauth2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-26 00:00:00.000000000 Z
12
+ date: 2012-06-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: addressable