Skip to content

Commit

Permalink
perf(token_is_current?): add simplistic cache to reduce overhad of re…
Browse files Browse the repository at this point in the history
…dundant token checks during validation calls
  • Loading branch information
booleanbetrayal committed Jun 17, 2015
1 parent 5e4e4c0 commit 425594e
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion app/models/devise_token_auth/concerns/user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
module DeviseTokenAuth::Concerns::User
extend ActiveSupport::Concern

def self.tokens_match?(token_hash, token)
@token_equality_cache ||= {}

key = "#{token_hash}/#{token}"
result = @token_equality_cache[key] ||= (BCrypt::Password.new(token_hash) == token)
if @token_equality_cache.size > 10000
@token_equality_cache = {}
end
result
end

included do
# Hack to check if devise is already enabled
unless self.method_defined?(:devise_modules)
Expand Down Expand Up @@ -111,7 +122,7 @@ def token_is_current?(token, client_id)
DateTime.strptime(expiry.to_s, '%s') > Time.now and

# ensure that the token is valid
BCrypt::Password.new(token_hash) == token
DeviseTokenAuth::Concerns::User.tokens_match?(token_hash, token)
)
end

Expand Down

0 comments on commit 425594e

Please sign in to comment.