signet-rails 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Signet::Rails
2
2
 
3
- A basic Rails wrapper around the [Signet](https://github.com/google/signet) gem
3
+ A basic Rails wrapper around the [Signet](https://github.com/google/signet) gem that handles persistence of a user's credentials on top of handling the auth flow within Rails applications. Check out [an example Rails app](https://github.com/myitcv/test-signet-rails) that utilises this gem.
4
4
 
5
- Work in progress
5
+ Please note this is very much work in progress... issues/suggestions/etc gratefully received. Raise an issue/create a pull-request.
6
6
 
7
7
  ## Installation
8
8
 
@@ -26,7 +26,7 @@ $ gem install signet-rails
26
26
 
27
27
  ## Usage
28
28
 
29
- TODO: Write usage instructions here
29
+ Check out [an example Rails app](https://github.com/myitcv/test-signet-rails) that utilises this gem.
30
30
 
31
31
  ## TODO
32
32
 
@@ -42,7 +42,7 @@ A list of items still todo or work in progress
42
42
  3. Better way of sourcing the Google default authorization_uri and token_credential_uri? From signet directly?
43
43
  4. Clear definition of the Signet options and the `signet-rails` options
44
44
  5. More Rails-esque way of getting the `rack.session` in `extract\_from\_env`?
45
- 6. Better way of loading persistance wrappers in builder?
45
+ 6. Better way of loading persistence wrappers in builder?
46
46
  7. Check to see whether we have all required signet options at the end of Builder.provder?
47
47
  8. Sort out `approval\_prompt` vs 'prompt'
48
48
  9. Better `auth\_options` split at the end of Builder.provider?
@@ -50,9 +50,13 @@ A list of items still todo or work in progress
50
50
  11. Refactor Handler.handle code... messy
51
51
  12. Document handling of callback in Rails
52
52
  13. Error handling...
53
- 14. Document the various `env` values that can/will be set and when (e.g. `signet.XXX.persistance\_obj` on `auth\_callback`)
53
+ 14. Document the various `env` values that can/will be set and when (e.g. `signet.XXX.persistence\_obj` on `auth\_callback`)
54
54
  15. Allow shortened form of common scopes (userprofile etc)
55
- 16. Do we need a require in the initializer?
55
+ 16. Provider `application_name` and `applicatio_version` when initialising the client (per signet error)
56
+
57
+ ## Credits
58
+
59
+ Credit where it is due, much of what you see here is derived from lessons learned using [omniauth](https://github.com/intridea/omniauth)* but also this [great post by Greg Baugues](http://blog.baugues.com/google-calendar-api-oauth2-and-ruby-on-rails)
56
60
 
57
61
  ## Contributing
58
62
 
@@ -38,10 +38,10 @@ module Signet
38
38
  combined_options[:authorization_uri] ||= 'https://accounts.google.com/o/oauth2/auth'
39
39
  combined_options[:token_credential_uri] ||= 'https://accounts.google.com/o/oauth2/token'
40
40
 
41
- # whether we handle the persistance of the auth callback or simply pass-through
41
+ # whether we handle the persistence of the auth callback or simply pass-through
42
42
  combined_options[:handle_auth_callback] ||= true
43
43
 
44
- # method to get the persistance object when creating a client via the factory
44
+ # method to get the persistence object when creating a client via the factory
45
45
  combined_options[:extract_from_env] ||= lambda do |env, client|
46
46
  u = nil
47
47
  session = env['rack.session']
@@ -54,7 +54,7 @@ module Signet
54
54
  u
55
55
  end
56
56
 
57
- # when on an auth_callback, how do we get the persistance object from the id?
57
+ # when on an auth_callback, how do we get the persistence object from the id?
58
58
  combined_options[:extract_by_oauth_id] ||= lambda do |id, client|
59
59
  u = nil
60
60
  begin
@@ -66,18 +66,18 @@ module Signet
66
66
  u
67
67
  end
68
68
 
69
- combined_options[:persistance_wrapper] ||= :active_record
70
- persistance_wrapper = lambda do |meth|
69
+ combined_options[:persistence_wrapper] ||= :active_record
70
+ persistence_wrapper = lambda do |meth|
71
71
  lambda do |context, client|
72
72
  y = meth.call context, client
73
- klass_str = combined_options[:persistance_wrapper].to_s
73
+ klass_str = combined_options[:persistence_wrapper].to_s
74
74
  require "signet/rails/wrappers/#{klass_str}"
75
75
  w = "Signet::Rails::Wrappers::#{klass_str.camelize}".constantize.new y, client
76
76
  end
77
77
  end
78
78
 
79
- combined_options[:extract_by_oauth_id] = persistance_wrapper.call combined_options[:extract_by_oauth_id]
80
- combined_options[:extract_from_env] = persistance_wrapper.call combined_options[:extract_from_env]
79
+ combined_options[:extract_by_oauth_id] = persistence_wrapper.call combined_options[:extract_by_oauth_id]
80
+ combined_options[:extract_from_env] = persistence_wrapper.call combined_options[:extract_from_env]
81
81
 
82
82
  # TODO: check here we have the basics?
83
83
 
@@ -5,14 +5,19 @@ module Signet
5
5
 
6
6
  class Factory
7
7
  def self.create_from_env name, env, opt_hsh = {load_token: true}
8
-
9
- # TODO: thread safe? best approach? Other uses below
10
- client = env["signet.#{name.to_s}.instance"]
11
-
12
- return client if !!client
13
8
 
14
- # TODO: again, not pretty...
9
+ # TODO: not pretty...thread safe? best approach? Other uses below
15
10
  handler = env["signet.#{name.to_s}"]
11
+ instance = env["signet.#{name.to_s}.instance"]
12
+
13
+ #client = instance.obj
14
+ client = instance
15
+
16
+ return client if !!client
17
+
18
+ if handler.nil?
19
+ raise ArgumentError, "Unable to find signet handler named #{name.to_s}"
20
+ end
16
21
 
17
22
  client = Signet::OAuth2::Client.new handler.options
18
23
 
@@ -20,7 +25,8 @@ module Signet
20
25
  obj = handler.options[:extract_from_env].call env, client
21
26
  handler.load_token_state obj, client
22
27
 
23
- env["signet.#{name.to_s}.instance"] = obj
28
+ #instance.obj = obj
29
+ env["signet.#{name.to_s}.instance"] = obj
24
30
  end
25
31
 
26
32
  client
@@ -50,14 +50,10 @@ module Signet
50
50
  client.fetch_access_token!
51
51
 
52
52
  if options[:handle_auth_callback]
53
- # TODO: remove
54
- puts '******************************************'
55
- puts client.decoded_id_token.inspect
56
-
57
- obj = options[:extract_by_oauth_id].call client.decoded_id_token['id'], client
53
+ obj = options[:extract_by_oauth_id].call client.decoded_id_token['sub'], client
58
54
  persist_token_state obj, client
59
55
  obj.persist
60
- env["signet.#{options[:name]}.persistance_obj"] = obj.obj
56
+ env["signet.#{options[:name]}.persistence_obj"] = obj.obj
61
57
  else
62
58
  env["signet.#{options[:name]}.auth_client"] = client
63
59
  end
@@ -69,7 +65,8 @@ module Signet
69
65
  def persist_token_state wrapper, client
70
66
  for i in options[:persist_attrs]
71
67
  if client.respond_to?(i) && wrapper.obj.respond_to?(i.to_s+'=')
72
- wrapper.obj.method(i.to_s+'=').call(client.method(i).call)
68
+ # only transfer the value if it is non-nil
69
+ wrapper.obj.method(i.to_s+'=').call(client.method(i).call) unless client.method(i).call.nil?
73
70
  end
74
71
  end
75
72
  end
@@ -92,9 +89,10 @@ module Signet
92
89
 
93
90
  status, headers, body = @app.call(env)
94
91
 
95
- obj = env["signet.#{options[:name]}.instance"]
96
- if !!obj
97
- obj.persist
92
+ instance = env["signet.#{options[:name]}.instance"]
93
+ if !!instance
94
+ persist_token_state instance, instance.client
95
+ instance.persist
98
96
  end
99
97
 
100
98
  end
@@ -1,5 +1,5 @@
1
1
  module Signet
2
2
  module Rails
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
@@ -7,9 +7,8 @@ module Signet
7
7
  @client = client
8
8
  end
9
9
 
10
- def obj
11
- @obj
12
- end
10
+ attr_reader :obj
11
+ attr_reader :client
13
12
 
14
13
  def persist
15
14
  if @obj.changed?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: signet-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
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: 2013-06-17 00:00:00.000000000 Z
12
+ date: 2013-08-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler