Class: Verikloak::Audience::Middleware
- Inherits:
-
Object
- Object
- Verikloak::Audience::Middleware
- Includes:
- SkipPathMatcher
- Defined in:
- lib/verikloak/audience/middleware.rb
Overview
Rack middleware that validates audience claims according to configured profile.
Place this middleware after the core Middleware so that verified token claims are already available in the Rack env.
Instance Method Summary collapse
-
#call(env) ⇒ Array(Integer, Hash, #each)
Evaluate the request against the audience profile.
-
#initialize(app, **opts) ⇒ Middleware
constructor
A new instance of Middleware.
Constructor Details
#initialize(app, **opts) ⇒ Middleware
Returns a new instance of Middleware.
28 29 30 31 32 33 34 |
# File 'lib/verikloak/audience/middleware.rb', line 28 def initialize(app, **opts) @app = app @config = Verikloak::Audience.config.dup apply_overrides!(opts) @config.validate! unless skip_validation? compile_skip_paths(@config.skip_paths || []) end |
Instance Method Details
#call(env) ⇒ Array(Integer, Hash, #each)
Evaluate the request against the audience profile.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/verikloak/audience/middleware.rb', line 40 def call(env) # Skip audience validation for configured paths (e.g., health checks) path = env['PATH_INFO'] || env['REQUEST_PATH'] || '' return @app.call(env) if skip?(path) claims = read_claims(env) begin = Checker.ok?(claims, @config) rescue Verikloak::Audience::ConfigurationError => e return configuration_error_response(env, e) end return @app.call(env) if log_rejection(env, claims) Verikloak::ErrorResponse.build( code: 'insufficient_audience', message: "Audience not acceptable for profile #{@config.profile}", status: 403 ) end |