Skip to content

Commit ba0fd66

Browse files
committed
Refactor CredentialAuthenticator to work like the TokenAuthenticator
Work with the CredentialAuthenticator object directly and not the json body of the response. This lets us handle things like caching and other detailed behavior involving headers and response codes.
1 parent 1bdb912 commit ba0fd66

File tree

5 files changed

+34
-26
lines changed

5 files changed

+34
-26
lines changed

lib/kracken/authenticator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ class Authenticator
77
# Login the user with their credentails. Used for proxying the
88
# authentication to the auth server, normally from a mobile app
99
def self.user_with_credentials(email, password)
10-
response = Kracken::CredentialAuthenticator.new.fetch(email, password)
11-
response ? self.new(response).to_app_user : nil
10+
auth = Kracken::CredentialAuthenticator.new.fetch(email, password)
11+
self.new(auth.body).to_app_user
1212
end
1313

1414
# Login the user with an auth token. Used for API authentication for the

lib/kracken/credential_authenticator.rb

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
module Kracken
22
class CredentialAuthenticator
3+
attr_reader :response
4+
35
def fetch(email, password)
4-
response = connection.post do |req|
6+
@response = connection.post do |req|
57
req.url '/auth/radius/login.json'
68
req.headers['Content-Type'] = 'application/json'
7-
req.body = body(email, password).to_json
9+
req.body = post_body(email, password).to_json
810
end
911

10-
# An attempt to raise error when approprate:
11-
if response.status == 404
12-
nil
13-
elsif response.status == 401
12+
if response.status == 401
13+
raise TokenUnauthorized, "Invalid credentials"
14+
elsif response.status == 404
1415
raise TokenUnauthorized, "Invalid credentials"
15-
elsif response.success?
16+
elsif !response.success?
17+
raise RequestError
18+
end
19+
20+
self
21+
end
22+
23+
def body
24+
if response
1625
JSON.parse(response.body)
17-
else
18-
raise Kracken::RequestError
1926
end
2027
end
2128

2229
private
2330

24-
def body(email, password)
31+
def post_body(email, password)
2532
{
2633
user: {
2734
email: email,

spec/kracken/authenticator_spec.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@ module Kracken
55

66

77
describe ".with_credentials" do
8-
it "returns nil when nothing is found" do
9-
expect_any_instance_of(CredentialAuthenticator)
10-
.to receive(:fetch)
11-
.and_return(nil)
12-
13-
expect(Authenticator.user_with_credentials("[email protected]", "secret")).to be_nil
14-
end
8+
let(:cred_auth) {
9+
object_double(
10+
CredentialAuthenticator.new,
11+
body: { 'uid' => 1 }
12+
)
13+
}
1514

1615
it "creates a user using the user_class" do
1716
expect_any_instance_of(CredentialAuthenticator)
1817
.to receive(:fetch)
19-
.and_return({'uid' => 1})
18+
.and_return(cred_auth)
2019

2120
expect(Authenticator.user_with_credentials("[email protected]", "secret").class).to eq User
2221
end
@@ -25,7 +24,7 @@ module Kracken
2524
it "sets the user's uid" do
2625
expect_any_instance_of(CredentialAuthenticator)
2726
.to receive(:fetch)
28-
.and_return({'uid' => 1})
27+
.and_return(cred_auth)
2928

3029
expect(Authenticator.user_with_credentials("[email protected]", "secret").uid).to eq 1
3130
end

spec/kracken/credential_authenticator_spec.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,25 @@ def set_request(status, body=nil)
1515
set_request 200, json
1616

1717
response = login.fetch "[email protected]", "secret"
18-
expect(response['uid']).to eq "1"
18+
expect(response.body['uid']).to eq "1"
1919
end
2020

2121
it "raises an error on 500" do
2222
login = CredentialAuthenticator.new
2323

2424
set_request 500
2525

26-
expect{login.fetch "[email protected]", "secret"}.to raise_error(RequestError)
26+
expect{login.fetch "[email protected]", "secret"}
27+
.to raise_error(RequestError)
2728
end
2829

29-
it "returns nil for a 404" do
30+
it "raises an error on 404" do
3031
login = CredentialAuthenticator.new
3132

3233
set_request 404
3334

34-
expect(login.fetch "[email protected]", "secret").to be_nil
35+
expect{login.fetch("[email protected]", "secret")}
36+
.to raise_error(TokenUnauthorized)
3537
end
3638

3739
end

spec/kracken/token_authenticator_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def set_request(status, body=nil)
2525
expect{login.fetch "secret"}.to raise_error(RequestError)
2626
end
2727

28-
it "returns nil for a 404" do
28+
it "raises an error on 404" do
2929
login = TokenAuthenticator.new
3030

3131
set_request 404

0 commit comments

Comments
 (0)