Class: Warden::SharedSession::EncryptedCookie

Inherits:
Object
  • Object
show all
Defined in:
lib/warden/shared_session/encrypted_cookie.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store:, cookie:, secret:) ⇒ EncryptedCookie

Returns a new instance of EncryptedCookie.



5
6
7
8
9
10
11
# File 'lib/warden/shared_session/encrypted_cookie.rb', line 5

def initialize(store:, cookie:, secret:)
  @store = store
  @cookie = cookie
  @secret = secret

  @encryptor ||= ActiveSupport::MessageEncryptor.new(secret)
end

Instance Attribute Details

Returns the value of attribute cookie.



3
4
5
# File 'lib/warden/shared_session/encrypted_cookie.rb', line 3

def cookie
  @cookie
end

#encryptorObject (readonly)

Returns the value of attribute encryptor.



3
4
5
# File 'lib/warden/shared_session/encrypted_cookie.rb', line 3

def encryptor
  @encryptor
end

#secretObject (readonly)

Returns the value of attribute secret.



3
4
5
# File 'lib/warden/shared_session/encrypted_cookie.rb', line 3

def secret
  @secret
end

#storeObject (readonly)

Returns the value of attribute store.



3
4
5
# File 'lib/warden/shared_session/encrypted_cookie.rb', line 3

def store
  @store
end

Instance Method Details

#clearObject



24
25
26
# File 'lib/warden/shared_session/encrypted_cookie.rb', line 24

def clear
  store.delete(cookie)
end

#getObject



13
14
15
16
17
18
# File 'lib/warden/shared_session/encrypted_cookie.rb', line 13

def get
  value = store[cookie]
  return nil unless value

  JSON(encryptor.decrypt_and_verify(value))
end

#put(data) ⇒ Object



20
21
22
# File 'lib/warden/shared_session/encrypted_cookie.rb', line 20

def put(data)
  store[cookie] = encryptor.encrypt_and_sign(data.to_json)
end