Class: Wix::Apps::SignedInstanceMiddleware

Inherits:
Struct
  • Object
show all
Defined in:
lib/wix-apps/signed_instance_middleware.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initializes the middleware to secure a given set of paths. Options: :secret_key - the Wix secret key as String :secured_paths (optional) - an Array of String and Regexp objects which every request’s path is matched against. Matching paths are required to pass a Wix signed instance.



9
10
11
12
13
14
# File 'lib/wix-apps/signed_instance_middleware.rb', line 9

def initialize(app, options={})
  self.app = app
  self.secret_key = options[:secret_key]
  self.secured_paths = options[:secured_paths] || []
  self.paths = options[:paths] || []
end

Instance Attribute Details

#appObject

Returns the value of attribute app

Returns:

  • (Object)

    the current value of app



3
4
5
# File 'lib/wix-apps/signed_instance_middleware.rb', line 3

def app
  @app
end

#pathsObject

Returns the value of attribute paths

Returns:

  • (Object)

    the current value of paths



3
4
5
# File 'lib/wix-apps/signed_instance_middleware.rb', line 3

def paths
  @paths
end

#secret_keyObject

Returns the value of attribute secret_key

Returns:

  • (Object)

    the current value of secret_key



3
4
5
# File 'lib/wix-apps/signed_instance_middleware.rb', line 3

def secret_key
  @secret_key
end

#secured_pathsObject

Returns the value of attribute secured_paths

Returns:

  • (Object)

    the current value of secured_paths



3
4
5
# File 'lib/wix-apps/signed_instance_middleware.rb', line 3

def secured_paths
  @secured_paths
end

Instance Method Details

#call(env) ⇒ Array

Checks current URL path for Wix instance requirement, parses given signed instance and adds GET param ‘parsed_instance’ with the instance’s parsed properties.

Parameters:

  • env (Hash)

    The current environment hash

Returns:

  • (Array)

    The typical [<code>, <headers>, <body>] rack response Array



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wix-apps/signed_instance_middleware.rb', line 19

def call(env)
  path = env['PATH_INFO']

  secured_path = secured_path? path
  if secured_path || path?(path)
    # path must be handled (instance is either required or optional)
    request = Rack::Request.new(env)

    env['wix.instance'] = nil
    if request.params.has_key? 'instance'
      # Wix' "instance" parameter was supplied so it must be parseable. parse and set it into env.
      begin
        env['wix.instance'] = Wix::Apps::SignedInstance.new(request.params['instance'], secret: secret_key)
      rescue Wix::Apps::SignedInstanceParseError
        # 403 Forbidden
        return [403, {}, ['Invalid Wix instance']]
      end
    elsif secured_path
      # instance is required but Wix' "instance" parameter is missing
      # 401 Unauthorized
      return [401, {}, ['Unauthorized']]
    end
  end
  app.call(env)
end