verikloak-rails 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 +4 -4
- data/CHANGELOG.md +20 -0
- data/README.md +19 -6
- data/lib/generators/verikloak/install/install_generator.rb +2 -0
- data/lib/verikloak/rails/configuration.rb +25 -2
- data/lib/verikloak/rails/railtie.rb +68 -11
- data/lib/verikloak/rails/version.rb +1 -1
- metadata +26 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0b1a2ddc3ca2180e8850394cbfbeb31ee7e79347a14b1589fbed46249e282460
|
4
|
+
data.tar.gz: ee5f73e8edc0895ac4f9c999db71bc818e23d609952637c0f5ce17b6d7795517
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 882290c28579b912a7212ca3183d722d8b1f0e6d44b68f99c983351d4fe57d20fba702f0bf4671f575b1ef7a50b8da9c55f7b08220c264b2af2a6ae07f22814a
|
7
|
+
data.tar.gz: 4188be35ed688f8bb58b0a46a22b0e4e7e4f98218707f2f665c50a265b1e613aad2074114d7ffe0715494d3bd6185463b075989e5e4930e00f238999ee7b2a4d
|
data/CHANGELOG.md
CHANGED
@@ -5,6 +5,26 @@ 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
|
+
## [0.2.3] - 2025-09-22
|
10
|
+
|
11
|
+
### Changed
|
12
|
+
- Provide a safe default audience (`'rails-api'`) so fresh installs keep `Verikloak::Middleware` active and remain compatible with the optional `verikloak-audience` gem.
|
13
|
+
|
14
|
+
---
|
15
|
+
|
16
|
+
## [0.2.2] - 2025-09-21
|
17
|
+
|
18
|
+
### Added
|
19
|
+
- Automatically insert `Verikloak::Bff::HeaderGuard` when the optional gem is available, with configuration toggles and ordering controls.
|
20
|
+
- Configuration knobs for positioning the base `Verikloak::Middleware` within the Rack stack.
|
21
|
+
|
22
|
+
### Changed
|
23
|
+
- Disable the built-in Pundit rescue when `verikloak-pundit` is loaded unless explicitly configured.
|
24
|
+
|
25
|
+
### Documentation
|
26
|
+
- Note related gems in the installer output and README, including new configuration options for middleware ordering and BFF auto-insertion.
|
27
|
+
|
8
28
|
---
|
9
29
|
|
10
30
|
## [0.2.1] - 2025-09-21
|
data/README.md
CHANGED
@@ -83,9 +83,10 @@ end
|
|
83
83
|
|
84
84
|
## Middleware
|
85
85
|
### Inserted Middleware
|
86
|
-
| Component | Inserted
|
86
|
+
| Component | Inserted relative to | Purpose |
|
87
87
|
| --- | --- | --- |
|
88
|
-
| `Verikloak::
|
88
|
+
| `Verikloak::Bff::HeaderGuard` (optional) | Before `Verikloak::Middleware` by default when the gem is present | Normalize or enforce trusted proxy headers such as `X-Forwarded-Access-Token` |
|
89
|
+
| `Verikloak::Middleware` | After `Rails::Rack::Logger` by default (configurable) | Validate Bearer JWT (OIDC discovery + JWKS), set `verikloak.user`/`verikloak.token`, and honor `skip_paths` |
|
89
90
|
|
90
91
|
### BFF Integration
|
91
92
|
Support for BFF header handling (e.g., normalizing or enforcing `X-Forwarded-Access-Token`) now lives in a dedicated gem: verikloak-bff.
|
@@ -94,6 +95,8 @@ Note: verikloak-bff's `HeaderGuard` never overwrites an existing `Authorization`
|
|
94
95
|
- Gem: https://github.com/taiyaky/verikloak-bff
|
95
96
|
- Rails guide: `docs/rails.md` in that repository
|
96
97
|
|
98
|
+
When `verikloak-bff` is on the load path, `verikloak-rails` automatically inserts `Verikloak::Bff::HeaderGuard` before the base middleware so forwarded headers are normalized before verification. Control this via `config.verikloak.auto_insert_bff_header_guard` and the `bff_header_guard_insert_before/after` knobs.
|
99
|
+
|
97
100
|
Use verikloak-bff alongside this gem when you front Rails with a BFF/proxy such as oauth2-proxy and need to enforce trusted forwarding and header consistency.
|
98
101
|
|
99
102
|
## Configuration (initializer)
|
@@ -103,7 +106,7 @@ Keys under `config.verikloak`:
|
|
103
106
|
| Key | Type | Description | Default |
|
104
107
|
| --- | --- | --- | --- |
|
105
108
|
| `discovery_url` | String | OIDC discovery URL | `nil` |
|
106
|
-
| `audience` | String or Array | Expected `aud` | `
|
109
|
+
| `audience` | String or Array | Expected `aud` | `'rails-api'` |
|
107
110
|
| `issuer` | String | Expected `iss` | `nil` |
|
108
111
|
| `leeway` | Integer | Clock skew allowance (seconds) | `60` |
|
109
112
|
| `skip_paths` | Array<String> | Paths to skip verification | `['/up','/health','/rails/health']` |
|
@@ -111,7 +114,12 @@ Keys under `config.verikloak`:
|
|
111
114
|
| `error_renderer` | Object responding to `render(controller, error)` | Override error rendering | built-in JSON renderer |
|
112
115
|
| `auto_include_controller` | Boolean | Auto-include controller concern | `true` |
|
113
116
|
| `render_500_json` | Boolean | Rescue `StandardError`, log the exception, and render JSON 500 | `false` |
|
114
|
-
| `rescue_pundit` | Boolean | Rescue `Pundit::NotAuthorizedError` to 403 JSON when Pundit is present | `true` |
|
117
|
+
| `rescue_pundit` | Boolean | Rescue `Pundit::NotAuthorizedError` to 403 JSON when Pundit is present (auto-disabled when `verikloak-pundit` is loaded) | `true` |
|
118
|
+
| `middleware_insert_before` | Object/String/Symbol | Insert `Verikloak::Middleware` before this Rack middleware | `nil` |
|
119
|
+
| `middleware_insert_after` | Object/String/Symbol | Insert `Verikloak::Middleware` after this Rack middleware (`Rails::Rack::Logger` when `nil`) | `nil` |
|
120
|
+
| `auto_insert_bff_header_guard` | Boolean | Auto insert `Verikloak::Bff::HeaderGuard` when the gem is present | `true` |
|
121
|
+
| `bff_header_guard_insert_before` | Object/String/Symbol | Insert the header guard before this middleware (`Verikloak::Middleware` when `nil`) | `nil` |
|
122
|
+
| `bff_header_guard_insert_after` | Object/String/Symbol | Insert the header guard after this middleware | `nil` |
|
115
123
|
|
116
124
|
Environment variable examples are in the generated initializer.
|
117
125
|
|
@@ -127,7 +135,10 @@ Rails.application.configure do
|
|
127
135
|
# Optional but recommended when you know it
|
128
136
|
# config.verikloak.issuer = 'https://idp.example.com/realms/myrealm'
|
129
137
|
|
130
|
-
# For BFF/proxy header handling, see verikloak-bff
|
138
|
+
# For BFF/proxy header handling, see verikloak-bff (auto inserted when present)
|
139
|
+
# To customize ordering:
|
140
|
+
# config.verikloak.middleware_insert_before = Rack::Attack
|
141
|
+
# config.verikloak.auto_insert_bff_header_guard = false
|
131
142
|
end
|
132
143
|
```
|
133
144
|
|
@@ -221,8 +232,10 @@ end
|
|
221
232
|
## Optional Pundit Rescue
|
222
233
|
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.
|
223
234
|
|
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
|
+
|
224
237
|
### Toggle
|
225
|
-
Toggle with `config.verikloak.rescue_pundit` (default: true). Environment example:
|
238
|
+
Toggle with `config.verikloak.rescue_pundit` (default: true unless overridden by `verikloak-pundit`). Environment example:
|
226
239
|
|
227
240
|
```ruby
|
228
241
|
# config/initializers/verikloak.rb
|
@@ -30,6 +30,8 @@ module Verikloak
|
|
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
|
33
|
+
4) (Optional) For BFF/proxy setups, add gem 'verikloak-bff' to normalize headers.
|
34
|
+
5) (Optional) When using Pundit policies, consider gem 'verikloak-pundit' for richer errors.
|
33
35
|
MSG
|
34
36
|
end
|
35
37
|
end
|
@@ -38,16 +38,34 @@ module Verikloak
|
|
38
38
|
# @!attribute [rw] rescue_pundit
|
39
39
|
# Rescue `Pundit::NotAuthorizedError` and render JSON 403 responses.
|
40
40
|
# @return [Boolean]
|
41
|
+
# @!attribute [rw] middleware_insert_before
|
42
|
+
# Rack middleware to insert `Verikloak::Middleware` before.
|
43
|
+
# @return [Object, String, Symbol, nil]
|
44
|
+
# @!attribute [rw] middleware_insert_after
|
45
|
+
# Rack middleware to insert `Verikloak::Middleware` after.
|
46
|
+
# @return [Object, String, Symbol, nil]
|
47
|
+
# @!attribute [rw] auto_insert_bff_header_guard
|
48
|
+
# Auto-insert `Verikloak::Bff::HeaderGuard` when available.
|
49
|
+
# @return [Boolean]
|
50
|
+
# @!attribute [rw] bff_header_guard_insert_before
|
51
|
+
# Rack middleware to insert the header guard before.
|
52
|
+
# @return [Object, String, Symbol, nil]
|
53
|
+
# @!attribute [rw] bff_header_guard_insert_after
|
54
|
+
# Rack middleware to insert the header guard after.
|
55
|
+
# @return [Object, String, Symbol, nil]
|
41
56
|
class Configuration
|
42
57
|
attr_accessor :discovery_url, :audience, :issuer, :leeway, :skip_paths,
|
43
58
|
:logger_tags, :error_renderer, :auto_include_controller,
|
44
|
-
:render_500_json, :rescue_pundit
|
59
|
+
:render_500_json, :rescue_pundit,
|
60
|
+
:middleware_insert_before, :middleware_insert_after,
|
61
|
+
:auto_insert_bff_header_guard,
|
62
|
+
:bff_header_guard_insert_before, :bff_header_guard_insert_after
|
45
63
|
|
46
64
|
# Initialize configuration with sensible defaults for Rails apps.
|
47
65
|
# @return [void]
|
48
66
|
def initialize
|
49
67
|
@discovery_url = nil
|
50
|
-
@audience =
|
68
|
+
@audience = 'rails-api'
|
51
69
|
@issuer = nil
|
52
70
|
@leeway = 60
|
53
71
|
@skip_paths = ['/up', '/health', '/rails/health']
|
@@ -56,6 +74,11 @@ module Verikloak
|
|
56
74
|
@auto_include_controller = true
|
57
75
|
@render_500_json = false
|
58
76
|
@rescue_pundit = true
|
77
|
+
@middleware_insert_before = nil
|
78
|
+
@middleware_insert_after = nil
|
79
|
+
@auto_insert_bff_header_guard = true
|
80
|
+
@bff_header_guard_insert_before = nil
|
81
|
+
@bff_header_guard_insert_after = nil
|
59
82
|
end
|
60
83
|
|
61
84
|
# Options forwarded to the base Verikloak Rack middleware.
|
@@ -16,17 +16,8 @@ module Verikloak
|
|
16
16
|
# Apply configuration and insert middleware.
|
17
17
|
# @return [void]
|
18
18
|
initializer 'verikloak.configure' do |app|
|
19
|
-
Verikloak::Rails.
|
20
|
-
|
21
|
-
%i[discovery_url audience issuer leeway skip_paths
|
22
|
-
logger_tags error_renderer auto_include_controller
|
23
|
-
render_500_json rescue_pundit].each do |key|
|
24
|
-
c.send("#{key}=", rails_cfg[key]) if rails_cfg.key?(key)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
app.middleware.insert_after ::Rails::Rack::Logger,
|
28
|
-
::Verikloak::Middleware,
|
29
|
-
**Verikloak::Rails.config.middleware_options
|
19
|
+
stack = ::Verikloak::Rails::Railtie.send(:configure_middleware, app)
|
20
|
+
::Verikloak::Rails::Railtie.send(:configure_bff_guard, stack)
|
30
21
|
end
|
31
22
|
|
32
23
|
# Optionally include the controller concern when ActionController loads.
|
@@ -36,6 +27,72 @@ module Verikloak
|
|
36
27
|
include Verikloak::Rails::Controller if Verikloak::Rails.config.auto_include_controller
|
37
28
|
end
|
38
29
|
end
|
30
|
+
|
31
|
+
class << self
|
32
|
+
private
|
33
|
+
|
34
|
+
# Apply configured options and insert the base middleware into the stack.
|
35
|
+
#
|
36
|
+
# @param app [Rails::Application] application being initialized
|
37
|
+
# @return [ActionDispatch::MiddlewareStackProxy] configured middleware stack
|
38
|
+
def configure_middleware(app)
|
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
|
55
|
+
end
|
56
|
+
stack
|
57
|
+
end
|
58
|
+
|
59
|
+
# Insert the optional HeaderGuard middleware when verikloak-bff is present.
|
60
|
+
#
|
61
|
+
# @param stack [ActionDispatch::MiddlewareStackProxy]
|
62
|
+
# @return [void]
|
63
|
+
def configure_bff_guard(stack)
|
64
|
+
return unless Verikloak::Rails.config.auto_insert_bff_header_guard
|
65
|
+
return unless defined?(::Verikloak::Bff::HeaderGuard)
|
66
|
+
|
67
|
+
guard_before = Verikloak::Rails.config.bff_header_guard_insert_before
|
68
|
+
guard_after = Verikloak::Rails.config.bff_header_guard_insert_after
|
69
|
+
if guard_before
|
70
|
+
stack.insert_before guard_before, ::Verikloak::Bff::HeaderGuard
|
71
|
+
elsif guard_after
|
72
|
+
stack.insert_after guard_after, ::Verikloak::Bff::HeaderGuard
|
73
|
+
else
|
74
|
+
stack.insert_before ::Verikloak::Middleware, ::Verikloak::Bff::HeaderGuard
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Sync configuration from the Rails application into Verikloak::Rails.
|
79
|
+
#
|
80
|
+
# @param app [Rails::Application]
|
81
|
+
# @return [void]
|
82
|
+
def apply_configuration(app)
|
83
|
+
Verikloak::Rails.configure do |c|
|
84
|
+
rails_cfg = app.config.verikloak
|
85
|
+
%i[discovery_url audience issuer leeway skip_paths
|
86
|
+
logger_tags error_renderer auto_include_controller
|
87
|
+
render_500_json rescue_pundit middleware_insert_before
|
88
|
+
middleware_insert_after auto_insert_bff_header_guard
|
89
|
+
bff_header_guard_insert_before bff_header_guard_insert_after].each do |key|
|
90
|
+
c.send("#{key}=", rails_cfg[key]) if rails_cfg.key?(key)
|
91
|
+
end
|
92
|
+
c.rescue_pundit = false if !rails_cfg.key?(:rescue_pundit) && defined?(::Verikloak::Pundit)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
39
96
|
end
|
40
97
|
end
|
41
98
|
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
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- taiyaky
|
@@ -9,6 +9,26 @@ bindir: bin
|
|
9
9
|
cert_chain: []
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: rack
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '2.2'
|
19
|
+
- - "<"
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '4.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '2.2'
|
29
|
+
- - "<"
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: '4.0'
|
12
32
|
- !ruby/object:Gem::Dependency
|
13
33
|
name: rails
|
14
34
|
requirement: !ruby/object:Gem::Requirement
|
@@ -35,20 +55,20 @@ dependencies:
|
|
35
55
|
requirements:
|
36
56
|
- - ">="
|
37
57
|
- !ruby/object:Gem::Version
|
38
|
-
version: 0.
|
58
|
+
version: 0.2.0
|
39
59
|
- - "<"
|
40
60
|
- !ruby/object:Gem::Version
|
41
|
-
version:
|
61
|
+
version: 1.0.0
|
42
62
|
type: :runtime
|
43
63
|
prerelease: false
|
44
64
|
version_requirements: !ruby/object:Gem::Requirement
|
45
65
|
requirements:
|
46
66
|
- - ">="
|
47
67
|
- !ruby/object:Gem::Version
|
48
|
-
version: 0.
|
68
|
+
version: 0.2.0
|
49
69
|
- - "<"
|
50
70
|
- !ruby/object:Gem::Version
|
51
|
-
version:
|
71
|
+
version: 1.0.0
|
52
72
|
description: 'Rails integration for Verikloak: auto middleware, helpers, and standardized
|
53
73
|
JSON errors.'
|
54
74
|
executables: []
|
@@ -73,7 +93,7 @@ metadata:
|
|
73
93
|
source_code_uri: https://github.com/taiyaky/verikloak-rails
|
74
94
|
changelog_uri: https://github.com/taiyaky/verikloak-rails/blob/main/CHANGELOG.md
|
75
95
|
bug_tracker_uri: https://github.com/taiyaky/verikloak-rails/issues
|
76
|
-
documentation_uri: https://rubydoc.info/gems/verikloak-rails/0.2.
|
96
|
+
documentation_uri: https://rubydoc.info/gems/verikloak-rails/0.2.3
|
77
97
|
rubygems_mfa_required: 'true'
|
78
98
|
rdoc_options: []
|
79
99
|
require_paths:
|