Last active
August 29, 2015 14:17
-
-
Save JosephBrianChambers/dd39cd71616cca3561c0 to your computer and use it in GitHub Desktop.
Token encryption migration using attr_encrypted gem
This file contains hidden or 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
# Given mongoid Account model | |
class Account | |
include Mongoid::Document | |
field encrypted_token | |
field token_version | |
attr_encryptor :token, key: proc { |account| ENV["ACCOUNT_TOKEN_ENCRYPTION_KEY_V#{account.token_version}"] }, encode: true | |
end | |
# Steps to Migrate token to new encryption version | |
# 1. set new ENV variable (naming important) | |
ENV['ACCOUNT_TOKEN_VERSION2'] = 'new_encryption_key' | |
# 2. Update token_version and token | |
a = Account.new(_id: '5:1234', token: 'insta_token') | |
a.save | |
# => true | |
a.encrypted_token | |
# => "y5yjzWD34w2TKPAOd84G8w==\n" | |
a.token | |
# => "insta_token" | |
a.update_attributes(token_version: 2, token: a.token) | |
# => true | |
a.encrypted_token | |
# => "l3nk0xuoH9myvmVt2CwfUw==\n" | |
a.token | |
# => "insta_token" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment