Class: Warden::CookieSession::EncryptedCookie

Inherits:
Object
  • Object
show all
Defined in:
lib/warden/cookie_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.

Raises:

  • (ArgumentError)


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

def initialize(store:, cookie:, secret:)
  @store = store
  @cookie = cookie
  @secret = secret
  raise ArgumentError.new('secret must be 32 bytes') if @secret.length != 32

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

Instance Attribute Details

Returns the value of attribute cookie.



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

def cookie
  @cookie
end

#encryptorObject (readonly)

Returns the value of attribute encryptor.



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

def encryptor
  @encryptor
end

#secretObject (readonly)

Returns the value of attribute secret.



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

def secret
  @secret
end

#storeObject (readonly)

Returns the value of attribute store.



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

def store
  @store
end

Instance Method Details

#clear(domain) ⇒ Object



30
31
32
# File 'lib/warden/cookie_session/encrypted_cookie.rb', line 30

def clear(domain)
  store.delete(cookie, domain: domain)
end

#getObject



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

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

  JSON(encryptor.decrypt_and_verify(value))
end

#put(data, domain) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/warden/cookie_session/encrypted_cookie.rb', line 21

def put(data, domain)
  c = {
    value:  encryptor.encrypt_and_sign(data.to_json),
    domain: domain
  }

  store[cookie] = c
end