eBooru/app/logical/session_loader.rb

134 lines
4.0 KiB
Ruby
Raw Permalink Normal View History

# frozen_string_literal: true
class SessionLoader
2015-06-18 20:03:33 -04:00
class AuthenticationFailure < Exception ; end
2013-03-20 18:43:17 -04:00
attr_reader :session, :cookies, :request, :params
2013-03-19 08:10:10 -04:00
def initialize(request)
@request = request
@session = request.session
@cookies = request.cookie_jar
@params = request.parameters
@remember_validator = ActiveSupport::MessageVerifier.new(Danbooru.config.remember_key, serializer: JSON, digest: "SHA256")
end
2013-03-19 08:10:10 -04:00
def load
CurrentUser.user = User.anonymous
CurrentUser.ip_addr = request.remote_ip
if has_api_authentication?
load_session_for_api
elsif session[:user_id]
load_session_user
2019-10-31 09:41:21 -04:00
elsif has_remember_token?
load_remember_token
end
2013-03-19 08:10:10 -04:00
CurrentUser.user.unban! if CurrentUser.user.ban_expired?
if CurrentUser.user.is_blocked?
recent_ban = CurrentUser.user.recent_ban
ban_message = "Account is banned: forever"
if recent_ban && recent_ban.expires_at.present?
ban_message = "Account is suspended for another #{recent_ban.expire_days}"
end
raise AuthenticationFailure.new(ban_message)
end
set_statement_timeout
update_last_logged_in_at
update_last_ip_addr
set_time_zone
set_safe_mode
2020-04-12 22:08:33 -04:00
refresh_old_remember_token
DanbooruLogger.initialize(CurrentUser.user)
end
def has_api_authentication?
request.authorization.present? || params[:login].present? || params[:api_key].present?
end
2019-10-31 09:41:21 -04:00
def has_remember_token?
cookies.encrypted[:remember].present?
end
private
Raise error on unpermitted params. Fail loudly if we forget to whitelist a param instead of silently ignoring it. misc models: convert to strong params. artist commentaries: convert to strong params. * Disallow changing or setting post_id to a nonexistent post. artists: convert to strong params. * Disallow setting `is_banned` in create/update actions. Changing it this way instead of with the ban/unban actions would leave the artist in a partially banned state. bans: convert to strong params. * Disallow changing the user_id after the ban has been created. comments: convert to strong params. favorite groups: convert to strong params. news updates: convert to strong params. post appeals: convert to strong params. post flags: convert to strong params. * Disallow users from setting the `is_deleted` / `is_resolved` flags. ip bans: convert to strong params. user feedbacks: convert to strong params. * Disallow users from setting `disable_dmail_notification` when creating feedbacks. * Disallow changing the user_id after the feedback has been created. notes: convert to strong params. wiki pages: convert to strong params. * Also fix non-Builders being able to delete wiki pages. saved searches: convert to strong params. pools: convert to strong params. * Disallow setting `post_count` or `is_deleted` in create/update actions. janitor trials: convert to strong params. post disapprovals: convert to strong params. * Factor out quick-mod bar to shared partial. * Fix quick-mod bar to use `Post#is_approvable?` to determine visibility of Approve button. dmail filters: convert to strong params. password resets: convert to strong params. user name change requests: convert to strong params. posts: convert to strong params. users: convert to strong params. * Disallow setting password_hash, last_logged_in_at, last_forum_read_at, has_mail, and dmail_filter_attributes[user_id]. * Remove initialize_default_image_size (dead code). uploads: convert to strong params. * Remove `initialize_status` because status already defaults to pending in the database. tag aliases/implications: convert to strong params. tags: convert to strong params. forum posts: convert to strong params. * Disallow changing the topic_id after creating the post. * Disallow setting is_deleted (destroy/undelete actions should be used instead). * Remove is_sticky / is_locked (nonexistent attributes). forum topics: convert to strong params. * merges https://github.com/evazion/danbooru/tree/wip-rails-5.1 * lock pg gem to 0.21 (1.0.0 is incompatible with rails 5.1.4) * switch to factorybot and change all references Co-authored-by: r888888888 <r888888888@gmail.com> Co-authored-by: evazion <noizave@gmail.com> add diffs
2018-04-02 13:51:26 -04:00
def set_statement_timeout
timeout = CurrentUser.user.statement_timeout
ActiveRecord::Base.connection.execute("set statement_timeout = #{timeout}")
end
2013-03-19 08:10:10 -04:00
2019-10-31 09:41:21 -04:00
def load_remember_token
2020-04-12 22:08:33 -04:00
begin
message = @remember_validator.verify(cookies.encrypted[:remember], purpose: "rbr")
return if message.nil?
pieces = message.split(":")
return unless pieces.length == 2
user = User.find_by_id(pieces[0].to_i)
2020-04-12 22:08:33 -04:00
return unless user
return if pieces[1].to_i != user.password_token
2020-04-12 22:08:33 -04:00
CurrentUser.user = user
session[:user_id] = user.id
session[:ph] = user.password_token # This has been validated by the remember token
2020-04-12 22:08:33 -04:00
rescue
return
end
end
def refresh_old_remember_token
if cookies.encrypted[:remember] && !CurrentUser.is_anonymous?
cookies.encrypted[:remember] = {value: @remember_validator.generate("#{CurrentUser.id}:#{CurrentUser.password_token}", purpose: "rbr", expires_in: 14.days), expires: Time.now + 14.days, httponly: true, same_site: :lax, secure: Rails.env.production?}
2020-04-12 22:08:33 -04:00
end
2019-10-31 09:41:21 -04:00
end
2013-03-20 18:43:17 -04:00
def load_session_for_api
if request.authorization
authenticate_basic_auth
elsif params[:login].present? && params[:api_key].present?
authenticate_api_key(params[:login], params[:api_key])
else
raise AuthenticationFailure
2013-03-20 18:43:17 -04:00
end
end
2013-03-20 18:43:17 -04:00
def authenticate_basic_auth
credentials = ::Base64.decode64(request.authorization.split(' ', 2).last || '')
login, api_key = credentials.split(":", 2)
2013-03-20 18:43:17 -04:00
authenticate_api_key(login, api_key)
end
2013-03-20 18:43:17 -04:00
def authenticate_api_key(name, api_key)
user = User.authenticate_api_key(name, api_key)
raise AuthenticationFailure if user.nil?
CurrentUser.user = user
2013-03-20 18:43:17 -04:00
end
def load_session_user
user = User.find_by_id(session[:user_id])
raise AuthenticationFailure if user.nil?
return if session[:ph] != user.password_token
CurrentUser.user = user
end
2013-03-19 08:10:10 -04:00
def update_last_logged_in_at
return if CurrentUser.is_anonymous?
return if CurrentUser.last_logged_in_at && CurrentUser.last_logged_in_at > 1.week.ago
CurrentUser.user.update_attribute(:last_logged_in_at, Time.now)
2016-02-17 19:59:27 -05:00
end
2016-08-24 18:58:22 -04:00
def update_last_ip_addr
return if CurrentUser.is_anonymous?
return if CurrentUser.user.last_ip_addr == @request.remote_ip
CurrentUser.user.update_attribute(:last_ip_addr, @request.remote_ip)
end
def set_time_zone
Time.zone = CurrentUser.user.time_zone
end
def set_safe_mode
safe_mode = Danbooru.config.safe_mode? || params[:safe_mode].to_s.truthy? || CurrentUser.user.enable_safe_mode?
CurrentUser.safe_mode = safe_mode
end
end