Skip to content

Commit a536158

Browse files
committed
allow unauthenticated custom clients with nil config
and treat custom clients with missing id or password as an error, not unauthenticated client
1 parent 4ef2226 commit a536158

File tree

5 files changed

+119
-23
lines changed

5 files changed

+119
-23
lines changed

lib/minisky/minisky.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ def initialize(host, config_file, options = {})
5353
raise AuthError, "Missing user id or password in the config file #{@config_file}"
5454
end
5555
else
56-
@config = {}
57-
@send_auth_headers = false
58-
@auto_manage_tokens = false
56+
@config = nil
5957
end
6058

6159
if active_repl?

lib/minisky/requests.rb

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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"

spec/custom_client_spec.rb

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'json'
2+
require_relative 'shared/ex_incomplete_auth'
23
require_relative 'shared/ex_requests'
34
require_relative 'shared/ex_unauthed'
45

@@ -9,8 +10,8 @@ class CustomJSONClient
910

1011
attr_reader :config
1112

12-
def initialize
13-
@config = JSON.parse(File.read(CONFIG_FILE))
13+
def initialize(config_file = CONFIG_FILE)
14+
@config = config_file && JSON.parse(File.read(config_file))
1415
end
1516

1617
def host
@@ -41,38 +42,70 @@ def save_config
4142
File.write('test.json', JSON.generate(data))
4243
end
4344

44-
include_examples "authenticated requests", 'at.x.com'
45+
it 'should send auth headers by default' do
46+
subject.send_auth_headers.should == true
47+
end
48+
49+
it 'should manage tokens by default' do
50+
subject.auto_manage_tokens.should == true
51+
end
52+
53+
it 'should not set default progress' do
54+
subject.progress.should be_nil
55+
end
56+
57+
describe '(requests)' do
58+
include_examples "authenticated requests", 'at.x.com'
59+
end
60+
end
61+
62+
context 'with no user config,' do
63+
subject { CustomJSONClient.new(nil) }
64+
65+
it 'should not send auth headers' do
66+
subject.send_auth_headers.should == false
67+
end
68+
69+
it 'should not manage tokens' do
70+
subject.auto_manage_tokens.should == false
71+
end
72+
73+
it 'should not set default progress' do
74+
subject.progress.should be_nil
75+
end
76+
77+
include_examples "unauthenticated user"
4578
end
4679

4780
context 'if id field is nil,' do
4881
before do
49-
File.write('test.json', JSON.generate(data.merge('id' => nil)))
82+
File.write('test.json', JSON.generate(id: nil, pass: 'ok'))
5083
end
5184

52-
include_examples "unauthenticated user"
85+
include_examples "custom client with incomplete auth"
5386
end
5487

5588
context 'if id field is not included' do
5689
before do
57-
File.write('test.json', JSON.generate(data.slice('pass', 'access_token', 'refresh_token')))
90+
File.write('test.json', JSON.generate(pass: 'ok'))
5891
end
5992

60-
include_examples "unauthenticated user"
93+
include_examples "custom client with incomplete auth"
6194
end
6295

6396
context 'if pass field is nil' do
6497
before do
65-
File.write('test.json', JSON.generate(data.merge('pass' => nil)))
98+
File.write('test.json', JSON.generate(id: 'id', pass: nil))
6699
end
67100

68-
include_examples "unauthenticated user"
101+
include_examples "custom client with incomplete auth"
69102
end
70103

71104
context 'if pass field is not included' do
72105
before do
73-
File.write('test.json', JSON.generate(data.slice('id', 'access_token', 'refresh_token')))
106+
File.write('test.json', JSON.generate(id: 'id'))
74107
end
75108

76-
include_examples "unauthenticated user"
109+
include_examples "custom client with incomplete auth"
77110
end
78111
end

spec/shared/ex_incomplete_auth.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
shared_examples "custom client with incomplete auth" do
2+
it 'should have send_auth_headers enabled' do
3+
subject.send_auth_headers.should == true
4+
end
5+
6+
it 'should have auto_manage_tokens enabled' do
7+
subject.auto_manage_tokens.should == true
8+
end
9+
10+
it 'should fail on get_request' do
11+
expect { subject.get_request('com.example.service.getStuff') }.to raise_error(Minisky::AuthError)
12+
end
13+
14+
it 'should fail on post_request' do
15+
expect { subject.post_request('com.example.service.doStuff', 'qqq') }.to raise_error(Minisky::AuthError)
16+
end
17+
18+
it 'should fail on fetch_all' do
19+
expect { subject.fetch_all('com.example.service.fetchStuff', {}, field: 'feed') }.to raise_error(Minisky::AuthError)
20+
end
21+
22+
it 'should fail on check_access' do
23+
expect { subject.check_access }.to raise_error(Minisky::AuthError)
24+
end
25+
26+
it 'should fail on log_in' do
27+
expect { subject.log_in }.to raise_error(Minisky::AuthError)
28+
end
29+
30+
it 'should fail on perform_token_refresh' do
31+
expect { subject.perform_token_refresh }.to raise_error(Minisky::AuthError)
32+
end
33+
34+
# todo perform w/ access token
35+
# todo test if properties turned off
36+
end

spec/user_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,20 @@
3737
subject.logged_in?.should be true
3838
end
3939
end
40+
41+
context '#has_credentials?' do
42+
it 'should return false if id is missing' do
43+
subject.instance_variable_get('@config')['id'] = nil
44+
subject.has_credentials?.should be false
45+
end
46+
47+
it 'should return false if pass is missing' do
48+
subject.instance_variable_get('@config')['pass'] = nil
49+
subject.has_credentials?.should be false
50+
end
51+
52+
it 'should return true if both id and pass are set' do
53+
subject.has_credentials?.should be true
54+
end
55+
end
4056
end

0 commit comments

Comments
 (0)