When a user logs in with their email/password combo and gets authenticated to our website, the backend sends the web browser an encrypted cookie based off of their memberId
with us. While this encrypted cookie has not expired, the web browser transmits it to the backend, where we can decrypt it and figure out the memberId
it is associated with.
We are now in the process of using a more secure encryption/decryption algorithm and now realizing we can't only change the encryption/decryption algorithm, but we may also need to ensure backwards compatibility with the old encryption/decryption algorithm in case there are users of our website that still have cookies in their web browser that were encrypted using the old algorithm.
I have an idea about how do handle this, and I'm hoping to get some feedback from this community. Here is what I think the backend decrypt function should do:
decrypt(cookie) {
// check if cookie was encrypted using old encryption
if ( cookie.name == "old_cookie")
memberId = decryptOld(cookie.value)) // decrypts the value using old scheme
cookie.name = "new_cookie" // so we know next encrypt will use new scheme
// cookie was encrypted using new encryption
else{
memberId = decryptNew(cookie.value) // decrypts the value using new scheme
}
return memberID;
}
This is what I think the encrypt() function on the backend will look like:
encrypt(cookie) {
// check if cookie has old name
if ( cookie.name == "old_cookie")
cookie.name = "new_cookie" // so we know next encrypt will use new scheme
encMemberId = encryptNew(cookie.value)) // encrypts the value using new scheme
// cookie using new name
else{
encMemberId = encryptNew(cookie.value) // decrypts the value using new scheme
}
return encMemberID;
}
This way, I am essentially trying to be backwards compatible and allowing for cookies using the old encryption scheme to migrate to using the new encryption scheme. Does this make sense to everyone? Or am I missing something here?