Class: Hanko::Rails::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/hanko/rails/middleware.rb

Overview

Rack middleware that extracts a Hanko JWT from the request cookie or Authorization: Bearer header, verifies it against the Hanko JWKS endpoint, and stores the decoded session payload in env['hanko.session'].

Paths listed in Configuration#exclude_paths are skipped entirely.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Initializes the middleware with the next Rack application.

Parameters:

  • app (#call)

    the next Rack application in the middleware stack



15
16
17
# File 'lib/hanko/rails/middleware.rb', line 15

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Array<(Integer, Hash, #each)>

Processes an incoming request, extracts and verifies the Hanko token, and forwards the request to the next middleware.

Parameters:

  • env (Hash)

    the Rack environment hash

Returns:

  • (Array<(Integer, Hash, #each)>)

    the Rack response triplet



24
25
26
27
28
29
30
31
32
33
# File 'lib/hanko/rails/middleware.rb', line 24

def call(env)
  request = Rack::Request.new(env)

  unless excluded_path?(request.path)
    token = extract_token(request)
    env['hanko.session'] = verify_token(token) if token
  end

  @app.call(env)
end