2014-08-18 08:55:30 +08:00
|
|
|
require_dependency 'rate_limiter'
|
2014-11-26 14:25:54 +08:00
|
|
|
require_dependency 'single_sign_on'
|
2014-08-18 08:55:30 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
class SessionController < ApplicationController
|
2017-08-16 13:43:05 +08:00
|
|
|
class LocalLoginNotAllowed < StandardError; end
|
|
|
|
rescue_from LocalLoginNotAllowed do
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil, status: 500
|
2017-08-16 13:43:05 +08:00
|
|
|
end
|
2013-07-29 13:13:13 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
before_action :check_local_login_allowed, only: %i(create forgot_password)
|
|
|
|
skip_before_action :redirect_to_login_if_required
|
|
|
|
skip_before_action :preload_json, :check_xhr, only: ['sso', 'sso_login', 'become', 'sso_provider', 'destroy']
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2017-03-13 19:19:42 +08:00
|
|
|
ACTIVATE_USER_KEY = "activate_user"
|
|
|
|
|
2013-07-29 13:13:13 +08:00
|
|
|
def csrf
|
2017-07-28 09:20:09 +08:00
|
|
|
render json: { csrf: form_authenticity_token }
|
2013-07-29 13:13:13 +08:00
|
|
|
end
|
|
|
|
|
2014-02-25 11:30:49 +08:00
|
|
|
def sso
|
2016-09-16 11:48:50 +08: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)
|
|
|
|
return_path = "#{uri.path}#{uri.query ? "?" << uri.query : ""}"
|
2015-06-02 19:29:27 +08:00
|
|
|
end
|
|
|
|
|
2016-09-16 11:48:50 +08:00
|
|
|
session.delete(:destination_url)
|
|
|
|
cookies.delete(:destination_url)
|
|
|
|
|
2015-08-11 23:27:56 +08:00
|
|
|
if SiteSetting.enable_sso?
|
2016-04-08 09:20:01 +08: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
|
|
|
|
redirect_to sso.to_url
|
2014-02-25 11:30:49 +08:00
|
|
|
else
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil, status: 404
|
2014-02-25 11:30:49 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def sso_provider(payload = nil)
|
2014-11-26 14:25:54 +08:00
|
|
|
payload ||= request.query_string
|
|
|
|
if SiteSetting.enable_sso_provider
|
|
|
|
sso = SingleSignOn.parse(payload, SiteSetting.sso_secret)
|
|
|
|
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-27 09:24:21 +08:00
|
|
|
sso.admin = current_user.admin?
|
|
|
|
sso.moderator = current_user.moderator?
|
2017-03-22 21:08:38 +08:00
|
|
|
|
|
|
|
if sso.return_sso_url.blank?
|
2017-04-10 20:01:25 +08:00
|
|
|
render plain: "return_sso_url is blank, it must be provided", status: 400
|
2017-03-22 21:08:38 +08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-10-25 13:00:19 +08: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 14:25:54 +08:00
|
|
|
else
|
|
|
|
session[:sso_payload] = request.query_string
|
2015-03-09 08:45:36 +08:00
|
|
|
redirect_to path('/login')
|
2014-11-26 14:25:54 +08:00
|
|
|
end
|
|
|
|
else
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil, status: 404
|
2014-11-26 14:25:54 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-08 00:25:25 +08:00
|
|
|
# For use in development mode only when login options could be limited or disabled.
|
|
|
|
# NEVER allow this to work in production.
|
|
|
|
def become
|
|
|
|
raise Discourse::InvalidAccess.new unless Rails.env.development?
|
|
|
|
user = User.find_by_username(params[:session_id])
|
|
|
|
raise "User #{params[:session_id]} not found" if user.blank?
|
|
|
|
|
|
|
|
log_on_user(user)
|
2015-03-09 08:45:36 +08:00
|
|
|
redirect_to path("/")
|
2014-10-08 00:25:25 +08:00
|
|
|
end
|
|
|
|
|
2014-02-25 11:30:49 +08:00
|
|
|
def sso_login
|
2017-03-22 02:04:25 +08:00
|
|
|
raise Discourse::NotFound.new unless SiteSetting.enable_sso
|
2014-02-25 11:30:49 +08:00
|
|
|
|
|
|
|
sso = DiscourseSingleSignOn.parse(request.query_string)
|
|
|
|
if !sso.nonce_valid?
|
2016-04-08 09:20:01 +08:00
|
|
|
if SiteSetting.verbose_sso_logging
|
|
|
|
Rails.logger.warn("Verbose SSO log: Nonce has already expired\n\n#{sso.diagnostics}")
|
|
|
|
end
|
2017-03-22 02:04:25 +08:00
|
|
|
return render_sso_error(text: I18n.t("sso.timeout_expired"), status: 419)
|
2015-02-26 04:59:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
if ScreenedIpAddress.should_block?(request.remote_ip)
|
2016-04-08 09:20:01 +08: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-22 02:04:25 +08:00
|
|
|
return render_sso_error(text: I18n.t("sso.unknown_error"), status: 500)
|
2014-02-25 11:30:49 +08:00
|
|
|
end
|
|
|
|
|
2014-02-26 06:58:30 +08:00
|
|
|
return_path = sso.return_path
|
2014-02-25 11:30:49 +08:00
|
|
|
sso.expire_nonce!
|
|
|
|
|
2014-11-24 07:02:22 +08:00
|
|
|
begin
|
2015-02-24 04:58:45 +08:00
|
|
|
if user = sso.lookup_or_create_user(request.remote_ip)
|
|
|
|
|
2014-11-24 07:02:22 +08:00
|
|
|
if SiteSetting.must_approve_users? && !user.approved?
|
2015-05-27 12:06:45 +08:00
|
|
|
if SiteSetting.sso_not_approved_url.present?
|
2015-05-30 11:19:07 +08:00
|
|
|
redirect_to SiteSetting.sso_not_approved_url
|
2015-05-27 12:06:45 +08:00
|
|
|
else
|
2017-03-22 02:04:25 +08:00
|
|
|
render_sso_error(text: I18n.t("sso.account_not_approved"), status: 403)
|
2015-05-27 12:06:45 +08:00
|
|
|
end
|
2015-05-11 20:17:32 +08:00
|
|
|
return
|
2015-05-16 01:01:30 +08:00
|
|
|
elsif !user.active?
|
|
|
|
activation = UserActivator.new(user, request, session, cookies)
|
|
|
|
activation.finish
|
|
|
|
session["user_created_message"] = activation.message
|
2017-07-28 09:20:09 +08:00
|
|
|
redirect_to(users_account_created_path) && (return)
|
2014-11-24 07:02:22 +08:00
|
|
|
else
|
2016-04-08 09:20:01 +08:00
|
|
|
if SiteSetting.verbose_sso_logging
|
|
|
|
Rails.logger.warn("Verbose SSO log: User was logged on #{user.username}\n\n#{sso.diagnostics}")
|
|
|
|
end
|
2014-11-24 07:02:22 +08:00
|
|
|
log_on_user user
|
|
|
|
end
|
2015-01-23 01:20:17 +08:00
|
|
|
|
|
|
|
# If it's not a relative URL check the host
|
|
|
|
if return_path !~ /^\/[^\/]/
|
|
|
|
begin
|
|
|
|
uri = URI(return_path)
|
2016-12-16 10:37:44 +08:00
|
|
|
return_path = path("/") unless SiteSetting.sso_allows_all_return_paths || uri.host == Discourse.current_hostname
|
2015-01-23 01:20:17 +08:00
|
|
|
rescue
|
2015-03-09 08:45:36 +08:00
|
|
|
return_path = path("/")
|
2015-01-23 01:20:17 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-24 07:02:22 +08:00
|
|
|
redirect_to return_path
|
2014-02-26 07:27:39 +08:00
|
|
|
else
|
2017-03-22 02:04:25 +08:00
|
|
|
render_sso_error(text: I18n.t("sso.not_found"), status: 500)
|
2014-02-26 07:27:39 +08:00
|
|
|
end
|
2016-03-24 01:30:38 +08:00
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
2017-03-22 02:37:21 +08:00
|
|
|
|
2016-06-30 13:12:25 +08:00
|
|
|
if SiteSetting.verbose_sso_logging
|
2017-04-13 11:39:26 +08: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 13:12:25 +08:00
|
|
|
EOF
|
|
|
|
end
|
2017-03-22 02:37:21 +08: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-22 03:37:46 +08: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-22 02:37:21 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
render_sso_error(text: text || I18n.t("sso.unknown_error"), status: 500)
|
|
|
|
|
2014-11-24 07:02:22 +08:00
|
|
|
rescue => e
|
2016-02-25 04:57:01 +08:00
|
|
|
message = "Failed to create or lookup user: #{e}."
|
2016-02-25 06:35:45 +08:00
|
|
|
message << "\n\n" << "-" * 100 << "\n\n"
|
2016-04-08 09:20:01 +08:00
|
|
|
message << sso.diagnostics
|
2016-02-25 06:35:45 +08:00
|
|
|
message << "\n\n" << "-" * 100 << "\n\n"
|
2016-02-25 04:57:01 +08:00
|
|
|
message << e.backtrace.join("\n")
|
|
|
|
|
|
|
|
Rails.logger.error(message)
|
2014-11-24 07:02:22 +08:00
|
|
|
|
2017-03-22 02:04:25 +08:00
|
|
|
render_sso_error(text: I18n.t("sso.unknown_error"), status: 500)
|
2014-02-25 11:30:49 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def create
|
2017-02-22 01:23:35 +08:00
|
|
|
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!
|
2014-09-25 08:06:44 +08:00
|
|
|
|
2013-06-06 15:14:32 +08:00
|
|
|
params.require(:login)
|
|
|
|
params.require(:password)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-09-12 03:22:11 +08:00
|
|
|
return invalid_credentials if params[:password].length > User.max_password_length
|
|
|
|
|
2013-11-15 23:27:43 +08:00
|
|
|
login = params[:login].strip
|
|
|
|
login = login[1..-1] if login[0] == "@"
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-11-15 23:27:43 +08:00
|
|
|
if user = User.find_by_username_or_email(login)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-11-15 23:27:43 +08:00
|
|
|
# If their password is correct
|
|
|
|
unless user.confirm_password?(params[:password])
|
|
|
|
invalid_credentials
|
|
|
|
return
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
# If the site requires user approval and the user is not approved yet
|
2013-11-15 23:27:43 +08:00
|
|
|
if login_not_approved_for?(user)
|
|
|
|
login_not_approved
|
2013-02-06 03:16:51 +08:00
|
|
|
return
|
|
|
|
end
|
2013-11-28 09:39:59 +08:00
|
|
|
|
2014-01-22 05:53:46 +08: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-28 09:39:59 +08:00
|
|
|
else
|
|
|
|
invalid_credentials
|
|
|
|
return
|
2013-11-15 23:27:43 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-11-15 23:27:43 +08:00
|
|
|
if user.suspended?
|
|
|
|
failed_to_login(user)
|
|
|
|
return
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2015-03-03 01:13:10 +08:00
|
|
|
if ScreenedIpAddress.should_block?(request.remote_ip)
|
2015-02-24 04:58:45 +08:00
|
|
|
return not_allowed_from_ip_address(user)
|
2014-09-05 06:50:27 +08:00
|
|
|
end
|
|
|
|
|
2015-03-03 01:13:10 +08:00
|
|
|
if ScreenedIpAddress.block_admin_login?(user, request.remote_ip)
|
|
|
|
return admin_not_allowed_from_ip_address(user)
|
|
|
|
end
|
|
|
|
|
2014-04-29 01:46:28 +08:00
|
|
|
(user.active && user.email_confirmed?) ? login(user) : not_activated(user)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def forgot_password
|
2013-06-06 15:14:32 +08:00
|
|
|
params.require(:login)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-08-18 08:55:30 +08: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!
|
|
|
|
|
2016-12-19 08:03:07 +08:00
|
|
|
RateLimiter.new(nil, "forgot-password-login-hour-#{params[:login].to_s[0..100]}", 12, 1.hour).performed!
|
|
|
|
RateLimiter.new(nil, "forgot-password-login-min-#{params[:login].to_s[0..100]}", 3, 1.minute).performed!
|
|
|
|
|
2013-10-24 15:59:58 +08:00
|
|
|
user = User.find_by_username_or_email(params[:login])
|
2017-03-14 14:33:06 +08:00
|
|
|
user_presence = user.present? && user.id > 0 && !user.staged
|
2014-12-10 14:17:49 +08:00
|
|
|
if user_presence
|
2013-02-06 03:16:51 +08:00
|
|
|
email_token = user.email_tokens.create(email: user.email)
|
2016-04-07 12:38:43 +08:00
|
|
|
Jobs.enqueue(:critical_user_email, type: :forgot_password, user_id: user.id, email_token: email_token.token)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2014-09-11 10:04:44 +08:00
|
|
|
|
|
|
|
json = { result: "ok" }
|
2017-10-04 03:28:15 +08:00
|
|
|
unless SiteSetting.hide_email_address_taken
|
2014-12-10 14:17:49 +08:00
|
|
|
json[:user_found] = user_presence
|
2014-09-11 10:04:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
render json: json
|
2014-08-18 08:55:30 +08:00
|
|
|
|
|
|
|
rescue RateLimiter::LimitExceeded
|
|
|
|
render_json_error(I18n.t("rate_limiter.slow_down"))
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-02-06 02:46:24 +08:00
|
|
|
def current
|
|
|
|
if current_user.present?
|
|
|
|
render_serialized(current_user, CurrentUserSerializer)
|
|
|
|
else
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil, status: 404
|
2014-02-06 02:46:24 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def destroy
|
2013-08-27 13:56:12 +08:00
|
|
|
reset_session
|
2013-10-09 12:10:37 +08:00
|
|
|
log_off_user
|
2016-06-17 09:27:52 +08:00
|
|
|
if request.xhr?
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil
|
2016-06-17 09:27:52 +08:00
|
|
|
else
|
|
|
|
redirect_to (params[:return_url] || path("/"))
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2017-08-16 13:43:05 +08:00
|
|
|
protected
|
2013-11-15 23:27:43 +08:00
|
|
|
|
2017-08-16 13:43:05 +08:00
|
|
|
def check_local_login_allowed
|
|
|
|
if SiteSetting.enable_sso || !SiteSetting.enable_local_logins
|
|
|
|
raise LocalLoginNotAllowed, "SSO takes over local login or the local login is disallowed."
|
|
|
|
end
|
2014-03-26 12:39:44 +08:00
|
|
|
end
|
|
|
|
|
2017-08-16 13:43:05 +08:00
|
|
|
private
|
|
|
|
|
2013-11-15 23:27:43 +08:00
|
|
|
def login_not_approved_for?(user)
|
|
|
|
SiteSetting.must_approve_users? && !user.approved? && !user.admin?
|
|
|
|
end
|
|
|
|
|
|
|
|
def invalid_credentials
|
2017-07-28 09:20:09 +08:00
|
|
|
render json: { error: I18n.t("login.incorrect_username_email_or_password") }
|
2013-11-15 23:27:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def login_not_approved
|
2017-07-28 09:20:09 +08:00
|
|
|
render json: { error: I18n.t("login.not_approved") }
|
2013-11-15 23:27:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def not_activated(user)
|
2017-03-13 20:20:25 +08:00
|
|
|
session[ACTIVATE_USER_KEY] = user.id
|
2013-11-15 23:27:43 +08: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-05 06:50:27 +08:00
|
|
|
def not_allowed_from_ip_address(user)
|
2017-07-28 09:20:09 +08:00
|
|
|
render json: { error: I18n.t("login.not_allowed_from_ip_address", username: user.username) }
|
2014-09-05 06:50:27 +08:00
|
|
|
end
|
|
|
|
|
2015-03-03 01:13:10 +08:00
|
|
|
def admin_not_allowed_from_ip_address(user)
|
2017-07-28 09:20:09 +08:00
|
|
|
render json: { error: I18n.t("login.admin_not_allowed_from_ip_address", username: user.username) }
|
2015-03-03 01:13:10 +08:00
|
|
|
end
|
|
|
|
|
2013-11-15 23:27:43 +08:00
|
|
|
def failed_to_login(user)
|
|
|
|
message = user.suspend_reason ? "login.suspended_with_reason" : "login.suspended"
|
|
|
|
|
2016-02-20 01:19:20 +08:00
|
|
|
render json: {
|
2017-07-28 09:20:09 +08:00
|
|
|
error: I18n.t(message, date: I18n.l(user.suspended_till, format: :date_only),
|
|
|
|
reason: Rack::Utils.escape_html(user.suspend_reason)),
|
2016-02-20 01:19:20 +08:00
|
|
|
reason: 'suspended'
|
|
|
|
}
|
2013-11-15 23:27:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def login(user)
|
2017-03-13 19:19:42 +08:00
|
|
|
session.delete(ACTIVATE_USER_KEY)
|
2013-11-15 23:27:43 +08:00
|
|
|
log_on_user(user)
|
2014-11-26 14:25:54 +08:00
|
|
|
|
|
|
|
if payload = session.delete(:sso_payload)
|
|
|
|
sso_provider(payload)
|
2017-08-31 12:06:56 +08:00
|
|
|
else
|
|
|
|
render_serialized(user, UserSerializer)
|
2014-11-26 14:25:54 +08:00
|
|
|
end
|
2013-11-15 23:27:43 +08:00
|
|
|
end
|
|
|
|
|
2017-03-22 02:04:25 +08:00
|
|
|
def render_sso_error(status:, text:)
|
|
|
|
@sso_error = text
|
|
|
|
render status: status, layout: 'no_ember'
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|