signet 0.11.0 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +47 -36
  3. data/Gemfile +5 -4
  4. data/README.md +4 -5
  5. data/Rakefile +86 -37
  6. data/lib/signet.rb +17 -14
  7. data/lib/signet/errors.rb +4 -4
  8. data/lib/signet/oauth_1.rb +128 -153
  9. data/lib/signet/oauth_1/client.rb +309 -343
  10. data/lib/signet/oauth_1/credential.rb +40 -37
  11. data/lib/signet/oauth_1/server.rb +197 -203
  12. data/lib/signet/oauth_1/signature_methods/hmac_sha1.rb +11 -10
  13. data/lib/signet/oauth_1/signature_methods/plaintext.rb +8 -7
  14. data/lib/signet/oauth_1/signature_methods/rsa_sha1.rb +11 -11
  15. data/lib/signet/oauth_2.rb +41 -43
  16. data/lib/signet/oauth_2/client.rb +302 -313
  17. data/lib/signet/version.rb +2 -73
  18. data/signet.gemspec +37 -39
  19. data/spec/signet/oauth_1/client_spec.rb +313 -315
  20. data/spec/signet/oauth_1/credential_spec.rb +64 -56
  21. data/spec/signet/oauth_1/server_spec.rb +362 -362
  22. data/spec/signet/oauth_1/signature_methods/hmac_sha1_spec.rb +26 -26
  23. data/spec/signet/oauth_1/signature_methods/plaintext_spec.rb +28 -28
  24. data/spec/signet/oauth_1/signature_methods/rsa_sha1_spec.rb +34 -35
  25. data/spec/signet/oauth_1_spec.rb +527 -524
  26. data/spec/signet/oauth_2/client_spec.rb +612 -576
  27. data/spec/signet/oauth_2_spec.rb +88 -89
  28. data/spec/signet_spec.rb +41 -41
  29. data/spec/spec_helper.rb +7 -7
  30. data/spec/spec_helper_spec.rb +8 -8
  31. metadata +50 -43
  32. data/tasks/clobber.rake +0 -2
  33. data/tasks/gem.rake +0 -34
  34. data/tasks/git.rake +0 -40
  35. data/tasks/metrics.rake +0 -41
  36. data/tasks/spec.rake +0 -34
  37. data/tasks/wiki.rake +0 -38
  38. data/tasks/yard.rake +0 -21
@@ -1,23 +1,24 @@
1
- require 'openssl'
2
- require 'signet'
1
+ require "openssl"
2
+ require "signet"
3
3
 
4
4
  module Signet #:nodoc:
5
5
  module OAuth1
6
6
  module HMACSHA1
7
- def self.generate_signature(
8
- base_string, client_credential_secret, token_credential_secret)
7
+ def self.generate_signature \
8
+ base_string, client_credential_secret, token_credential_secret
9
+
9
10
  # Both the client secret and token secret must be escaped
10
11
  client_credential_secret =
11
- Signet::OAuth1.encode(client_credential_secret)
12
+ Signet::OAuth1.encode client_credential_secret
12
13
  token_credential_secret =
13
- Signet::OAuth1.encode(token_credential_secret)
14
+ Signet::OAuth1.encode token_credential_secret
14
15
  # The key for the signature is just the client secret and token
15
16
  # secret joined by the '&' character. If the token secret is omitted,
16
17
  # the '&' must still be present.
17
- key = [client_credential_secret, token_credential_secret].join("&")
18
- return Base64.encode64(OpenSSL::HMAC.digest(
19
- OpenSSL::Digest.new('sha1'), key, base_string
20
- )).strip
18
+ key = [client_credential_secret, token_credential_secret].join "&"
19
+ Base64.encode64(OpenSSL::HMAC.digest(
20
+ OpenSSL::Digest.new("sha1"), key, base_string
21
+ )).strip
21
22
  end
22
23
  end
23
24
  end
@@ -1,20 +1,21 @@
1
- require 'signet'
1
+ require "signet"
2
2
 
3
3
  module Signet #:nodoc:
4
4
  module OAuth1
5
5
  module PLAINTEXT
6
- def self.generate_signature(
7
- base_string, client_credential_secret, token_credential_secret)
6
+ def self.generate_signature \
7
+ _base_string, client_credential_secret, token_credential_secret
8
+
8
9
  # Both the client secret and token secret must be escaped
9
10
  client_credential_secret =
10
- Signet::OAuth1.encode(client_credential_secret)
11
+ Signet::OAuth1.encode client_credential_secret
11
12
  token_credential_secret =
12
- Signet::OAuth1.encode(token_credential_secret)
13
+ Signet::OAuth1.encode token_credential_secret
13
14
  # The key for the signature is just the client secret and token
14
15
  # secret joined by the '&' character. If the token secret is omitted,
15
16
  # the '&' must still be present.
16
- key = [client_credential_secret, token_credential_secret].join("&")
17
- return Signet::OAuth1.encode(key).strip
17
+ key = [client_credential_secret, token_credential_secret].join "&"
18
+ Signet::OAuth1.encode(key).strip
18
19
  end
19
20
  end
20
21
  end
@@ -1,20 +1,20 @@
1
- require 'digest/sha1'
2
- require 'base64'
3
- require 'openssl'
4
- require 'signet'
1
+ require "digest/sha1"
2
+ require "base64"
3
+ require "openssl"
4
+ require "signet"
5
5
 
6
6
  module Signet #:nodoc:
7
7
  module OAuth1
8
8
  module RSASHA1
9
- def self.generate_signature(
10
- base_string, client_credential_secret, token_credential_secret)
9
+ def self.generate_signature \
10
+ base_string, client_credential_secret, _token_credential_secret
11
11
 
12
- private_key = OpenSSL::PKey::RSA.new(client_credential_secret)
13
- signature = private_key.sign(OpenSSL::Digest::SHA1.new, base_string)
14
- #using strict_encode64 because the encode64 method adds newline characters after ever 60 chars
15
- return Base64.strict_encode64(signature).strip
16
- end
17
12
 
13
+ private_key = OpenSSL::PKey::RSA.new client_credential_secret
14
+ signature = private_key.sign OpenSSL::Digest::SHA1.new, base_string
15
+ # using strict_encode64 because the encode64 method adds newline characters after ever 60 chars
16
+ Base64.strict_encode64(signature).strip
17
+ end
18
18
  end
19
19
  end
20
20
  end
@@ -12,9 +12,9 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- require 'base64'
16
- require 'signet'
17
- require 'multi_json'
15
+ require "base64"
16
+ require "signet"
17
+ require "multi_json"
18
18
 
19
19
  module Signet #:nodoc:
20
20
  ##
@@ -23,63 +23,61 @@ module Signet #:nodoc:
23
23
  # This module will be updated periodically to support newer drafts of the
24
24
  # specification, as they become widely deployed.
25
25
  module OAuth2
26
- def self.parse_authorization_header(field_value)
26
+ def self.parse_authorization_header field_value
27
27
  auth_scheme = field_value[/^([-._0-9a-zA-Z]+)/, 1]
28
28
  case auth_scheme
29
29
  when /^Basic$/i
30
30
  # HTTP Basic is allowed in OAuth 2
31
- return self.parse_basic_credentials(field_value[/^Basic\s+(.*)$/i, 1])
31
+ return parse_basic_credentials(field_value[/^Basic\s+(.*)$/i, 1])
32
32
  when /^OAuth$/i
33
33
  # Other token types may be supported eventually
34
- return self.parse_bearer_credentials(field_value[/^OAuth\s+(.*)$/i, 1])
34
+ return parse_bearer_credentials(field_value[/^OAuth\s+(.*)$/i, 1])
35
35
  else
36
36
  raise ParseError,
37
- 'Parsing non-OAuth Authorization headers is out of scope.'
37
+ "Parsing non-OAuth Authorization headers is out of scope."
38
38
  end
39
39
  end
40
40
 
41
- def self.parse_www_authenticate_header(field_value)
41
+ def self.parse_www_authenticate_header field_value
42
42
  auth_scheme = field_value[/^([-._0-9a-zA-Z]+)/, 1]
43
43
  case auth_scheme
44
44
  when /^OAuth$/i
45
45
  # Other token types may be supported eventually
46
- return self.parse_oauth_challenge(field_value[/^OAuth\s+(.*)$/i, 1])
46
+ return parse_oauth_challenge(field_value[/^OAuth\s+(.*)$/i, 1])
47
47
  else
48
48
  raise ParseError,
49
- 'Parsing non-OAuth WWW-Authenticate headers is out of scope.'
49
+ "Parsing non-OAuth WWW-Authenticate headers is out of scope."
50
50
  end
51
51
  end
52
52
 
53
- def self.parse_basic_credentials(credential_string)
54
- decoded = Base64.decode64(credential_string)
55
- client_id, client_secret = decoded.split(':', 2)
56
- return [['client_id', client_id], ['client_secret', client_secret]]
53
+ def self.parse_basic_credentials credential_string
54
+ decoded = Base64.decode64 credential_string
55
+ client_id, client_secret = decoded.split ":", 2
56
+ [["client_id", client_id], ["client_secret", client_secret]]
57
57
  end
58
58
 
59
- def self.parse_bearer_credentials(credential_string)
59
+ def self.parse_bearer_credentials credential_string
60
60
  access_token = credential_string[/^([^,\s]+)(?:\s|,|$)/i, 1]
61
61
  parameters = []
62
- parameters << ['access_token', access_token]
62
+ parameters << ["access_token", access_token]
63
63
  auth_param_string = credential_string[/^(?:[^,\s]+)\s*,\s*(.*)$/i, 1]
64
64
  if auth_param_string
65
65
  # This code will rarely get called, but is included for completeness
66
- parameters.concat(Signet.parse_auth_param_list(auth_param_string))
66
+ parameters.concat Signet.parse_auth_param_list(auth_param_string)
67
67
  end
68
- return parameters
68
+ parameters
69
69
  end
70
70
 
71
- def self.parse_oauth_challenge(challenge_string)
72
- return Signet.parse_auth_param_list(challenge_string)
71
+ def self.parse_oauth_challenge challenge_string
72
+ Signet.parse_auth_param_list challenge_string
73
73
  end
74
74
 
75
- def self.parse_credentials(body, content_type)
76
- if !body.kind_of?(String)
77
- raise TypeError, "Expected String, got #{body.class}."
78
- end
75
+ def self.parse_credentials body, content_type
76
+ raise TypeError, "Expected String, got #{body.class}." unless body.is_a? String
79
77
  case content_type
80
- when /^application\/json.*/
81
- return MultiJson.load(body)
82
- when /^application\/x-www-form-urlencoded.*/
78
+ when %r{^application/json.*}
79
+ return MultiJson.load body
80
+ when %r{^application/x-www-form-urlencoded.*}
83
81
  return Hash[Addressable::URI.form_unencode(body)]
84
82
  else
85
83
  raise ArgumentError, "Invalid content type '#{content_type}'"
@@ -97,14 +95,14 @@ module Signet #:nodoc:
97
95
  #
98
96
  # @return [String]
99
97
  # The value for the HTTP Basic Authorization header.
100
- def self.generate_basic_authorization_header(client_id, client_password)
98
+ def self.generate_basic_authorization_header client_id, client_password
101
99
  if client_id =~ /:/
102
100
  raise ArgumentError,
103
- "A client identifier may not contain a ':' character."
101
+ "A client identifier may not contain a ':' character."
104
102
  end
105
- return 'Basic ' + Base64.encode64(
106
- client_id + ':' + client_password
107
- ).gsub(/\n/, '')
103
+ "Basic " + Base64.encode64(
104
+ client_id + ":" + client_password
105
+ ).delete("\n")
108
106
  end
109
107
 
110
108
  ##
@@ -117,19 +115,19 @@ module Signet #:nodoc:
117
115
  #
118
116
  # @return [String]
119
117
  # The value for the HTTP Basic Authorization header.
120
- def self.generate_bearer_authorization_header(
121
- access_token, auth_params=nil)
118
+ def self.generate_bearer_authorization_header \
119
+ access_token, auth_params = nil
120
+
122
121
  # TODO: escaping?
123
122
  header = "Bearer #{access_token}"
124
123
  if auth_params && !auth_params.empty?
125
124
  header += (", " +
126
- (auth_params.inject([]) do |accu, (key, value)|
125
+ (auth_params.each_with_object [] do |(key, value), accu|
127
126
  accu << "#{key}=\"#{value}\""
128
- accu
129
127
  end).join(", ")
130
- )
128
+ )
131
129
  end
132
- return header
130
+ header
133
131
  end
134
132
 
135
133
  ##
@@ -140,15 +138,15 @@ module Signet #:nodoc:
140
138
  # The base authorization endpoint URI.
141
139
  #
142
140
  # @return [String] The authorization URI to redirect the user to.
143
- def self.generate_authorization_uri(authorization_uri, parameters={})
144
- for key, value in parameters
145
- parameters.delete(key) if value.nil?
141
+ def self.generate_authorization_uri authorization_uri, parameters = {}
142
+ parameters.each do |key, value|
143
+ parameters.delete key if value.nil?
146
144
  end
147
145
  parsed_uri = Addressable::URI.parse(authorization_uri).dup
148
146
  query_values = parsed_uri.query_values || {}
149
- query_values = query_values.merge(parameters)
147
+ query_values = query_values.merge parameters
150
148
  parsed_uri.query_values = query_values
151
- return parsed_uri.normalize.to_s
149
+ parsed_uri.normalize.to_s
152
150
  end
153
151
  end
154
152
  end
@@ -12,20 +12,19 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- require 'faraday'
16
- require 'stringio'
17
- require 'addressable/uri'
18
- require 'signet'
19
- require 'signet/errors'
20
- require 'signet/oauth_2'
21
- require 'jwt'
22
- require 'date'
15
+ require "faraday"
16
+ require "stringio"
17
+ require "addressable/uri"
18
+ require "signet"
19
+ require "signet/errors"
20
+ require "signet/oauth_2"
21
+ require "jwt"
22
+ require "date"
23
23
 
24
24
  module Signet
25
25
  module OAuth2
26
26
  class Client
27
-
28
- OOB_MODES = %w(urn:ietf:wg:oauth:2.0:oob:auto urn:ietf:wg:oauth:2.0:oob oob)
27
+ OOB_MODES = ["urn:ietf:wg:oauth:2.0:oob:auto", "urn:ietf:wg:oauth:2.0:oob", "oob"].freeze
29
28
 
30
29
  ##
31
30
  # Creates an OAuth 2.0 client.
@@ -89,7 +88,7 @@ module Signet
89
88
  # )
90
89
  #
91
90
  # @see Signet::OAuth2::Client#update!
92
- def initialize options={}
91
+ def initialize options = {}
93
92
  @authorization_uri = nil
94
93
  @token_credential_uri = nil
95
94
  @client_id = nil
@@ -105,8 +104,11 @@ module Signet
105
104
  @state = nil
106
105
  @username = nil
107
106
  @access_type = nil
108
- self.update!(options)
107
+ update! options
109
108
  end
109
+ # rubocop:disable Metrics/AbcSize
110
+ # rubocop:disable Metrics/CyclomaticComplexity
111
+ # rubocop:disable Metrics/PerceivedComplexity
110
112
 
111
113
  ##
112
114
  # Updates an OAuth 2.0 client.
@@ -170,32 +172,35 @@ module Signet
170
172
  #
171
173
  # @see Signet::OAuth2::Client#initialize
172
174
  # @see Signet::OAuth2::Client#update_token!
173
- def update!(options={})
175
+ def update! options = {}
174
176
  # Normalize all keys to symbols to allow indifferent access.
175
- options = deep_hash_normalize(options)
176
-
177
- self.authorization_uri = options[:authorization_uri] if options.has_key?(:authorization_uri)
178
- self.token_credential_uri = options[:token_credential_uri] if options.has_key?(:token_credential_uri)
179
- self.client_id = options[:client_id] if options.has_key?(:client_id)
180
- self.client_secret = options[:client_secret] if options.has_key?(:client_secret)
181
- self.scope = options[:scope] if options.has_key?(:scope)
182
- self.state = options[:state] if options.has_key?(:state)
183
- self.code = options[:code] if options.has_key?(:code)
184
- self.redirect_uri = options[:redirect_uri] if options.has_key?(:redirect_uri)
185
- self.username = options[:username] if options.has_key?(:username)
186
- self.password = options[:password] if options.has_key?(:password)
187
- self.issuer = options[:issuer] if options.has_key?(:issuer)
188
- self.person = options[:person] if options.has_key?(:person)
189
- self.sub = options[:sub] if options.has_key?(:sub)
177
+ options = deep_hash_normalize options
178
+
179
+ self.authorization_uri = options[:authorization_uri] if options.key? :authorization_uri
180
+ self.token_credential_uri = options[:token_credential_uri] if options.key? :token_credential_uri
181
+ self.client_id = options[:client_id] if options.key? :client_id
182
+ self.client_secret = options[:client_secret] if options.key? :client_secret
183
+ self.scope = options[:scope] if options.key? :scope
184
+ self.state = options[:state] if options.key? :state
185
+ self.code = options[:code] if options.key? :code
186
+ self.redirect_uri = options[:redirect_uri] if options.key? :redirect_uri
187
+ self.username = options[:username] if options.key? :username
188
+ self.password = options[:password] if options.key? :password
189
+ self.issuer = options[:issuer] if options.key? :issuer
190
+ self.person = options[:person] if options.key? :person
191
+ self.sub = options[:sub] if options.key? :sub
190
192
  self.expiry = options[:expiry] || 60
191
- self.audience = options[:audience] if options.has_key?(:audience)
192
- self.signing_key = options[:signing_key] if options.has_key?(:signing_key)
193
+ self.audience = options[:audience] if options.key? :audience
194
+ self.signing_key = options[:signing_key] if options.key? :signing_key
193
195
  self.extension_parameters = options[:extension_parameters] || {}
194
196
  self.additional_parameters = options[:additional_parameters] || {}
195
197
  self.access_type = options.fetch(:access_type) { :offline }
196
- self.update_token!(options)
197
- return self
198
+ update_token! options
199
+ self
198
200
  end
201
+ # rubocop:enable Metrics/AbcSize
202
+ # rubocop:enable Metrics/CyclomaticComplexity
203
+ # rubocop:enable Metrics/PerceivedComplexity
199
204
 
200
205
  ##
201
206
  # Updates an OAuth 2.0 client.
@@ -225,29 +230,33 @@ module Signet
225
230
  #
226
231
  # @see Signet::OAuth2::Client#initialize
227
232
  # @see Signet::OAuth2::Client#update!
228
- def update_token!(options={})
233
+ def update_token! options = {}
229
234
  # Normalize all keys to symbols to allow indifferent access internally
230
- options = deep_hash_normalize(options)
235
+ options = deep_hash_normalize options
231
236
 
232
- self.expires_in = options[:expires] if options.has_key?(:expires)
233
- self.expires_in = options[:expires_in] if options.has_key?(:expires_in)
234
- self.expires_at = options[:expires_at] if options.has_key?(:expires_at)
237
+ self.expires_in = options[:expires] if options.key? :expires
238
+ self.expires_in = options[:expires_in] if options.key? :expires_in
239
+ self.expires_at = options[:expires_at] if options.key? :expires_at
235
240
 
236
241
  # By default, the token is issued at `Time.now` when `expires_in` is
237
242
  # set, but this can be used to supply a more precise time.
238
- self.issued_at = options[:issued_at] if options.has_key?(:issued_at)
243
+ self.issued_at = options[:issued_at] if options.key? :issued_at
239
244
 
240
245
  # Special case where we want expires_at to be relative to issued_at
241
- if options.has_key?(:issued_at) && options.has_key?(:expires_in)
246
+ if options.key?(:issued_at) && options.key?(:expires_in)
242
247
  set_relative_expires_at options[:issued_at], options[:expires_in]
243
248
  end
244
249
 
245
- self.access_token = options[:access_token] if options.has_key?(:access_token)
246
- self.refresh_token = options[:refresh_token] if options.has_key?(:refresh_token)
247
- self.id_token = options[:id_token] if options.has_key?(:id_token)
250
+ self.access_token = options[:access_token] if options.key? :access_token
251
+ self.refresh_token = options[:refresh_token] if options.key? :refresh_token
252
+ self.id_token = options[:id_token] if options.key? :id_token
248
253
 
249
- return self
254
+ self
250
255
  end
256
+ # rubocop:disable Metrics/AbcSize
257
+ # rubocop:disable Metrics/CyclomaticComplexity
258
+ # rubocop:disable Metrics/MethodLength
259
+ # rubocop:disable Metrics/PerceivedComplexity
251
260
 
252
261
  ##
253
262
  # Returns the authorization URI that the user should be redirected to.
@@ -255,34 +264,24 @@ module Signet
255
264
  # @return [Addressable::URI] The authorization URI.
256
265
  #
257
266
  # @see Signet::OAuth2.generate_authorization_uri
258
- def authorization_uri(options={})
267
+ def authorization_uri options = {}
259
268
  # Normalize external input
260
- options = deep_hash_normalize(options)
269
+ options = deep_hash_normalize options
261
270
 
262
- return nil if @authorization_uri == nil
263
- unless options[:response_type]
264
- options[:response_type] = :code
265
- end
266
- if !options[:access_type] && access_type
267
- options[:access_type] = access_type
268
- end
269
- options[:client_id] ||= self.client_id
270
- options[:redirect_uri] ||= self.redirect_uri
271
+ return nil if @authorization_uri.nil?
272
+ options[:response_type] = :code unless options[:response_type]
273
+ options[:access_type] = access_type if !options[:access_type] && access_type
274
+ options[:client_id] ||= client_id
275
+ options[:redirect_uri] ||= redirect_uri
271
276
  if options[:prompt] && options[:approval_prompt]
272
277
  raise ArgumentError, "prompt and approval_prompt are mutually exclusive parameters"
273
278
  end
274
- if !options[:client_id]
275
- raise ArgumentError, "Missing required client identifier."
276
- end
277
- unless options[:redirect_uri]
278
- raise ArgumentError, "Missing required redirect URI."
279
- end
280
- if !options[:scope] && self.scope
281
- options[:scope] = self.scope.join(' ')
282
- end
283
- options[:state] = self.state unless options[:state]
284
- options.merge!(self.additional_parameters.merge(options[:additional_parameters] || {}))
285
- options.delete(:additional_parameters)
279
+ raise ArgumentError, "Missing required client identifier." unless options[:client_id]
280
+ raise ArgumentError, "Missing required redirect URI." unless options[:redirect_uri]
281
+ options[:scope] = scope.join " " if !options[:scope] && scope
282
+ options[:state] = state unless options[:state]
283
+ options.merge!(additional_parameters.merge(options[:additional_parameters] || {}))
284
+ options.delete :additional_parameters
286
285
  options = Hash[options.map do |key, option|
287
286
  [key.to_s, option]
288
287
  end]
@@ -291,20 +290,24 @@ module Signet
291
290
  @authorization_uri, options
292
291
  )
293
292
  )
294
- if uri.normalized_scheme != 'https'
293
+ if uri.normalized_scheme != "https"
295
294
  raise Signet::UnsafeOperationError,
296
- 'Authorization endpoint must be protected by TLS.'
295
+ "Authorization endpoint must be protected by TLS."
297
296
  end
298
- return uri
297
+ uri
299
298
  end
299
+ # rubocop:enable Metrics/AbcSize
300
+ # rubocop:enable Metrics/CyclomaticComplexity
301
+ # rubocop:enable Metrics/MethodLength
302
+ # rubocop:enable Metrics/PerceivedComplexity
300
303
 
301
304
  ##
302
305
  # Sets the authorization URI for this client.
303
306
  #
304
307
  # @param [Addressable::URI, Hash, String, #to_str] new_authorization_uri
305
308
  # The authorization URI.
306
- def authorization_uri=(new_authorization_uri)
307
- @authorization_uri = coerce_uri(new_authorization_uri)
309
+ def authorization_uri= new_authorization_uri
310
+ @authorization_uri = coerce_uri new_authorization_uri
308
311
  end
309
312
 
310
313
  ##
@@ -312,7 +315,7 @@ module Signet
312
315
  #
313
316
  # @return [Addressable::URI] The token credential URI.
314
317
  def token_credential_uri
315
- return @token_credential_uri
318
+ @token_credential_uri
316
319
  end
317
320
 
318
321
  ##
@@ -320,17 +323,17 @@ module Signet
320
323
  #
321
324
  # @param [Addressable::URI, Hash, String, #to_str] new_token_credential_uri
322
325
  # The token credential URI.
323
- def token_credential_uri=(new_token_credential_uri)
324
- @token_credential_uri = coerce_uri(new_token_credential_uri)
326
+ def token_credential_uri= new_token_credential_uri
327
+ @token_credential_uri = coerce_uri new_token_credential_uri
325
328
  end
326
329
 
327
330
  # Addressable expects URIs formatted as hashes to come in with symbols as keys.
328
331
  # Returns nil implicitly for the nil case.
329
- def coerce_uri(incoming_uri)
332
+ def coerce_uri incoming_uri
330
333
  if incoming_uri.is_a? Hash
331
- Addressable::URI.new(deep_hash_normalize(incoming_uri))
334
+ Addressable::URI.new deep_hash_normalize(incoming_uri)
332
335
  elsif incoming_uri
333
- Addressable::URI.parse(incoming_uri)
336
+ Addressable::URI.parse incoming_uri
334
337
  end
335
338
  end
336
339
 
@@ -339,7 +342,7 @@ module Signet
339
342
  #
340
343
  # @return [String, Symbol] The current access type.
341
344
  def access_type
342
- return @access_type
345
+ @access_type
343
346
  end
344
347
 
345
348
  ##
@@ -347,7 +350,7 @@ module Signet
347
350
  #
348
351
  # @param [String, Symbol] new_access_type
349
352
  # The current access type.
350
- def access_type=(new_access_type)
353
+ def access_type= new_access_type
351
354
  @access_type = new_access_type
352
355
  end
353
356
 
@@ -356,7 +359,7 @@ module Signet
356
359
  #
357
360
  # @return [String] The client identifier.
358
361
  def client_id
359
- return @client_id
362
+ @client_id
360
363
  end
361
364
 
362
365
  ##
@@ -364,7 +367,7 @@ module Signet
364
367
  #
365
368
  # @param [String] new_client_id
366
369
  # The client identifier.
367
- def client_id=(new_client_id)
370
+ def client_id= new_client_id
368
371
  @client_id = new_client_id
369
372
  end
370
373
 
@@ -373,7 +376,7 @@ module Signet
373
376
  #
374
377
  # @return [String] The client secret.
375
378
  def client_secret
376
- return @client_secret
379
+ @client_secret
377
380
  end
378
381
 
379
382
  ##
@@ -381,7 +384,7 @@ module Signet
381
384
  #
382
385
  # @param [String] new_client_secret
383
386
  # The client secret.
384
- def client_secret=(new_client_secret)
387
+ def client_secret= new_client_secret
385
388
  @client_secret = new_client_secret
386
389
  end
387
390
 
@@ -391,7 +394,7 @@ module Signet
391
394
  #
392
395
  # @return [Array] The scope of access the client is requesting.
393
396
  def scope
394
- return @scope
397
+ @scope
395
398
  end
396
399
 
397
400
  ##
@@ -401,18 +404,18 @@ module Signet
401
404
  # The scope of access the client is requesting. This may be
402
405
  # expressed as either an Array of String objects or as a
403
406
  # space-delimited String.
404
- def scope=(new_scope)
407
+ def scope= new_scope
405
408
  case new_scope
406
409
  when Array
407
410
  new_scope.each do |scope|
408
- if scope.include?(' ')
411
+ if scope.include? " "
409
412
  raise ArgumentError,
410
- "Individual scopes cannot contain the space character."
413
+ "Individual scopes cannot contain the space character."
411
414
  end
412
415
  end
413
416
  @scope = new_scope
414
417
  when String
415
- @scope = new_scope.split(' ')
418
+ @scope = new_scope.split " "
416
419
  when nil
417
420
  @scope = nil
418
421
  else
@@ -425,7 +428,7 @@ module Signet
425
428
  #
426
429
  # @return [String] The state value.
427
430
  def state
428
- return @state
431
+ @state
429
432
  end
430
433
 
431
434
  ##
@@ -433,7 +436,7 @@ module Signet
433
436
  #
434
437
  # @param [String] new_state
435
438
  # The state value.
436
- def state=(new_state)
439
+ def state= new_state
437
440
  @state = new_state
438
441
  end
439
442
 
@@ -443,7 +446,7 @@ module Signet
443
446
  #
444
447
  # @return [String] The authorization code.
445
448
  def code
446
- return @code
449
+ @code
447
450
  end
448
451
 
449
452
  ##
@@ -452,7 +455,7 @@ module Signet
452
455
  #
453
456
  # @param [String] new_code
454
457
  # The authorization code.
455
- def code=(new_code)
458
+ def code= new_code
456
459
  @code = new_code
457
460
  end
458
461
 
@@ -461,7 +464,7 @@ module Signet
461
464
  #
462
465
  # @return [String] The redirect URI.
463
466
  def redirect_uri
464
- return @redirect_uri
467
+ @redirect_uri
465
468
  end
466
469
 
467
470
  ##
@@ -469,14 +472,14 @@ module Signet
469
472
  #
470
473
  # @param [String] new_redirect_uri
471
474
  # The redirect URI.
472
- def redirect_uri=(new_redirect_uri)
473
- new_redirect_uri = Addressable::URI.parse(new_redirect_uri)
474
- #TODO - Better solution to allow google postmessage flow. For now, make an exception to the spec.
475
- if new_redirect_uri == nil|| new_redirect_uri.absolute? || uri_is_postmessage?(new_redirect_uri) || uri_is_oob?(new_redirect_uri)
476
- @redirect_uri = new_redirect_uri
477
- else
475
+ def redirect_uri= new_redirect_uri
476
+ new_redirect_uri = Addressable::URI.parse new_redirect_uri
477
+ # TODO: - Better solution to allow google postmessage flow. For now, make an exception to the spec.
478
+ unless new_redirect_uri.nil? || new_redirect_uri.absolute? || uri_is_postmessage?(new_redirect_uri) ||
479
+ uri_is_oob?(new_redirect_uri)
478
480
  raise ArgumentError, "Redirect URI must be an absolute URI."
479
481
  end
482
+ @redirect_uri = new_redirect_uri
480
483
  end
481
484
 
482
485
  ##
@@ -485,7 +488,7 @@ module Signet
485
488
  #
486
489
  # @return [String] The username.
487
490
  def username
488
- return @username
491
+ @username
489
492
  end
490
493
 
491
494
  ##
@@ -494,7 +497,7 @@ module Signet
494
497
  #
495
498
  # @param [String] new_username
496
499
  # The username.
497
- def username=(new_username)
500
+ def username= new_username
498
501
  @username = new_username
499
502
  end
500
503
 
@@ -504,7 +507,7 @@ module Signet
504
507
  #
505
508
  # @return [String] The password.
506
509
  def password
507
- return @password
510
+ @password
508
511
  end
509
512
 
510
513
  ##
@@ -513,7 +516,7 @@ module Signet
513
516
  #
514
517
  # @param [String] new_password
515
518
  # The password.
516
- def password=(new_password)
519
+ def password= new_password
517
520
  @password = new_password
518
521
  end
519
522
 
@@ -523,7 +526,7 @@ module Signet
523
526
  #
524
527
  # @return [String] Issuer id.
525
528
  def issuer
526
- return @issuer
529
+ @issuer
527
530
  end
528
531
 
529
532
  ##
@@ -532,17 +535,17 @@ module Signet
532
535
  #
533
536
  # @param [String] new_issuer
534
537
  # Issuer ID (typical in email adddress form).
535
- def issuer=(new_issuer)
538
+ def issuer= new_issuer
536
539
  @issuer = new_issuer
537
540
  end
538
541
 
539
542
  ##
540
- # Returns the issuer ID associated with this client.
543
+ # Returns the target audience ID when issuing assertions.
541
544
  # Used only by the assertion grant type.
542
545
  #
543
546
  # @return [String] Target audience ID.
544
547
  def audience
545
- return @audience
548
+ @audience
546
549
  end
547
550
 
548
551
  ##
@@ -551,7 +554,7 @@ module Signet
551
554
  #
552
555
  # @param [String] new_audience
553
556
  # Target audience ID
554
- def audience=(new_audience)
557
+ def audience= new_audience
555
558
  @audience = new_audience
556
559
  end
557
560
 
@@ -561,7 +564,7 @@ module Signet
561
564
  #
562
565
  # @return [String] Target user for impersonation.
563
566
  def principal
564
- return @principal
567
+ @principal
565
568
  end
566
569
 
567
570
  ##
@@ -570,12 +573,12 @@ module Signet
570
573
  #
571
574
  # @param [String] new_person
572
575
  # Target user for impersonation
573
- def principal=(new_person)
576
+ def principal= new_person
574
577
  @principal = new_person
575
578
  end
576
579
 
577
- alias_method :person, :principal
578
- alias_method :person=, :principal=
580
+ alias person principal
581
+ alias person= principal=
579
582
 
580
583
  ##
581
584
  # The target "sub" when issuing assertions.
@@ -589,7 +592,7 @@ module Signet
589
592
  #
590
593
  # @return [Integer] Assertion expiry, in seconds
591
594
  def expiry
592
- return @expiry
595
+ @expiry
593
596
  end
594
597
 
595
598
  ##
@@ -598,18 +601,17 @@ module Signet
598
601
  #
599
602
  # @param [Integer, String] new_expiry
600
603
  # Assertion expiry, in seconds
601
- def expiry=(new_expiry)
604
+ def expiry= new_expiry
602
605
  @expiry = new_expiry ? new_expiry.to_i : nil
603
606
  end
604
607
 
605
-
606
608
  ##
607
609
  # Returns the signing key associated with this client.
608
610
  # Used only by the assertion grant type.
609
611
  #
610
612
  # @return [String,OpenSSL::PKey] Signing key
611
613
  def signing_key
612
- return @signing_key
614
+ @signing_key
613
615
  end
614
616
 
615
617
  ##
@@ -618,7 +620,7 @@ module Signet
618
620
  #
619
621
  # @param [String, OpenSSL::Pkey] new_key
620
622
  # Signing key. Either private key for RSA or string for HMAC algorithm
621
- def signing_key=(new_key)
623
+ def signing_key= new_key
622
624
  @signing_key = new_key
623
625
  end
624
626
 
@@ -626,7 +628,7 @@ module Signet
626
628
  # Algorithm used for signing JWTs
627
629
  # @return [String] Signing algorithm
628
630
  def signing_algorithm
629
- self.signing_key.is_a?(String) ? "HS256" : "RS256"
631
+ signing_key.is_a?(String) ? "HS256" : "RS256"
630
632
  end
631
633
 
632
634
  ##
@@ -635,7 +637,7 @@ module Signet
635
637
  #
636
638
  # @return [Hash] The extension parameters.
637
639
  def extension_parameters
638
- return @extension_parameters ||= {}
640
+ @extension_parameters ||= {}
639
641
  end
640
642
 
641
643
  ##
@@ -644,12 +646,12 @@ module Signet
644
646
  #
645
647
  # @param [Hash] new_extension_parameters
646
648
  # The parameters.
647
- def extension_parameters=(new_extension_parameters)
648
- if new_extension_parameters.respond_to?(:to_hash)
649
+ def extension_parameters= new_extension_parameters
650
+ if new_extension_parameters.respond_to? :to_hash
649
651
  @extension_parameters = new_extension_parameters.to_hash
650
652
  else
651
653
  raise TypeError,
652
- "Expected Hash, got #{new_extension_parameters.class}."
654
+ "Expected Hash, got #{new_extension_parameters.class}."
653
655
  end
654
656
  end
655
657
 
@@ -658,7 +660,7 @@ module Signet
658
660
  #
659
661
  # @return [Hash] The pass through parameters.
660
662
  def additional_parameters
661
- return @additional_parameters ||= {}
663
+ @additional_parameters ||= {}
662
664
  end
663
665
 
664
666
  ##
@@ -666,8 +668,8 @@ module Signet
666
668
  #
667
669
  # @param [Hash] new_additional_parameters
668
670
  # The parameters.
669
- def additional_parameters=(new_additional_parameters)
670
- if new_additional_parameters.respond_to?(:to_hash)
671
+ def additional_parameters= new_additional_parameters
672
+ if new_additional_parameters.respond_to? :to_hash
671
673
  @additional_parameters = new_additional_parameters.to_hash
672
674
  else
673
675
  raise TypeError,
@@ -680,7 +682,7 @@ module Signet
680
682
  #
681
683
  # @return [String] The refresh token.
682
684
  def refresh_token
683
- return @refresh_token ||= nil
685
+ @refresh_token ||= nil
684
686
  end
685
687
 
686
688
  ##
@@ -688,7 +690,7 @@ module Signet
688
690
  #
689
691
  # @param [String] new_refresh_token
690
692
  # The refresh token.
691
- def refresh_token=(new_refresh_token)
693
+ def refresh_token= new_refresh_token
692
694
  @refresh_token = new_refresh_token
693
695
  end
694
696
 
@@ -697,7 +699,7 @@ module Signet
697
699
  #
698
700
  # @return [String] The access token.
699
701
  def access_token
700
- return @access_token ||= nil
702
+ @access_token ||= nil
701
703
  end
702
704
 
703
705
  ##
@@ -705,7 +707,7 @@ module Signet
705
707
  #
706
708
  # @param [String] new_access_token
707
709
  # The access token.
708
- def access_token=(new_access_token)
710
+ def access_token= new_access_token
709
711
  @access_token = new_access_token
710
712
  end
711
713
 
@@ -714,7 +716,7 @@ module Signet
714
716
  #
715
717
  # @return [String] The ID token.
716
718
  def id_token
717
- return @id_token ||= nil
719
+ @id_token ||= nil
718
720
  end
719
721
 
720
722
  ##
@@ -722,7 +724,7 @@ module Signet
722
724
  #
723
725
  # @param [String] new_id_token
724
726
  # The ID token.
725
- def id_token=(new_id_token)
727
+ def id_token= new_id_token
726
728
  @id_token = new_id_token
727
729
  end
728
730
 
@@ -734,17 +736,16 @@ module Signet
734
736
  # omitted.
735
737
  #
736
738
  # @return [String] The decoded ID token.
737
- def decoded_id_token public_key=nil, options = {}, &keyfinder
739
+ def decoded_id_token public_key = nil, options = {}, &keyfinder
738
740
  options[:algorithm] ||= signing_algorithm
739
- verify = !!(public_key || keyfinder)
740
- payload, _header = JWT.decode(self.id_token, public_key, verify, options, &keyfinder)
741
- if !payload.has_key?('aud')
742
- raise Signet::UnsafeOperationError, 'No ID token audience declared.'
743
- elsif payload['aud'] != self.client_id
741
+ verify = !public_key.nil? || block_given?
742
+ payload, _header = JWT.decode(id_token, public_key, verify, options, &keyfinder)
743
+ raise Signet::UnsafeOperationError, "No ID token audience declared." unless payload.key? "aud"
744
+ unless Array(payload["aud"]).include?(client_id)
744
745
  raise Signet::UnsafeOperationError,
745
- 'ID token audience did not match Client ID.'
746
+ "ID token audience did not match Client ID."
746
747
  end
747
- return payload
748
+ payload
748
749
  end
749
750
 
750
751
  ##
@@ -790,8 +791,8 @@ module Signet
790
791
  #
791
792
  # @param [String,Integer,Time] new_issued_at
792
793
  # The access token issuance time.
793
- def issued_at=(new_issued_at)
794
- @issued_at = normalize_timestamp(new_issued_at)
794
+ def issued_at= new_issued_at
795
+ @issued_at = normalize_timestamp new_issued_at
795
796
  end
796
797
 
797
798
  ##
@@ -809,7 +810,7 @@ module Signet
809
810
  # not expire.
810
811
  # @param [String,Integer,Time, nil] new_expires_at
811
812
  # The access token expiration time.
812
- def expires_at=(new_expires_at)
813
+ def expires_at= new_expires_at
813
814
  @expires_at = normalize_timestamp new_expires_at
814
815
  end
815
816
 
@@ -820,7 +821,7 @@ module Signet
820
821
  # @return [TrueClass, FalseClass]
821
822
  # The expiration state of the access token.
822
823
  def expired?
823
- return self.expires_at != nil && Time.now >= self.expires_at
824
+ !expires_at.nil? && Time.now >= expires_at
824
825
  end
825
826
 
826
827
  ##
@@ -832,8 +833,8 @@ module Signet
832
833
  # expired.
833
834
  # @return [TrueClass, FalseClass]
834
835
  # The expiration state of the access token.
835
- def expires_within?(sec)
836
- return self.expires_at != nil && Time.now >= (self.expires_at - sec)
836
+ def expires_within? sec
837
+ !expires_at.nil? && Time.now >= (expires_at - sec)
837
838
  end
838
839
 
839
840
  ##
@@ -849,7 +850,6 @@ module Signet
849
850
  @expires_at = nil
850
851
  end
851
852
 
852
-
853
853
  ##
854
854
  # Returns the inferred grant type, based on the current state of the
855
855
  # client object. Returns `"none"` if the client has insufficient
@@ -859,52 +859,45 @@ module Signet
859
859
  # The inferred grant type.
860
860
  def grant_type
861
861
  @grant_type ||= nil
862
- if @grant_type
863
- return @grant_type
864
- else
865
- if self.code && self.redirect_uri
866
- 'authorization_code'
867
- elsif self.refresh_token
868
- 'refresh_token'
869
- elsif self.username && self.password
870
- 'password'
871
- elsif self.issuer && self.signing_key
872
- 'urn:ietf:params:oauth:grant-type:jwt-bearer'
873
- else
874
- # We don't have sufficient auth information, assume an out-of-band
875
- # authorization arrangement between the client and server, or an
876
- # extension grant type.
877
- nil
878
- end
862
+ return @grant_type if @grant_type
863
+ if code && redirect_uri
864
+ "authorization_code"
865
+ elsif refresh_token
866
+ "refresh_token"
867
+ elsif username && password
868
+ "password"
869
+ elsif issuer && signing_key
870
+ "urn:ietf:params:oauth:grant-type:jwt-bearer"
879
871
  end
880
872
  end
881
873
 
882
- def grant_type=(new_grant_type)
874
+ def grant_type= new_grant_type
883
875
  case new_grant_type
884
- when 'authorization_code', 'refresh_token',
885
- 'password', 'client_credentials'
876
+ when "authorization_code", "refresh_token",
877
+ "password", "client_credentials"
886
878
  @grant_type = new_grant_type
887
879
  else
888
- @grant_type = Addressable::URI.parse(new_grant_type)
880
+ @grant_type = Addressable::URI.parse new_grant_type
889
881
  end
890
882
  end
891
883
 
892
- def to_jwt(options={})
893
- options = deep_hash_normalize(options)
884
+ def to_jwt options = {}
885
+ options = deep_hash_normalize options
894
886
 
895
887
  now = Time.new
896
888
  skew = options[:skew] || 60
897
889
  assertion = {
898
- "iss" => self.issuer,
899
- "aud" => self.audience,
900
- "exp" => (now + self.expiry).to_i,
890
+ "iss" => issuer,
891
+ "aud" => audience,
892
+ "exp" => (now + expiry).to_i,
901
893
  "iat" => (now - skew).to_i
902
894
  }
903
- assertion['scope'] = self.scope.join(' ') unless self.scope.nil?
904
- assertion['prn'] = self.person unless self.person.nil?
905
- assertion['sub'] = self.sub unless self.sub.nil?
906
- JWT.encode(assertion, self.signing_key, self.signing_algorithm)
895
+ assertion["scope"] = scope.join " " unless scope.nil?
896
+ assertion["prn"] = person unless person.nil?
897
+ assertion["sub"] = sub unless sub.nil?
898
+ JWT.encode assertion, signing_key, signing_algorithm
907
899
  end
900
+ # rubocop:disable Style/MethodDefParentheses
908
901
 
909
902
  ##
910
903
  # Serialize the client object to JSON.
@@ -913,29 +906,34 @@ module Signet
913
906
  #
914
907
  # @return [String] A serialized JSON representation of the client.
915
908
  def to_json(*)
916
- return MultiJson.dump({
917
- 'authorization_uri' => self.authorization_uri ? self.authorization_uri.to_s : nil,
918
- 'token_credential_uri' => self.token_credential_uri ? self.token_credential_uri.to_s : nil,
919
- 'client_id' => self.client_id,
920
- 'client_secret' => self.client_secret,
921
- 'scope' => self.scope,
922
- 'state' => self.state,
923
- 'code' => self.code,
924
- 'redirect_uri' => self.redirect_uri ? self.redirect_uri.to_s : nil,
925
- 'username' => self.username,
926
- 'password' => self.password,
927
- 'issuer' => self.issuer,
928
- 'audience' => self.audience,
929
- 'person' => self.person,
930
- 'expiry' => self.expiry,
931
- 'expires_at' => self.expires_at ? self.expires_at.to_i : nil,
932
- 'signing_key' => self.signing_key,
933
- 'refresh_token' => self.refresh_token,
934
- 'access_token' => self.access_token,
935
- 'id_token' => self.id_token,
936
- 'extension_parameters' => self.extension_parameters
937
- })
909
+ MultiJson.dump(
910
+ "authorization_uri" => authorization_uri ? authorization_uri.to_s : nil,
911
+ "token_credential_uri" => token_credential_uri ? token_credential_uri.to_s : nil,
912
+ "client_id" => client_id,
913
+ "client_secret" => client_secret,
914
+ "scope" => scope,
915
+ "state" => state,
916
+ "code" => code,
917
+ "redirect_uri" => redirect_uri ? redirect_uri.to_s : nil,
918
+ "username" => username,
919
+ "password" => password,
920
+ "issuer" => issuer,
921
+ "audience" => audience,
922
+ "person" => person,
923
+ "expiry" => expiry,
924
+ "expires_at" => expires_at ? expires_at.to_i : nil,
925
+ "signing_key" => signing_key,
926
+ "refresh_token" => refresh_token,
927
+ "access_token" => access_token,
928
+ "id_token" => id_token,
929
+ "extension_parameters" => extension_parameters
930
+ )
938
931
  end
932
+ # rubocop:enable Style/MethodDefParentheses
933
+ # rubocop:disable Metrics/AbcSize
934
+ # rubocop:disable Metrics/CyclomaticComplexity
935
+ # rubocop:disable Metrics/MethodLength
936
+ # rubocop:disable Metrics/PerceivedComplexity
939
937
 
940
938
  ##
941
939
  # Generates a request for token credentials.
@@ -947,58 +945,58 @@ module Signet
947
945
  #
948
946
  # @private
949
947
  # @return [Array] The request object.
950
- def generate_access_token_request(options={})
951
- options = deep_hash_normalize(options)
952
-
953
- parameters = {"grant_type" => self.grant_type}
954
- case self.grant_type
955
- when 'authorization_code'
956
- parameters['code'] = self.code
957
- parameters['redirect_uri'] = self.redirect_uri
958
- when 'password'
959
- parameters['username'] = self.username
960
- parameters['password'] = self.password
961
- when 'refresh_token'
962
- parameters['refresh_token'] = self.refresh_token
963
- when 'urn:ietf:params:oauth:grant-type:jwt-bearer'
964
- parameters['assertion'] = self.to_jwt(options)
948
+ def generate_access_token_request options = {}
949
+ options = deep_hash_normalize options
950
+
951
+ parameters = { "grant_type" => grant_type }
952
+ case grant_type
953
+ when "authorization_code"
954
+ parameters["code"] = code
955
+ parameters["redirect_uri"] = redirect_uri
956
+ when "password"
957
+ parameters["username"] = username
958
+ parameters["password"] = password
959
+ when "refresh_token"
960
+ parameters["refresh_token"] = refresh_token
961
+ when "urn:ietf:params:oauth:grant-type:jwt-bearer"
962
+ parameters["assertion"] = to_jwt options
965
963
  else
966
- if self.redirect_uri
964
+ if redirect_uri
967
965
  # Grant type was intended to be `authorization_code` because of
968
966
  # the presence of the redirect URI.
969
- raise ArgumentError, 'Missing authorization code.'
967
+ raise ArgumentError, "Missing authorization code."
970
968
  end
971
- parameters.merge!(self.extension_parameters)
969
+ parameters.merge! extension_parameters
972
970
  end
973
- parameters['client_id'] = self.client_id unless self.client_id.nil?
974
- parameters['client_secret'] = self.client_secret unless self.client_secret.nil?
971
+ parameters["client_id"] = client_id unless client_id.nil?
972
+ parameters["client_secret"] = client_secret unless client_secret.nil?
975
973
  if options[:scope]
976
- parameters['scope'] = options[:scope]
977
- elsif options[:use_configured_scope] && !self.scope.nil?
978
- parameters['scope'] = self.scope
974
+ parameters["scope"] = options[:scope]
975
+ elsif options[:use_configured_scope] && !scope.nil?
976
+ parameters["scope"] = scope
979
977
  end
980
- additional = self.additional_parameters.merge(options[:additional_parameters] || {})
978
+ additional = additional_parameters.merge(options[:additional_parameters] || {})
981
979
  additional.each { |k, v| parameters[k.to_s] = v }
982
980
  parameters
983
981
  end
982
+ # rubocop:enable Metrics/CyclomaticComplexity
983
+ # rubocop:enable Metrics/PerceivedComplexity
984
984
 
985
- def fetch_access_token(options={})
986
- if self.token_credential_uri.nil?
987
- raise ArgumentError, 'Missing token endpoint URI.'
988
- end
985
+ def fetch_access_token options = {}
986
+ raise ArgumentError, "Missing token endpoint URI." if token_credential_uri.nil?
989
987
 
990
- options = deep_hash_normalize(options)
988
+ options = deep_hash_normalize options
991
989
 
992
990
  client = options[:connection] ||= Faraday.default_connection
993
- url = Addressable::URI.parse(self.token_credential_uri).normalize.to_s
994
- parameters = self.generate_access_token_request(options)
995
- if client.is_a?(Faraday::Connection)
991
+ url = Addressable::URI.parse(token_credential_uri).normalize.to_s
992
+ parameters = generate_access_token_request options
993
+ if client.is_a? Faraday::Connection
996
994
  response = client.post url,
997
- Addressable::URI.form_encode(parameters),
998
- { 'Content-Type' => 'application/x-www-form-urlencoded' }
995
+ Addressable::URI.form_encode(parameters),
996
+ "Content-Type" => "application/x-www-form-urlencoded"
999
997
  status = response.status.to_i
1000
998
  body = response.body
1001
- content_type = response.headers['Content-type']
999
+ content_type = response.headers["Content-type"]
1002
1000
  else
1003
1001
  # Hurley
1004
1002
  response = client.post url, parameters
@@ -1007,49 +1005,46 @@ module Signet
1007
1005
  content_type = response.header[:content_type]
1008
1006
  end
1009
1007
 
1010
- if status == 200
1011
- return ::Signet::OAuth2.parse_credentials(body, content_type)
1012
- elsif [400, 401, 403].include?(status)
1013
- message = 'Authorization failed.'
1014
- if body.to_s.strip.length > 0
1015
- message += " Server message:\n#{response.body.to_s.strip}"
1016
- end
1008
+ return ::Signet::OAuth2.parse_credentials body, content_type if status == 200
1009
+
1010
+ message = " Server message:\n#{response.body.to_s.strip}" unless body.to_s.strip.empty?
1011
+ if [400, 401, 403].include? status
1012
+ message = "Authorization failed." + message
1017
1013
  raise ::Signet::AuthorizationError.new(
1018
- message, :response => response
1014
+ message, response: response
1019
1015
  )
1020
1016
  elsif status.to_s[0] == "5"
1021
- message = 'Remote server error.'
1022
- if body.to_s.strip.length > 0
1023
- message += " Server message:\n#{response.body.to_s.strip}"
1024
- end
1025
- raise ::Signet::RemoteServerError.new(message)
1017
+ message = "Remote server error." + message
1018
+ raise ::Signet::RemoteServerError, message
1026
1019
  else
1027
- message = "Unexpected status code: #{response.status}."
1028
- if body.to_s.strip.length > 0
1029
- message += " Server message:\n#{response.body.to_s.strip}"
1030
- end
1031
- raise ::Signet::UnexpectedStatusError.new(message)
1020
+ message = "Unexpected status code: #{response.status}." + message
1021
+ raise ::Signet::UnexpectedStatusError, message
1032
1022
  end
1033
1023
  end
1024
+ # rubocop:enable Metrics/AbcSize
1025
+ # rubocop:enable Metrics/MethodLength
1034
1026
 
1035
- def fetch_access_token!(options={})
1036
- token_hash = self.fetch_access_token(options)
1027
+ def fetch_access_token! options = {}
1028
+ token_hash = fetch_access_token options
1037
1029
  if token_hash
1038
1030
  # No-op for grant types other than `authorization_code`.
1039
1031
  # An authorization code is a one-time use token and is immediately
1040
1032
  # revoked after usage.
1041
1033
  self.code = nil
1042
1034
  self.issued_at = Time.now
1043
- self.update_token!(token_hash)
1035
+ update_token! token_hash
1044
1036
  end
1045
- return token_hash
1037
+ token_hash
1046
1038
  end
1047
1039
 
1048
1040
  ##
1049
1041
  # Refresh the access token, if possible
1050
- def refresh!(options={})
1051
- self.fetch_access_token!(options)
1042
+ def refresh! options = {}
1043
+ fetch_access_token! options
1052
1044
  end
1045
+ # rubocop:disable Metrics/AbcSize
1046
+ # rubocop:disable Metrics/MethodLength
1047
+ # rubocop:disable Metrics/PerceivedComplexity
1053
1048
 
1054
1049
  ##
1055
1050
  # Generates an authenticated request for protected resources.
@@ -1071,55 +1066,54 @@ module Signet
1071
1066
  # - <code>:realm</code> -
1072
1067
  # The Authorization realm. See RFC 2617.
1073
1068
  # @return [Faraday::Request] The request object.
1074
- def generate_authenticated_request(options={})
1075
- options = deep_hash_normalize(options)
1069
+ def generate_authenticated_request options = {}
1070
+ options = deep_hash_normalize options
1076
1071
 
1077
- if self.access_token == nil
1078
- raise ArgumentError, 'Missing access token.'
1079
- end
1072
+ raise ArgumentError, "Missing access token." if access_token.nil?
1080
1073
  options = {
1081
- :realm => nil
1074
+ realm: nil
1082
1075
  }.merge(options)
1083
1076
 
1084
- if options[:request].kind_of?(Faraday::Request)
1077
+ if options[:request].is_a? Faraday::Request
1085
1078
  request = options[:request]
1086
1079
  else
1087
- if options[:request].kind_of?(Array)
1080
+ if options[:request].is_a? Array
1088
1081
  method, uri, headers, body = options[:request]
1089
1082
  else
1090
1083
  method = options[:method] || :get
1091
1084
  uri = options[:uri]
1092
1085
  headers = options[:headers] || []
1093
- body = options[:body] || ''
1086
+ body = options[:body] || ""
1094
1087
  end
1095
- headers = headers.to_a if headers.kind_of?(Hash)
1088
+ headers = headers.to_a if headers.is_a? Hash
1096
1089
  request_components = {
1097
- :method => method,
1098
- :uri => uri,
1099
- :headers => headers,
1100
- :body => body
1090
+ method: method,
1091
+ uri: uri,
1092
+ headers: headers,
1093
+ body: body
1101
1094
  }
1102
1095
  # Verify that we have all pieces required to return an HTTP request
1103
1096
  request_components.each do |(key, value)|
1104
- unless value
1105
- raise ArgumentError, "Missing :#{key} parameter."
1106
- end
1097
+ raise ArgumentError, "Missing :#{key} parameter." unless value
1107
1098
  end
1108
1099
  method = method.to_s.downcase.to_sym
1109
- request = options[:connection].build_request(method.to_s.downcase.to_sym) do |req|
1110
- req.url(Addressable::URI.parse(uri).normalize.to_s)
1111
- req.headers = Faraday::Utils::Headers.new(headers)
1100
+ request = options[:connection].build_request method.to_s.downcase.to_sym do |req|
1101
+ req.url Addressable::URI.parse(uri).normalize.to_s
1102
+ req.headers = Faraday::Utils::Headers.new headers
1112
1103
  req.body = body
1113
1104
  end
1114
1105
  end
1115
1106
 
1116
- request['Authorization'] = ::Signet::OAuth2.generate_bearer_authorization_header(
1117
- self.access_token,
1118
- options[:realm] ? [['realm', options[:realm]]] : nil
1107
+ request["Authorization"] = ::Signet::OAuth2.generate_bearer_authorization_header(
1108
+ access_token,
1109
+ options[:realm] ? [["realm", options[:realm]]] : nil
1119
1110
  )
1120
- request['Cache-Control'] = 'no-store'
1121
- return request
1111
+ request["Cache-Control"] = "no-store"
1112
+ request
1122
1113
  end
1114
+ # rubocop:enable Metrics/AbcSize
1115
+ # rubocop:enable Metrics/MethodLength
1116
+ # rubocop:enable Metrics/PerceivedComplexity
1123
1117
 
1124
1118
  ##
1125
1119
  # Transmits a request for a protected resource.
@@ -1151,27 +1145,22 @@ module Signet
1151
1145
  # )
1152
1146
  #
1153
1147
  # @return [Array] The response object.
1154
- def fetch_protected_resource(options={})
1155
- options = deep_hash_normalize(options)
1148
+ def fetch_protected_resource options = {}
1149
+ options = deep_hash_normalize options
1156
1150
 
1157
1151
  options[:connection] ||= Faraday.default_connection
1158
- request = self.generate_authenticated_request(options)
1159
- request_env = request.to_env(options[:connection])
1152
+ request = generate_authenticated_request options
1153
+ request_env = request.to_env options[:connection]
1160
1154
  request_env[:request] ||= request
1161
- response = options[:connection].app.call(request_env)
1162
- if response.status.to_i == 401
1163
- # When accessing a protected resource, we only want to raise an
1164
- # error for 401 responses.
1165
- message = 'Authorization failed.'
1166
- if response.body.to_s.strip.length > 0
1167
- message += " Server message:\n#{response.body.to_s.strip}"
1168
- end
1169
- raise ::Signet::AuthorizationError.new(
1170
- message, :request => request, :response => response
1171
- )
1172
- else
1173
- return response
1174
- end
1155
+ response = options[:connection].app.call request_env
1156
+ return response unless response.status.to_i == 401
1157
+ # When accessing a protected resource, we only want to raise an
1158
+ # error for 401 responses.
1159
+ message = "Authorization failed."
1160
+ message += " Server message:\n#{response.body.to_s.strip}" unless response.body.to_s.strip.empty?
1161
+ raise ::Signet::AuthorizationError.new(
1162
+ message, request: request, response: response
1163
+ )
1175
1164
  end
1176
1165
 
1177
1166
  private
@@ -1179,33 +1168,33 @@ module Signet
1179
1168
  ##
1180
1169
  # Check if URI is Google's postmessage flow (not a valid redirect_uri by spec, but allowed)
1181
1170
  # @private
1182
- def uri_is_postmessage?(uri)
1183
- return uri.to_s.casecmp('postmessage') == 0
1171
+ def uri_is_postmessage? uri
1172
+ uri.to_s.casecmp("postmessage").zero?
1184
1173
  end
1185
1174
 
1186
1175
  ##
1187
1176
  # Check if the URI is a out-of-band
1188
1177
  # @private
1189
- def uri_is_oob?(uri)
1190
- return OOB_MODES.include?(uri.to_s)
1178
+ def uri_is_oob? uri
1179
+ OOB_MODES.include? uri.to_s
1191
1180
  end
1192
1181
 
1193
1182
  # Convert all keys in this hash (nested) to symbols for uniform retrieval
1194
- def recursive_hash_normalize_keys(val)
1183
+ def recursive_hash_normalize_keys val
1195
1184
  if val.is_a? Hash
1196
- deep_hash_normalize(val)
1185
+ deep_hash_normalize val
1197
1186
  else
1198
1187
  val
1199
1188
  end
1200
1189
  end
1201
1190
 
1202
- def deep_hash_normalize(old_hash)
1191
+ def deep_hash_normalize old_hash
1203
1192
  sym_hash = {}
1204
- old_hash and old_hash.each {|k,v| sym_hash[k.to_sym] = recursive_hash_normalize_keys(v)}
1193
+ old_hash&.each { |k, v| sym_hash[k.to_sym] = recursive_hash_normalize_keys v }
1205
1194
  sym_hash
1206
1195
  end
1207
1196
 
1208
- def normalize_timestamp(time)
1197
+ def normalize_timestamp time
1209
1198
  case time
1210
1199
  when NilClass
1211
1200
  nil
@@ -1214,15 +1203,15 @@ module Signet
1214
1203
  when DateTime
1215
1204
  time.to_time
1216
1205
  when String
1217
- Time.parse(time)
1206
+ Time.parse time
1218
1207
  when Integer
1219
- Time.at(time)
1208
+ Time.at time
1220
1209
  else
1221
- fail "Invalid time value #{time}"
1210
+ raise "Invalid time value #{time}"
1222
1211
  end
1223
1212
  end
1224
1213
 
1225
- def set_relative_expires_at(issued_at, expires_in)
1214
+ def set_relative_expires_at issued_at, expires_in
1226
1215
  self.issued_at = issued_at
1227
1216
  # Using local expires_in because if self.expires_in is used, it returns
1228
1217
  # the time left before the token expires