verikloak-bff 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 91439a22efdb87206de07fbff74c284cba33a05cb1c7392070c0b9f7c837734f
4
- data.tar.gz: be08bb99047e72502cad67dbea7c41b916aef6d49fb103e3dbf53ab79082f6db
3
+ metadata.gz: 8fbec3da2912582f3684c1682fb75b45f9d26c3a1ebf3b7a41d4a5392fbe36c1
4
+ data.tar.gz: d1b5b9f750bd3cdbbf3308f1581c8327c039f87b660cde73a799a83740f818ae
5
5
  SHA512:
6
- metadata.gz: 135054bc3b1556597924c843a834d87c46c291da4d880ed63ef611cafb9705984694062f64d25a08f8d3f932f304cfb7277c4d25f25dfb3f35ad3d83f0768e47
7
- data.tar.gz: 3358b1f9f3114e9a00a7d4edd283e5b2c81dd6166ec1a5718a581f5f83a7c1cf75d27f300da01d5dc25763f4569a50be2393203130c5a67adb00ffb1a42fc1d9
6
+ metadata.gz: 33655caab83ec29f7159264b660aadb878a61c3ca52c4f23e4256cdb30519384833d7ea8244de5c8252368227ef3999691eb1aa282d907c42e94db69fb39ecb3
7
+ data.tar.gz: d1442646ce08bd0a98ca211f781f0ba623d58d51323ed86492c01cee60086a27c5f27c1fd8d65e4b7ab5ea10c52a9bf6b4942e09c0be80cc8e4d250d6bfb4f48
data/CHANGELOG.md CHANGED
@@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ---
9
9
 
10
+ ## [0.2.2] - 2025-09-23
11
+
12
+ ### Changed
13
+ - Improved middleware class extraction logic to reduce code duplication while maintaining functionality
14
+
15
+ ## [0.2.1] - 2025-09-23
16
+
17
+ ### Fixed
18
+ - Skip inserting `Verikloak::BFF::HeaderGuard` in Rails when `Verikloak::Middleware` is absent (e.g., discovery not configured)
19
+ so that generators and boot sequences no longer fail.
20
+
10
21
  ## [0.2.0] - 2025-09-22
11
22
 
12
23
  ### Added
data/README.md CHANGED
@@ -23,8 +23,22 @@ bundle add verikloak-bff
23
23
 
24
24
  ## Usage
25
25
 
26
- - Rack-only apps: `use Verikloak::BFF::HeaderGuard` before your core Verikloak middleware.
27
- - Rails apps: see the full guide in [docs/rails.md](docs/rails.md) (Gemfile, middleware order、initializer、proxy examples)。
26
+ ### Rack Applications
27
+ Add to your `config.ru`:
28
+ ```ruby
29
+ use Verikloak::BFF::HeaderGuard, trusted_proxies: ['127.0.0.1', '10.0.0.0/8']
30
+ # Place before your core Verikloak middleware
31
+ ```
32
+
33
+ ### Rails Applications
34
+ Simply add to your Gemfile and the middleware will be automatically integrated:
35
+ ```ruby
36
+ gem 'verikloak-bff'
37
+ ```
38
+
39
+ The gem automatically inserts `Verikloak::BFF::HeaderGuard` into the Rails middleware stack after the core `Verikloak::Middleware`. If the core middleware is not present (e.g., discovery not configured), it gracefully skips insertion with a warning, allowing Rails to boot normally.
40
+
41
+ For detailed configuration, proxy setup examples, and troubleshooting, see [docs/rails.md](docs/rails.md).
28
42
 
29
43
  ## Consistency mapping
30
44
 
@@ -0,0 +1,163 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Verikloak
4
+ module BFF
5
+ # Rails-specific functionality for Verikloak BFF
6
+ module Rails
7
+ # Middleware management utilities for Rails applications
8
+ #
9
+ # This module provides functionality to insert Verikloak BFF middleware
10
+ # into Rails middleware stack, with proper error handling for cases where
11
+ # the core Verikloak middleware is not present.
12
+ module Middleware
13
+ module_function
14
+
15
+ # Inserts Verikloak::BFF::HeaderGuard middleware after Verikloak::Middleware
16
+ #
17
+ # Attempts to insert the HeaderGuard middleware into the Rails middleware stack
18
+ # after the core Verikloak::Middleware. If the core middleware is not present,
19
+ # logs a warning and gracefully skips the insertion.
20
+ #
21
+ # @param stack [ActionDispatch::MiddlewareStack] Rails middleware stack
22
+ # @param logger [Logger, nil] Optional logger for warning messages
23
+ # @return [Boolean] true if insertion succeeded, false if skipped due to missing core
24
+ # @raise [RuntimeError] Re-raises non-middleware-related runtime errors
25
+ #
26
+ # @example Inserting middleware in Rails configuration
27
+ # Verikloak::BFF::Rails::Middleware.insert_after_core(
28
+ # Rails.application.config.middleware,
29
+ # logger: Rails.logger
30
+ # )
31
+ def insert_after_core(stack, logger: nil)
32
+ return false unless auto_insert_enabled?
33
+
34
+ unless core_present?(stack)
35
+ log_skip(logger)
36
+ return false
37
+ end
38
+
39
+ stack.insert_after(::Verikloak::Middleware, ::Verikloak::BFF::HeaderGuard)
40
+ true
41
+ rescue RuntimeError => e
42
+ raise unless missing_core?(e)
43
+
44
+ log_skip(logger)
45
+ false
46
+ end
47
+
48
+ # Determine whether automatic insertion is enabled via Verikloak core configuration.
49
+ #
50
+ # When the core gem exposes +auto_insert_bff_header_guard+, respect that flag so
51
+ # consumers can opt out of automatic middleware wiring without triggering warnings.
52
+ # Any failures while reading configuration default to enabling insertion in order
53
+ # to preserve the previous behavior.
54
+ #
55
+ # @return [Boolean]
56
+ def auto_insert_enabled?
57
+ return true unless defined?(::Verikloak)
58
+ return true unless ::Verikloak.respond_to?(:config)
59
+
60
+ config = ::Verikloak.config
61
+ return true unless config
62
+ return config.auto_insert_bff_header_guard if config.respond_to?(:auto_insert_bff_header_guard)
63
+
64
+ true
65
+ rescue StandardError
66
+ true
67
+ end
68
+
69
+ # Detect whether the Verikloak core middleware is already present in the stack.
70
+ #
71
+ # @param stack [#include?, #each, nil]
72
+ # @return [Boolean]
73
+ def core_present?(stack)
74
+ return false unless stack
75
+
76
+ if stack.respond_to?(:include?)
77
+ begin
78
+ return true if stack.include?(::Verikloak::Middleware)
79
+ rescue StandardError
80
+ # Fall back to manual enumeration when include? is unsupported for this stack
81
+ end
82
+ end
83
+
84
+ return false unless stack.respond_to?(:each)
85
+
86
+ stack.each do |middleware|
87
+ return true if middleware_matches_core?(middleware)
88
+ end
89
+
90
+ false
91
+ end
92
+
93
+ # Check whether a middleware entry represents the Verikloak core middleware.
94
+ #
95
+ # @param middleware [Object]
96
+ # @return [Boolean]
97
+ def middleware_matches_core?(middleware)
98
+ candidate = middleware.is_a?(Array) ? middleware.first : middleware
99
+
100
+ klass = extract_middleware_class(candidate)
101
+
102
+ klass == ::Verikloak::Middleware ||
103
+ (klass.is_a?(String) && klass == 'Verikloak::Middleware') ||
104
+ (klass.respond_to?(:name) && klass.name == 'Verikloak::Middleware')
105
+ end
106
+
107
+ # Extracts the class or class-like identifier from a middleware candidate.
108
+ #
109
+ # @param candidate [Object]
110
+ # @return [Class, String, Object]
111
+ def extract_middleware_class(candidate)
112
+ if candidate.respond_to?(:klass)
113
+ candidate.klass
114
+ elsif candidate.respond_to?(:name)
115
+ candidate.name
116
+ else
117
+ candidate
118
+ end
119
+ end
120
+
121
+ # Checks if the error indicates missing core Verikloak middleware
122
+ #
123
+ # Examines a RuntimeError to determine if it was caused by attempting
124
+ # to insert middleware after a non-existent Verikloak::Middleware.
125
+ #
126
+ # @param error [RuntimeError] The error to examine
127
+ # @return [Boolean] true if error indicates missing Verikloak::Middleware
128
+ #
129
+ # @example Checking for missing middleware error
130
+ # begin
131
+ # stack.insert_after(::Verikloak::Middleware, SomeMiddleware)
132
+ # rescue RuntimeError => e
133
+ # puts "Missing core!" if missing_core?(e)
134
+ # end
135
+ def missing_core?(error)
136
+ error.message.include?('No such middleware') &&
137
+ error.message.include?('Verikloak::Middleware')
138
+ end
139
+
140
+ # Logs a warning message about skipping middleware insertion
141
+ #
142
+ # Outputs a descriptive warning message explaining why the HeaderGuard
143
+ # middleware insertion was skipped and provides guidance for resolution.
144
+ # Uses the provided logger if available, otherwise falls back to warn().
145
+ #
146
+ # @param logger [Logger, nil] Optional logger instance for structured logging
147
+ #
148
+ # @example Logging with Rails logger
149
+ # log_skip(Rails.logger)
150
+ #
151
+ # @example Logging without logger (uses warn)
152
+ # log_skip(nil)
153
+ def log_skip(logger)
154
+ message = <<~MSG.chomp
155
+ [verikloak-bff] Skipping Verikloak::BFF::HeaderGuard insertion because Verikloak::Middleware is not present. Configure verikloak-rails discovery settings and restart once core verification is enabled.
156
+ MSG
157
+
158
+ logger ? logger.warn(message) : warn(message)
159
+ end
160
+ end
161
+ end
162
+ end
163
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'rails'
4
+
5
+ module Verikloak
6
+ # Module providing Verikloak BFF (Backend for Frontend) functionality
7
+ module BFF
8
+ # Railtie class for integrating Verikloak BFF middleware into Rails applications
9
+ #
10
+ # This class automatically inserts Verikloak::BFF::Rails::Middleware into the
11
+ # Rails initialization process. The middleware is inserted after the
12
+ # 'verikloak.middleware' initializer and configured with an appropriate logger.
13
+ #
14
+ # @example Automatic initialization in Rails applications
15
+ # # Simply adding verikloak-bff to Gemfile automatically enables it
16
+ # gem 'verikloak-bff'
17
+ #
18
+ # @see Verikloak::BFF::Rails::Middleware
19
+ class Railtie < ::Rails::Railtie
20
+ # Initializer that inserts Verikloak BFF middleware into Rails application
21
+ #
22
+ # This initializer runs after 'verikloak.middleware' and inserts
23
+ # Verikloak::BFF::Rails::Middleware at the appropriate position.
24
+ # Uses Rails.logger as the logger if available.
25
+ #
26
+ # @param app [Rails::Application] Rails application instance
27
+ initializer 'verikloak.bff.insert_middleware', after: 'verikloak.middleware' do |app|
28
+ logger = ::Rails.logger if defined?(::Rails.logger)
29
+
30
+ Verikloak::BFF::Rails::Middleware.insert_after_core(app.config.middleware, logger: logger)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -5,6 +5,6 @@
5
5
  # @return [String]
6
6
  module Verikloak
7
7
  module BFF
8
- VERSION = '0.2.0'
8
+ VERSION = '0.2.2'
9
9
  end
10
10
  end
data/lib/verikloak-bff.rb CHANGED
@@ -18,3 +18,6 @@ require 'verikloak/bff/proxy_trust'
18
18
  require 'verikloak/bff/forwarded_token'
19
19
  require 'verikloak/bff/consistency_checks'
20
20
  require 'verikloak/bff/header_guard'
21
+ require 'verikloak/bff/rails'
22
+
23
+ require 'verikloak/bff/railtie' if defined?(Rails::Railtie)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: verikloak-bff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - taiyaky
@@ -55,7 +55,7 @@ dependencies:
55
55
  requirements:
56
56
  - - ">="
57
57
  - !ruby/object:Gem::Version
58
- version: 0.1.5
58
+ version: 0.2.0
59
59
  - - "<"
60
60
  - !ruby/object:Gem::Version
61
61
  version: 1.0.0
@@ -65,7 +65,7 @@ dependencies:
65
65
  requirements:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: 0.1.5
68
+ version: 0.2.0
69
69
  - - "<"
70
70
  - !ruby/object:Gem::Version
71
71
  version: 1.0.0
@@ -87,6 +87,8 @@ files:
87
87
  - lib/verikloak/bff/forwarded_token.rb
88
88
  - lib/verikloak/bff/header_guard.rb
89
89
  - lib/verikloak/bff/proxy_trust.rb
90
+ - lib/verikloak/bff/rails.rb
91
+ - lib/verikloak/bff/railtie.rb
90
92
  - lib/verikloak/bff/version.rb
91
93
  - lib/verikloak/header_sources.rb
92
94
  homepage: https://github.com/taiyaky/verikloak-bff
@@ -96,7 +98,7 @@ metadata:
96
98
  source_code_uri: https://github.com/taiyaky/verikloak-bff
97
99
  changelog_uri: https://github.com/taiyaky/verikloak-bff/blob/main/CHANGELOG.md
98
100
  bug_tracker_uri: https://github.com/taiyaky/verikloak-bff/issues
99
- documentation_uri: https://rubydoc.info/gems/verikloak-bff/0.2.0
101
+ documentation_uri: https://rubydoc.info/gems/verikloak-bff/0.2.2
100
102
  rubygems_mfa_required: 'true'
101
103
  rdoc_options: []
102
104
  require_paths: