better error message when API authentication fails

This commit is contained in:
Arpit Jalan 2017-10-20 20:00:13 +05:30
parent df4102466c
commit 804b4f32f8
4 changed files with 4 additions and 3 deletions

View File

@ -171,7 +171,7 @@ class ApplicationController < ActionController::Base
begin
current_user
rescue Discourse::InvalidAccess
return render plain: I18n.t(type), status: status_code
return render plain: I18n.t(opts[:custom_message] || type), status: status_code
end
render html: build_not_found_page(status_code, opts[:include_ember] ? 'application' : 'no_ember')

View File

@ -164,6 +164,7 @@ en:
not_logged_in: "You need to be logged in to do that."
not_found: "The requested URL or resource could not be found."
invalid_access: "You are not permitted to view the requested resource."
invalid_api_credentials: "You are not permitted to view the requested resource. The API username or key is invalid."
read_only_mode_enabled: "The site is in read only mode. Interactions are disabled."
reading_time: "Reading time"

View File

@ -76,7 +76,7 @@ class Auth::DefaultCurrentUserProvider
# possible we have an api call, impersonate
if api_key
current_user = lookup_api_user(api_key, request)
raise Discourse::InvalidAccess unless current_user
raise Discourse::InvalidAccess.new(I18n.t('invalid_api_credentials'), nil, custom_message: "invalid_api_credentials") unless current_user
raise Discourse::InvalidAccess if current_user.suspended? || !current_user.active
@env[API_KEY_ENV] = true
end

View File

@ -19,7 +19,7 @@ describe Auth::DefaultCurrentUserProvider do
it "raises errors for incorrect api_key" do
expect {
provider("/?api_key=INCORRECT").current_user
}.to raise_error(Discourse::InvalidAccess)
}.to raise_error(Discourse::InvalidAccess, /API username or key is invalid/)
end
it "finds a user for a correct per-user api key" do