mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: Add 2FA support to the Discourse Connect Provider protocol (#16386)
Discourse has the Discourse Connect Provider protocol that makes it possible to use a Discourse instance as an identity provider for external sites. As a natural extension to this protocol, this PR adds a new feature that makes it possible to use Discourse as a 2FA provider as well as an identity provider. The rationale for this change is that it's very difficult to implement 2FA support in a website and if you have multiple websites that need to have 2FA, it's unrealistic to build and maintain a separate 2FA implementation for each one. But with this change, you can piggyback on Discourse to take care of all the 2FA details for you for as many sites as you wish. To use Discourse as a 2FA provider, you'll need to follow this guide: https://meta.discourse.org/t/-/32974. It walks you through what you need to implement on your end/site and how to configure your Discourse instance. Once you're done, there is only one additional thing you need to do which is to include `require_2fa=true` in the payload that you send to Discourse. When Discourse sees `require_2fa=true`, it'll prompt the user to confirm their 2FA using whatever methods they've enabled (TOTP or security keys), and once they confirm they'll be redirected back to the return URL you've configured and the payload will contain `confirmed_2fa=true`. If the user has no 2FA methods enabled however, the payload will not contain `confirmed_2fa`, but it will contain `no_2fa_methods=true`. You'll need to be careful to re-run all the security checks and ensure the user can still access the resource on your site after they return from Discourse. This is very important because there's nothing that guarantees the user that will come back from Discourse after they confirm 2FA is the same user that you've redirected to Discourse. Internal ticket: t62183.
This commit is contained in:
@@ -251,9 +251,13 @@ class ApplicationController < ActionController::Base
|
||||
end
|
||||
|
||||
rescue_from SecondFactor::AuthManager::SecondFactorRequired do |e|
|
||||
render json: {
|
||||
second_factor_challenge_nonce: e.nonce
|
||||
}, status: 403
|
||||
if request.xhr?
|
||||
render json: {
|
||||
second_factor_challenge_nonce: e.nonce
|
||||
}, status: 403
|
||||
else
|
||||
redirect_to session_2fa_path(nonce: e.nonce)
|
||||
end
|
||||
end
|
||||
|
||||
rescue_from SecondFactor::BadChallenge do |e|
|
||||
@@ -482,6 +486,11 @@ class ApplicationController < ActionController::Base
|
||||
end
|
||||
|
||||
def guardian
|
||||
# sometimes we log on a user in the middle of a request so we should throw
|
||||
# away the cached guardian instance when we do that
|
||||
if (@guardian&.user).blank? && current_user.present?
|
||||
@guardian = Guardian.new(current_user, request)
|
||||
end
|
||||
@guardian ||= Guardian.new(current_user, request)
|
||||
end
|
||||
|
||||
@@ -978,13 +987,15 @@ class ApplicationController < ActionController::Base
|
||||
end
|
||||
end
|
||||
|
||||
def run_second_factor!(action_class)
|
||||
action = action_class.new(guardian)
|
||||
def run_second_factor!(action_class, action_data = nil)
|
||||
action = action_class.new(guardian, request, action_data)
|
||||
manager = SecondFactor::AuthManager.new(guardian, action)
|
||||
yield(manager) if block_given?
|
||||
result = manager.run!(request, params, secure_session)
|
||||
|
||||
if !result.no_second_factors_enabled? && !result.second_factor_auth_completed?
|
||||
if !result.no_second_factors_enabled? &&
|
||||
!result.second_factor_auth_completed? &&
|
||||
!result.second_factor_auth_skipped?
|
||||
# should never happen, but I want to know if somehow it does! (osama)
|
||||
raise "2fa process ended up in a bad state!"
|
||||
end
|
||||
|
@@ -39,77 +39,56 @@ class SessionController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
def sso_provider(payload = nil)
|
||||
if SiteSetting.enable_discourse_connect_provider
|
||||
begin
|
||||
if !payload
|
||||
params.require(:sso)
|
||||
payload = request.query_string
|
||||
end
|
||||
sso = DiscourseConnectProvider.parse(payload)
|
||||
rescue DiscourseConnectProvider::BlankSecret
|
||||
render plain: I18n.t("discourse_connect.missing_secret"), status: 400
|
||||
return
|
||||
rescue DiscourseConnectProvider::ParseError => e
|
||||
if SiteSetting.verbose_discourse_connect_logging
|
||||
Rails.logger.warn("Verbose SSO log: Signature parse error\n\n#{e.message}\n\n#{sso&.diagnostics}")
|
||||
end
|
||||
def sso_provider(payload = nil, confirmed_2fa_during_login = false)
|
||||
if !SiteSetting.enable_discourse_connect_provider
|
||||
render body: nil, status: 404
|
||||
return
|
||||
end
|
||||
|
||||
# Do NOT pass the error text to the client, it would give them the correct signature
|
||||
render plain: I18n.t("discourse_connect.login_error"), status: 422
|
||||
return
|
||||
end
|
||||
result = run_second_factor!(
|
||||
SecondFactor::Actions::DiscourseConnectProvider,
|
||||
payload: payload,
|
||||
confirmed_2fa_during_login: confirmed_2fa_during_login
|
||||
)
|
||||
|
||||
if sso.return_sso_url.blank?
|
||||
render plain: "return_sso_url is blank, it must be provided", status: 400
|
||||
return
|
||||
end
|
||||
|
||||
if sso.logout
|
||||
params[:return_url] = sso.return_sso_url
|
||||
if result.second_factor_auth_skipped?
|
||||
data = result.data
|
||||
if data[:logout]
|
||||
params[:return_url] = data[:return_sso_url]
|
||||
destroy
|
||||
return
|
||||
end
|
||||
|
||||
if current_user
|
||||
sso.name = current_user.name
|
||||
sso.username = current_user.username
|
||||
sso.email = current_user.email
|
||||
sso.external_id = current_user.id.to_s
|
||||
sso.admin = current_user.admin?
|
||||
sso.moderator = current_user.moderator?
|
||||
sso.groups = current_user.groups.pluck(:name).join(",")
|
||||
|
||||
if current_user.uploaded_avatar.present?
|
||||
base_url = Discourse.store.external? ? "#{Discourse.store.absolute_base_url}/" : Discourse.base_url
|
||||
avatar_url = "#{base_url}#{Discourse.store.get_path_for_upload(current_user.uploaded_avatar)}"
|
||||
sso.avatar_url = UrlHelper.absolute Discourse.store.cdn_url(avatar_url)
|
||||
end
|
||||
|
||||
if current_user.user_profile.profile_background_upload.present?
|
||||
sso.profile_background_url = UrlHelper.absolute(upload_cdn_path(
|
||||
current_user.user_profile.profile_background_upload.url
|
||||
))
|
||||
end
|
||||
|
||||
if current_user.user_profile.card_background_upload.present?
|
||||
sso.card_background_url = UrlHelper.absolute(upload_cdn_path(
|
||||
current_user.user_profile.card_background_upload.url
|
||||
))
|
||||
end
|
||||
|
||||
if request.xhr?
|
||||
cookies[:sso_destination_url] = sso.to_url(sso.return_sso_url)
|
||||
else
|
||||
redirect_to sso.to_url(sso.return_sso_url)
|
||||
end
|
||||
else
|
||||
cookies[:sso_payload] = request.query_string
|
||||
if data[:no_current_user]
|
||||
cookies[:sso_payload] = payload || request.query_string
|
||||
redirect_to path('/login')
|
||||
return
|
||||
end
|
||||
else
|
||||
render body: nil, status: 404
|
||||
|
||||
if request.xhr?
|
||||
# for the login modal
|
||||
cookies[:sso_destination_url] = data[:sso_redirect_url]
|
||||
else
|
||||
redirect_to data[:sso_redirect_url]
|
||||
end
|
||||
elsif result.no_second_factors_enabled?
|
||||
if request.xhr?
|
||||
# for the login modal
|
||||
cookies[:sso_destination_url] = result.data[:sso_redirect_url]
|
||||
else
|
||||
redirect_to result.data[:sso_redirect_url]
|
||||
end
|
||||
elsif result.second_factor_auth_completed?
|
||||
redirect_url = result.data[:sso_redirect_url]
|
||||
render json: success_json.merge(redirect_url: redirect_url)
|
||||
end
|
||||
rescue DiscourseConnectProvider::BlankSecret
|
||||
render plain: I18n.t("discourse_connect.missing_secret"), status: 400
|
||||
rescue DiscourseConnectProvider::ParseError => e
|
||||
# Do NOT pass the error text to the client, it would give them the correct signature
|
||||
render plain: I18n.t("discourse_connect.login_error"), status: 422
|
||||
rescue DiscourseConnectProvider::BlankReturnUrl
|
||||
render plain: "return_sso_url is blank, it must be provided", status: 400
|
||||
end
|
||||
|
||||
# For use in development mode only when login options could be limited or disabled.
|
||||
@@ -334,11 +313,12 @@ class SessionController < ApplicationController
|
||||
return render json: payload
|
||||
end
|
||||
|
||||
if !authenticate_second_factor(user)
|
||||
second_factor_auth_result = authenticate_second_factor(user)
|
||||
if !second_factor_auth_result.ok
|
||||
return render(json: @second_factor_failure_payload)
|
||||
end
|
||||
|
||||
(user.active && user.email_confirmed?) ? login(user) : not_activated(user)
|
||||
(user.active && user.email_confirmed?) ? login(user, second_factor_auth_result) : not_activated(user)
|
||||
end
|
||||
|
||||
def email_login_info
|
||||
@@ -388,7 +368,7 @@ class SessionController < ApplicationController
|
||||
|
||||
rate_limit_second_factor!(user)
|
||||
|
||||
if user.present? && !authenticate_second_factor(user)
|
||||
if user.present? && !authenticate_second_factor(user).ok
|
||||
return render(json: @second_factor_failure_payload)
|
||||
end
|
||||
|
||||
@@ -534,7 +514,7 @@ class SessionController < ApplicationController
|
||||
ok: true,
|
||||
callback_method: challenge[:callback_method],
|
||||
callback_path: challenge[:callback_path],
|
||||
redirect_path: challenge[:redirect_path]
|
||||
redirect_url: challenge[:redirect_url]
|
||||
}, status: 200
|
||||
end
|
||||
|
||||
@@ -651,10 +631,10 @@ class SessionController < ApplicationController
|
||||
failure_payload.merge!(Webauthn.allowed_credentials(user, secure_session))
|
||||
end
|
||||
@second_factor_failure_payload = failed_json.merge(failure_payload)
|
||||
return false
|
||||
return second_factor_authentication_result
|
||||
end
|
||||
|
||||
true
|
||||
second_factor_authentication_result
|
||||
end
|
||||
|
||||
def login_error_check(user)
|
||||
@@ -706,13 +686,18 @@ class SessionController < ApplicationController
|
||||
}
|
||||
end
|
||||
|
||||
def login(user)
|
||||
def login(user, second_factor_auth_result)
|
||||
session.delete(ACTIVATE_USER_KEY)
|
||||
user.update_timezone_if_missing(params[:timezone])
|
||||
log_on_user(user)
|
||||
|
||||
if payload = cookies.delete(:sso_payload)
|
||||
sso_provider(payload)
|
||||
confirmed_2fa_during_login = (
|
||||
second_factor_auth_result&.ok &&
|
||||
second_factor_auth_result.used_2fa_method.present? &&
|
||||
second_factor_auth_result.used_2fa_method != UserSecondFactor.methods[:backup_codes]
|
||||
)
|
||||
sso_provider(payload, confirmed_2fa_during_login)
|
||||
else
|
||||
render_serialized(user, UserSerializer)
|
||||
end
|
||||
|
Reference in New Issue
Block a user