verikloak-bff 0.2.4 → 0.2.6

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: 459eed20d30ee0052dda42e6e1b18c4ac4f58aa897ba24a0090d19931a28690e
4
- data.tar.gz: 20d3f106c2439d3f63b683a79f900d9c072893ec730d9d294e610817b560777e
3
+ metadata.gz: edf579dc8e103482358c6ef7577c90c0b4aa24ae53b31c0c82bf89e5ffb4a68a
4
+ data.tar.gz: b976af45f7ffef721bd69263ed29cafaea1fdd711a1f94fff75a0045af8fc648
5
5
  SHA512:
6
- metadata.gz: 47838af8ec9b12a4570e967c188ffcaf7a4ce9556aa06409d6fce1906bc4fd0b16568342baacec85a46883f9161fe015459996453e724e37c7c6a6d7e339e1c8
7
- data.tar.gz: b12bcca4f52ba05a8aba8fe145c1664743e70127120642f7bf334aa8db1499301badd988d5a92040d359fc2e61f94d1bbf79699f972fc7f05c12867702a10e72
6
+ metadata.gz: 2354503e6faf4768d680057ae347e44d5e0eadfe29ef49f01585a0db859addcb53f881e49f73956e07721799534ac444c2ae13f6c9ac93983256d4d61bdaad0d
7
+ data.tar.gz: 6543a6dd768d1cd2d11fe71a976dcbbd52bd1ad40021177a7b798dfd4b20b9560384b13cc1f1adb861b61865f5a076b746999d7939b225b65a54bda2249106dd
data/CHANGELOG.md CHANGED
@@ -7,6 +7,29 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ---
9
9
 
10
+ ## [0.2.6] - 2025-12-31
11
+
12
+ ### Fixed
13
+ - **Rails 8.x+ compatibility**: Remove `after_initialize` middleware insertion from generator template to avoid `FrozenError` when middleware stack is frozen.
14
+
15
+ ### Changed
16
+ - Generator (`rails g verikloak:bff:install`) now creates a **configuration-only** initializer. Middleware insertion is handled automatically by `verikloak-rails`.
17
+ - Generated initializer includes comprehensive configuration options with documentation comments.
18
+ - **Breaking**: Minimum `verikloak` dependency raised from `>= 0.2.0` to `>= 0.3.0`.
19
+
20
+ ### Documentation
21
+ - Add "Rails Integration" section explaining automatic middleware detection with `verikloak-rails`.
22
+ - Add warning about Rails 8.x+ middleware stack freeze in `after_initialize`.
23
+ - Add "oauth2-proxy Integration" section with header configuration reference and recommended settings.
24
+ - Document manual middleware setup option for users not using `verikloak-rails`.
25
+ - Update `docs/rails.md` with clearer setup instructions and Rails 8.x support note.
26
+
27
+ ## [0.2.5] - 2025-09-28
28
+
29
+ ### Changed
30
+ - Align the install generator under `Verikloak::Bff::Generators` while retaining the `Verikloak::BFF::Generators` alias to avoid constant redefinition warnings during reloads.
31
+
32
+
10
33
  ## [0.2.4] - 2025-09-27
11
34
 
12
35
  ### Changed
data/README.md CHANGED
@@ -31,16 +31,25 @@ use Verikloak::BFF::HeaderGuard, trusted_proxies: ['127.0.0.1', '10.0.0.0/8']
31
31
  ```
32
32
 
33
33
  ### Rails Applications
34
- Add the gem to your Gemfile and run the install generator to drop an initializer that wires the middleware into Rails:
34
+ Add both gems to your Gemfile:
35
35
  ```ruby
36
+ gem 'verikloak-rails'
36
37
  gem 'verikloak-bff'
37
38
  ```
38
39
 
40
+ Run the install generator to create a configuration file:
39
41
  ```sh
40
42
  bin/rails g verikloak:bff:install
41
43
  ```
42
44
 
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.
45
+ The generator creates `config/initializers/verikloak_bff.rb` with BFF configuration options. **Edit this file to set `trusted_proxies`** (required) and customize other options as needed.
46
+
47
+ **Note**: Middleware insertion is handled automatically by `verikloak-rails`. The generated initializer only contains configuration—no manual middleware setup is required.
48
+
49
+ > **Without verikloak-rails**: You can also configure middleware manually in `config/application.rb`:
50
+ > ```ruby
51
+ > config.middleware.insert_before Verikloak::Middleware, Verikloak::BFF::HeaderGuard
52
+ > ```
44
53
 
45
54
  For detailed configuration, proxy setup examples, and troubleshooting, see [docs/rails.md](docs/rails.md).
46
55
 
@@ -110,6 +119,95 @@ For full reverse proxy examples (Nginx auth_request / oauth2-proxy), see [docs/r
110
119
  - Claims consistency modes
111
120
  - Default `:enforce` mode rejects requests with mismatches. Switch to `claims_consistency_mode: :log_only` when you only need observability signals; downstream services must continue verifying JWT signatures, issuer, audience, and expirations.
112
121
 
122
+ ## Rails Integration
123
+
124
+ ### Recommended: Use with verikloak-rails (Auto-detection)
125
+
126
+ When both `verikloak-rails` and `verikloak-bff` are installed, the railtie automatically inserts the BFF middleware. No manual configuration is required.
127
+
128
+ ```ruby
129
+ # Gemfile
130
+ gem 'verikloak-rails'
131
+ gem 'verikloak-bff'
132
+ ```
133
+
134
+ The middleware stack will be configured as:
135
+ ```
136
+ [Verikloak::BFF::HeaderGuard] → [Verikloak::Middleware] → [Your App]
137
+ ```
138
+
139
+ ### ⚠️ Rails 8.x+ Middleware Stack Freeze
140
+
141
+ In Rails 8.x and later, the middleware stack is frozen after the `after_initialize` callback. Manual middleware insertion in `after_initialize` will raise `FrozenError`:
142
+
143
+ ```ruby
144
+ # ❌ This will NOT work in Rails 8.x+
145
+ Rails.application.config.after_initialize do
146
+ Rails.application.config.middleware.insert_after(
147
+ Verikloak::Middleware,
148
+ Verikloak::BFF::HeaderGuard
149
+ )
150
+ end
151
+ # => FrozenError: can't modify frozen Array
152
+ ```
153
+
154
+ **Solution**: Use the automatic detection feature with `verikloak-rails`, or insert middleware in `config/application.rb` or an initializer (which runs before the stack is frozen):
155
+
156
+ ```ruby
157
+ # ✅ This works - config/application.rb
158
+ module MyApp
159
+ class Application < Rails::Application
160
+ config.middleware.insert_before Verikloak::Middleware, Verikloak::BFF::HeaderGuard
161
+ end
162
+ end
163
+ ```
164
+
165
+ ## oauth2-proxy Integration
166
+
167
+ ### Header Configuration Reference
168
+
169
+ | oauth2-proxy Setting | Header Sent | HeaderGuard Behavior |
170
+ |---------------------|-------------|---------------------|
171
+ | `--pass-access-token=true` | Cookie (`_oauth2_proxy`) | Not supported (cookie-based) |
172
+ | `--pass-access-token=true` + nginx | `X-Forwarded-Access-Token` | Normalized to `Authorization` (default) |
173
+ | `--set-authorization-header=true` | `Authorization: Bearer <token>` | Used directly |
174
+ | `--set-xauthrequest=true` | `X-Auth-Request-Access-Token` | Requires `token_header_priority` config |
175
+
176
+ ### Recommended oauth2-proxy Configuration
177
+
178
+ For best compatibility, configure oauth2-proxy to emit `X-Forwarded-Access-Token` via nginx:
179
+
180
+ ```bash
181
+ oauth2-proxy \
182
+ --pass-access-token=true \
183
+ --set-authorization-header=false \
184
+ --set-xauthrequest=true \
185
+ --upstream=http://rails-api:3000
186
+ ```
187
+
188
+ Then configure nginx to relay the token:
189
+
190
+ ```nginx
191
+ proxy_set_header X-Forwarded-Access-Token $upstream_http_x_auth_request_access_token;
192
+ ```
193
+
194
+ ### Using X-Auth-Request-Access-Token Directly
195
+
196
+ If you prefer to use `X-Auth-Request-Access-Token` without nginx relay, configure both `forwarded_header_name` and `token_header_priority`:
197
+
198
+ ```ruby
199
+ use Verikloak::BFF::HeaderGuard,
200
+ trusted_proxies: ['10.0.0.0/8'],
201
+ # Set the forwarded header to X-Auth-Request-Access-Token
202
+ forwarded_header_name: 'HTTP_X_AUTH_REQUEST_ACCESS_TOKEN',
203
+ # Also add to priority list for Authorization seeding
204
+ token_header_priority: ['HTTP_X_AUTH_REQUEST_ACCESS_TOKEN']
205
+ ```
206
+
207
+ > **Note**: If you only set `token_header_priority` without changing `forwarded_header_name`,
208
+ > `require_forwarded_header: true` will still expect `X-Forwarded-Access-Token` and return 401
209
+ > when it's missing.
210
+
113
211
  ## Development (for contributors)
114
212
  Clone and install dependencies:
115
213
 
@@ -1,42 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rails/generators/base'
3
+ require 'rails/generators'
4
4
 
5
5
  module Verikloak
6
- module BFF
7
- # Namespace for Rails generators related to Verikloak BFF
6
+ module Bff
7
+ # Rails generators supporting verikloak-bff integration.
8
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
9
+ # Generator to install the Verikloak BFF initializer into a Rails app.
21
10
  #
22
11
  # @see Verikloak::BFF::Rails::Middleware
12
+ # @example Default usage
13
+ # rails g verikloak:bff:install
23
14
  class InstallGenerator < ::Rails::Generators::Base
24
15
  source_root File.expand_path('templates', __dir__)
16
+ desc 'Creates the Verikloak BFF initializer for Rails applications.'
25
17
 
26
- # Configuration option for specifying the initializer file path.
27
- #
28
18
  # @option options [String] :initializer ('config/initializers/verikloak_bff.rb')
29
- # The path where the initializer file will be created
19
+ # Path for the generated initializer file.
30
20
  class_option :initializer, type: :string,
31
21
  default: 'config/initializers/verikloak_bff.rb',
32
22
  desc: 'Path for the generated initializer'
33
23
 
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.
24
+ # Copies the initializer template to the desired location.
40
25
  #
41
26
  # @return [void]
42
27
  def create_initializer
@@ -46,3 +31,11 @@ module Verikloak
46
31
  end
47
32
  end
48
33
  end
34
+
35
+ # Maintain legacy constant path for consumers referencing Verikloak::BFF::Generators.
36
+ module Verikloak
37
+ # Legacy namespace alias for backward compatibility.
38
+ module BFF
39
+ Generators = Bff::Generators unless defined?(Generators)
40
+ end
41
+ end
@@ -1,11 +1,115 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Configure verikloak-bff to insert its HeaderGuard middleware only after the
4
- # Verikloak core middleware is present. This avoids boot errors when the core
5
- # gem has not yet been installed.
6
- Rails.application.config.after_initialize do
7
- Verikloak::BFF::Rails::Middleware.insert_after_core(
8
- Rails.application.config.middleware,
9
- logger: Rails.logger
10
- )
3
+ # Verikloak BFF Configuration
4
+ #
5
+ # This file configures the Verikloak::BFF::HeaderGuard middleware.
6
+ #
7
+ # IMPORTANT: Middleware insertion is handled automatically by verikloak-rails.
8
+ # Make sure you have both gems in your Gemfile:
9
+ #
10
+ # gem 'verikloak-rails'
11
+ # gem 'verikloak-bff'
12
+ #
13
+ # The middleware stack will be configured as:
14
+ # [Verikloak::BFF::HeaderGuard] → [Verikloak::Middleware] → [Your App]
15
+ #
16
+ # If you are NOT using verikloak-rails, you must manually insert the middleware
17
+ # in config/application.rb (NOT in after_initialize, which causes FrozenError in Rails 8.x+):
18
+ #
19
+ # config.middleware.insert_before Verikloak::Middleware, Verikloak::BFF::HeaderGuard
20
+ #
21
+ Verikloak::BFF.configure do |config|
22
+ # ==========================================================================
23
+ # Trust Boundary (REQUIRED)
24
+ # ==========================================================================
25
+ # Allowlist for trusted proxy peers. Requests from untrusted peers are rejected.
26
+ # Supports IP addresses, CIDR ranges, Regexp, or Proc.
27
+ #
28
+ # SECURITY: Keep this list as specific as possible. Review whenever proxy
29
+ # topology changes to avoid unintentionally widening the trust boundary.
30
+ #
31
+ config.trusted_proxies = ENV.fetch('TRUSTED_PROXIES', '127.0.0.1').split(',').map(&:strip)
32
+
33
+ # ==========================================================================
34
+ # Peer Selection
35
+ # ==========================================================================
36
+ # How to determine the client's peer IP for trust decisions.
37
+ #
38
+ # :remote_then_xff (default) - Prefer REMOTE_ADDR, then fall back to XFF
39
+ # :xff_only - Use only X-Forwarded-For header
40
+ #
41
+ # config.peer_preference = :remote_then_xff
42
+
43
+ # Which X-Forwarded-For entry to use when multiple proxies are involved.
44
+ #
45
+ # :rightmost (default) - Nearest proxy (recommended for most setups)
46
+ # :leftmost - Original client (use only if you trust all proxies)
47
+ #
48
+ # config.xff_strategy = :rightmost
49
+
50
+ # ==========================================================================
51
+ # Token Selection & Header Handling
52
+ # ==========================================================================
53
+ # Prefer X-Forwarded-Access-Token over Authorization header.
54
+ #
55
+ # config.prefer_forwarded = true
56
+
57
+ # Reject requests without X-Forwarded-Access-Token (blocks direct access).
58
+ # Enable this to ensure all requests go through the BFF proxy.
59
+ #
60
+ # config.require_forwarded_header = false
61
+
62
+ # Require Authorization and X-Forwarded-Access-Token to contain the same token
63
+ # when both are present.
64
+ #
65
+ # config.enforce_header_consistency = true
66
+
67
+ # Remove external X-Auth-Request-* headers before passing downstream.
68
+ # Prevents header injection attacks.
69
+ #
70
+ # config.strip_suspicious_headers = true
71
+
72
+ # ==========================================================================
73
+ # Claims Consistency (Optional)
74
+ # ==========================================================================
75
+ # Compare X-Auth-Request-* headers against JWT claims.
76
+ # Useful for detecting token substitution attacks.
77
+ #
78
+ # config.enforce_claims_consistency = {
79
+ # email: :email, # X-Auth-Request-Email == JWT email claim
80
+ # user: :sub, # X-Auth-Request-User == JWT sub claim
81
+ # groups: :realm_roles # X-Auth-Request-Groups ⊆ JWT realm_access.roles
82
+ # }
83
+
84
+ # :enforce (default) - Reject mismatches with 403
85
+ # :log_only - Log mismatches but allow request to proceed
86
+ #
87
+ # config.claims_consistency_mode = :enforce
88
+
89
+ # ==========================================================================
90
+ # Header Name Customization
91
+ # ==========================================================================
92
+ # Customize forwarded token header name (if your proxy uses a different header).
93
+ #
94
+ # config.forwarded_header_name = 'HTTP_X_FORWARDED_ACCESS_TOKEN'
95
+
96
+ # Customize X-Auth-Request-* header names.
97
+ #
98
+ # config.auth_request_headers = {
99
+ # email: 'HTTP_X_AUTH_REQUEST_EMAIL',
100
+ # user: 'HTTP_X_AUTH_REQUEST_USER',
101
+ # groups: 'HTTP_X_AUTH_REQUEST_GROUPS'
102
+ # }
103
+
104
+ # Priority list for seeding Authorization header when empty.
105
+ #
106
+ # config.token_header_priority = ['HTTP_X_FORWARDED_ACCESS_TOKEN']
107
+
108
+ # ==========================================================================
109
+ # Logging
110
+ # ==========================================================================
111
+ # Structured logging hook for observability.
112
+ # Payload includes: rid (request_id), sub, kid, iss, aud
113
+ #
114
+ # config.log_with = ->(payload) { Rails.logger.info(payload.to_json) }
11
115
  end
@@ -5,6 +5,6 @@
5
5
  # @return [String]
6
6
  module Verikloak
7
7
  module BFF
8
- VERSION = '0.2.4'
8
+ VERSION = '0.2.6'
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.4
4
+ version: 0.2.6
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.2.0
58
+ version: 0.3.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.2.0
68
+ version: 0.3.0
69
69
  - - "<"
70
70
  - !ruby/object:Gem::Version
71
71
  version: 1.0.0
@@ -101,7 +101,7 @@ metadata:
101
101
  source_code_uri: https://github.com/taiyaky/verikloak-bff
102
102
  changelog_uri: https://github.com/taiyaky/verikloak-bff/blob/main/CHANGELOG.md
103
103
  bug_tracker_uri: https://github.com/taiyaky/verikloak-bff/issues
104
- documentation_uri: https://rubydoc.info/gems/verikloak-bff/0.2.4
104
+ documentation_uri: https://rubydoc.info/gems/verikloak-bff/0.2.6
105
105
  rubygems_mfa_required: 'true'
106
106
  rdoc_options: []
107
107
  require_paths: