verikloak-rails 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: 3f102e217c566e0cbe01b75979d16fd5636449c8cc38f58e35d8147be44d05d8
4
- data.tar.gz: 1426fccde358d9d5440b99749debff98775cbb8d2e8492be96bf468df2ff259f
3
+ metadata.gz: a6126b5013614bb5cb7c0594126c21456db9762a9e3d307108e5f8c1ce197155
4
+ data.tar.gz: e8504432260a732881946e72cb5551e00178108ba641b802b78c4ca42879e664
5
5
  SHA512:
6
- metadata.gz: c72c02908f4f946f4455b80341858cf329261c7d6da24d6ac6c6ab6ca484e636c45e6bf5e5ed23c29073c327e9338e7c42189e33e942ed5192b4792c44dd7ea4
7
- data.tar.gz: 9bae0bcc441bb9544a7365056f3ddf214bfe0ca4c8345c98d78ce6727cfca77ea6ad5a5511c415542f1882af7a996ce999e4e2912fc9c5203018d7d47b8d6007
6
+ metadata.gz: ecaf3de303c489d429c6157232f3159333ddec36e87ac0499784181e944c9fe25f59aceb72452b69f297bc7ff0761c069df13cf78ab776c5311a04d6d6c0e9be
7
+ data.tar.gz: e335daefda43368d7c0b58b87372214a8e999bad0424c7e5e2e4e5d5f1ced49b51190e900865ff34d1a277a72eb6fd172b64fc559f0eb246fac4f1d114902bfc
data/CHANGELOG.md CHANGED
@@ -5,21 +5,42 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ---
9
+
10
+ ## [0.2.6] - 2025-09-23
11
+
12
+ ### Fixed
13
+ - Leave `config.verikloak.rescue_pundit` commented in the installer initializer so `verikloak-pundit` can automatically disable the built-in rescue.
14
+
15
+ ### Documentation
16
+ - Align README compatibility with the current `verikloak` dependency range.
17
+ - Clarify how the Pundit rescue interacts with the optional `verikloak-pundit` gem and adjust examples accordingly.
18
+
19
+ ## [0.2.5] - 2025-09-23
20
+
21
+ ### Added
22
+ - Integration test coverage for missing discovery URL scenarios
23
+ - `reset!` method for configuration cleanup in test environments
24
+
25
+ ### Fixed
26
+ - Graceful handling of missing or blank discovery URLs during middleware configuration
27
+ - Skip middleware insertion and log warning when discovery URL is not configured
28
+ - Only configure BFF header guard when base middleware is successfully inserted
29
+
30
+ ### Changed
31
+ - Improved error handling and validation for discovery URL configuration
32
+ - Enhanced middleware insertion logic with better separation of concerns
8
33
 
9
34
  ## [0.2.4] - 2025-09-23
10
35
 
11
36
  ### Fixed
12
37
  - Package the installer template so `rails g verikloak:install` works in packaged gems (no more missing `initializer.rb.erb`).
13
38
 
14
- ---
15
-
16
39
  ## [0.2.3] - 2025-09-22
17
40
 
18
41
  ### Changed
19
42
  - Provide a safe default audience (`'rails-api'`) so fresh installs keep `Verikloak::Middleware` active and remain compatible with the optional `verikloak-audience` gem.
20
43
 
21
- ---
22
-
23
44
  ## [0.2.2] - 2025-09-21
24
45
 
25
46
  ### Added
@@ -32,8 +53,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
32
53
  ### Documentation
33
54
  - Note related gems in the installer output and README, including new configuration options for middleware ordering and BFF auto-insertion.
34
55
 
35
- ---
36
-
37
56
  ## [0.2.1] - 2025-09-21
38
57
 
39
58
  ### Changed
data/README.md CHANGED
@@ -21,7 +21,7 @@ Provide drop-in, token-based authentication for Rails APIs via Verikloak (OIDC d
21
21
  ## Compatibility
22
22
  - Ruby: >= 3.1
23
23
  - Rails: 6.1 – 8.x
24
- - verikloak: >= 0.1.2, < 0.2
24
+ - verikloak: >= 0.2.0, < 1.0.0
25
25
 
26
26
  ## Quick Start
27
27
  ```bash
@@ -114,7 +114,7 @@ Keys under `config.verikloak`:
114
114
  | `error_renderer` | Object responding to `render(controller, error)` | Override error rendering | built-in JSON renderer |
115
115
  | `auto_include_controller` | Boolean | Auto-include controller concern | `true` |
116
116
  | `render_500_json` | Boolean | Rescue `StandardError`, log the exception, and render JSON 500 | `false` |
117
- | `rescue_pundit` | Boolean | Rescue `Pundit::NotAuthorizedError` to 403 JSON when Pundit is present (auto-disabled when `verikloak-pundit` is loaded) | `true` |
117
+ | `rescue_pundit` | Boolean | Rescue `Pundit::NotAuthorizedError` to 403 JSON when Pundit is present<br/>(auto-disabled when `verikloak-pundit` is loaded and the initializer leaves it unset) | `true` |
118
118
  | `middleware_insert_before` | Object/String/Symbol | Insert `Verikloak::Middleware` before this Rack middleware | `nil` |
119
119
  | `middleware_insert_after` | Object/String/Symbol | Insert `Verikloak::Middleware` after this Rack middleware (`Rails::Rack::Logger` when `nil`) | `nil` |
120
120
  | `auto_insert_bff_header_guard` | Boolean | Auto insert `Verikloak::Bff::HeaderGuard` when the gem is present | `true` |
@@ -157,8 +157,9 @@ Rails.application.configure do
157
157
  config.verikloak.logger_tags = %i[request_id sub]
158
158
  config.verikloak.render_500_json = ENV.fetch('VERIKLOAK_RENDER_500', 'false') == 'true'
159
159
 
160
- # Optional Pundit rescue (403 JSON)
161
- config.verikloak.rescue_pundit = ENV.fetch('VERIKLOAK_RESCUE_PUNDIT', 'true') == 'true'
160
+ # Optional Pundit rescue (403 JSON). Leave commented if you use
161
+ # verikloak-pundit so it can disable the built-in handler automatically.
162
+ # config.verikloak.rescue_pundit = ENV.fetch('VERIKLOAK_RESCUE_PUNDIT', 'true') == 'true'
162
163
  end
163
164
  ```
164
165
 
@@ -232,10 +233,10 @@ end
232
233
  ## Optional Pundit Rescue
233
234
  If the `pundit` gem is present, `Pundit::NotAuthorizedError` is rescued to a standardized 403 JSON. This is a lightweight convenience only; deeper Pundit integration (policies, helpers) is out of scope and can live in a separate plugin.
234
235
 
235
- When the optional [`verikloak-pundit`](https://github.com/taiyaky/verikloak-pundit) gem is loaded, the built-in rescue is automatically disabled to avoid double-handling errors. Explicitly set `config.verikloak.rescue_pundit` if you prefer different behavior.
236
+ When the optional [`verikloak-pundit`](https://github.com/taiyaky/verikloak-pundit) gem is loaded, the built-in rescue is automatically disabled to avoid double-handling errors—as long as the initializer leaves `config.verikloak.rescue_pundit` unset. Uncomment the initializer line (or set the value elsewhere) if you prefer different behavior.
236
237
 
237
238
  ### Toggle
238
- Toggle with `config.verikloak.rescue_pundit` (default: true unless overridden by `verikloak-pundit`). Environment example:
239
+ Toggle with `config.verikloak.rescue_pundit` (default: true; leave unset to allow `verikloak-pundit` to disable it). Environment example:
239
240
 
240
241
  ```ruby
241
242
  # config/initializers/verikloak.rb
@@ -26,7 +26,7 @@ module Verikloak
26
26
  ✅ verikloak: initializer created.
27
27
 
28
28
  Next steps:
29
- 1) Ensure the base gem is installed: gem 'verikloak', '>= 0.1.2', '< 0.2'
29
+ 1) Ensure the base gem is installed: gem 'verikloak', '>= 0.2.0', '< 1.0.0'
30
30
  2) Set discovery_url / audience in config/initializers/verikloak.rb
31
31
  3) (Optional) If you disable auto-include, add this line to ApplicationController:
32
32
  include Verikloak::Rails::Controller
@@ -10,5 +10,8 @@ Rails.application.configure do
10
10
  config.verikloak.logger_tags = %i[request_id sub]
11
11
  config.verikloak.auto_include_controller = true
12
12
  config.verikloak.render_500_json = ENV.fetch('VERIKLOAK_RENDER_500', 'false') == 'true'
13
- config.verikloak.rescue_pundit = ENV.fetch('VERIKLOAK_RESCUE_PUNDIT', 'true') == 'true'
13
+
14
+ # Optional Pundit rescue (403 JSON). Leave commented so `verikloak-pundit`
15
+ # can auto-disable the built-in handler when it is on the load path.
16
+ # config.verikloak.rescue_pundit = ENV.fetch('VERIKLOAK_RESCUE_PUNDIT', 'true') == 'true'
14
17
  end
@@ -17,7 +17,7 @@ module Verikloak
17
17
  # @return [void]
18
18
  initializer 'verikloak.configure' do |app|
19
19
  stack = ::Verikloak::Rails::Railtie.send(:configure_middleware, app)
20
- ::Verikloak::Rails::Railtie.send(:configure_bff_guard, stack)
20
+ ::Verikloak::Rails::Railtie.send(:configure_bff_guard, stack) if stack
21
21
  end
22
22
 
23
23
  # Optionally include the controller concern when ActionController loads.
@@ -37,23 +37,13 @@ module Verikloak
37
37
  # @return [ActionDispatch::MiddlewareStackProxy] configured middleware stack
38
38
  def configure_middleware(app)
39
39
  apply_configuration(app)
40
- base_options = Verikloak::Rails.config.middleware_options
41
- stack = app.middleware
42
- if (before = Verikloak::Rails.config.middleware_insert_before)
43
- stack.insert_before before,
44
- ::Verikloak::Middleware,
45
- **base_options
46
- else
47
- after = Verikloak::Rails.config.middleware_insert_after || ::Rails::Rack::Logger
48
- if after
49
- stack.insert_after after,
50
- ::Verikloak::Middleware,
51
- **base_options
52
- else
53
- stack.use ::Verikloak::Middleware, **base_options
54
- end
40
+
41
+ unless discovery_url_present?
42
+ log_missing_discovery_url_warning
43
+ return
55
44
  end
56
- stack
45
+
46
+ insert_base_middleware(app)
57
47
  end
58
48
 
59
49
  # Insert the optional HeaderGuard middleware when verikloak-bff is present.
@@ -92,6 +82,69 @@ module Verikloak
92
82
  c.rescue_pundit = false if !rails_cfg.key?(:rescue_pundit) && defined?(::Verikloak::Pundit)
93
83
  end
94
84
  end
85
+
86
+ # Check if discovery_url is present and valid.
87
+ #
88
+ # @return [Boolean] true if discovery_url is configured and not empty
89
+ def discovery_url_present?
90
+ discovery_url = Verikloak::Rails.config.discovery_url
91
+ return false unless discovery_url
92
+
93
+ return !discovery_url.blank? if discovery_url.respond_to?(:blank?)
94
+ return !discovery_url.empty? if discovery_url.respond_to?(:empty?)
95
+
96
+ true
97
+ end
98
+
99
+ # Log a warning message when discovery_url is missing.
100
+ # Uses Rails.logger if available, falls back to warn.
101
+ #
102
+ # @return [void]
103
+ def log_missing_discovery_url_warning
104
+ message = '[verikloak] discovery_url is not configured; skipping middleware insertion.'
105
+ if defined?(::Rails) && ::Rails.respond_to?(:logger) && ::Rails.logger
106
+ ::Rails.logger.warn(message)
107
+ else
108
+ warn(message)
109
+ end
110
+ end
111
+
112
+ # Insert the base Verikloak::Middleware into the application middleware stack.
113
+ # Respects the configured insertion point (before or after specified middleware).
114
+ #
115
+ # @param app [Rails::Application] the Rails application
116
+ # @return [ActionDispatch::MiddlewareStackProxy] the configured middleware stack
117
+ def insert_base_middleware(app)
118
+ stack = app.middleware
119
+ base_options = Verikloak::Rails.config.middleware_options
120
+
121
+ if (before = Verikloak::Rails.config.middleware_insert_before)
122
+ stack.insert_before before,
123
+ ::Verikloak::Middleware,
124
+ **base_options
125
+ else
126
+ insert_middleware_after(stack, base_options)
127
+ end
128
+
129
+ stack
130
+ end
131
+
132
+ # Insert middleware after a specified middleware or at the default position.
133
+ # Handles the case where no specific insertion point is configured.
134
+ #
135
+ # @param stack [ActionDispatch::MiddlewareStackProxy] the middleware stack
136
+ # @param base_options [Hash] options to pass to the middleware
137
+ # @return [void]
138
+ def insert_middleware_after(stack, base_options)
139
+ after = Verikloak::Rails.config.middleware_insert_after || ::Rails::Rack::Logger
140
+ if after
141
+ stack.insert_after after,
142
+ ::Verikloak::Middleware,
143
+ **base_options
144
+ else
145
+ stack.use ::Verikloak::Middleware, **base_options
146
+ end
147
+ end
95
148
  end
96
149
  end
97
150
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Verikloak
4
4
  module Rails
5
- VERSION = '0.2.4'
5
+ VERSION = '0.2.6'
6
6
  end
7
7
  end
@@ -35,6 +35,16 @@ module Verikloak
35
35
  def configure
36
36
  yield(config)
37
37
  end
38
+
39
+ # Reset configuration to its default state.
40
+ #
41
+ # Primarily intended for test environments that need to ensure a clean
42
+ # configuration between examples.
43
+ #
44
+ # @return [void]
45
+ def reset!
46
+ @config = nil
47
+ end
38
48
  end
39
49
  end
40
50
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: verikloak-rails
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
@@ -94,7 +94,7 @@ metadata:
94
94
  source_code_uri: https://github.com/taiyaky/verikloak-rails
95
95
  changelog_uri: https://github.com/taiyaky/verikloak-rails/blob/main/CHANGELOG.md
96
96
  bug_tracker_uri: https://github.com/taiyaky/verikloak-rails/issues
97
- documentation_uri: https://rubydoc.info/gems/verikloak-rails/0.2.4
97
+ documentation_uri: https://rubydoc.info/gems/verikloak-rails/0.2.6
98
98
  rubygems_mfa_required: 'true'
99
99
  rdoc_options: []
100
100
  require_paths: