Skip to content

Instantly share code, notes, and snippets.

@AlexanderEkdahl
Created September 1, 2013 16:45
Show Gist options
  • Save AlexanderEkdahl/6405625 to your computer and use it in GitHub Desktop.
Save AlexanderEkdahl/6405625 to your computer and use it in GitHub Desktop.
module Verifiable
extend ActiveSupport::Concern
def verification_key
ActiveSupport::MessageEncryptor
.new(Rails.application.config.secret_key_base)
.encrypt_and_sign("#{self.class} #{id}")
end
module ClassMethods
def find_by_verification_key(value)
class_name, id = ActiveSupport::MessageEncryptor
.new(Rails.application.config.secret_key_base)
.decrypt_and_verify(value).split
find(id) if class_name == self.name
rescue ActiveSupport::MessageVerifier::InvalidSignature
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment