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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 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 < ActiveRecord::Base
  19. belongs_to :user
  20. validates_uniqueness_of :value
  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. def generate_new_token
  39. self.value = Token.generate_token_value
  40. end
  41. # Return true if token has expired
  42. def expired?
  43. validity_time = self.class.invalid_when_created_before(action)
  44. validity_time.present? && created_on < validity_time
  45. end
  46. def max_instances
  47. Token.actions.has_key?(action) ? Token.actions[action][:max_instances] : 1
  48. end
  49. def self.invalid_when_created_before(action = nil)
  50. if Token.actions.has_key?(action)
  51. validity_time = Token.actions[action][:validity_time]
  52. validity_time = validity_time.call(action) if validity_time.respond_to? :call
  53. else
  54. validity_time = self.validity_time
  55. end
  56. if validity_time
  57. Time.now - validity_time
  58. end
  59. end
  60. # Delete all expired tokens
  61. def self.destroy_expired
  62. t = Token.arel_table
  63. # Unknown actions have default validity_time
  64. condition = t[:action].not_in(self.actions.keys).and(t[:created_on].lt(invalid_when_created_before))
  65. self.actions.each do |action, options|
  66. validity_time = invalid_when_created_before(action)
  67. # Do not delete tokens, which don't become invalid
  68. next if validity_time.nil?
  69. condition = condition.or(
  70. t[:action].eq(action).and(t[:created_on].lt(validity_time))
  71. )
  72. end
  73. Token.where(condition).delete_all
  74. end
  75. # Returns the active user who owns the key for the given action
  76. def self.find_active_user(action, key, validity_days=nil)
  77. user = find_user(action, key, validity_days)
  78. if user && user.active?
  79. user
  80. end
  81. end
  82. # Returns the user who owns the key for the given action
  83. def self.find_user(action, key, validity_days=nil)
  84. token = find_token(action, key, validity_days)
  85. if token
  86. token.user
  87. end
  88. end
  89. # Returns the token for action and key with an optional
  90. # validity duration (in number of days)
  91. def self.find_token(action, key, validity_days=nil)
  92. action = action.to_s
  93. key = key.to_s
  94. return nil unless action.present? && /\A[a-z0-9]+\z/i.match?(key)
  95. token = Token.find_by(:action => action, :value => key)
  96. if token && (token.action == action) && (token.value == key) && token.user
  97. if validity_days.nil? || (token.created_on > validity_days.days.ago)
  98. token
  99. end
  100. end
  101. end
  102. def self.generate_token_value
  103. Redmine::Utils.random_hex(20)
  104. end
  105. private
  106. # Removes obsolete tokens (same user and action)
  107. def delete_previous_tokens
  108. if user
  109. scope = Token.where(:user_id => user.id, :action => action)
  110. if max_instances > 1
  111. ids = scope.order(:updated_on => :desc).offset(max_instances - 1).ids
  112. if ids.any?
  113. Token.delete(ids)
  114. end
  115. else
  116. scope.delete_all
  117. end
  118. end
  119. end
  120. end