Skip to content

A platform for community discussion. Free, open, simple.

DEV: Show unauthorized message when OAuth::Unauthorized is hit (#37776)

Penar Musaraj8eaf428ee209
diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb
index 02f3f10540d..29eeee423c5 100644
--- a/app/controllers/users/omniauth_callbacks_controller.rb
+++ b/app/controllers/users/omniauth_callbacks_controller.rb
@@ -105,7 +105,7 @@ class Users::OmniauthCallbacksController < ApplicationController
true
end
- ALLOWED_FAILURE_ERRORS = %w[csrf_detected request_error invalid_iat].index_by { _1 }
+ ALLOWED_FAILURE_ERRORS = %w[csrf_detected request_error invalid_iat unauthorized].index_by { _1 }
def failure
error_name = params[:message].to_s.gsub(/[^\w-]/, "").presence
diff --git a/config/initializers/009-omniauth.rb b/config/initializers/009-omniauth.rb
index e931d771fbb..6fca1ff6ecc 100644
--- a/config/initializers/009-omniauth.rb
+++ b/config/initializers/009-omniauth.rb
@@ -39,7 +39,7 @@ OmniAuth.config.on_failure do |env|
when OAuth::Unauthorized
# OAuth1 (i.e. Twitter) makes a web request during the setup phase
# If it fails, Omniauth does not handle the error. Handle it here
- env["omniauth.error.type"] = "request_error"
+ env["omniauth.error.type"] = "unauthorized"
when JWT::InvalidIatError
# Happens for openid-connect (including google) providers, when the server clock is wrong
env["omniauth.error.type"] = "invalid_iat"
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index a8d96007560..49bb69dab72 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -3089,6 +3089,8 @@ en:
csrf_detected: "Sorry, the authorization timed out, or you have switched browsers. Please try again."
request_error: "Sorry, an error occurred when starting the authorization. Please try again."
invalid_iat: "Sorry, we are unable to verify the authorization token due to server clock differences. Please try again."
+ unauthorized: "Sorry, the login provider did not authorize your request. Please try again or contact an administrator."
+
omniauth_error_unknown: "Something went wrong processing your login, please try again."
omniauth_confirm_title: "Log in using %{provider}"
omniauth_confirm_button: "Continue"
diff --git a/spec/requests/omniauth_callbacks_controller_spec.rb b/spec/requests/omniauth_callbacks_controller_spec.rb
index 20db4c3e3ab..69c40f7f967 100644
--- a/spec/requests/omniauth_callbacks_controller_spec.rb
+++ b/spec/requests/omniauth_callbacks_controller_spec.rb
@@ -168,7 +168,7 @@ RSpec.describe Users::OmniauthCallbacksController do
)
post "/auth/google_oauth2"
expect(response.status).to eq(302)
- expect(response.location).to include("/auth/failure?message=request_error")
+ expect(response.location).to include("/auth/failure?message=unauthorized")
OmniAuth::Strategies::GoogleOauth2
.any_instance
diff --git a/spec/system/social_authentication_spec.rb b/spec/system/social_authentication_spec.rb
index 5a45859f096..8790e4c42b7 100644
--- a/spec/system/social_authentication_spec.rb
+++ b/spec/system/social_authentication_spec.rb
@@ -575,6 +575,22 @@ shared_examples "social authentication scenarios" do
end
end
end
+
+ context "when the provider returns an error" do
+ before { SiteSetting.enable_google_oauth2_logins = true }
+
+ it "shows the unauthorized error message" do
+ visit("/auth/failure?message=unauthorized")
+
+ expect(page).to have_css(".alert-error", text: I18n.t("login.omniauth_error.unauthorized"))
+ end
+
+ it "shows the request_error message" do
+ visit("/auth/failure?message=request_error")
+
+ expect(page).to have_css(".alert-error", text: I18n.t("login.omniauth_error.request_error"))
+ end
+ end
end
describe "Social authentication", type: :system do