verikloak-rails 0.2.0 → 0.2.1
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 +11 -0
- data/README.md +2 -2
- data/lib/verikloak/rails/configuration.rb +5 -0
- data/lib/verikloak/rails/controller.rb +45 -3
- data/lib/verikloak/rails/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1add58266f9789a0e9263c36d5e33ab80207b02a87b999d22b5fa65b813f5f36
|
4
|
+
data.tar.gz: 58d956084cc1631e4a0854744627dd3ba95350f7a9c1687665b7025fd943addb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 183bb26475f75704c35880db14e67ea5a83a3c3923d3ca8c6788c92e7735a2d00deabb681f0e65b8bf561e2b6a5aa0cfb4e7ccc24760c091dbe0145f87558311
|
7
|
+
data.tar.gz: 24bc9a344903243e2c63015c4429b8ea713467df15636021f91fc4698a39600ecd0a84e9caccac489ffc1e1d809353961170008ceabed24a106ad65e1b720fef
|
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.1] - 2025-09-21
|
11
|
+
|
12
|
+
### Changed
|
13
|
+
- Simplify `with_required_audience!` to always raise `Verikloak::Error`, letting the shared handler render forbidden responses
|
14
|
+
|
15
|
+
### Fixed
|
16
|
+
- Ensure the 500 JSON renderer logs exceptions against the actual Rails logger even when wrapped by tagged logging adapters
|
17
|
+
|
18
|
+
### Documentation
|
19
|
+
- Describe the `rescue_pundit` configuration flag and default initializer settings
|
20
|
+
|
10
21
|
## [0.2.0] - 2025-09-14
|
11
22
|
|
12
23
|
### Breaking
|
data/README.md
CHANGED
@@ -40,7 +40,7 @@ Then configure `config/initializers/verikloak.rb`.
|
|
40
40
|
| `current_user_claims` | Verified JWT claims (string keys) | `Hash` or `nil` | — |
|
41
41
|
| `current_subject` | Convenience accessor for `sub` claim | `String` or `nil` | — |
|
42
42
|
| `current_token` | Raw Bearer token from the request | `String` or `nil` | — |
|
43
|
-
| `with_required_audience!(*aud)` | Enforce that `aud` includes all required entries | `void` |
|
43
|
+
| `with_required_audience!(*aud)` | Enforce that `aud` includes all required entries | `void` | Raises `Verikloak::Error('forbidden')` so the concern renders standardized 403 JSON and halts the action |
|
44
44
|
|
45
45
|
### Data Sources
|
46
46
|
| Value | Rack env keys | Fallback (RequestStore) | Notes |
|
@@ -110,7 +110,7 @@ Keys under `config.verikloak`:
|
|
110
110
|
| `logger_tags` | Array<Symbol> | Tags to add to Rails logs. Supports `:request_id`, `:sub` | `[:request_id, :sub]` |
|
111
111
|
| `error_renderer` | Object responding to `render(controller, error)` | Override error rendering | built-in JSON renderer |
|
112
112
|
| `auto_include_controller` | Boolean | Auto-include controller concern | `true` |
|
113
|
-
| `render_500_json` | Boolean | Rescue `StandardError
|
113
|
+
| `render_500_json` | Boolean | Rescue `StandardError`, log the exception, and render JSON 500 | `false` |
|
114
114
|
| `rescue_pundit` | Boolean | Rescue `Pundit::NotAuthorizedError` to 403 JSON when Pundit is present | `true` |
|
115
115
|
|
116
116
|
Environment variable examples are in the generated initializer.
|
@@ -35,11 +35,16 @@ module Verikloak
|
|
35
35
|
# @!attribute [rw] render_500_json
|
36
36
|
# Rescue StandardError and render a JSON 500 response.
|
37
37
|
# @return [Boolean]
|
38
|
+
# @!attribute [rw] rescue_pundit
|
39
|
+
# Rescue `Pundit::NotAuthorizedError` and render JSON 403 responses.
|
40
|
+
# @return [Boolean]
|
38
41
|
class Configuration
|
39
42
|
attr_accessor :discovery_url, :audience, :issuer, :leeway, :skip_paths,
|
40
43
|
:logger_tags, :error_renderer, :auto_include_controller,
|
41
44
|
:render_500_json, :rescue_pundit
|
42
45
|
|
46
|
+
# Initialize configuration with sensible defaults for Rails apps.
|
47
|
+
# @return [void]
|
43
48
|
def initialize
|
44
49
|
@discovery_url = nil
|
45
50
|
@audience = nil
|
@@ -16,7 +16,8 @@ module Verikloak
|
|
16
16
|
before_action :authenticate_user!
|
17
17
|
# Register generic error handler first so specific handlers take precedence.
|
18
18
|
if Verikloak::Rails.config.render_500_json
|
19
|
-
rescue_from StandardError do |
|
19
|
+
rescue_from StandardError do |e|
|
20
|
+
_verikloak_log_internal_error(e)
|
20
21
|
render json: { error: 'internal_server_error', message: 'An unexpected error occurred' },
|
21
22
|
status: :internal_server_error
|
22
23
|
end
|
@@ -84,13 +85,14 @@ module Verikloak
|
|
84
85
|
#
|
85
86
|
# @param required [Array<String>] one or more audiences to require
|
86
87
|
# @return [void]
|
88
|
+
# @raise [Verikloak::Error] when the required audience is missing
|
87
89
|
# @example
|
88
90
|
# with_required_audience!('my-api', 'payments')
|
89
91
|
def with_required_audience!(*required)
|
90
92
|
aud = Array(current_user_claims&.dig('aud'))
|
91
93
|
return if required.flatten.all? { |r| aud.include?(r) }
|
92
94
|
|
93
|
-
|
95
|
+
raise ::Verikloak::Error.new('forbidden', 'Required audience not satisfied')
|
94
96
|
end
|
95
97
|
|
96
98
|
private
|
@@ -118,10 +120,50 @@ module Verikloak
|
|
118
120
|
end
|
119
121
|
if Verikloak::Rails.config.logger_tags.include?(:sub)
|
120
122
|
sub = current_subject
|
121
|
-
|
123
|
+
if sub
|
124
|
+
sanitized = sub.to_s.gsub(/[[:cntrl:]]+/, ' ').strip
|
125
|
+
tags << "sub:#{sanitized}" unless sanitized.empty?
|
126
|
+
end
|
122
127
|
end
|
123
128
|
tags
|
124
129
|
end
|
130
|
+
|
131
|
+
# Write StandardError details to the controller or Rails logger when
|
132
|
+
# rendering the generic 500 JSON response. Logging ensures the
|
133
|
+
# underlying failure is still visible to operators even though the
|
134
|
+
# response body is static.
|
135
|
+
#
|
136
|
+
# @param exception [Exception]
|
137
|
+
# @return [void]
|
138
|
+
def _verikloak_log_internal_error(exception)
|
139
|
+
target_logger = _verikloak_base_logger
|
140
|
+
return unless target_logger.respond_to?(:error)
|
141
|
+
|
142
|
+
target_logger.error("[Verikloak] #{exception.class}: #{exception.message}")
|
143
|
+
backtrace = exception.backtrace
|
144
|
+
target_logger.error(backtrace.join("\n")) if backtrace&.any?
|
145
|
+
rescue StandardError
|
146
|
+
# Never allow logging failures to interfere with request handling.
|
147
|
+
nil
|
148
|
+
end
|
149
|
+
|
150
|
+
# Locate the innermost logger that responds to `error`.
|
151
|
+
# @return [Object, nil]
|
152
|
+
def _verikloak_base_logger
|
153
|
+
root_logger = if defined?(::Rails) && ::Rails.respond_to?(:logger)
|
154
|
+
::Rails.logger
|
155
|
+
elsif respond_to?(:logger)
|
156
|
+
logger
|
157
|
+
end
|
158
|
+
current = root_logger
|
159
|
+
while current.respond_to?(:logger)
|
160
|
+
next_logger = current.logger
|
161
|
+
break if next_logger.nil? || next_logger.equal?(current)
|
162
|
+
|
163
|
+
current = next_logger
|
164
|
+
end
|
165
|
+
current
|
166
|
+
end
|
125
167
|
end
|
126
168
|
end
|
127
169
|
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.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- taiyaky
|
@@ -73,7 +73,7 @@ metadata:
|
|
73
73
|
source_code_uri: https://github.com/taiyaky/verikloak-rails
|
74
74
|
changelog_uri: https://github.com/taiyaky/verikloak-rails/blob/main/CHANGELOG.md
|
75
75
|
bug_tracker_uri: https://github.com/taiyaky/verikloak-rails/issues
|
76
|
-
documentation_uri: https://rubydoc.info/gems/verikloak-rails/0.2.
|
76
|
+
documentation_uri: https://rubydoc.info/gems/verikloak-rails/0.2.1
|
77
77
|
rubygems_mfa_required: 'true'
|
78
78
|
rdoc_options: []
|
79
79
|
require_paths:
|