Class: ActionController::RequestForgeryProtection::CookieStore

Inherits:
Object
  • Object
show all
Defined in:
actionpack/lib/action_controller/metal/request_forgery_protection.rb

Instance Method Summary collapse

Constructor Details

#initialize(cookie = :csrf_token) ⇒ CookieStore

Returns a new instance of CookieStore.


308
309
310
# File 'actionpack/lib/action_controller/metal/request_forgery_protection.rb', line 308

def initialize(cookie = :csrf_token)
  @cookie_name = cookie
end

Instance Method Details

#fetch(request) ⇒ Object


312
313
314
315
316
317
318
319
320
321
322
# File 'actionpack/lib/action_controller/metal/request_forgery_protection.rb', line 312

def fetch(request)
  contents = request.cookie_jar.encrypted[@cookie_name]
  return nil if contents.nil?

  value = JSON.parse(contents)
  return nil unless value.dig("session_id", "public_id") == request.session.id_was&.public_id

  value["token"]
rescue JSON::ParserError
  nil
end

#reset(request) ⇒ Object


335
336
337
# File 'actionpack/lib/action_controller/metal/request_forgery_protection.rb', line 335

def reset(request)
  request.cookie_jar.delete(@cookie_name)
end

#store(request, csrf_token) ⇒ Object


324
325
326
327
328
329
330
331
332
333
# File 'actionpack/lib/action_controller/metal/request_forgery_protection.rb', line 324

def store(request, csrf_token)
  request.cookie_jar.encrypted.permanent[@cookie_name] = {
    value: {
      token: csrf_token,
      session_id: request.session.id,
    }.to_json,
    httponly: true,
    same_site: :lax,
  }
end