Class: ShopifyApp::SameSiteCookieMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_shopify_app/middleware/same_site_cookie_middleware.rb

Constant Summary collapse

"\n"

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ SameSiteCookieMiddleware

Returns a new instance of SameSiteCookieMiddleware.



6
7
8
# File 'lib/ruby_shopify_app/middleware/same_site_cookie_middleware.rb', line 6

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruby_shopify_app/middleware/same_site_cookie_middleware.rb', line 10

def call(env)
  status, headers, body = @app.call(env)
  user_agent = env['HTTP_USER_AGENT']

  if headers && headers['Set-Cookie'] &&
      BrowserSniffer.new(user_agent).same_site_none_compatible? &&
      ShopifyApp.configuration.enable_same_site_none &&
      Rack::Request.new(env).ssl?

    set_cookies = headers['Set-Cookie']
      .split(COOKIE_SEPARATOR)
      .compact
      .map do |cookie|
        cookie << '; Secure' unless cookie =~ /;\s*secure/i
        cookie << '; SameSite=None' if ShopifyApp.configuration.embedded_app?
        cookie
      end

    headers['Set-Cookie'] = set_cookies.join(COOKIE_SEPARATOR)
  end

  [status, headers, body]
end