Created
April 20, 2017 20:39
-
-
Save bruz/fab4cae0275a756b7e88245fc761c434 to your computer and use it in GitHub Desktop.
Decrypt Rails 4 session cookie
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# `cookie` is the session cookie string | |
message = URI.unescape cookie | |
config = Rails.application.config | |
key_generator = ActiveSupport::KeyGenerator.new( | |
Rails.application.secrets.secret_key_base, iterations: 1000 | |
) | |
secret = key_generator.generate_key( | |
config.action_dispatch.encrypted_cookie_salt | |
) | |
sign_secret = key_generator.generate_key( | |
config.action_dispatch.encrypted_signed_cookie_salt | |
) | |
encryptor = ActiveSupport::MessageEncryptor.new(secret, sign_secret, serializer: JSON) | |
encryptor.decrypt_and_verify message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment