Class: SecureHeaders::Cookie

Inherits:
Object
  • Object
show all
Defined in:
lib/secure_headers/headers/cookie.rb

Constant Summary collapse

{
  httponly: true,
  secure: true,
  samesite: { lax: true },
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cookie, config) ⇒ Cookie

Returns a new instance of Cookie.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/secure_headers/headers/cookie.rb', line 22

def initialize(cookie, config)
  @raw_cookie = cookie
  unless config == OPT_OUT
    config ||= {}
    config = COOKIE_DEFAULTS.merge(config)
  end
  @config = config
  @attributes = {
    httponly: nil,
    samesite: nil,
    secure: nil,
  }

  parse(cookie)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



14
15
16
# File 'lib/secure_headers/headers/cookie.rb', line 14

def config
  @config
end

Returns the value of attribute raw_cookie.



14
15
16
# File 'lib/secure_headers/headers/cookie.rb', line 14

def raw_cookie
  @raw_cookie
end

Class Method Details

.validate_config!(config) ⇒ Object



10
11
12
# File 'lib/secure_headers/headers/cookie.rb', line 10

def self.validate_config!(config)
  CookiesConfig.new(config).validate!
end

Instance Method Details

#httponly?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/secure_headers/headers/cookie.rb', line 50

def httponly?
  flag_cookie?(:httponly) && !already_flagged?(:httponly)
end

#samesite?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/secure_headers/headers/cookie.rb', line 54

def samesite?
  flag_samesite? && !already_flagged?(:samesite)
end

#secure?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/secure_headers/headers/cookie.rb', line 46

def secure?
  flag_cookie?(:secure) && !already_flagged?(:secure)
end

#to_sObject



38
39
40
41
42
43
44
# File 'lib/secure_headers/headers/cookie.rb', line 38

def to_s
  @raw_cookie.dup.tap do |c|
    c << "; secure" if secure?
    c << "; HttpOnly" if httponly?
    c << "; #{samesite_cookie}" if samesite?
  end
end