Class: Skadi::CookieManager

Inherits:
Object
  • Object
show all
Defined in:
lib/skadi/cookie_manager.rb

Constant Summary collapse

ANONYMITY_SET_KEY =
"skadi_anonymity_set"
TRACKING_TOKEN_KEY =
"skadi_id"
TRACK_USER_KEY =
"skadi_track_user"
UUID_REGEX =
/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ CookieManager

Returns a new instance of CookieManager.



11
12
13
14
# File 'lib/skadi/cookie_manager.rb', line 11

def initialize(request)
  @request = request
  @cookies = request.cookie_jar
end

Instance Attribute Details

#cookiesActionDispatch::Cookies::CookieJar (readonly)

Returns:

  • (ActionDispatch::Cookies::CookieJar)


9
10
11
# File 'lib/skadi/cookie_manager.rb', line 9

def cookies
  @cookies
end

#requestActionDispatch::Cookies::CookieJar (readonly)

Returns:

  • (ActionDispatch::Cookies::CookieJar)


9
10
11
# File 'lib/skadi/cookie_manager.rb', line 9

def request
  @request
end

Instance Method Details

#renew!Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/skadi/cookie_manager.rb', line 16

def renew!
  set_cookie ANONYMITY_SET_KEY, cookies[ANONYMITY_SET_KEY] if [ "1", "0" ].include?(cookies[ANONYMITY_SET_KEY])
  set_cookie TRACK_USER_KEY, cookies[TRACK_USER_KEY] if [ "1", "0" ].include?(cookies[TRACK_USER_KEY])

  if cookies.has_key? TRACKING_TOKEN_KEY
    token = tracking_token
    if token
      set_cookie TRACKING_TOKEN_KEY, token
    else
      # If the key is set, but the token returned by `tracking_token()` is nil, then the cookie is malformed and we delete it
      delete_cookie TRACKING_TOKEN_KEY
    end
  end
end

#track_usersObject



40
41
42
43
# File 'lib/skadi/cookie_manager.rb', line 40

def track_users
  return true if cookies[TRACK_USER_KEY] == "1"
  return false if cookies[TRACK_USER_KEY] == "0"
end

#track_users=(new_value) ⇒ Object



45
46
47
# File 'lib/skadi/cookie_manager.rb', line 45

def track_users=(new_value)
  set_cookie TRACK_USER_KEY, new_value ? "1" : "0"
end

#tracking_tokenObject



49
50
51
52
53
54
# File 'lib/skadi/cookie_manager.rb', line 49

def tracking_token
  token = cookies[TRACKING_TOKEN_KEY]

  # The cookie is user input, so we ensure the cookie is a UUID as expected
  token&.match(UUID_REGEX) ? token : nil
end

#tracking_token=(new_value) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/skadi/cookie_manager.rb', line 56

def tracking_token=(new_value)
  if new_value.nil?
    delete_cookie(TRACKING_TOKEN_KEY)

    return
  end

  set_cookie TRACKING_TOKEN_KEY, new_value
end

#use_anonymity_setsObject



31
32
33
34
# File 'lib/skadi/cookie_manager.rb', line 31

def use_anonymity_sets
  return true if cookies[ANONYMITY_SET_KEY] == "1"
  return false if cookies[ANONYMITY_SET_KEY] == "0"
end

#use_anonymity_sets=(new_value) ⇒ Object



36
37
38
# File 'lib/skadi/cookie_manager.rb', line 36

def use_anonymity_sets=(new_value)
  set_cookie ANONYMITY_SET_KEY, new_value ? "1" : "0"
end