Class: Vodka::Server::Middleware::SignedRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/vodka/server/middleware/signed_request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ SignedRequest

Returns a new instance of SignedRequest.



7
8
9
# File 'lib/vodka/server/middleware/signed_request.rb', line 7

def initialize(app, options = {})
  @app, @options = app, options
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



5
6
7
# File 'lib/vodka/server/middleware/signed_request.rb', line 5

def app
  @app
end

#envObject (readonly)

Returns the value of attribute env.



5
6
7
# File 'lib/vodka/server/middleware/signed_request.rb', line 5

def env
  @env
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/vodka/server/middleware/signed_request.rb', line 11

def call(env)
  @env = env

  return app.call(env) if Vodka::Server.config.perform_request_signing == false
  return app.call(env) unless env['REQUEST_PATH'].start_with?(Vodka::Server.config.prefix)

  request_signature_valid? ? app.call(env) : forbidden
end

#expected_request_signatureObject



32
33
34
# File 'lib/vodka/server/middleware/signed_request.rb', line 32

def expected_request_signature
  Vodka::Server.config.digest.hexdigest([request_id, Vodka::Server.config.request_secret].join)
end

#forbiddenObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/vodka/server/middleware/signed_request.rb', line 40

def forbidden
  headers = {
    'Content-Type'         => 'application/json; charset=utf-8',
    'X-Response-Id'        => request_id,
    'X-Response-Signature' => response_signature
  }
  data = {
    data: nil,
    errors: {
      vodka_error: '403 Forbidden'
    },
    metadata: {}
  }
  [403, headers, [MultiJson.dump(data)]]
end

#request_idObject



24
25
26
# File 'lib/vodka/server/middleware/signed_request.rb', line 24

def request_id
  env['HTTP_X_REQUEST_ID']
end

#request_signatureObject



28
29
30
# File 'lib/vodka/server/middleware/signed_request.rb', line 28

def request_signature
  env['HTTP_X_REQUEST_SIGNATURE']
end

#request_signature_valid?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/vodka/server/middleware/signed_request.rb', line 20

def request_signature_valid?
  request_signature == expected_request_signature
end

#response_signatureObject



36
37
38
# File 'lib/vodka/server/middleware/signed_request.rb', line 36

def response_signature
  Vodka::Server.config.digest.hexdigest([request_id, Vodka::Server.config.response_secret].join)
end