signet-rails 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -28,6 +28,23 @@ module Signet
28
28
  combined_options[:persist_attrs] ||= [:refresh_token, :access_token, :expires_in, :issued_at]
29
29
  combined_options[:name] ||= :google
30
30
 
31
+ # is this a login-based OAuth2 adapter? If so, the callback will be used to identify a
32
+ # user and create one if necessary
33
+ # Options: :login, :webserver
34
+ combined_options[:type] = :login
35
+
36
+ # name of hash-behaving attribute on our wrapper that contains credentials
37
+ # keyed by :name
38
+ # {
39
+ # "google": {
40
+ # "uid": "012345676789abcde",
41
+ # "refresh_token": "my_first_refresh_token",
42
+ # "access_token": "my_first_access_token",
43
+ # "expires_in": 123
44
+ # }
45
+ # }
46
+ combined_options[:storage_attr] ||= :signet
47
+
31
48
  # TODO: see https://developers.google.com/accounts/docs/OAuth2Login#authenticationuriparameters
32
49
  combined_options[:approval_prompt] ||= 'auto'
33
50
 
@@ -41,29 +58,50 @@ module Signet
41
58
  # whether we handle the persistence of the auth callback or simply pass-through
42
59
  combined_options[:handle_auth_callback] ||= true
43
60
 
44
- # method to get the persistence object when creating a client via the factory
61
+ # The following lambda will be used when creating a new client in a factory
62
+ # to get the persistence object
45
63
  combined_options[:extract_from_env] ||= lambda do |env, client|
46
- u = nil
64
+ oac = nil
47
65
  session = env['rack.session']
48
66
  if !!session && !!session[:user_id]
49
67
  begin
50
68
  u = User.find(session[:user_id])
69
+ oac = u.o_auth2_credentials.where(name: combined_options[:name]).first
51
70
  rescue ActiveRecord::RecordNotFound => e
52
71
  end
53
72
  end
54
- u
73
+ oac
55
74
  end
56
75
 
57
- # when on an auth_callback, how do we get the persistence object from the id?
76
+ # The following lambda will be used when handling the callback from the oauth server
77
+ # In this flow we might not yet have established a session... need to handle two
78
+ # flows, one for login, one not
79
+ # when on a login auth_callback, how do we get the persistence object from the JWT?
58
80
  combined_options[:extract_by_oauth_id] ||= lambda do |id, client|
59
- u = nil
81
+ oac = nil
60
82
  begin
61
- u = User.where(uid: id).first_or_initialize(
62
- refresh_token: client.refresh_token
63
- )
83
+ u = nil
84
+ if combined_options[:type] == :login
85
+ u = User.first_or_initialize(uid: id)
86
+ u.save
87
+ else
88
+ session = env['rack.session']
89
+ if !!session && !!session[:user_id]
90
+ begin
91
+ u = User.find(session[:user_id])
92
+ rescue ActiveRecord::RecordNotFound => e
93
+ end
94
+ else
95
+ raise "Expected to be able to find user in session"
96
+ end
97
+ end
98
+
99
+ oac = u.o_auth2_credentials.first_or_initialize(name: combined_options[:name])
100
+
64
101
  rescue ActiveRecord::RecordNotFound => e
65
102
  end
66
- u
103
+
104
+ oac
67
105
  end
68
106
 
69
107
  combined_options[:persistence_wrapper] ||= :active_record
@@ -63,18 +63,32 @@ module Signet
63
63
  end
64
64
 
65
65
  def persist_token_state wrapper, client
66
+ if not wrapper.obj.respond_to?(options[:storage_attr])
67
+ raise "Persistence object does not support the storage attribute #{options[:storage_attr]}"
68
+ end
69
+
70
+ if (store_hash = wrapper.obj.method(options[:storage_attr]).call).nil?
71
+ store_hash = wrapper.obj.method("#{options[:storage_attr]}=").call({})
72
+ end
73
+
66
74
  for i in options[:persist_attrs]
67
- if client.respond_to?(i) && wrapper.obj.respond_to?(i.to_s+'=')
75
+ if client.respond_to?(i)
68
76
  # 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?
77
+ store_hash[i.to_s] = client.method(i).call unless client.method(i).call.nil?
70
78
  end
71
79
  end
72
80
  end
73
81
 
74
82
  def load_token_state wrapper, client
75
- for i in options[:persist_attrs]
76
- if wrapper.obj.respond_to?(i) && client.respond_to?(i.to_s+'=')
77
- client.method(i.to_s+'=').call(wrapper.obj.method(i).call)
83
+ if not wrapper.obj.respond_to?(options[:storage_attr])
84
+ raise "Persistence object does not support the storage attribute #{options[:storage_attr]}"
85
+ end
86
+
87
+ if not (store_hash = wrapper.obj.method(options[:storage_attr]).call).nil?
88
+ for i in options[:persist_attrs]
89
+ if client.respond_to?(i.to_s+'=')
90
+ client.method(i.to_s+'=').call(store_hash[i.to_s])
91
+ end
78
92
  end
79
93
  end
80
94
  end
@@ -1,5 +1,5 @@
1
1
  module Signet
2
2
  module Rails
3
- VERSION = "0.0.5"
3
+ VERSION = "0.0.6"
4
4
  end
5
5
  end
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.5
4
+ version: 0.0.6
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-08-07 00:00:00.000000000 Z
12
+ date: 2013-08-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler