Class: Rack::Bacoo::SessionCookie

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/bacoo/session_cookie.rb

Constant Summary collapse

WRONG_USERNAME =
SecureRandom.hex
WRONG_PASSWORD =
BCrypt::Password.create(SecureRandom.hex, cost: 1)

Instance Method Summary collapse

Constructor Details

#initialize(cookie_attributes, encryptor, env) ⇒ SessionCookie

Returns a new instance of SessionCookie.



12
13
14
15
16
17
# File 'lib/rack/bacoo/session_cookie.rb', line 12

def initialize(cookie_attributes, encryptor, env)
  @cookie_attributes = cookie_attributes.dup
  @cookie_name = @cookie_attributes.delete(:cookie_name)
  @encryptor = encryptor
  @env = env
end

Instance Method Details

#passwordObject



23
24
25
# File 'lib/rack/bacoo/session_cookie.rb', line 23

def password
  get.nil? ? WRONG_PASSWORD : get.last
end

#set(headers, username, encrypted_password) ⇒ Object



27
28
29
30
31
# File 'lib/rack/bacoo/session_cookie.rb', line 27

def set(headers, username, encrypted_password)
  value = @encryptor.encrypt("#{username}:#{encrypted_password}:#{expires_at}")
  value = @cookie_attributes.merge(value: value)
  Utils.set_cookie_header!(headers, @cookie_name, value)
end

#usernameObject



19
20
21
# File 'lib/rack/bacoo/session_cookie.rb', line 19

def username
  get.nil? ? WRONG_USERNAME : get.first
end