HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux vmi1674223.contaboserver.net 5.4.0-182-generic #202-Ubuntu SMP Fri Apr 26 12:29:36 UTC 2024 x86_64
User: root (0)
PHP: 7.4.3-4ubuntu2.22
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //opt/openproject/app/models/user_password.rb
#-- encoding: UTF-8
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2020 the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2017 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# See docs/COPYRIGHT.rdoc for more details.
#++

class UserPassword < ApplicationRecord
  belongs_to :user, inverse_of: :passwords

  # passwords must never be modified, so doing this on create should be enough
  before_create :salt_and_hash_password!

  attr_accessor :plain_password

  ##
  # Fixes the active UserPassword Type to use.
  # This could allow for an entrypoint for plugins or customization
  def self.active_type
    UserPassword::Bcrypt
  end

  ##
  # Determines whether the hashed value of +plain+ matches the stored password hash.
  def matches_plaintext?(plain, update_legacy: true)
    if hash_matches?(plain)

      # Update hash if necessary
      if update_legacy
        rehash_as_active(plain)
      end

      return true
    end

    false
  end

  ##
  # Rehash the password using the currently active strategy.
  # This replaces the password and keeps expiry date identical.
  def rehash_as_active(plain)
    active_class = UserPassword.active_type

    unless is_a?(active_class)
      active = becomes!(active_class)
      active.rehash_from_legacy(plain)

      active
    end
  rescue => e
    Rails.logger.error("Unable to re-hash UserPassword for #{user.login}: #{e.message}")
  end

  ##
  # Actually derive and save the password using +active_type+
  # We require a public interface since +becomes!+ does change the STI instance,
  # but returns, not changes the actual current object.
  def rehash_from_legacy(plain)
    self.hashed_password = derive_password!(plain)
    save!
  end

  def expired?
    days_valid = Setting.password_days_valid.to_i.days
    return false if days_valid == 0
    created_at < (Time.now - days_valid)
  end

  protected

  # Save hashed_password from the initially passed plain password
  # if it is is set.
  def salt_and_hash_password!
    return if plain_password.nil?
    self.hashed_password = derive_password!(plain_password)
  end

  # Require the implementation to provide a secure comparisation
  def hash_matches?(_plain)
    raise NotImplementedError, 'Must be overridden by subclass'
  end

  def derive_password!(_input)
    raise NotImplementedError, 'Must be overridden by subclass'
  end
end