Skip to content

Commit

Permalink
Add a way to identify cookie acceptance inside rack app.
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Jul 7, 2012
1 parent e115dae commit def3868
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/rack/policy/cookie_limiter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ class CookieLimiter
CONSENT_TOKEN = "cookie_limiter".freeze

attr_reader :app, :options

# The environment of the request
attr_reader :env

# HTTP message
attr_accessor :status, :headers, :body

# @option options [String] :consent_token
Expand All @@ -33,13 +38,26 @@ def call(env)
end

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

# Identifies the approval of cookie policy inside rack app.
#
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

# Returns `false` if the cookie policy disallows cookie storage
# for a given request, or `true` otherwise.
#
Expand Down Expand Up @@ -86,7 +104,6 @@ def parse_cookies
def clear_cookies!(request, response)
cookies = parse_cookies
headers.delete(SET_COOKIE)
request.env.delete(HTTP_COOKIE)
revalidate_cache!

cookies.merge(request.cookies).each do |key, value|
Expand Down

0 comments on commit def3868

Please sign in to comment.