2012-06-17 03:15:46 +02:00
|
|
|
class LoginController < ApplicationController
|
2012-07-01 00:43:45 +02:00
|
|
|
before_filter :authenticate_user
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2012-07-01 00:43:45 +02:00
|
|
|
def logout
|
|
|
|
if @user
|
|
|
|
reset_session
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|
|
|
|
|
2012-07-01 00:43:45 +02:00
|
|
|
redirect_to "/"
|
|
|
|
end
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2012-07-01 00:43:45 +02:00
|
|
|
def index
|
2012-09-07 16:18:15 +02:00
|
|
|
@title = "Login"
|
2014-08-08 17:31:06 +02:00
|
|
|
@referer ||= request.referer
|
2012-06-17 03:15:46 +02:00
|
|
|
render :action => "index"
|
|
|
|
end
|
|
|
|
|
|
|
|
def login
|
2014-01-12 22:09:32 +01:00
|
|
|
if params[:email].to_s.match(/@/)
|
|
|
|
user = User.where(:email => params[:email]).first
|
|
|
|
else
|
|
|
|
user = User.where(:username => params[:email]).first
|
|
|
|
end
|
|
|
|
|
2015-11-18 19:08:45 +01:00
|
|
|
begin
|
|
|
|
if !user
|
|
|
|
raise "no user"
|
|
|
|
end
|
|
|
|
|
|
|
|
if !user.try(:authenticate, params[:password].to_s)
|
|
|
|
raise "authentication failed"
|
|
|
|
end
|
|
|
|
|
|
|
|
if user.is_banned?
|
|
|
|
raise "user is banned"
|
|
|
|
end
|
|
|
|
|
|
|
|
if !user.is_active?
|
|
|
|
user.undelete!
|
|
|
|
flash[:success] = "Your account has been reactivated and your " <<
|
|
|
|
"unmoderated comments have been undeleted."
|
|
|
|
end
|
|
|
|
|
2015-10-11 19:53:07 +02:00
|
|
|
if !user.password_digest.to_s.match(/^\$2a\$#{BCrypt::Engine::DEFAULT_COST}\$/)
|
|
|
|
user.password = user.password_confirmation = params[:password].to_s
|
|
|
|
user.save!
|
|
|
|
end
|
|
|
|
|
2017-05-17 11:29:54 +02:00
|
|
|
if user.has_2fa?
|
|
|
|
session[:twofa_u] = user.session_token
|
|
|
|
return redirect_to "/login/2fa"
|
|
|
|
end
|
|
|
|
|
|
|
|
session[:u] = user.session_token
|
|
|
|
|
2014-08-08 17:16:06 +02:00
|
|
|
if (rd = session[:redirect_to]).present?
|
|
|
|
session.delete(:redirect_to)
|
|
|
|
return redirect_to rd
|
2014-08-08 17:31:06 +02:00
|
|
|
elsif params[:referer].present?
|
|
|
|
begin
|
|
|
|
ru = URI.parse(params[:referer])
|
|
|
|
if ru.host == Rails.application.domain
|
|
|
|
return redirect_to ru.to_s
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.error "error parsing referer: #{e}"
|
|
|
|
end
|
2014-08-08 17:16:06 +02:00
|
|
|
end
|
2014-08-08 17:31:06 +02:00
|
|
|
|
|
|
|
return redirect_to "/"
|
2015-11-18 19:08:45 +01:00
|
|
|
rescue
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|
|
|
|
|
2016-11-24 16:11:41 +01:00
|
|
|
flash.now[:error] = I18n.t 'controllers.login_controller.flashlogininvalid'
|
2014-08-08 17:31:06 +02:00
|
|
|
@referer = params[:referer]
|
2012-06-17 03:15:46 +02:00
|
|
|
index
|
|
|
|
end
|
|
|
|
|
|
|
|
def forgot_password
|
2012-09-07 16:18:15 +02:00
|
|
|
@title = "Reset Password"
|
2012-06-17 03:15:46 +02:00
|
|
|
render :action => "forgot_password"
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_password
|
2013-02-08 17:39:51 +01:00
|
|
|
@found_user = User.where("email = ? OR username = ?", params[:email].to_s,
|
|
|
|
params[:email].to_s).first
|
2012-06-17 03:15:46 +02:00
|
|
|
|
|
|
|
if !@found_user
|
2012-07-01 20:31:31 +02:00
|
|
|
flash.now[:error] = "Invalid e-mail address or username."
|
2012-06-17 03:15:46 +02:00
|
|
|
return forgot_password
|
|
|
|
end
|
|
|
|
|
2012-07-03 18:59:50 +02:00
|
|
|
@found_user.initiate_password_reset_for_ip(request.remote_ip)
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2012-07-01 20:31:31 +02:00
|
|
|
flash.now[:success] = "Password reset instructions have been e-mailed " <<
|
|
|
|
"to you."
|
2012-06-17 03:15:46 +02:00
|
|
|
return index
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_new_password
|
2012-09-07 16:18:15 +02:00
|
|
|
@title = "Reset Password"
|
|
|
|
|
2014-04-15 07:46:14 +02:00
|
|
|
if (m = params[:token].to_s.match(/^(\d+)-/)) &&
|
|
|
|
(Time.now - Time.at(m[1].to_i)) < 24.hours
|
|
|
|
@reset_user = User.where(:password_reset_token => params[:token].to_s).first
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|
|
|
|
|
2015-09-06 21:51:16 +02:00
|
|
|
if @reset_user && !@reset_user.is_banned?
|
2014-04-15 07:46:14 +02:00
|
|
|
if params[:password].present?
|
|
|
|
@reset_user.password = params[:password]
|
|
|
|
@reset_user.password_confirmation = params[:password_confirmation]
|
|
|
|
@reset_user.password_reset_token = nil
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2014-04-15 07:46:14 +02:00
|
|
|
# this will get reset upon save
|
|
|
|
@reset_user.session_token = nil
|
2012-07-01 20:31:31 +02:00
|
|
|
|
2015-09-06 21:51:16 +02:00
|
|
|
if !@reset_user.is_active? && !@reset_user.is_banned?
|
|
|
|
@reset_user.deleted_at = nil
|
|
|
|
end
|
|
|
|
|
2014-04-15 07:46:14 +02:00
|
|
|
if @reset_user.save && @reset_user.is_active?
|
|
|
|
session[:u] = @reset_user.session_token
|
|
|
|
return redirect_to "/"
|
2015-09-06 21:51:16 +02:00
|
|
|
else
|
|
|
|
flash[:error] = "Could not reset password."
|
2014-04-15 07:46:14 +02:00
|
|
|
end
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|
2014-04-15 07:46:14 +02:00
|
|
|
else
|
|
|
|
flash[:error] = "Invalid reset token. It may have already been " <<
|
|
|
|
"used or you may have copied it incorrectly."
|
2015-01-03 01:33:13 +01:00
|
|
|
return redirect_to forgot_password_path
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|
|
|
|
end
|
2017-05-17 11:29:54 +02:00
|
|
|
|
|
|
|
def twofa
|
|
|
|
if tmpu = find_twofa_user
|
|
|
|
Rails.logger.info " Authenticated as user #{tmpu.id} " <<
|
|
|
|
"(#{tmpu.username}), verifying TOTP"
|
|
|
|
else
|
|
|
|
reset_session
|
|
|
|
return redirect_to "/login"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def twofa_verify
|
|
|
|
if (tmpu = find_twofa_user) && tmpu.authenticate_totp(params[:totp_code])
|
|
|
|
session[:u] = tmpu.session_token
|
|
|
|
session.delete(:twofa_u)
|
|
|
|
return redirect_to "/"
|
|
|
|
else
|
|
|
|
flash[:error] = "Your TOTP code did not match. Please try again."
|
|
|
|
return redirect_to "/login/2fa"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def find_twofa_user
|
|
|
|
if session[:twofa_u].present?
|
|
|
|
return User.where(:session_token => session[:twofa_u]).first
|
|
|
|
end
|
|
|
|
end
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|