lib/super_token.rb in super_token-1.0.0 vs lib/super_token.rb in super_token-1.1.0
- old
+ new
@@ -1,5 +1,7 @@
+# frozen_string_literal: true
+
require 'active_record'
module ActiveRecord
module SecureToken
extend ActiveSupport::Concern
@@ -26,15 +28,15 @@
# Note that it's still possible to generate a race condition in the database in the same way that
# {validates_uniqueness_of}[rdoc-ref:Validations::ClassMethods#validates_uniqueness_of] can.
# You're encouraged to add a unique index in the database to deal with this even more unlikely scenario.
def has_secure_token(attribute = :token, length: 24, prefix: '')
# Load securerandom only when has_secure_token is used.
- require 'active_support/core_ext/securerandom'
- define_method("regenerate_#{attribute}") { update! attribute => self.class.generate_unique_secure_token(length, prefix) }
- before_create { self.send("#{attribute}=", self.class.generate_unique_secure_token(length, prefix)) unless self.send("#{attribute}?")}
+ require 'active_support/core_ext/securerandom' unless SecureRandom.respond_to?(:base58)
+ define_method("regenerate_#{attribute}") { update! attribute => self.class.generate_unique_secure_token_with_prefix(length, prefix) }
+ before_create { self.send("#{attribute}=", self.class.generate_unique_secure_token_with_prefix(length, prefix)) unless self.send("#{attribute}?")}
end
- def generate_unique_secure_token(length, prefix)
+ def generate_unique_secure_token_with_prefix(length, prefix)
token_length = length - prefix.length
prefix + SecureRandom.base58(token_length)
end
end
end
\ No newline at end of file