Class: CohesiveMarketplaceMiddleware::AuthMiddleware

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ void

Initializes a new instance of the middleware.

Parameters:

  • app (Object)

    The application object.



36
37
38
39
# File 'lib/cohesive_marketplace_middleware.rb', line 36

def initialize(app)
  @app = app
  @ignore_paths = CohesiveMarketplaceMiddleware.collect_ignore_paths
end

Instance Method Details

#call(env) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cohesive_marketplace_middleware.rb', line 41

def call(env)
  # Check if the current path should be ignored.
  if !(@ignore_paths.any? { |prefix| env["REQUEST_PATH"] && env["REQUEST_PATH"].start_with?(prefix) })
    authorization_header = env["HTTP_AUTHORIZATION"]
    if authorization_header&.start_with?("Bearer ")
      token = authorization_header.sub("Bearer ", "")
      begin
        env[AUTH_DETAILS_ENV_KEY] = CohesiveMarketplaceSDK.validate_jwt token
      rescue => exception
        puts exception
        return [401, {"Content-Type" => "text/plain"}, [exception.message]]
      end
    else
      return [401, {"Content-Type" => "text/plain"}, ["No Token in auth header"]]
    end
  end
  @app.call(env)
end