Class: Rack::Fernet

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/fernet.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, secret, content_type = "application/json") ⇒ Fernet

Returns a new instance of Fernet.



26
27
28
29
30
# File 'lib/rack/fernet.rb', line 26

def initialize(app, secret, content_type="application/json")
  @app = app
  @secret = secret
  @content_type = content_type
end

Instance Method Details

#call(env) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rack/fernet.rb', line 32

def call(env)
  payload = env["rack.input"].read
  env["CONTENT_TYPE"] = @content_type

  unless payload.empty?
    payload = decrypt_request(env, payload)
    env["rack.input"] = StringIO.new(payload)
  end

  status, headers, body = @app.call(env)
  str_body = read_body(body)
  unless str_body.empty?
    encoded = encrypt_response(env, str_body)
    headers['Content-Type'] = 'application/octet-stream'
    headers['Content-Length'] = encoded.length
    body = [ encoded ]
  end
  [status, headers, body]
rescue ::Fernet::Error
  bad_request
end