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

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