verikloak-bff 0.2.1 → 0.2.3

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: 255d2d606e30f92f8c562d91d975be06975a7ba373bd607aa5057e5abcc279a6
4
- data.tar.gz: 6f81b6407f5a1a6e5d307cb65981f48e12c5356ec411ba05d21829681bedb084
3
+ metadata.gz: 2e2d5b0966fddd564dcd1b6e48fb871f3239542204176f0291f4249f5770b713
4
+ data.tar.gz: c1f1c43a58c7428263757d2f051c3f988921d84b30fc5fbdc8995f0c5d55ef4c
5
5
  SHA512:
6
- metadata.gz: 995250cf2096de074988c0a00d4c2de0ff4b03c798089acfd0f0e0e5e3c0cdc3717da82f4f85be74db68fe40ced3cda5c27ae9f14ae41385a38a24e2eb9c4814
7
- data.tar.gz: 157ddfbc41d48b9903b59f732ef8128a4d27127fee098d4a6e2388ca713ce15a749acad287b4995f87515d70233f74d4f44f7ac9d289de34d548d26bef3e9d17
6
+ metadata.gz: a4c8c80a0fccd793d93b2ef98374fd0bca253d5a36ff4d690dbd67de622bebd92ad27ef141d3c65c56ddd8c1934b9e9818a51eb3ef0a40b780ad0bc491263503
7
+ data.tar.gz: 339f5c58833ebb8ef19b511725c3f540e7506f097032235852088e0594dc917702723f8084f1abbdfd050b41f738486665d1c8b93891fb9fc7293c31d6e0483d
data/CHANGELOG.md CHANGED
@@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ---
9
9
 
10
+ ## [0.2.3] - 2025-09-23
11
+
12
+ ### Changed
13
+ - Stop inserting `Verikloak::BFF::HeaderGuard` automatically via the Railtie and provide a `rails g verikloak:bff:install` generator that drops an initializer to opt in when the core middleware is ready.
14
+
15
+ ## [0.2.2] - 2025-09-23
16
+
17
+ ### Changed
18
+ - Improved middleware class extraction logic to reduce code duplication while maintaining functionality
19
+
10
20
  ## [0.2.1] - 2025-09-23
11
21
 
12
22
  ### Fixed
data/README.md CHANGED
@@ -31,12 +31,16 @@ use Verikloak::BFF::HeaderGuard, trusted_proxies: ['127.0.0.1', '10.0.0.0/8']
31
31
  ```
32
32
 
33
33
  ### Rails Applications
34
- Simply add to your Gemfile and the middleware will be automatically integrated:
34
+ Add the gem to your Gemfile and run the install generator to drop an initializer that wires the middleware into Rails:
35
35
  ```ruby
36
36
  gem 'verikloak-bff'
37
37
  ```
38
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.
39
+ ```sh
40
+ bin/rails g verikloak:bff:install
41
+ ```
42
+
43
+ The generated initializer inserts `Verikloak::BFF::HeaderGuard` after the core `Verikloak::Middleware` during boot. If the core middleware is not present (for example, when verikloak-rails has not been fully configured yet), the initializer logs a warning and allows Rails to boot normally.
40
44
 
41
45
  For detailed configuration, proxy setup examples, and troubleshooting, see [docs/rails.md](docs/rails.md).
42
46
 
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators/base'
4
+
5
+ module Verikloak
6
+ module BFF
7
+ # Namespace for Rails generators related to Verikloak BFF
8
+ module Generators
9
+ # Rails generator for installing Verikloak BFF middleware integration.
10
+ #
11
+ # This generator creates a Rails initializer that safely inserts the
12
+ # Verikloak::BFF::HeaderGuard middleware into the Rails middleware stack.
13
+ # It replaces automatic middleware insertion to avoid boot failures when
14
+ # core Verikloak middleware is not yet configured.
15
+ #
16
+ # @example Basic usage
17
+ # rails g verikloak:bff:install
18
+ #
19
+ # @example Custom initializer path
20
+ # rails g verikloak:bff:install --initializer=config/initializers/custom_bff.rb
21
+ #
22
+ # @see Verikloak::BFF::Rails::Middleware
23
+ class InstallGenerator < ::Rails::Generators::Base
24
+ source_root File.expand_path('templates', __dir__)
25
+
26
+ # Configuration option for specifying the initializer file path.
27
+ #
28
+ # @option options [String] :initializer ('config/initializers/verikloak_bff.rb')
29
+ # The path where the initializer file will be created
30
+ class_option :initializer, type: :string,
31
+ default: 'config/initializers/verikloak_bff.rb',
32
+ desc: 'Path for the generated initializer'
33
+
34
+ # Creates the Rails initializer file from template.
35
+ #
36
+ # Generates a Rails initializer that safely inserts the HeaderGuard
37
+ # middleware into the middleware stack with proper error handling.
38
+ # The initializer uses Verikloak::BFF::Rails::Middleware.insert_after_core
39
+ # to ensure graceful handling when core middleware is missing.
40
+ #
41
+ # @return [void]
42
+ def create_initializer
43
+ template 'initializer.rb.tt', initializer_path
44
+ end
45
+
46
+ private
47
+
48
+ # Returns the path where the initializer should be created.
49
+ #
50
+ # @return [String] The initializer file path from options
51
+ def initializer_path
52
+ options[:initializer]
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -29,6 +29,13 @@ module Verikloak
29
29
  # logger: Rails.logger
30
30
  # )
31
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
+
32
39
  stack.insert_after(::Verikloak::Middleware, ::Verikloak::BFF::HeaderGuard)
33
40
  true
34
41
  rescue RuntimeError => e
@@ -38,6 +45,79 @@ module Verikloak
38
45
  false
39
46
  end
40
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
+
41
121
  # Checks if the error indicates missing core Verikloak middleware
42
122
  #
43
123
  # Examines a RuntimeError to determine if it was caused by attempting
@@ -75,11 +155,7 @@ module Verikloak
75
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.
76
156
  MSG
77
157
 
78
- if logger
79
- logger.warn(message)
80
- else
81
- warn(message)
82
- end
158
+ logger ? logger.warn(message) : warn(message)
83
159
  end
84
160
  end
85
161
  end
@@ -5,29 +5,27 @@ require_relative 'rails'
5
5
  module Verikloak
6
6
  # Module providing Verikloak BFF (Backend for Frontend) functionality
7
7
  module BFF
8
- # Railtie class for integrating Verikloak BFF middleware into Rails applications
8
+ # Railtie for integrating Verikloak BFF with Rails applications.
9
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.
10
+ # This Railtie provides access to the BFF installation generator instead of
11
+ # automatically inserting middleware, which could cause boot failures when
12
+ # core Verikloak middleware is not yet configured.
13
13
  #
14
- # @example Automatic initialization in Rails applications
15
- # # Simply adding verikloak-bff to Gemfile automatically enables it
16
- # gem 'verikloak-bff'
14
+ # @example Installing BFF middleware
15
+ # rails g verikloak:bff:install
17
16
  #
18
17
  # @see Verikloak::BFF::Rails::Middleware
19
18
  class Railtie < ::Rails::Railtie
20
- # Initializer that inserts Verikloak BFF middleware into Rails application
19
+ # Loads the install generator when Rails generator infrastructure is available.
21
20
  #
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.
21
+ # Makes the `verikloak:bff:install` generator discoverable through `rails g`
22
+ # while keeping generators optional for non-Rails environments.
25
23
  #
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)
24
+ # @return [void]
25
+ initializer 'verikloak.bff.load_generators' do
26
+ next unless defined?(Rails::Generators)
29
27
 
30
- Verikloak::BFF::Rails::Middleware.insert_after_core(app.config.middleware, logger: logger)
28
+ require_relative '../../generators/verikloak/bff/install/install_generator'
31
29
  end
32
30
  end
33
31
  end
@@ -5,6 +5,6 @@
5
5
  # @return [String]
6
6
  module Verikloak
7
7
  module BFF
8
- VERSION = '0.2.1'
8
+ VERSION = '0.2.3'
9
9
  end
10
10
  end
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.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - taiyaky
@@ -78,6 +78,7 @@ files:
78
78
  - CHANGELOG.md
79
79
  - LICENSE
80
80
  - README.md
81
+ - lib/generators/verikloak/bff/install/install_generator.rb
81
82
  - lib/verikloak-bff.rb
82
83
  - lib/verikloak/bff.rb
83
84
  - lib/verikloak/bff/configuration.rb
@@ -98,7 +99,7 @@ metadata:
98
99
  source_code_uri: https://github.com/taiyaky/verikloak-bff
99
100
  changelog_uri: https://github.com/taiyaky/verikloak-bff/blob/main/CHANGELOG.md
100
101
  bug_tracker_uri: https://github.com/taiyaky/verikloak-bff/issues
101
- documentation_uri: https://rubydoc.info/gems/verikloak-bff/0.2.1
102
+ documentation_uri: https://rubydoc.info/gems/verikloak-bff/0.2.3
102
103
  rubygems_mfa_required: 'true'
103
104
  rdoc_options: []
104
105
  require_paths: