Module: SafeCookies::Helpers
- Included in:
- Middleware
- Defined in:
- lib/safe_cookies/helpers.rb
Constant Summary collapse
- KNOWN_COOKIES_DIVIDER =
'|'
Instance Method Summary collapse
-
#cache_application_cookies_string ⇒ Object
Since we have to operate on and modify the actual @headers hash that the application returns, cache the @headers string so that later on, we still know what the application did set.
-
#cookies_have_been_rewritten_before? ⇒ Boolean
boolean.
- #http_only(cookie) ⇒ Object
- #known_cookie_names ⇒ Object
-
#request_cookies ⇒ Object
returns the request cookies minus ignored cookies.
- #rewritable_request_cookies ⇒ Object
- #secure(cookie) ⇒ Object
- #set_cookie!(name, value, options) ⇒ Object
- #should_be_http_only?(cookie) ⇒ Boolean
- #should_be_secure?(cookie) ⇒ Boolean
- #ssl? ⇒ Boolean
- #stored_application_cookie_names ⇒ Object
Instance Method Details
#cache_application_cookies_string ⇒ Object
Since we have to operate on and modify the actual @headers hash that the application returns, cache the @headers string so that later on, we still know what the application did set.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/safe_cookies/helpers.rb', line 9 def = @headers['Set-Cookie'] # Rack 1.1 returns an Array = .join("\n") if .is_a?(Array) if and .length > 0 @application_cookies_string = end # else, @application_cookies_string will be `nil` end |
#cookies_have_been_rewritten_before? ⇒ Boolean
boolean
77 78 79 |
# File 'lib/safe_cookies/helpers.rb', line 77 def @request..has_key? SECURED_COOKIE_NAME end |
#http_only(cookie) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/safe_cookies/helpers.rb', line 29 def http_only() if should_be_http_only?() and !~ /(^|;\s)HttpOnly($|;)/ "#{}; HttpOnly" else end end |
#known_cookie_names ⇒ Object
68 69 70 71 72 |
# File 'lib/safe_cookies/helpers.rb', line 68 def known = [STORE_COOKIE_NAME, SECURED_COOKIE_NAME] known += known += @config..keys end |
#request_cookies ⇒ Object
returns the request cookies minus ignored cookies
55 56 57 |
# File 'lib/safe_cookies/helpers.rb', line 55 def Util.except!(@request..dup, *@config.) end |
#rewritable_request_cookies ⇒ Object
64 65 66 |
# File 'lib/safe_cookies/helpers.rb', line 64 def Util.slice(, *@config..keys) end |
#secure(cookie) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/safe_cookies/helpers.rb', line 20 def secure() # Regexp from https://github.com/tobmatth/rack-ssl-enforcer/ if should_be_secure?() and !~ /(^|;\s)secure($|;)/ "#{}; secure" else end end |
#set_cookie!(name, value, options) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/safe_cookies/helpers.rb', line 37 def (name, value, ) = .dup expire_after = .delete(:expire_after) [:expires] = Time.now + expire_after if expire_after [:path] = '/' unless .has_key?(:path) # allow setting path = nil [:value] = value [:secure] = should_be_secure?(name) [:httponly] = should_be_http_only?(name) # Rack magic Rack::Utils.(@headers, name, ) end |
#should_be_http_only?(cookie) ⇒ Boolean
95 96 97 98 |
# File 'lib/safe_cookies/helpers.rb', line 95 def should_be_http_only?() = .split('=').first.strip not @config.() end |
#should_be_secure?(cookie) ⇒ Boolean
81 82 83 84 |
# File 'lib/safe_cookies/helpers.rb', line 81 def should_be_secure?() = .split('=').first.strip ssl? and not @config.() end |
#ssl? ⇒ Boolean
86 87 88 89 90 91 92 93 |
# File 'lib/safe_cookies/helpers.rb', line 86 def ssl? if @request.respond_to?(:ssl?) @request.ssl? else # older Rack versions @request.scheme == 'https' end end |
#stored_application_cookie_names ⇒ Object
59 60 61 62 |
# File 'lib/safe_cookies/helpers.rb', line 59 def = @request.[STORE_COOKIE_NAME] || "" .split(KNOWN_COOKIES_DIVIDER) end |