Class: JWTSignedRequest::Middlewares::Rack

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

Constant Summary collapse

UNAUTHORIZED_STATUS_CODE =
401

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Rack.



9
10
11
12
13
14
15
# File 'lib/jwt_signed_request/middlewares/rack.rb', line 9

def initialize(app, options = {})
  @app = app
  @secret_key = options[:secret_key]
  @algorithm = options[:algorithm]
  @leeway = options[:leeway]
  @exclude_paths = options[:exclude_paths]
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/jwt_signed_request/middlewares/rack.rb', line 17

def call(env)
  begin
    unless excluded_path?(env)
      args = {
        request: ::Rack::Request.new(env),
        secret_key: secret_key,
        algorithm: algorithm,
        leeway: leeway
      }.reject { |_, value| value.nil? }

      ::JWTSignedRequest.verify(**args)
    end

    app.call(env)
  rescue ::JWTSignedRequest::UnauthorizedRequestError => e
    [UNAUTHORIZED_STATUS_CODE, {'Content-Type' => 'application/json'} , []]
  end
end