Module: HasMoreSecureToken::ClassMethods
- Defined in:
- lib/has_more_secure_token.rb
Instance Method Summary collapse
- #finds_secure_token_by_digest(attribute, length: MINIMUM_TOKEN_LENGTH, digest: 'sha256') ⇒ void
- #has_secure_token(attribute = :token, length: MINIMUM_TOKEN_LENGTH, find_by_digest: nil) ⇒ void
Instance Method Details
#finds_secure_token_by_digest(attribute, length: MINIMUM_TOKEN_LENGTH, digest: 'sha256') ⇒ void
This method returns an undefined value.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/has_more_secure_token.rb', line 25 def finds_secure_token_by_digest(attribute, length: MINIMUM_TOKEN_LENGTH, digest: 'sha256') # The digest must be a valid algorithm. openssl_digest = OpenSSL::Digest.new(digest) # An arel node that matches the format of the digest in the index. token_arel = arel_token_digest(attribute, digest) define_singleton_method(:"find_by_#{attribute}") do |token| # Short-circuit to avoid the database query if the token is obviously invalid. return if token.blank? || (token = token.to_s).length != length # A bound parameter that matches the format of the digest in the index. token_hash = bind_token_param(attribute.to_s, openssl_digest.digest(token)) # Produces: # digest("table_name"."attribute", 'SHA256') = $1 find_by(token_arel.eq(token_hash)) end define_singleton_method(:"find_by_#{attribute}!") do |token| send(:"find_by_#{attribute}", token) or raise_record_not_find_by_token! end end |
#has_secure_token(attribute = :token, length: MINIMUM_TOKEN_LENGTH, find_by_digest: nil) ⇒ void
This method returns an undefined value.
15 16 17 18 19 |
# File 'lib/has_more_secure_token.rb', line 15 def has_secure_token(attribute = :token, length: MINIMUM_TOKEN_LENGTH, find_by_digest: nil) # rubocop:disable Naming/PredicateName finds_secure_token_by_digest(attribute, length: length, digest: find_by_digest) if find_by_digest super(attribute, length: length) end |