2025-07-18 17:35:57 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2013-06-20 16:14:55 +02:00
|
|
|
#-- copyright
|
2020-01-15 11:31:26 +01:00
|
|
|
# OpenProject is an open source project management software.
|
2024-07-30 13:42:36 +02:00
|
|
|
# Copyright (C) the OpenProject GmbH
|
2013-06-20 16:14:55 +02:00
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
|
# modify it under the terms of the GNU General Public License version 3.
|
|
|
|
|
#
|
2013-09-16 17:59:31 +02:00
|
|
|
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
2021-01-13 17:47:45 +01:00
|
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
2013-09-16 17:59:31 +02:00
|
|
|
# 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.
|
|
|
|
|
#
|
2021-09-02 21:49:06 +02:00
|
|
|
# See COPYRIGHT and LICENSE files for more details.
|
2013-06-20 16:14:55 +02:00
|
|
|
#++
|
|
|
|
|
|
2020-04-09 11:54:26 +02:00
|
|
|
class UserPassword < ApplicationRecord
|
2014-11-03 21:10:39 +01:00
|
|
|
belongs_to :user, inverse_of: :passwords
|
2013-06-20 16:14:55 +02:00
|
|
|
|
|
|
|
|
# passwords must never be modified, so doing this on create should be enough
|
|
|
|
|
before_create :salt_and_hash_password!
|
|
|
|
|
|
|
|
|
|
attr_accessor :plain_password
|
|
|
|
|
|
2016-08-29 20:36:42 +02:00
|
|
|
##
|
|
|
|
|
# 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.
|
2025-10-30 17:58:26 +00:00
|
|
|
# This replaces the password and keeps expiration date identical.
|
2016-08-29 20:36:42 +02:00
|
|
|
def rehash_as_active(plain)
|
|
|
|
|
active_class = UserPassword.active_type
|
|
|
|
|
|
|
|
|
|
unless is_a?(active_class)
|
2016-09-05 14:01:06 +02:00
|
|
|
active = becomes!(active_class)
|
|
|
|
|
active.rehash_from_legacy(plain)
|
|
|
|
|
|
|
|
|
|
active
|
2016-08-29 20:36:42 +02:00
|
|
|
end
|
2021-02-11 16:02:18 +01:00
|
|
|
rescue StandardError => e
|
2016-08-29 20:36:42 +02:00
|
|
|
Rails.logger.error("Unable to re-hash UserPassword for #{user.login}: #{e.message}")
|
2013-06-20 16:14:55 +02:00
|
|
|
end
|
|
|
|
|
|
2016-09-05 14:01:06 +02:00
|
|
|
##
|
|
|
|
|
# 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
|
|
|
|
|
|
2013-07-22 14:31:12 +02:00
|
|
|
def expired?
|
|
|
|
|
days_valid = Setting.password_days_valid.to_i.days
|
|
|
|
|
return false if days_valid == 0
|
2021-02-11 16:02:18 +01:00
|
|
|
|
2014-11-03 21:35:22 +01:00
|
|
|
created_at < (Time.now - days_valid)
|
2013-07-22 14:31:12 +02:00
|
|
|
end
|
|
|
|
|
|
2016-08-29 20:36:42 +02:00
|
|
|
protected
|
2013-06-20 16:14:55 +02:00
|
|
|
|
2016-08-29 20:36:42 +02:00
|
|
|
# Save hashed_password from the initially passed plain password
|
2021-07-21 11:26:31 -04:00
|
|
|
# if it's set.
|
2016-08-29 20:36:42 +02:00
|
|
|
def salt_and_hash_password!
|
|
|
|
|
return if plain_password.nil?
|
2021-02-11 16:02:18 +01:00
|
|
|
|
2016-08-29 20:36:42 +02:00
|
|
|
self.hashed_password = derive_password!(plain_password)
|
2013-06-20 16:14:55 +02:00
|
|
|
end
|
|
|
|
|
|
2021-01-22 09:14:21 -05:00
|
|
|
# Require the implementation to provide a secure comparison
|
2016-08-29 20:36:42 +02:00
|
|
|
def hash_matches?(_plain)
|
2026-03-26 13:28:12 +01:00
|
|
|
raise SubclassResponsibilityError
|
2013-06-20 16:14:55 +02:00
|
|
|
end
|
|
|
|
|
|
2016-08-29 20:36:42 +02:00
|
|
|
def derive_password!(_input)
|
2026-03-26 13:28:12 +01:00
|
|
|
raise SubclassResponsibilityError
|
2013-06-20 16:14:55 +02:00
|
|
|
end
|
|
|
|
|
end
|