You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When you need a very fast solution, you might want to stick to encrypted cookies in the user's browser. The particular use case for this solution is a small administration app that will never have thousands of users, but is used in a security and privacy aware context.
classSession < ApplicationRecordself.primary_key=:session_idaround_save:silence_logsclass << selfdeffind_by_session_id(session_id)Session.find_or_initialize_by(session_id: session_id)endenddefsession_id=(sid)@session_id=sid || SecureRandom.hex(16)super(@session_id)enddefsession_idread_attribute(:session_id) || @session_idenddefdata=(json)super(EncryptionService.new(salt: "your salt").encrypt(json))enddefdataencrypted_data=read_attribute(:data)EncryptionService.new(salt: "your salt").decrypt(encrypted_data)unlesssession_id.nil? || encrypted_data.blank?# rescue in case the secret changed (no rollover implemented yet)# the salt is wrong# or some other issue prevented decryption# and delete the flawed session datarescueActiveSupport::MessageEncryptor::InvalidMessagedelete && nilendprivate# simple, reliable log silencingdefsilence_logsRails.logger.silencedoyield# saves / updates the sessionendendend
By going through the issues here and while trying to implement this solution, I've got the impression that the documentation of this gem is outdated and lacking. Maybe this implementation helps someone to achieve something similar faster than me.
The text was updated successfully, but these errors were encountered:
@breim My implementation of the EncryptionService turned out to be a bit too CPU intense to use it with many concurrent sessions. So, I prefer to not add it, so it won't be blindly copied.
But if you look for examples using ActiveSupport::MessageEncryptor you will get the idea.
Encrypted database sessions
Hey, I would like to share my solution to how I use the gem activerecord-session_store to:
config/initializers/session_store.rb
app/models/session.rb
The
EncryptionService
used in this example is a small class based on ActiveSupport::MessageEncryptordatabase schema
The index on the
updated_at
column is there to delete sessions older than 30 days by runningrake db:sessions:trim
as a scheduled task.Hope it helps!
By going through the issues here and while trying to implement this solution, I've got the impression that the documentation of this gem is outdated and lacking. Maybe this implementation helps someone to achieve something similar faster than me.
The text was updated successfully, but these errors were encountered: