Skip to content

Commit e7a225e

Browse files
committed
Raise better exception based on token validation with server
Having the token fail should not raise a request error, so capture a 401 response code and raise that as a specific TokenUnauthorized exception. Other errors will raise a RequestError (or possibly some other one) but that should be much more descriptive when troubleshooting errors. Move `request_http_token_authentication` to the token module, since it have more to do with tokens than it does with API.
1 parent 4a70667 commit e7a225e

File tree

5 files changed

+14
-15
lines changed

5 files changed

+14
-15
lines changed

lib/kracken/controllers/json_api_compatible.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,6 @@ def self.included(base)
8686
end
8787
end
8888

89-
# NOTE: Monkey-patch until this is merged into the gem
90-
def request_http_token_authentication(realm = 'Application')
91-
headers["WWW-Authenticate"] = %(Token realm="#{realm.gsub(/"/, "")}")
92-
raise TokenUnauthorized
93-
end
94-
9589
module DataIntegrity
9690
# Scan each item in the data root and enforce it has an id set.
9791
def enforce_resource_ids!

lib/kracken/controllers/token_authenticatable.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ def self.included(base)
1010
base.instance_exec do
1111
before_action :authenticate_user_with_token!
1212
helper_method :current_user
13-
14-
rescue_from Kracken::RequestError do |_|
15-
# TODO: Handle other types of errors (such as if the server is down)
16-
raise TokenUnauthorized,
17-
"Invalid credentials"
18-
end
1913
end
2014

2115

2216
end
2317

2418
attr_reader :current_user
2519

20+
# NOTE: Monkey-patch until this is merged into the gem
21+
def request_http_token_authentication(realm = 'Application')
22+
headers["WWW-Authenticate"] = %(Token realm="#{realm.gsub(/"/, "")}")
23+
raise TokenUnauthorized, "Invalid Credentials"
24+
end
25+
2626
private
2727

2828
# `authenticate_or_request_with_http_token` is a nice Rails helper:

lib/kracken/credential_authenticator.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ def fetch(email, password)
1010
# An attempt to raise error when approprate:
1111
if response.status == 404
1212
nil
13+
elsif response.status == 401
14+
raise TokenUnauthorized, "Invalid credentials"
1315
elsif response.success?
1416
JSON.parse(response.body)
1517
else

lib/kracken/json_api/exception_wrapper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ class ExceptionWrapper < ActionDispatch::ExceptionWrapper
44
cattr_accessor :rescue_with_details_responses
55
@@rescue_with_details_responses = Hash.new
66
@@rescue_with_details_responses.merge!(
7-
'Kracken::Controllers::ResourceNotFound' => :not_found,
8-
'Kracken::Controllers::TokenUnauthorized' => :unauthorized,
9-
'Kracken::Controllers::UnprocessableEntity' => :unprocessable_entity,
7+
'Kracken::ResourceNotFound' => :not_found,
8+
'Kracken::TokenUnauthorized' => :unauthorized,
9+
'Kracken::UnprocessableEntity' => :unprocessable_entity,
1010
)
1111

1212
def self.status_code_for_exception(class_name)

lib/kracken/token_authenticator.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ def fetch(token)
1010
# An attempt to raise error when approprate:
1111
if response.status == 404
1212
nil
13+
elsif response.status == 401
14+
raise TokenUnauthorized, "Invalid credentials"
1315
elsif response.success?
1416
JSON.parse(response.body)
1517
else
@@ -22,5 +24,6 @@ def fetch(token)
2224
def connection
2325
@connection ||= Faraday.new(url: PROVIDER_URL)
2426
end
27+
2528
end
2629
end

0 commit comments

Comments
 (0)