@@ -13,6 +13,10 @@ def initialize(config)
1313 @config = config
1414 end
1515
16+ def has_credentials?
17+ !!( id && pass )
18+ end
19+
1620 def logged_in?
1721 !!( access_token && refresh_token )
1822 end
@@ -49,19 +53,20 @@ module Requests
4953 attr_writer :send_auth_headers
5054 attr_writer :auto_manage_tokens
5155
52- # Tells whether to set authentication headers automatically (default: true).
56+ # Tells whether to set authentication headers automatically (default: true if there
57+ # is a user config).
5358 #
5459 # If false, you will need to pass `auth: 'sometoken'` explicitly to requests that
5560 # require authentication.
5661 #
5762 # @return [Boolean] whether to set authentication headers in requests
5863 #
5964 def send_auth_headers
60- instance_variable_defined? ( '@send_auth_headers' ) ? @send_auth_headers : true
65+ instance_variable_defined? ( '@send_auth_headers' ) ? @send_auth_headers : ( config != nil )
6166 end
6267
6368 # Tells whether the library should manage the access & refresh tokens automatically
64- # for you (default: true).
69+ # for you (default: true if there is a user config ).
6570 #
6671 # If true, {#check_access} is called before each request to make sure that there is a
6772 # fresh access token available; if false, you will need to call {#log_in} and
@@ -70,7 +75,7 @@ def send_auth_headers
7075 # @return [Boolean] whether to automatically manage access tokens
7176 #
7277 def auto_manage_tokens
73- instance_variable_defined? ( '@auto_manage_tokens' ) ? @auto_manage_tokens : true
78+ instance_variable_defined? ( '@auto_manage_tokens' ) ? @auto_manage_tokens : ( config != nil )
7479 end
7580
7681 alias progress default_progress
@@ -85,7 +90,7 @@ def base_url
8590 end
8691
8792 def user
88- @user ||= User . new ( config )
93+ @user ||= config && User . new ( config )
8994 end
9095
9196 # Sends a GET request to the service's API.
@@ -307,7 +312,11 @@ def fetch_all(method, params = nil, auth: default_auth_mode,
307312 # - if a token has invalid format
308313
309314 def check_access
310- if !user . logged_in?
315+ if !user
316+ raise AuthError , "User config is missing"
317+ elsif !user . has_credentials?
318+ raise AuthError , "User id or password is missing"
319+ elsif !user . logged_in?
311320 log_in
312321 :logged_in
313322 elsif access_token_expired?
@@ -332,7 +341,7 @@ def check_access
332341 # @raise [BadResponse] if the server responds with an error status code
333342
334343 def log_in
335- if user . id . nil? || user . pass . nil ?
344+ if user . nil? || ! user . has_credentials ?
336345 raise AuthError , "To log in, please provide a user id and password"
337346 end
338347
@@ -368,7 +377,7 @@ def log_in
368377 # @raise [BadResponse] if the server responds with an error status code
369378
370379 def perform_token_refresh
371- if user . refresh_token . nil?
380+ if user & .refresh_token . nil?
372381 raise AuthError , "Can't refresh access token - refresh token is missing"
373382 end
374383
@@ -406,6 +415,10 @@ def access_token_expired?
406415 #
407416
408417 def reset_tokens
418+ if !user
419+ raise AuthError , "User config is missing"
420+ end
421+
409422 user . access_token = nil
410423 user . refresh_token = nil
411424 save_config
@@ -447,7 +460,7 @@ def authentication_header(auth)
447460 if auth . is_a? ( String )
448461 { 'Authorization' => "Bearer #{ auth } " }
449462 elsif auth
450- if user . access_token
463+ if user & .access_token
451464 { 'Authorization' => "Bearer #{ user . access_token } " }
452465 else
453466 raise AuthError , "Can't send auth headers, access token is missing"
0 commit comments