2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
class SessionController < ApplicationController
|
2020-01-16 19:25:31 -06:00
|
|
|
before_action :check_local_login_allowed, only: %i(create forgot_password)
|
2017-04-20 10:17:24 -05:00
|
|
|
before_action :rate_limit_login, only: %i(create email_login)
|
2020-01-15 04:27:12 -06:00
|
|
|
before_action :rate_limit_second_factor_totp, only: %i(create email_login)
|
2017-08-30 23:06:56 -05:00
|
|
|
skip_before_action :redirect_to_login_if_required
|
2019-06-12 09:37:26 -05:00
|
|
|
skip_before_action :preload_json, :check_xhr, only: %i(sso sso_login sso_provider destroy one_time_password)
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2017-03-13 06:19:42 -05:00
|
|
|
ACTIVATE_USER_KEY = "activate_user"
|
|
|
|
|
2013-07-29 00:13:13 -05:00
|
|
|
def csrf
|
2017-07-27 20:20:09 -05:00
|
|
|
render json: { csrf: form_authenticity_token }
|
2013-07-29 00:13:13 -05:00
|
|
|
end
|
|
|
|
|
2014-02-24 21:30:49 -06:00
|
|
|
def sso
|
2016-09-15 22:48:50 -05:00
|
|
|
destination_url = cookies[:destination_url] || session[:destination_url]
|
|
|
|
return_path = params[:return_path] || path('/')
|
|
|
|
|
|
|
|
if destination_url && return_path == path('/')
|
|
|
|
uri = URI::parse(destination_url)
|
2019-05-13 03:45:23 -05:00
|
|
|
return_path = "#{uri.path}#{uri.query ? "?#{uri.query}" : ""}"
|
2015-06-02 06:29:27 -05:00
|
|
|
end
|
|
|
|
|
2016-09-15 22:48:50 -05:00
|
|
|
session.delete(:destination_url)
|
|
|
|
cookies.delete(:destination_url)
|
|
|
|
|
2015-08-11 10:27:56 -05:00
|
|
|
if SiteSetting.enable_sso?
|
2016-04-07 20:20:01 -05:00
|
|
|
sso = DiscourseSingleSignOn.generate_sso(return_path)
|
|
|
|
if SiteSetting.verbose_sso_logging
|
|
|
|
Rails.logger.warn("Verbose SSO log: Started SSO process\n\n#{sso.diagnostics}")
|
|
|
|
end
|
2019-07-24 16:18:27 -05:00
|
|
|
redirect_to sso_url(sso)
|
2014-02-24 21:30:49 -06:00
|
|
|
else
|
2017-08-30 23:06:56 -05:00
|
|
|
render body: nil, status: 404
|
2014-02-24 21:30:49 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def sso_provider(payload = nil)
|
2014-11-26 00:25:54 -06:00
|
|
|
if SiteSetting.enable_sso_provider
|
2019-07-26 09:37:23 -05:00
|
|
|
begin
|
2019-12-16 05:33:24 -06:00
|
|
|
if !payload
|
|
|
|
params.require(:sso)
|
|
|
|
payload = request.query_string
|
|
|
|
end
|
2019-07-26 09:37:23 -05:00
|
|
|
sso = SingleSignOnProvider.parse(payload)
|
|
|
|
rescue SingleSignOnProvider::BlankSecret
|
|
|
|
render plain: I18n.t("sso.missing_secret"), status: 400
|
|
|
|
return
|
2020-02-12 17:03:25 -06:00
|
|
|
rescue SingleSignOnProvider::ParseError => e
|
|
|
|
if SiteSetting.verbose_sso_logging
|
|
|
|
Rails.logger.warn("Verbose SSO log: Signature parse error\n\n#{e.message}\n\n#{sso&.diagnostics}")
|
|
|
|
end
|
|
|
|
|
|
|
|
# Do NOT pass the error text to the client, it would give them the correct signature
|
|
|
|
render plain: I18n.t("sso.login_error"), status: 422
|
|
|
|
return
|
2019-07-26 09:37:23 -05:00
|
|
|
end
|
2018-05-11 17:41:27 -05:00
|
|
|
|
|
|
|
if sso.return_sso_url.blank?
|
|
|
|
render plain: "return_sso_url is blank, it must be provided", status: 400
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-02-03 11:53:14 -06:00
|
|
|
if sso.logout
|
|
|
|
params[:return_url] = sso.return_sso_url
|
|
|
|
destroy
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2014-11-26 00:25:54 -06:00
|
|
|
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
|
2014-11-26 19:24:21 -06:00
|
|
|
sso.admin = current_user.admin?
|
|
|
|
sso.moderator = current_user.moderator?
|
2018-04-09 23:37:10 -05:00
|
|
|
sso.groups = current_user.groups.pluck(:name).join(",")
|
2017-03-22 08:08:38 -05:00
|
|
|
|
2018-05-11 17:41:27 -05:00
|
|
|
if current_user.uploaded_avatar.present?
|
2018-06-06 07:57:30 -05:00
|
|
|
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)}"
|
2018-05-11 17:41:27 -05:00
|
|
|
sso.avatar_url = UrlHelper.absolute Discourse.store.cdn_url(avatar_url)
|
|
|
|
end
|
|
|
|
|
2019-04-28 22:58:52 -05:00
|
|
|
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
|
|
|
|
))
|
2018-05-11 17:41:27 -05:00
|
|
|
end
|
|
|
|
|
2019-04-28 22:58:52 -05:00
|
|
|
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
|
|
|
|
))
|
2017-03-22 08:08:38 -05:00
|
|
|
end
|
|
|
|
|
2015-10-25 00:00:19 -05:00
|
|
|
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
|
2014-11-26 00:25:54 -06:00
|
|
|
else
|
2018-05-11 17:41:27 -05:00
|
|
|
cookies[:sso_payload] = request.query_string
|
2015-03-08 19:45:36 -05:00
|
|
|
redirect_to path('/login')
|
2014-11-26 00:25:54 -06:00
|
|
|
end
|
|
|
|
else
|
2017-08-30 23:06:56 -05:00
|
|
|
render body: nil, status: 404
|
2014-11-26 00:25:54 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-07 11:25:25 -05:00
|
|
|
# For use in development mode only when login options could be limited or disabled.
|
|
|
|
# NEVER allow this to work in production.
|
2018-03-27 22:31:43 -05:00
|
|
|
if !Rails.env.production?
|
2018-03-27 22:22:43 -05:00
|
|
|
skip_before_action :check_xhr, only: [:become]
|
2014-10-07 11:25:25 -05:00
|
|
|
|
2018-03-27 22:22:43 -05:00
|
|
|
def become
|
2019-06-02 19:02:10 -05:00
|
|
|
|
2018-03-27 22:22:43 -05:00
|
|
|
raise Discourse::InvalidAccess if Rails.env.production?
|
2019-06-02 19:02:10 -05:00
|
|
|
|
|
|
|
if ENV['DISCOURSE_DEV_ALLOW_ANON_TO_IMPERSONATE'] != "1"
|
|
|
|
render(content_type: 'text/plain', inline: <<~TEXT)
|
|
|
|
To enable impersonating any user without typing passwords set the following ENV var
|
|
|
|
|
|
|
|
export DISCOURSE_DEV_ALLOW_ANON_TO_IMPERSONATE=1
|
|
|
|
|
|
|
|
You can do that in your bashrc of bash profile file or the script you use to launch the web server
|
|
|
|
TEXT
|
|
|
|
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-03-27 22:22:43 -05:00
|
|
|
user = User.find_by_username(params[:session_id])
|
|
|
|
raise "User #{params[:session_id]} not found" if user.blank?
|
|
|
|
|
|
|
|
log_on_user(user)
|
|
|
|
redirect_to path("/")
|
|
|
|
end
|
2014-10-07 11:25:25 -05:00
|
|
|
end
|
|
|
|
|
2014-02-24 21:30:49 -06:00
|
|
|
def sso_login
|
2017-03-21 13:04:25 -05:00
|
|
|
raise Discourse::NotFound.new unless SiteSetting.enable_sso
|
2014-02-24 21:30:49 -06:00
|
|
|
|
2018-10-11 18:33:30 -05:00
|
|
|
params.require(:sso)
|
|
|
|
params.require(:sig)
|
|
|
|
|
2018-12-07 09:01:44 -06:00
|
|
|
begin
|
|
|
|
sso = DiscourseSingleSignOn.parse(request.query_string)
|
|
|
|
rescue DiscourseSingleSignOn::ParseError => e
|
|
|
|
if SiteSetting.verbose_sso_logging
|
2018-12-19 09:24:35 -06:00
|
|
|
Rails.logger.warn("Verbose SSO log: Signature parse error\n\n#{e.message}\n\n#{sso&.diagnostics}")
|
2018-12-07 09:01:44 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# Do NOT pass the error text to the client, it would give them the correct signature
|
|
|
|
return render_sso_error(text: I18n.t("sso.login_error"), status: 422)
|
|
|
|
end
|
|
|
|
|
2014-02-24 21:30:49 -06:00
|
|
|
if !sso.nonce_valid?
|
2016-04-07 20:20:01 -05:00
|
|
|
if SiteSetting.verbose_sso_logging
|
|
|
|
Rails.logger.warn("Verbose SSO log: Nonce has already expired\n\n#{sso.diagnostics}")
|
|
|
|
end
|
2017-03-21 13:04:25 -05:00
|
|
|
return render_sso_error(text: I18n.t("sso.timeout_expired"), status: 419)
|
2015-02-25 14:59:11 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
if ScreenedIpAddress.should_block?(request.remote_ip)
|
2016-04-07 20:20:01 -05:00
|
|
|
if SiteSetting.verbose_sso_logging
|
|
|
|
Rails.logger.warn("Verbose SSO log: IP address is blocked #{request.remote_ip}\n\n#{sso.diagnostics}")
|
|
|
|
end
|
2017-03-21 13:04:25 -05:00
|
|
|
return render_sso_error(text: I18n.t("sso.unknown_error"), status: 500)
|
2014-02-24 21:30:49 -06:00
|
|
|
end
|
|
|
|
|
2014-02-25 16:58:30 -06:00
|
|
|
return_path = sso.return_path
|
2014-02-24 21:30:49 -06:00
|
|
|
sso.expire_nonce!
|
|
|
|
|
2014-11-23 17:02:22 -06:00
|
|
|
begin
|
2015-02-23 14:58:45 -06:00
|
|
|
if user = sso.lookup_or_create_user(request.remote_ip)
|
|
|
|
|
2017-11-13 23:52:00 -06:00
|
|
|
if user.suspended?
|
2019-04-24 01:38:56 -05:00
|
|
|
render_sso_error(text: failed_to_login(user)[:error], status: 403)
|
2017-11-13 23:52:00 -06:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2014-11-23 17:02:22 -06:00
|
|
|
if SiteSetting.must_approve_users? && !user.approved?
|
2015-05-26 23:06:45 -05:00
|
|
|
if SiteSetting.sso_not_approved_url.present?
|
2015-05-29 22:19:07 -05:00
|
|
|
redirect_to SiteSetting.sso_not_approved_url
|
2015-05-26 23:06:45 -05:00
|
|
|
else
|
2017-03-21 13:04:25 -05:00
|
|
|
render_sso_error(text: I18n.t("sso.account_not_approved"), status: 403)
|
2015-05-26 23:06:45 -05:00
|
|
|
end
|
2015-05-11 07:17:32 -05:00
|
|
|
return
|
2015-05-15 12:01:30 -05:00
|
|
|
elsif !user.active?
|
|
|
|
activation = UserActivator.new(user, request, session, cookies)
|
|
|
|
activation.finish
|
|
|
|
session["user_created_message"] = activation.message
|
2017-07-27 20:20:09 -05:00
|
|
|
redirect_to(users_account_created_path) && (return)
|
2014-11-23 17:02:22 -06:00
|
|
|
else
|
2016-04-07 20:20:01 -05:00
|
|
|
if SiteSetting.verbose_sso_logging
|
|
|
|
Rails.logger.warn("Verbose SSO log: User was logged on #{user.username}\n\n#{sso.diagnostics}")
|
|
|
|
end
|
2018-10-31 20:54:01 -05:00
|
|
|
if user.id != current_user&.id
|
|
|
|
log_on_user user
|
|
|
|
end
|
2014-11-23 17:02:22 -06:00
|
|
|
end
|
2015-01-22 11:20:17 -06:00
|
|
|
|
|
|
|
# If it's not a relative URL check the host
|
|
|
|
if return_path !~ /^\/[^\/]/
|
|
|
|
begin
|
|
|
|
uri = URI(return_path)
|
2018-11-09 00:03:42 -06:00
|
|
|
if (uri.hostname == Discourse.current_hostname)
|
2019-03-24 17:02:42 -05:00
|
|
|
return_path = uri.to_s
|
2018-11-09 00:03:42 -06:00
|
|
|
elsif !SiteSetting.sso_allows_all_return_paths
|
|
|
|
return_path = path("/")
|
|
|
|
end
|
2015-01-22 11:20:17 -06:00
|
|
|
rescue
|
2015-03-08 19:45:36 -05:00
|
|
|
return_path = path("/")
|
2015-01-22 11:20:17 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-24 17:02:42 -05:00
|
|
|
# this can be done more surgically with a regex
|
|
|
|
# but it the edge case of never supporting redirects back to
|
|
|
|
# any url with `/session/sso` in it anywhere is reasonable
|
|
|
|
if return_path.include?(path("/session/sso"))
|
2018-11-08 21:27:36 -06:00
|
|
|
return_path = path("/")
|
|
|
|
end
|
|
|
|
|
2014-11-23 17:02:22 -06:00
|
|
|
redirect_to return_path
|
2014-02-25 17:27:39 -06:00
|
|
|
else
|
2017-03-21 13:04:25 -05:00
|
|
|
render_sso_error(text: I18n.t("sso.not_found"), status: 500)
|
2014-02-25 17:27:39 -06:00
|
|
|
end
|
2016-03-23 12:30:38 -05:00
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
2017-03-21 13:37:21 -05:00
|
|
|
|
2016-06-30 00:12:25 -05:00
|
|
|
if SiteSetting.verbose_sso_logging
|
2017-04-12 22:39:26 -05:00
|
|
|
Rails.logger.warn(<<~EOF)
|
|
|
|
Verbose SSO log: Record was invalid: #{e.record.class.name} #{e.record.id}
|
|
|
|
#{e.record.errors.to_h}
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
#{e.record.attributes.slice(*SingleSignOn::ACCESSORS.map(&:to_s))}
|
|
|
|
|
|
|
|
SSO Diagnostics:
|
|
|
|
#{sso.diagnostics}
|
2016-06-30 00:12:25 -05:00
|
|
|
EOF
|
|
|
|
end
|
2017-03-21 13:37:21 -05:00
|
|
|
|
|
|
|
text = nil
|
|
|
|
|
|
|
|
# If there's a problem with the email we can explain that
|
|
|
|
if (e.record.is_a?(User) && e.record.errors[:email].present?)
|
2017-03-21 14:37:46 -05:00
|
|
|
if e.record.email.blank?
|
|
|
|
text = I18n.t("sso.no_email")
|
|
|
|
else
|
|
|
|
text = I18n.t("sso.email_error", email: ERB::Util.html_escape(e.record.email))
|
|
|
|
end
|
2017-03-21 13:37:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
render_sso_error(text: text || I18n.t("sso.unknown_error"), status: 500)
|
|
|
|
|
2020-04-07 21:42:28 -05:00
|
|
|
rescue DiscourseSingleSignOn::BlankExternalId
|
|
|
|
|
|
|
|
render_sso_error(text: I18n.t("sso.blank_id_error"), status: 500)
|
|
|
|
|
2014-11-23 17:02:22 -06:00
|
|
|
rescue => e
|
2019-05-13 03:45:23 -05:00
|
|
|
message = +"Failed to create or lookup user: #{e}."
|
2017-11-30 01:08:53 -06:00
|
|
|
message << " "
|
|
|
|
message << " #{sso.diagnostics}"
|
|
|
|
message << " "
|
|
|
|
message << " #{e.backtrace.join("\n")}"
|
2016-02-24 14:57:01 -06:00
|
|
|
|
|
|
|
Rails.logger.error(message)
|
2014-11-23 17:02:22 -06:00
|
|
|
|
2017-03-21 13:04:25 -05:00
|
|
|
render_sso_error(text: I18n.t("sso.unknown_error"), status: 500)
|
2014-02-24 21:30:49 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
def create
|
2013-06-06 02:14:32 -05:00
|
|
|
params.require(:login)
|
|
|
|
params.require(:password)
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-09-11 14:22:11 -05:00
|
|
|
return invalid_credentials if params[:password].length > User.max_password_length
|
|
|
|
|
2013-11-15 09:27:43 -06:00
|
|
|
login = params[:login].strip
|
|
|
|
login = login[1..-1] if login[0] == "@"
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-11-15 09:27:43 -06:00
|
|
|
if user = User.find_by_username_or_email(login)
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-11-15 09:27:43 -06:00
|
|
|
# If their password is correct
|
|
|
|
unless user.confirm_password?(params[:password])
|
|
|
|
invalid_credentials
|
|
|
|
return
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
# If the site requires user approval and the user is not approved yet
|
2013-11-15 09:27:43 -06:00
|
|
|
if login_not_approved_for?(user)
|
2017-04-20 10:17:24 -05:00
|
|
|
render json: login_not_approved
|
2013-02-05 13:16:51 -06:00
|
|
|
return
|
|
|
|
end
|
2013-11-27 19:39:59 -06:00
|
|
|
|
2014-01-21 15:53:46 -06:00
|
|
|
# User signed on with username and password, so let's prevent the invite link
|
|
|
|
# from being used to log in (if one exists).
|
|
|
|
Invite.invalidate_for_email(user.email)
|
2013-11-27 19:39:59 -06:00
|
|
|
else
|
|
|
|
invalid_credentials
|
|
|
|
return
|
2013-11-15 09:27:43 -06:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2017-04-20 10:17:24 -05:00
|
|
|
if payload = login_error_check(user)
|
2020-01-15 04:27:12 -06:00
|
|
|
return render json: payload
|
|
|
|
end
|
2018-02-20 00:44:51 -06:00
|
|
|
|
2020-01-15 04:27:12 -06:00
|
|
|
if !authenticate_second_factor(user)
|
|
|
|
return render(json: @second_factor_failure_payload)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2019-10-01 21:08:41 -05:00
|
|
|
|
2020-01-15 04:27:12 -06:00
|
|
|
(user.active && user.email_confirmed?) ? login(user) : not_activated(user)
|
2017-04-20 10:17:24 -05:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2019-06-12 09:37:26 -05:00
|
|
|
def email_login_info
|
|
|
|
token = params[:token]
|
|
|
|
matched_token = EmailToken.confirmable(token)
|
2020-01-16 19:25:31 -06:00
|
|
|
user = matched_token&.user
|
2019-06-12 09:37:26 -05:00
|
|
|
|
2020-01-16 19:25:31 -06:00
|
|
|
check_local_login_allowed(user: user, check_login_via_email: true)
|
2020-01-12 20:10:07 -06:00
|
|
|
|
2019-06-12 09:37:26 -05:00
|
|
|
if matched_token
|
|
|
|
response = {
|
|
|
|
can_login: true,
|
|
|
|
token: token,
|
|
|
|
token_email: matched_token.email
|
|
|
|
}
|
|
|
|
|
2019-10-01 21:08:41 -05:00
|
|
|
matched_user = matched_token.user
|
|
|
|
if matched_user&.totp_enabled?
|
2019-06-12 09:37:26 -05:00
|
|
|
response.merge!(
|
|
|
|
second_factor_required: true,
|
2019-10-01 21:08:41 -05:00
|
|
|
backup_codes_enabled: matched_user&.backup_codes_enabled?
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
if matched_user&.security_keys_enabled?
|
2020-01-09 18:45:56 -06:00
|
|
|
Webauthn.stage_challenge(matched_user, secure_session)
|
2019-10-01 21:08:41 -05:00
|
|
|
response.merge!(
|
2020-01-09 18:45:56 -06:00
|
|
|
Webauthn.allowed_credentials(matched_user, secure_session).merge(security_key_required: true)
|
2019-06-12 09:37:26 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: response
|
|
|
|
else
|
|
|
|
render json: {
|
|
|
|
can_login: false,
|
|
|
|
error: I18n.t('email_login.invalid_token')
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-20 10:17:24 -05:00
|
|
|
def email_login
|
2018-02-20 00:44:51 -06:00
|
|
|
token = params[:token]
|
2019-06-12 09:37:26 -05:00
|
|
|
matched_token = EmailToken.confirmable(token)
|
2020-01-16 19:25:31 -06:00
|
|
|
user = matched_token&.user
|
2018-02-20 00:44:51 -06:00
|
|
|
|
2020-01-16 19:25:31 -06:00
|
|
|
check_local_login_allowed(user: user, check_login_via_email: true)
|
2020-01-12 20:10:07 -06:00
|
|
|
|
2020-01-15 04:27:12 -06:00
|
|
|
if user.present? && !authenticate_second_factor(user)
|
|
|
|
return render(json: @second_factor_failure_payload)
|
2018-02-21 01:46:53 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
if user = EmailToken.confirm(token)
|
2017-04-20 10:17:24 -05:00
|
|
|
if login_not_approved_for?(user)
|
2019-06-12 09:37:26 -05:00
|
|
|
return render json: login_not_approved
|
2017-04-20 10:17:24 -05:00
|
|
|
elsif payload = login_error_check(user)
|
2019-06-12 09:37:26 -05:00
|
|
|
return render json: payload
|
2017-04-20 10:17:24 -05:00
|
|
|
else
|
2020-04-10 13:19:39 -05:00
|
|
|
user.update_timezone_if_missing(params[:timezone])
|
2017-04-20 10:17:24 -05:00
|
|
|
log_on_user(user)
|
2019-06-12 09:37:26 -05:00
|
|
|
return render json: success_json
|
2017-04-20 10:17:24 -05:00
|
|
|
end
|
2015-03-02 11:13:10 -06:00
|
|
|
end
|
2018-02-20 00:44:51 -06:00
|
|
|
|
2019-11-14 14:10:51 -06:00
|
|
|
render json: { error: I18n.t('email_login.invalid_token') }
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2019-04-01 12:18:53 -05:00
|
|
|
def one_time_password
|
2019-12-03 03:05:53 -06:00
|
|
|
@otp_username = otp_username = Discourse.redis.get "otp_#{params[:token]}"
|
2019-04-01 12:18:53 -05:00
|
|
|
|
|
|
|
if otp_username && user = User.find_by_username(otp_username)
|
2019-06-12 12:32:13 -05:00
|
|
|
if current_user&.username == otp_username
|
2019-12-03 03:05:53 -06:00
|
|
|
Discourse.redis.del "otp_#{params[:token]}"
|
2019-06-12 12:32:13 -05:00
|
|
|
return redirect_to path("/")
|
|
|
|
elsif request.post?
|
|
|
|
log_on_user(user)
|
2019-12-03 03:05:53 -06:00
|
|
|
Discourse.redis.del "otp_#{params[:token]}"
|
2019-06-12 12:32:13 -05:00
|
|
|
return redirect_to path("/")
|
|
|
|
else
|
|
|
|
# Display the form
|
|
|
|
end
|
2019-04-01 12:18:53 -05:00
|
|
|
else
|
|
|
|
@error = I18n.t('user_api_key.invalid_token')
|
|
|
|
end
|
|
|
|
|
2019-08-08 07:57:18 -05:00
|
|
|
render layout: 'no_ember', locals: { hide_auth_buttons: true }
|
2019-04-01 12:18:53 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
def forgot_password
|
2013-06-06 02:14:32 -05:00
|
|
|
params.require(:login)
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2020-05-07 22:30:16 -05:00
|
|
|
if ScreenedIpAddress.should_block?(request.remote_ip)
|
|
|
|
return render_json_error(I18n.t("login.reset_not_allowed_from_ip_address"))
|
|
|
|
end
|
|
|
|
|
2014-08-17 19:55:30 -05:00
|
|
|
RateLimiter.new(nil, "forgot-password-hr-#{request.remote_ip}", 6, 1.hour).performed!
|
|
|
|
RateLimiter.new(nil, "forgot-password-min-#{request.remote_ip}", 3, 1.minute).performed!
|
|
|
|
|
2013-10-24 02:59:58 -05:00
|
|
|
user = User.find_by_username_or_email(params[:login])
|
2020-05-07 22:30:16 -05:00
|
|
|
|
|
|
|
if user
|
|
|
|
RateLimiter.new(nil, "forgot-password-login-day-#{user.username}", 6, 1.day).performed!
|
|
|
|
else
|
|
|
|
RateLimiter.new(nil, "forgot-password-login-hour-#{params[:login].to_s[0..100]}", 5, 1.hour).performed!
|
|
|
|
end
|
|
|
|
|
2019-02-08 12:34:54 -06:00
|
|
|
user_presence = user.present? && user.human? && !user.staged
|
2014-12-10 00:17:49 -06:00
|
|
|
if user_presence
|
2013-02-05 13:16:51 -06:00
|
|
|
email_token = user.email_tokens.create(email: user.email)
|
2016-04-06 23:38:43 -05:00
|
|
|
Jobs.enqueue(:critical_user_email, type: :forgot_password, user_id: user.id, email_token: email_token.token)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2014-09-10 21:04:44 -05:00
|
|
|
|
2018-02-14 17:46:33 -06:00
|
|
|
json = success_json
|
2017-10-03 14:28:15 -05:00
|
|
|
unless SiteSetting.hide_email_address_taken
|
2014-12-10 00:17:49 -06:00
|
|
|
json[:user_found] = user_presence
|
2014-09-10 21:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
render json: json
|
2014-08-17 19:55:30 -05:00
|
|
|
|
|
|
|
rescue RateLimiter::LimitExceeded
|
|
|
|
render_json_error(I18n.t("rate_limiter.slow_down"))
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2014-02-05 12:46:24 -06:00
|
|
|
def current
|
|
|
|
if current_user.present?
|
|
|
|
render_serialized(current_user, CurrentUserSerializer)
|
|
|
|
else
|
2017-08-30 23:06:56 -05:00
|
|
|
render body: nil, status: 404
|
2014-02-05 12:46:24 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
def destroy
|
2013-08-27 00:56:12 -05:00
|
|
|
reset_session
|
2013-10-08 23:10:37 -05:00
|
|
|
log_off_user
|
2016-06-16 20:27:52 -05:00
|
|
|
if request.xhr?
|
2017-08-30 23:06:56 -05:00
|
|
|
render body: nil
|
2016-06-16 20:27:52 -05:00
|
|
|
else
|
|
|
|
redirect_to (params[:return_url] || path("/"))
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2017-08-16 00:43:05 -05:00
|
|
|
protected
|
2013-11-15 09:27:43 -06:00
|
|
|
|
2020-01-16 19:25:31 -06:00
|
|
|
def check_local_login_allowed(user: nil, check_login_via_email: false)
|
|
|
|
# admin-login can get around enabled SSO/disabled local logins
|
|
|
|
return if user&.admin?
|
|
|
|
|
|
|
|
if (check_login_via_email && !SiteSetting.enable_local_logins_via_email) ||
|
|
|
|
SiteSetting.enable_sso ||
|
|
|
|
!SiteSetting.enable_local_logins
|
2020-01-20 00:11:58 -06:00
|
|
|
raise Discourse::InvalidAccess, "SSO takes over local login or the local login is disallowed."
|
2017-08-16 00:43:05 -05:00
|
|
|
end
|
2014-03-25 23:39:44 -05:00
|
|
|
end
|
|
|
|
|
2017-08-16 00:43:05 -05:00
|
|
|
private
|
|
|
|
|
2020-01-15 04:27:12 -06:00
|
|
|
def authenticate_second_factor(user)
|
|
|
|
second_factor_authentication_result = user.authenticate_second_factor(params, secure_session)
|
|
|
|
if !second_factor_authentication_result.ok
|
|
|
|
failure_payload = second_factor_authentication_result.to_h
|
|
|
|
if user.security_keys_enabled?
|
|
|
|
Webauthn.stage_challenge(user, secure_session)
|
|
|
|
failure_payload.merge!(Webauthn.allowed_credentials(user, secure_session))
|
|
|
|
end
|
|
|
|
@second_factor_failure_payload = failed_json.merge(failure_payload)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2017-04-20 10:17:24 -05:00
|
|
|
def login_error_check(user)
|
|
|
|
return failed_to_login(user) if user.suspended?
|
|
|
|
|
|
|
|
if ScreenedIpAddress.should_block?(request.remote_ip)
|
|
|
|
return not_allowed_from_ip_address(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
if ScreenedIpAddress.block_admin_login?(user, request.remote_ip)
|
2019-12-09 18:48:27 -06:00
|
|
|
admin_not_allowed_from_ip_address(user)
|
2017-04-20 10:17:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-15 09:27:43 -06:00
|
|
|
def login_not_approved_for?(user)
|
|
|
|
SiteSetting.must_approve_users? && !user.approved? && !user.admin?
|
|
|
|
end
|
|
|
|
|
|
|
|
def invalid_credentials
|
2017-07-27 20:20:09 -05:00
|
|
|
render json: { error: I18n.t("login.incorrect_username_email_or_password") }
|
2013-11-15 09:27:43 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def login_not_approved
|
2017-04-20 10:17:24 -05:00
|
|
|
{ error: I18n.t("login.not_approved") }
|
2013-11-15 09:27:43 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def not_activated(user)
|
2017-03-13 07:20:25 -05:00
|
|
|
session[ACTIVATE_USER_KEY] = user.id
|
2013-11-15 09:27:43 -06:00
|
|
|
render json: {
|
|
|
|
error: I18n.t("login.not_activated"),
|
|
|
|
reason: 'not_activated',
|
|
|
|
sent_to_email: user.find_email || user.email,
|
|
|
|
current_email: user.email
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-09-04 17:50:27 -05:00
|
|
|
def not_allowed_from_ip_address(user)
|
2017-04-20 10:17:24 -05:00
|
|
|
{ error: I18n.t("login.not_allowed_from_ip_address", username: user.username) }
|
2014-09-04 17:50:27 -05:00
|
|
|
end
|
|
|
|
|
2015-03-02 11:13:10 -06:00
|
|
|
def admin_not_allowed_from_ip_address(user)
|
2017-04-20 10:17:24 -05:00
|
|
|
{ error: I18n.t("login.admin_not_allowed_from_ip_address", username: user.username) }
|
2015-03-02 11:13:10 -06:00
|
|
|
end
|
|
|
|
|
2013-11-15 09:27:43 -06:00
|
|
|
def failed_to_login(user)
|
|
|
|
message = user.suspend_reason ? "login.suspended_with_reason" : "login.suspended"
|
|
|
|
|
2017-04-20 10:17:24 -05:00
|
|
|
{
|
|
|
|
error: I18n.t(message,
|
|
|
|
date: I18n.l(user.suspended_till, format: :date_only),
|
|
|
|
reason: Rack::Utils.escape_html(user.suspend_reason)
|
|
|
|
),
|
2016-02-19 11:19:20 -06:00
|
|
|
reason: 'suspended'
|
|
|
|
}
|
2013-11-15 09:27:43 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def login(user)
|
2017-03-13 06:19:42 -05:00
|
|
|
session.delete(ACTIVATE_USER_KEY)
|
2019-11-24 18:49:27 -06:00
|
|
|
user.update_timezone_if_missing(params[:timezone])
|
2013-11-15 09:27:43 -06:00
|
|
|
log_on_user(user)
|
2014-11-26 00:25:54 -06:00
|
|
|
|
2018-05-11 17:41:27 -05:00
|
|
|
if payload = cookies.delete(:sso_payload)
|
2014-11-26 00:25:54 -06:00
|
|
|
sso_provider(payload)
|
2017-08-30 23:06:56 -05:00
|
|
|
else
|
|
|
|
render_serialized(user, UserSerializer)
|
2014-11-26 00:25:54 -06:00
|
|
|
end
|
2013-11-15 09:27:43 -06:00
|
|
|
end
|
|
|
|
|
2017-04-20 10:17:24 -05:00
|
|
|
def rate_limit_login
|
|
|
|
RateLimiter.new(
|
|
|
|
nil,
|
|
|
|
"login-hr-#{request.remote_ip}",
|
|
|
|
SiteSetting.max_logins_per_ip_per_hour,
|
|
|
|
1.hour
|
|
|
|
).performed!
|
|
|
|
|
|
|
|
RateLimiter.new(
|
|
|
|
nil,
|
|
|
|
"login-min-#{request.remote_ip}",
|
|
|
|
SiteSetting.max_logins_per_ip_per_minute,
|
|
|
|
1.minute
|
|
|
|
).performed!
|
|
|
|
end
|
|
|
|
|
2020-01-15 04:27:12 -06:00
|
|
|
def rate_limit_second_factor_totp
|
|
|
|
return if params[:second_factor_token].blank?
|
|
|
|
RateLimiter.new(nil, "second-factor-min-#{request.remote_ip}", 3, 1.minute).performed!
|
|
|
|
end
|
|
|
|
|
2017-03-21 13:04:25 -05:00
|
|
|
def render_sso_error(status:, text:)
|
|
|
|
@sso_error = text
|
|
|
|
render status: status, layout: 'no_ember'
|
|
|
|
end
|
2019-07-24 16:18:27 -05:00
|
|
|
|
|
|
|
# extension to allow plugins to customize the SSO URL
|
|
|
|
def sso_url(sso)
|
|
|
|
sso.to_url
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|