verikloak-pundit 0.2.2 → 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 +13 -0
- data/README.md +171 -0
- data/lib/verikloak/pundit/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a4618def320e58859e62b0562ac2d942a3e307a61b3c287bc3a748511c76a3d0
|
|
4
|
+
data.tar.gz: ce80df2707bf38a548ef51dc6ed68acd149e2c06b2ef02d2b0a20dc709040cbf
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 246e5840dc895ee184ef33d88ea92c5e73f58c6120217e2661e6757dd2539ee74f7ba2b2eb47502ca70115884a971ef11530458af6a8aa6f3900f96f70d1119e
|
|
7
|
+
data.tar.gz: 6d4762d24e1b90c71ea68a1642456a9b4f3132d6424ef9587bfc1f41d70c3f124ebb1c8e60ea49d273b3e73a1077ebaeac4106311e1244890a29d41af8169300
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,19 @@ 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-12-31
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- **Database User Integration Guide**: New README section documenting the custom `UserContext` pattern for combining JWT claims with database user models, including controller setup and policy examples.
|
|
14
|
+
- **Delegations Module Documentation**: Comprehensive usage guide for `Verikloak::Pundit::Delegations`, covering requirements, custom `UserContext` compatibility, and nil user handling patterns.
|
|
15
|
+
|
|
16
|
+
### Changed
|
|
17
|
+
- Clarified controller setup examples to use `Verikloak::Pundit::Controller` (this gem) instead of `Verikloak::Rails::Controller` (verikloak-rails gem).
|
|
18
|
+
- Added explicit notes about method origins (`verikloak_claims` vs `current_user_claims`) for users combining multiple Verikloak gems.
|
|
19
|
+
- Enhanced nil user handling documentation with `safe_has_role?` helper pattern for public endpoints.
|
|
20
|
+
- Bump minimum `verikloak` dependency from `>= 0.2.0` to `>= 0.3.0` to align with latest upstream releases.
|
|
21
|
+
- Update Ruby version to 3.4.8 in development environment.
|
|
22
|
+
|
|
10
23
|
## [0.2.2] - 2025-09-28
|
|
11
24
|
|
|
12
25
|
### Changed
|
data/README.md
CHANGED
|
@@ -118,6 +118,177 @@ end
|
|
|
118
118
|
that client id to `permission_resource_clients` when you need to consume those
|
|
119
119
|
roles from Rails.
|
|
120
120
|
|
|
121
|
+
## Integrating with Database User Models
|
|
122
|
+
|
|
123
|
+
### Overview
|
|
124
|
+
|
|
125
|
+
`Verikloak::Pundit::UserContext` wraps JWT claims for use in Pundit policies. However, real applications often need to access database User models for additional attributes (e.g., `user.admin?`, `user.organization_id`).
|
|
126
|
+
|
|
127
|
+
### Custom UserContext Pattern
|
|
128
|
+
|
|
129
|
+
Create a custom UserContext that holds both JWT claims and a database user reference:
|
|
130
|
+
|
|
131
|
+
```ruby
|
|
132
|
+
# app/lib/app_user_context.rb
|
|
133
|
+
class AppUserContext < Verikloak::Pundit::UserContext
|
|
134
|
+
attr_reader :db_user
|
|
135
|
+
|
|
136
|
+
def initialize(claims, db_user: nil, resource_client: nil, config: nil)
|
|
137
|
+
super(claims, resource_client: resource_client, config: config)
|
|
138
|
+
@db_user = db_user
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# Delegate database user methods
|
|
142
|
+
delegate :admin?, :organization_id, :active?, to: :db_user, allow_nil: true
|
|
143
|
+
|
|
144
|
+
# Custom authorization helpers
|
|
145
|
+
def owns?(record)
|
|
146
|
+
return false unless db_user && record
|
|
147
|
+
record.respond_to?(:user_id) && db_user.id == record.user_id
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def same_organization?(record)
|
|
151
|
+
return false unless db_user && record
|
|
152
|
+
record.respond_to?(:organization_id) && db_user.organization_id == record.organization_id
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Controller Setup
|
|
158
|
+
|
|
159
|
+
Override `pundit_user` in your ApplicationController. This example assumes you are using `verikloak-rails`, which provides `current_user_claims` and related helpers:
|
|
160
|
+
|
|
161
|
+
```ruby
|
|
162
|
+
# app/controllers/application_controller.rb
|
|
163
|
+
class ApplicationController < ActionController::API
|
|
164
|
+
include Pundit::Authorization
|
|
165
|
+
include Verikloak::Pundit::Controller # Provides pundit_user and verikloak_claims
|
|
166
|
+
|
|
167
|
+
# If using verikloak-rails for JWT verification:
|
|
168
|
+
# include Verikloak::Rails::Controller # Provides current_user_claims, current_subject, etc.
|
|
169
|
+
|
|
170
|
+
def pundit_user
|
|
171
|
+
@pundit_user ||= AppUserContext.new(
|
|
172
|
+
verikloak_claims, # From Verikloak::Pundit::Controller
|
|
173
|
+
db_user: find_or_create_current_user,
|
|
174
|
+
config: Verikloak::Pundit.config
|
|
175
|
+
)
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
private
|
|
179
|
+
|
|
180
|
+
def find_or_create_current_user
|
|
181
|
+
# Extract subject from claims
|
|
182
|
+
sub = verikloak_claims&.dig('sub')
|
|
183
|
+
return nil unless sub
|
|
184
|
+
|
|
185
|
+
User.find_or_create_by(sub: sub) do |user|
|
|
186
|
+
user.email = verikloak_claims&.dig('email')
|
|
187
|
+
user.name = verikloak_claims&.dig('name')
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
> **Note:** If you are also using `verikloak-rails`, you can use its `current_user_claims` method instead of `verikloak_claims`. Both provide access to the JWT claims from the Rack environment.
|
|
194
|
+
|
|
195
|
+
### Policy Example
|
|
196
|
+
|
|
197
|
+
Now your policies can use both JWT claims and database attributes:
|
|
198
|
+
|
|
199
|
+
```ruby
|
|
200
|
+
# app/policies/document_policy.rb
|
|
201
|
+
class DocumentPolicy < ApplicationPolicy
|
|
202
|
+
def show?
|
|
203
|
+
# Combine JWT roles with database attributes
|
|
204
|
+
user.has_role?(:admin) || user.owns?(record) || user.same_organization?(record)
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def update?
|
|
208
|
+
user.admin? || user.owns?(record) # Uses delegated db_user.admin?
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def destroy?
|
|
212
|
+
user.has_role?(:admin) && user.active? # JWT role + DB attribute
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
## Delegations Module
|
|
218
|
+
|
|
219
|
+
### Overview
|
|
220
|
+
|
|
221
|
+
`Verikloak::Pundit::Delegations` provides shortcut methods for role and permission checks in policies.
|
|
222
|
+
|
|
223
|
+
### Usage
|
|
224
|
+
|
|
225
|
+
Include in your ApplicationPolicy to access helpers directly:
|
|
226
|
+
|
|
227
|
+
```ruby
|
|
228
|
+
class ApplicationPolicy
|
|
229
|
+
include Verikloak::Pundit::Delegations
|
|
230
|
+
|
|
231
|
+
# Now you can use:
|
|
232
|
+
# - has_role?(:admin) instead of user.has_role?(:admin)
|
|
233
|
+
# - in_group?(:editors) instead of user.in_group?(:editors)
|
|
234
|
+
# - resource_role?(client, role)
|
|
235
|
+
# - has_permission?(:manage_all)
|
|
236
|
+
end
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### Requirements
|
|
240
|
+
|
|
241
|
+
- The policy must have a `user` method that returns a `Verikloak::Pundit::UserContext` (or subclass)
|
|
242
|
+
- If `user` is `nil`, delegation methods will raise `NoMethodError`
|
|
243
|
+
|
|
244
|
+
### Compatibility with Custom UserContext
|
|
245
|
+
|
|
246
|
+
Delegations work with any class that inherits from `Verikloak::Pundit::UserContext`:
|
|
247
|
+
|
|
248
|
+
```ruby
|
|
249
|
+
# Works with AppUserContext (shown above)
|
|
250
|
+
class DocumentPolicy < ApplicationPolicy
|
|
251
|
+
include Verikloak::Pundit::Delegations
|
|
252
|
+
|
|
253
|
+
def update?
|
|
254
|
+
has_role?(:admin) || has_permission?(:write_documents)
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### Handling nil user
|
|
260
|
+
|
|
261
|
+
For policies that may receive `nil` users (e.g., public endpoints), you **must** guard against nil before calling delegation methods:
|
|
262
|
+
|
|
263
|
+
```ruby
|
|
264
|
+
class PublicDocumentPolicy < ApplicationPolicy
|
|
265
|
+
def show?
|
|
266
|
+
return true if record.public?
|
|
267
|
+
return false unless user # Guard against nil user before using delegations
|
|
268
|
+
|
|
269
|
+
has_role?(:viewer)
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
Alternatively, create a helper method in your `ApplicationPolicy`:
|
|
275
|
+
|
|
276
|
+
```ruby
|
|
277
|
+
class ApplicationPolicy
|
|
278
|
+
include Verikloak::Pundit::Delegations
|
|
279
|
+
|
|
280
|
+
private
|
|
281
|
+
|
|
282
|
+
def authenticated?
|
|
283
|
+
!user.nil?
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
def safe_has_role?(role)
|
|
287
|
+
authenticated? && has_role?(role)
|
|
288
|
+
end
|
|
289
|
+
end
|
|
290
|
+
```
|
|
291
|
+
|
|
121
292
|
## Non-Rails / custom usage
|
|
122
293
|
|
|
123
294
|
```ruby
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: verikloak-pundit
|
|
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
|
|
@@ -49,7 +49,7 @@ dependencies:
|
|
|
49
49
|
requirements:
|
|
50
50
|
- - ">="
|
|
51
51
|
- !ruby/object:Gem::Version
|
|
52
|
-
version: 0.
|
|
52
|
+
version: 0.3.0
|
|
53
53
|
- - "<"
|
|
54
54
|
- !ruby/object:Gem::Version
|
|
55
55
|
version: 1.0.0
|
|
@@ -59,7 +59,7 @@ dependencies:
|
|
|
59
59
|
requirements:
|
|
60
60
|
- - ">="
|
|
61
61
|
- !ruby/object:Gem::Version
|
|
62
|
-
version: 0.
|
|
62
|
+
version: 0.3.0
|
|
63
63
|
- - "<"
|
|
64
64
|
- !ruby/object:Gem::Version
|
|
65
65
|
version: 1.0.0
|
|
@@ -94,7 +94,7 @@ metadata:
|
|
|
94
94
|
source_code_uri: https://github.com/taiyaky/verikloak-pundit
|
|
95
95
|
changelog_uri: https://github.com/taiyaky/verikloak-pundit/blob/main/CHANGELOG.md
|
|
96
96
|
bug_tracker_uri: https://github.com/taiyaky/verikloak-pundit/issues
|
|
97
|
-
documentation_uri: https://rubydoc.info/gems/verikloak-pundit/0.2.
|
|
97
|
+
documentation_uri: https://rubydoc.info/gems/verikloak-pundit/0.2.3
|
|
98
98
|
rubygems_mfa_required: 'true'
|
|
99
99
|
rdoc_options: []
|
|
100
100
|
require_paths:
|