Skip to content

Commit 76b9cee

Browse files
committed
Cache token authentication auth hash parsing
This will cache the parsing and creation/updating of the database when fetching a user by API token. The calls to the remote server still happen, however the etag header will be inspected and used to determine if we have a cached copy of the user model.
1 parent b104c6e commit 76b9cee

File tree

5 files changed

+36
-26
lines changed

5 files changed

+36
-26
lines changed

lib/kracken/authenticator.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Kracken
22
class Authenticator
3-
attr_reader :auth_hash, :user_class
3+
attr_reader :auth_hash
44

55
## Factory Methods
66

@@ -14,8 +14,11 @@ def self.user_with_credentials(email, password)
1414
# Login the user with an auth token. Used for API authentication for the
1515
# public APIs
1616
def self.user_with_token(token)
17-
response = Kracken::TokenAuthenticator.new.fetch(token)
18-
response ? self.new(response).to_app_user : nil
17+
auth = Kracken::TokenAuthenticator.new.fetch(token)
18+
19+
Rails.cache.fetch("auth/#{token}/#{auth.etag}") do
20+
self.new(auth.body).to_app_user
21+
end
1922
end
2023

2124
def initialize(response)

lib/kracken/token_authenticator.rb

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
module Kracken
22
class TokenAuthenticator
3-
3+
attr_reader :response
44
def fetch(token)
5-
response = connection.get do |req|
5+
@response = connection.get do |req|
66
req.url '/auth/radius/user.json'
77
req.params['oauth_token'] = token
88
end
99

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

2229
private

spec/dummy/config/environments/test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Dummy::Application.configure do
22
# Settings specified here will take precedence over those in config/application.rb.
3+
config.cache_store = :null_store
34

45
# The test environment is used exclusively to run your application's
56
# test suite. You never need to work with it otherwise. Remember that

spec/kracken/authenticator_spec.rb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,30 @@ module Kracken
3232
end
3333

3434
describe ".with_token" do
35-
it "returns nil when nothing is found" do
36-
expect_any_instance_of(TokenAuthenticator)
37-
.to receive(:fetch)
38-
.and_return(nil)
39-
40-
expect(Authenticator.user_with_token("secret")).to be_nil
41-
end
35+
let(:token_auth) {
36+
object_double(
37+
TokenAuthenticator.new,
38+
etag: "etag",
39+
body: { 'uid' => 1 }
40+
)
41+
}
4242

4343
it "creates a user using the user_class" do
4444
expect_any_instance_of(TokenAuthenticator)
4545
.to receive(:fetch)
46-
.and_return({'uid' => 1})
46+
.and_return(token_auth)
4747

4848
expect(Authenticator.user_with_token("secret").class).to eq User
4949
end
5050

5151
it "sets the user's uid" do
5252
expect_any_instance_of(TokenAuthenticator)
5353
.to receive(:fetch)
54-
.and_return({'uid' => 1})
54+
.with("secret")
55+
.and_return(token_auth)
5556

5657
expect(Authenticator.user_with_token("secret").uid).to eq 1
5758
end
5859
end
59-
60-
6160
end
6261
end

spec/kracken/token_authenticator_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def set_request(status, body=nil)
1414
set_request 200, json
1515

1616
response = login.fetch "secret"
17-
expect(response['uid']).to eq "1"
17+
expect(response.body['uid']).to eq "1"
1818
end
1919

2020
it "raises an error on 500" do
@@ -30,7 +30,7 @@ def set_request(status, body=nil)
3030

3131
set_request 404
3232

33-
expect(login.fetch "secret").to be_nil
33+
expect{login.fetch "secret"}.to raise_error(TokenUnauthorized)
3434
end
3535

3636
end

0 commit comments

Comments
 (0)