setsuzoku 0.11.0 → 0.11.9

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 36e5b7968d72649e85fed697caf9d4b04613faa1b69e347bc347712743ddb55a
4
- data.tar.gz: 11b0433cee31db3ffb7ee39d36c3c31dd4bab600b238a57a51519770d3453eff
3
+ metadata.gz: cd0ca4436f63ee80c7411553690acc0600a3556842acda30c580767de4d5fefe
4
+ data.tar.gz: 9cdf81df738bc16aef812b6cc5f68cb7e26b5a3a1666dcbf214a79ac16e22f28
5
5
  SHA512:
6
- metadata.gz: 27c41fe87bebe18b575359b471c3e7497292c5ba730b8698704352ad7f43f7a6d9824a8ee7d81c8d52e6d3dbb3247e7c4d3024b19cb1909c1e3bfc888fb0f823
7
- data.tar.gz: 1352f256aacaeaa77d3565c5d14992bba7689717ea2c928485f3a4304b62b110db47b1c532ccb0266909f02719539c19ca8165c828973c4a8528ef8e74f23437
6
+ metadata.gz: 888f8ee20d1bc364d6a0b9ddd54ab2cdeb8c9e54078ea5bc36ef5193fac5d75ee7310492c55493fa4e6dae2975103b31e087ca6f23557bca2d52f22622d5a700
7
+ data.tar.gz: a07a89413cae6b20f159eec21cd37f90e73e80d8ee8b1d0522f5186b71aa587483145c29deaff231337a805fb5291ba3c45e17a364cab37e0123890982ffd218
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- setsuzoku (0.11.0)
4
+ setsuzoku (0.11.9)
5
5
  activesupport (~> 5.0)
6
6
  faraday (~> 0.11)
7
7
  httparty (~> 0.16.4)
@@ -57,7 +57,7 @@ GEM
57
57
  safe_yaml (1.0.5)
58
58
  sorbet (0.5.5675)
59
59
  sorbet-static (= 0.5.5675)
60
- sorbet-runtime (0.5.5862)
60
+ sorbet-runtime (0.5.5878)
61
61
  sorbet-static (0.5.5675-universal-darwin-14)
62
62
  thread_safe (0.3.6)
63
63
  tzinfo (1.2.7)
@@ -80,7 +80,7 @@ module Setsuzoku
80
80
  self.external_api_handler.call_external_api_wrapper(plugin: self.plugin, request: request, action_details: action_details) do
81
81
  begin
82
82
  # raise if the token is invalid and needs refreshed, but don't raise if we are trying to get a refresh token
83
- raise Setsuzoku::Exception::InvalidAuthCredentials.new unless (request.action == :refresh_token || self.auth_strategy.auth_credential_valid?)
83
+ raise Setsuzoku::Exception::InvalidAuthCredentials.new unless (strategy == :auth || self.auth_strategy.auth_credential_valid?)
84
84
  raw_response = self.perform_external_call(request: request, action_details: action_details, **options)
85
85
  formatted_response = self.parse_response(response: raw_response, response_type: action_details[:actions][request.action][:response_type])
86
86
  success = true
@@ -14,6 +14,18 @@ module Setsuzoku
14
14
  sig { abstract.returns(Struct) }
15
15
  def self.stub_credential; end
16
16
 
17
+ # The status of the credential.
18
+ #
19
+ # @return [Hash] the status of the credential.
20
+ sig { abstract.returns(String) }
21
+ def status; end
22
+
23
+ # Setter for the status of the credential.
24
+ #
25
+ # @return [Hash] the status of the credential.
26
+ sig { abstract.params(val: T.nilable(String)).returns(String) }
27
+ def status=(val); end
28
+
17
29
  # A settings object for the credential.
18
30
  #
19
31
  # @return [Hash] the settings for the credential.
@@ -2,6 +2,8 @@
2
2
 
3
3
  require 'active_support/core_ext/class/subclasses'
4
4
  require 'active_support/inflector/methods'
5
+ require 'faraday'
6
+
5
7
  module Setsuzoku
6
8
  module DynamicSpecHelper
7
9
 
@@ -150,20 +152,21 @@ module Setsuzoku
150
152
  stub.each do |action_name, props|
151
153
  props.each do |propz|
152
154
  plugin.api_strategy.current_action = action_name
153
- default_headers = plugin.api_strategy.request_options(propz[:request_format])[:headers] || { 'Authorization' => 'Bearer stubbed_token', 'Content-Type' => "application/#{propz[:request_format]}" }
155
+ default_headers = plugin.api_strategy.request_options(propz[:request_format])[:headers] || { 'Content-Type' => "application/#{propz[:request_format]}" }
154
156
  if plugin.auth_strategy.is_a?(Setsuzoku::Service::WebService::AuthStrategies::BasicAuthStrategy)
155
157
  basic_auth = plugin.api_strategy.request_options(propz[:request_format])[:basic_auth]
156
158
  auth_header = { 'Authorization' => "Basic #{Base64.encode64("#{basic_auth[:username]}:#{basic_auth[:password]}").gsub("\n", '')}" }
157
- default_headers.merge!(auth_header)
159
+ elsif plugin.auth_strategy.is_a?(Setsuzoku::Service::WebService::AuthStrategies::OAuthStrategy)
160
+ auth_header = { 'Authorization' => "Bearer #{plugin.api_strategy.request_options(propz[:request_format])[:token]}" }
158
161
  end
162
+ default_headers.merge!(auth_header) if auth_header
159
163
  headers = propz[:headers] ? [propz[:headers]] : [default_headers]
160
164
  stub_directory ||= "#{plugin.class.plugin_namespace}/#{provider}"
165
+
161
166
  headers.each do |header|
162
- header = {
163
- 'Accept'=>'*/*',
164
- 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
165
- 'User-Agent'=>'Ruby'
166
- }.merge(header.deep_stringify_keys)
167
+ header = Faraday.new.headers
168
+ .merge({'Accept'=>'*/*','Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3'})
169
+ .merge(header.deep_stringify_keys)
167
170
 
168
171
  if propz[:stub_data]
169
172
  # we have specified specific stub data for this action
@@ -33,7 +33,7 @@ module Setsuzoku
33
33
  # media_type - 'json'
34
34
  #
35
35
  # @return [Hash] the parsed response object.
36
- sig { override.params(request: T.untyped, action_details: T::Hash[T.untyped, T.untyped], options: T.untyped).returns(Faraday::Response) }
36
+ sig { override.params(request: RestAPIRequest, action_details: T::Hash[T.untyped, T.untyped], options: T.untyped).returns(Faraday::Response) }
37
37
  def perform_external_call(request:, action_details:, **options)
38
38
  request_properties = self.get_request_properties(action_name: request.action, action_details: action_details, req_params: request.body)
39
39
  request_options = self.request_options(request_properties[:request_format], action_details[:actions][request.action])
@@ -43,11 +43,14 @@ module Setsuzoku
43
43
  @faraday = Faraday.new(url: request_properties[:formatted_full_url], request: { params_encoder: Faraday::FlatParamsEncoder }) do |faraday|
44
44
  faraday.request(:multipart) if options[:attachments].present?
45
45
  faraday.request(:url_encoded)
46
- # faraday.basic_auth(request_options[:basic_auth][:username], request_options[:basic_auth][:password]) if request_options[:basic_auth]
47
- faraday.request(:basic_auth, request_options[:basic_auth][:username], request_options[:basic_auth][:password]) if request_options[:basic_auth]
48
- # faraday.response request_properties[:response_format] if request_properties[:response_format]
49
- faraday.authorization(:Bearer, request_properties[:token]) if request_properties[:token]
50
- # request_options[:headers][:Authorization].prepend('Bearer ') if request_options[:headers][:Authorization]
46
+ #TODO: change these to faraday = self.auth_strategy.set_authorization!(request: faraday)
47
+ unless request.without_headers
48
+ if request_options[:token]
49
+ faraday.authorization(:Bearer, request_options[:token])
50
+ elsif request_options[:basic_auth]
51
+ faraday.request(:basic_auth, request_options[:basic_auth][:username], request_options[:basic_auth][:password])
52
+ end
53
+ end
51
54
  faraday.adapter Faraday.default_adapter
52
55
  end
53
56
 
@@ -60,7 +63,11 @@ module Setsuzoku
60
63
  req.body = payload
61
64
  end
62
65
  else
63
- @faraday.headers = request_options[:headers] if request_options[:headers]
66
+ if request.without_headers
67
+ @faraday.headers = {}
68
+ elsif request_options[:headers]
69
+ @faraday.headers = (@faraday.headers || {}).merge(request_options[:headers])
70
+ end
64
71
  resp = @faraday.send(request_properties[:request_method], request_properties[:formatted_full_url], full_request)
65
72
  end
66
73
  resp
@@ -81,7 +88,6 @@ module Setsuzoku
81
88
  request_method = request_method.downcase.to_sym
82
89
  request_format = action[:request_type]
83
90
  response_format = action[:response_type]
84
- token = action[:token]
85
91
  stub_data = action[:stub_data] if for_stub
86
92
  full_url = url + endpoint
87
93
  formatted_full_url, req_params = self.replace_dynamic_vars(full_url: full_url, req_params: req_params)
@@ -93,7 +99,6 @@ module Setsuzoku
93
99
  formatted_full_url: formatted_full_url,
94
100
  req_params: req_params,
95
101
  stub_data: stub_data,
96
- token: token
97
102
  }
98
103
  end
99
104
 
@@ -31,9 +31,7 @@ module Setsuzoku
31
31
  sig { override.returns(T::Hash[Symbol, T.untyped]) }
32
32
  def auth_headers
33
33
  {
34
- headers: {
35
- 'Authorization': "Bearer #{self.credential.token}"
36
- }
34
+ token: self.credential.token
37
35
  }
38
36
  end
39
37
 
@@ -73,7 +71,7 @@ module Setsuzoku
73
71
  # @return [Boolean] true if the token is invalid.
74
72
  sig { returns(T::Boolean) }
75
73
  def token_is_invalid?
76
- active = self.credential.respond_to?(:"status") ? self.credential.status != 'disabled' : true
74
+ active = self.credential.status != 'disabled'
77
75
  active && !self.credential.expires_on.blank? &&
78
76
  !self.credential.refresh_token.blank? &&
79
77
  (self.credential.expires_on < refresh_before_expiration_time)
@@ -112,38 +110,32 @@ module Setsuzoku
112
110
  sig { params(body: T::Hash[Symbol, String], action: Symbol).void }
113
111
  def get_token!(body, action)
114
112
  success = false
115
- request = self.api_strategy.request_class.new(action: action, body: body)
113
+ request = self.api_strategy.request_class.new(action: action, body: body, without_headers: true)
116
114
 
117
115
  resp = self.api_strategy.call_external_api(request: request, strategy: :auth)
118
116
 
117
+ attrs = {}
119
118
  if resp.data && resp.data[:access_token]
120
- self.credential.status = 'active' if self.credential.respond_to?(:"status=")
121
- self.credential.token = resp.data[:access_token]
122
- self.credential.refresh_token = resp.data[:refresh_token] if resp.data.key?(:refresh_token)
123
- self.credential.expires_on = if resp.data[:expires_in]
124
- resp.data[:expires_in].to_i.seconds.from_now
125
- # elsif resp.data[:issued_at] && self.class.token_valid_for
126
- # Time.at(resp.data[:issued_at].to_i / 1000) + self.class.token_valid_for
127
- end
128
-
129
- self.plugin.set_credential_fields!(resp) if self.plugin.respond_to?(:set_credential_fields!)
130
-
131
- #TODO: figure out a better way to do this when we add other integrations...
132
- #
133
- # if self.is_a?(FacebookIntegration)
134
- # resp = self.exchange_for_long_lived_token
135
- # if resp && resp.data['access_token']
136
- # self.token = resp.data['access_token']
137
- # self.refresh_token = resp.data['access_token']
138
- # self.expires_on = 59.days.from_now
139
- # end
140
- # end
119
+ attrs[:status] = 'active'
120
+ attrs[:token] = resp.data[:access_token]
121
+ attrs[:refresh_token] = resp.data[:refresh_token] if resp.data.key?(:refresh_token)
122
+ attrs[:expires_on] = resp.data[:expires_in].to_i.seconds.from_now if resp.data[:expires_in]
123
+
124
+ plugin_attrs = self.plugin.credential_fields_to_update(resp) if self.plugin.respond_to?(:credential_fields_to_update)
125
+ attrs.merge!(plugin_attrs)
141
126
  else
142
- self.credential.status = 'error' if self.credential.respond_to?(:"status=")
127
+ attrs[:status] = 'error'
143
128
  end
144
129
 
145
- if self.credential.respond_to?(:"save")
146
- self.credential.save && resp.success
130
+ # Only save Setsuzoku known fields, and any additional attributes the plugin specifies
131
+ # in credential_fields_to_update.
132
+ if self.credential.respond_to?(:"update_columns")
133
+ # we issue an update_columns instead of a save/update_attributes
134
+ # so as to only mutate these attributes
135
+ # and not any other fields that may have been set for the credential
136
+ save = self.credential.update_columns(attrs)
137
+ self.credential.touch if save && self.credential.respond_to?(:"touch")
138
+ save && resp.success
147
139
  else
148
140
  resp.success
149
141
  end
@@ -42,8 +42,8 @@ module Setsuzoku
42
42
  # @return [Struct] a stubbed basic_auth_credential-like struct.
43
43
  sig { returns(Struct) }
44
44
  def self.stub_credential
45
- s = Struct.new(:username, :password)
46
- s.new('stubbed_username', 'stubbed_password')
45
+ s = Struct.new(:status, :settings, :username, :password)
46
+ s.new('active', {}, 'stubbed_username', 'stubbed_password')
47
47
  end
48
48
  end
49
49
  end
@@ -73,8 +73,8 @@ module Setsuzoku
73
73
  # @return [Struct] a stubbed o_auth_credential-like struct.
74
74
  sig { returns(Struct) }
75
75
  def self.stub_credential
76
- s = Struct.new(:client_id, :client_secret, :redirect_url, :token, :refresh_token, :expires_on, :settings)
77
- s.new('stubbed_client_id', 'stubbed_client_secret', 'stubbed_redirect_url', 'stubbed_token', 'refresh_token', (Time.now + 30.days), 'extension': 'test')
76
+ s = Struct.new(:status, :settings, :client_id, :client_secret, :redirect_url, :token, :refresh_token, :expires_on)
77
+ s.new('active', {'extension': 'test'}, 'stubbed_client_id', 'stubbed_client_secret', 'stubbed_redirect_url', 'stubbed_token', 'refresh_token', (Time.now + 30.days))
78
78
  end
79
79
  end
80
80
  end
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Setsuzoku
5
- VERSION = '0.11.0'
5
+ VERSION = '0.11.9'
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: setsuzoku
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.11.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luke Stadtler
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-11 00:00:00.000000000 Z
11
+ date: 2020-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler