|
12 | 12 | ERR |
13 | 13 | end |
14 | 14 |
|
| 15 | +## |
| 16 | +# Cookies provide a convenient way to store small amounts of data on the client side that persists across requests. |
| 17 | +# They are commonly used for session management, personalization, and tracking user preferences. |
| 18 | +# |
| 19 | +# Rage cookies support both simple string-based cookies and encrypted cookies for sensitive data. |
| 20 | +# |
| 21 | +# To use cookies, add the `domain_name` gem to your `Gemfile`: |
| 22 | +# |
| 23 | +# ```bash |
| 24 | +# bundle add domain_name |
| 25 | +# ``` |
| 26 | +# |
| 27 | +# Additionally, if you need to use encrypted cookies, see {Session} for setup steps. |
| 28 | +# |
| 29 | +# ## Usage |
| 30 | +# |
| 31 | +# ### Basic Cookies |
| 32 | +# |
| 33 | +# Read and write simple string values: |
| 34 | +# |
| 35 | +# ```ruby |
| 36 | +# # Set a cookie |
| 37 | +# cookies[:user_name] = "Alice" |
| 38 | +# |
| 39 | +# # Read a cookie |
| 40 | +# cookies[:user_name] # => "Alice" |
| 41 | +# |
| 42 | +# # Delete a cookie |
| 43 | +# cookies.delete(:user_name) |
| 44 | +# ``` |
| 45 | +# |
| 46 | +# ### Cookie Options |
| 47 | +# |
| 48 | +# Set cookies with additional options for security and control: |
| 49 | +# |
| 50 | +# ```ruby |
| 51 | +# cookies[:user_id] = { |
| 52 | +# value: "12345", |
| 53 | +# expires: 1.year.from_now, |
| 54 | +# secure: true, |
| 55 | +# httponly: true, |
| 56 | +# same_site: :lax |
| 57 | +# } |
| 58 | +# ``` |
| 59 | +# |
| 60 | +# ### Encrypted Cookies |
| 61 | +# |
| 62 | +# Store sensitive data securely with automatic encryption: |
| 63 | +# |
| 64 | +# ```ruby |
| 65 | +# # Set an encrypted cookie |
| 66 | +# cookies.encrypted[:api_token] = "secret-token" |
| 67 | +# |
| 68 | +# # Read an encrypted cookie |
| 69 | +# cookies.encrypted[:api_token] # => "secret-token" |
| 70 | +# |
| 71 | +# ``` |
| 72 | +# |
| 73 | +# ### Permanent Cookies |
| 74 | +# |
| 75 | +# Create cookies that expire 20 years from now: |
| 76 | +# |
| 77 | +# ```ruby |
| 78 | +# cookies.permanent[:remember_token] = "token-value" |
| 79 | +# |
| 80 | +# # Can be combined with encrypted |
| 81 | +# cookies.permanent.encrypted[:user_id] = current_user.id |
| 82 | +# ``` |
| 83 | +# |
| 84 | +# ### Domain Configuration |
| 85 | +# |
| 86 | +# Control which domains can access your cookies: |
| 87 | +# |
| 88 | +# ```ruby |
| 89 | +# # Specific domain |
| 90 | +# cookies[:cross_domain] = { value: "data", domain: "example.com" } |
| 91 | +# |
| 92 | +# # All subdomains |
| 93 | +# cookies[:shared] = { value: "data", domain: :all } |
| 94 | +# |
| 95 | +# # Multiple allowed domains |
| 96 | +# cookies[:limited] = { value: "data", domain: ["app.example.com", "api.example.com"] } |
| 97 | +# ``` |
| 98 | +# |
| 99 | +# @see Session |
| 100 | +# |
15 | 101 | class Rage::Cookies |
16 | 102 | # @private |
17 | 103 | def initialize(env, headers) |
@@ -190,10 +276,13 @@ def load(value) |
190 | 276 | begin |
191 | 277 | box.decrypt(Base64.urlsafe_decode64(value).byteslice(2..)) |
192 | 278 | rescue ArgumentError |
| 279 | + Rage.logger.debug("Failed to decode encrypted cookie") |
193 | 280 | nil |
194 | 281 | rescue RbNaCl::CryptoError |
| 282 | + Rage.logger.debug("Failed to decrypt encrypted cookie") |
195 | 283 | i ||= 0 |
196 | 284 | if (box = fallback_boxes[i]) |
| 285 | + Rage.logger.debug("Trying to decrypt with fallback key ##{i + 1}") |
197 | 286 | i += 1 |
198 | 287 | retry |
199 | 288 | end |
|
0 commit comments