Class: UnifiedCsrfPrevention::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/unified_csrf_prevention/middleware.rb

Overview

Rack middleware to set the token and checksum cookies See github.com/xing/cross-application-csrf-prevention#token-generation

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



9
10
11
# File 'lib/unified_csrf_prevention/middleware.rb', line 9

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/unified_csrf_prevention/middleware.rb', line 13

def call(env)
  status, headers, body = @app.call(env)

  if env.key?(Core::TOKEN_RACK_ENV_VAR)
    token = env[Core::TOKEN_RACK_ENV_VAR]
    set_csrf_cookies!(headers, token)
    Rails.logger.info("Set CSRF token: #{token}")
  end

  [status, headers, body]
end