Class: KeycloakApiRails::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/keycloak-api-rails/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



6
7
8
# File 'lib/keycloak-api-rails/middleware.rb', line 6

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/keycloak-api-rails/middleware.rb', line 10

def call(env)
  method = env["REQUEST_METHOD"]
  path   = env["PATH_INFO"]

  if service.need_middleware_authentication?(method, path, env)
    logger.debug("Start authentication for #{method} : #{path}")
    begin
      authenticate(env)
    rescue TokenError => e
      logger.debug("The error causing the Token to fail: #{e.original_error&.message || e.message}")
      return authentication_failed(e.message)
    rescue HTTPError, MissingPublicKeysError => e
      logger.error("KeycloakApiRails: no token can be verified for #{method} : #{path}. #{e.class}: #{e.message}")
      return authentication_unavailable
    end
  else
    logger.debug("Skip authentication for #{method} : #{path}")
  end

  @app.call(env)
end