mirror of
https://github.com/discourse/discourse.git
synced 2025-09-06 10:50:21 +08:00
better error message when API authentication fails
This commit is contained in:
parent
df4102466c
commit
804b4f32f8
4 changed files with 4 additions and 3 deletions
|
@ -171,7 +171,7 @@ class ApplicationController < ActionController::Base
|
||||||
begin
|
begin
|
||||||
current_user
|
current_user
|
||||||
rescue Discourse::InvalidAccess
|
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
|
end
|
||||||
|
|
||||||
render html: build_not_found_page(status_code, opts[:include_ember] ? 'application' : 'no_ember')
|
render html: build_not_found_page(status_code, opts[:include_ember] ? 'application' : 'no_ember')
|
||||||
|
|
|
@ -164,6 +164,7 @@ en:
|
||||||
not_logged_in: "You need to be logged in to do that."
|
not_logged_in: "You need to be logged in to do that."
|
||||||
not_found: "The requested URL or resource could not be found."
|
not_found: "The requested URL or resource could not be found."
|
||||||
invalid_access: "You are not permitted to view the requested resource."
|
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."
|
read_only_mode_enabled: "The site is in read only mode. Interactions are disabled."
|
||||||
|
|
||||||
reading_time: "Reading time"
|
reading_time: "Reading time"
|
||||||
|
|
|
@ -76,7 +76,7 @@ class Auth::DefaultCurrentUserProvider
|
||||||
# possible we have an api call, impersonate
|
# possible we have an api call, impersonate
|
||||||
if api_key
|
if api_key
|
||||||
current_user = lookup_api_user(api_key, request)
|
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
|
raise Discourse::InvalidAccess if current_user.suspended? || !current_user.active
|
||||||
@env[API_KEY_ENV] = true
|
@env[API_KEY_ENV] = true
|
||||||
end
|
end
|
||||||
|
|
|
@ -19,7 +19,7 @@ describe Auth::DefaultCurrentUserProvider do
|
||||||
it "raises errors for incorrect api_key" do
|
it "raises errors for incorrect api_key" do
|
||||||
expect {
|
expect {
|
||||||
provider("/?api_key=INCORRECT").current_user
|
provider("/?api_key=INCORRECT").current_user
|
||||||
}.to raise_error(Discourse::InvalidAccess)
|
}.to raise_error(Discourse::InvalidAccess, /API username or key is invalid/)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "finds a user for a correct per-user api key" do
|
it "finds a user for a correct per-user api key" do
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue