Skip to content

Instantly share code, notes, and snippets.

@hbin
Created November 19, 2014 07:48
Show Gist options
  • Select an option

  • Save hbin/8cb59cc798da04bf1a9d to your computer and use it in GitHub Desktop.

Select an option

Save hbin/8cb59cc798da04bf1a9d to your computer and use it in GitHub Desktop.
def decrypt_session_cookie(cookie)
cookie = CGI.unescape(cookie)
config = Rails.application.config
encrypted_cookie_salt = config.action_dispatch.encrypted_cookie_salt # "encrypted cookie" by default
encrypted_signed_cookie_salt = config.action_dispatch.encrypted_signed_cookie_salt # "signed encrypted cookie" by default
key_generator = ActiveSupport::KeyGenerator.new(Rails.application.secrets.secret_key_base, iterations: 1000)
secret = key_generator.generate_key(encrypted_cookie_salt)
sign_secret = key_generator.generate_key(encrypted_signed_cookie_salt)
encryptor = ActiveSupport::MessageEncryptor.new(secret, sign_secret)
encryptor.decrypt_and_verify(cookie)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment