Class: Rack::Policy::CookieLimiter

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/rack/policy/cookie_limiter.rb

Overview

This is the class for limiting cookie storage on client machine.

Constant Summary collapse

"HTTP_COOKIE".freeze
"Set-Cookie".freeze
CACHE_CONTROL =
"Cache-Control".freeze
"cookie_limiter".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ CookieLimiter

Returns a new instance of CookieLimiter.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :consent_token (String)


24
25
26
# File 'lib/rack/policy/cookie_limiter.rb', line 24

def initialize(app, options={})
  @app, @options = app, options
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



14
15
16
# File 'lib/rack/policy/cookie_limiter.rb', line 14

def app
  @app
end

#bodyObject (readonly)

HTTP message



20
21
22
# File 'lib/rack/policy/cookie_limiter.rb', line 20

def body
  @body
end

#envObject (readonly)

The environment of the request



17
18
19
# File 'lib/rack/policy/cookie_limiter.rb', line 17

def env
  @env
end

#headersObject (readonly)

HTTP message



20
21
22
# File 'lib/rack/policy/cookie_limiter.rb', line 20

def headers
  @headers
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/rack/policy/cookie_limiter.rb', line 14

def options
  @options
end

#statusObject (readonly)

HTTP message



20
21
22
# File 'lib/rack/policy/cookie_limiter.rb', line 20

def status
  @status
end

Instance Method Details

#accepts?(request) ⇒ Boolean

Identifies the approval of cookie policy inside rack app.

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
# File 'lib/rack/policy/cookie_limiter.rb', line 52

def accepts?(request)
  if ( request.cookies.has_key?(consent_token.to_s) )
    @env['rack-policy.consent'] = 'true'
  else
    @env.delete(HTTP_COOKIE) if @env[HTTP_COOKIE]
    @env['rack-policy.consent'] = nil
  end
end

#allowed?(request) ⇒ Boolean

Returns ‘false` if the cookie policy disallows cookie storage for a given request, or `true` otherwise.

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
# File 'lib/rack/policy/cookie_limiter.rb', line 64

def allowed?(request)
  if ( request.cookies.has_key?(consent_token.to_s) ||
       parse_cookies.has_key?(consent_token.to_s) )
    true
  else
    false
  end
end

#call(env) ⇒ Object



36
37
38
# File 'lib/rack/policy/cookie_limiter.rb', line 36

def call(env)
  dup.call!(env)
end

#call!(env) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/rack/policy/cookie_limiter.rb', line 40

def call!(env)
  @env = env
  request = Rack::Request.new(env)
  accepts?(request)
  @status, @headers, @body = @app.call(env)
  response = Rack::Response.new body, status, headers
  clear_cookies!(request, response) unless allowed?(request)
  finish
end


28
29
30
# File 'lib/rack/policy/cookie_limiter.rb', line 28

def consent_token
  @consent_token ||= options[:consent_token] || CONSENT_TOKEN
end

#expiresObject



32
33
34
# File 'lib/rack/policy/cookie_limiter.rb', line 32

def expires
  Time.parse(options[:expires]) if options[:expires]
end

#finishObject

Finish http response with proper headers



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rack/policy/cookie_limiter.rb', line 74

def finish
  if [204, 304].include?(status.to_i) || (status.to_i / 100 == 1)
    headers.delete "Content-Length"
    headers.delete "Content-Type"
    [status.to_i, headers, []]
  elsif env['REQUEST_METHOD'] == 'HEAD'
    [status.to_i, headers, []]
  else
    [status.to_i, headers, body]
  end
end