You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

token.rb 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class Token < ApplicationRecord
  19. belongs_to :user
  20. validates_uniqueness_of :value, :case_sensitive => true
  21. before_create :delete_previous_tokens, :generate_new_token
  22. cattr_accessor :validity_time
  23. self.validity_time = 1.day
  24. class << self
  25. attr_reader :actions
  26. def add_action(name, options)
  27. options.assert_valid_keys(:max_instances, :validity_time)
  28. @actions ||= {}
  29. @actions[name.to_s] = options
  30. end
  31. end
  32. add_action :api, max_instances: 1, validity_time: nil
  33. add_action :autologin, max_instances: 10, validity_time: Proc.new {Setting.autologin.to_i.days}
  34. add_action :feeds, max_instances: 1, validity_time: nil
  35. add_action :recovery, max_instances: 1, validity_time: Proc.new {Token.validity_time}
  36. add_action :register, max_instances: 1, validity_time: Proc.new {Token.validity_time}
  37. add_action :session, max_instances: 10, validity_time: nil
  38. add_action :twofa_backup_code, max_instances: 10, validity_time: nil
  39. def generate_new_token
  40. self.value = Token.generate_token_value
  41. end
  42. # Return true if token has expired
  43. def expired?
  44. validity_time = self.class.invalid_when_created_before(action)
  45. validity_time.present? && created_on < validity_time
  46. end
  47. def max_instances
  48. Token.actions.has_key?(action) ? Token.actions[action][:max_instances] : 1
  49. end
  50. def self.invalid_when_created_before(action = nil)
  51. if Token.actions.has_key?(action)
  52. validity_time = Token.actions[action][:validity_time]
  53. validity_time = validity_time.call(action) if validity_time.respond_to? :call
  54. else
  55. validity_time = self.validity_time
  56. end
  57. if validity_time
  58. Time.now - validity_time
  59. end
  60. end
  61. # Delete all expired tokens
  62. def self.destroy_expired
  63. t = Token.arel_table
  64. # Unknown actions have default validity_time
  65. condition = t[:action].not_in(self.actions.keys).and(t[:created_on].lt(invalid_when_created_before))
  66. self.actions.each_key do |action|
  67. validity_time = invalid_when_created_before(action)
  68. # Do not delete tokens, which don't become invalid
  69. next if validity_time.nil?
  70. condition = condition.or(
  71. t[:action].eq(action).and(t[:created_on].lt(validity_time))
  72. )
  73. end
  74. Token.where(condition).delete_all
  75. end
  76. # Returns the active user who owns the key for the given action
  77. def self.find_active_user(action, key, validity_days=nil)
  78. user = find_user(action, key, validity_days)
  79. if user && user.active?
  80. user
  81. end
  82. end
  83. # Returns the user who owns the key for the given action
  84. def self.find_user(action, key, validity_days=nil)
  85. token = find_token(action, key, validity_days)
  86. if token
  87. token.user
  88. end
  89. end
  90. # Returns the token for action and key with an optional
  91. # validity duration (in number of days)
  92. def self.find_token(action, key, validity_days=nil)
  93. action = action.to_s
  94. key = key.to_s
  95. return nil unless action.present? && /\A[a-z0-9]+\z/i.match?(key)
  96. token = Token.find_by(:action => action, :value => key)
  97. return unless token
  98. return unless token.action == action
  99. return unless ActiveSupport::SecurityUtils.secure_compare(token.value.to_s, key)
  100. return unless token.user
  101. return unless validity_days.nil? || (token.created_on > validity_days.days.ago)
  102. token
  103. end
  104. def self.generate_token_value
  105. Redmine::Utils.random_hex(20)
  106. end
  107. private
  108. # Removes obsolete tokens (same user and action)
  109. def delete_previous_tokens
  110. if user
  111. scope = Token.where(:user_id => user.id, :action => action)
  112. if max_instances > 1
  113. ids = scope.order(:updated_on => :desc).offset(max_instances - 1).ids
  114. if ids.any?
  115. Token.delete(ids)
  116. end
  117. else
  118. scope.delete_all
  119. end
  120. end
  121. end
  122. end