veri 2.1.0 → 3.0.0
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 +37 -1
- data/README.md +153 -67
- data/{lib/veri/models → app/models/veri}/session.rb +48 -23
- data/lib/generators/veri/authentication_generator.rb +10 -2
- data/lib/generators/veri/templates/add_veri_authentication.rb.erb +8 -8
- data/lib/veri/controllers/concerns/authentication.rb +28 -18
- data/lib/veri/engine.rb +25 -0
- data/lib/veri/helpers/migration_helpers.rb +8 -9
- data/lib/veri/inputs/authenticatable.rb +2 -2
- data/lib/veri/inputs/base.rb +2 -2
- data/lib/veri/inputs/boolean.rb +11 -0
- data/lib/veri/inputs/duration.rb +1 -1
- data/lib/veri/inputs/hashing_algorithm.rb +1 -1
- data/lib/veri/inputs/model.rb +4 -6
- data/lib/veri/inputs/non_empty_string.rb +1 -1
- data/lib/veri/inputs/request.rb +11 -0
- data/lib/veri/inputs/tenant.rb +8 -14
- data/lib/veri/models/concerns/authenticatable.rb +36 -20
- data/lib/veri/models/tenant_declaration.rb +11 -0
- data/lib/veri/password/argon2.rb +0 -4
- data/lib/veri/password/bcrypt.rb +0 -4
- data/lib/veri/password/pbkdf2.rb +0 -4
- data/lib/veri/password/scrypt.rb +0 -4
- data/lib/veri/version.rb +1 -1
- data/lib/veri.rb +4 -2
- data/veri.gemspec +4 -5
- metadata +17 -15
- data/lib/veri/railtie.rb +0 -44
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 75dab586d4e890b7be8f0e8f6caf35ec3932f08d62a90e0d88b5f32dc08bb4a6
|
|
4
|
+
data.tar.gz: a82743b51f7f58f6c6951362b4165608eb35dafcc040376911dbd45f453d2709
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '09814524d65a467ea91c8a2692355612c682be9b22ca6d9b6052f015b9644b0cd02190fd9ac387902954b94a7c1d263489dcdbf2cf869530631eba9faa2525d7'
|
|
7
|
+
data.tar.gz: 8975d9e8bd2f83cb76aeb58a33291cf7ce18be20ae6a98a0fd156cdcc04b4704b2c0736711e0e6f4ffe00e05e1c37b5e70670e45b7030734c119389914d5dde2
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,39 @@
|
|
|
1
|
+
## v3.0.0
|
|
2
|
+
|
|
3
|
+
### Breaking
|
|
4
|
+
|
|
5
|
+
- Dropped support for Rails 8.0 and earlier
|
|
6
|
+
- Dropped support for Ruby 3.2
|
|
7
|
+
- Changed `user_agent` column in `veri_sessions` table from string to text
|
|
8
|
+
- Renamed account locking to suspension
|
|
9
|
+
- Added algorithm markers to stored password hashes
|
|
10
|
+
- `update_password` now terminates all sessions of the user by default
|
|
11
|
+
- The tenant is now passed to `log_in` and `current_tenant` returns the tenant of the current session
|
|
12
|
+
- Models used as tenants must now be declared with `veri_tenant`
|
|
13
|
+
|
|
14
|
+
### Features
|
|
15
|
+
|
|
16
|
+
- Added `veri_tenant` to declare tenant models and delete their sessions when they are destroyed
|
|
17
|
+
- Added `shapeshifter_sessions` association to fetch the sessions in which a user is impersonating someone
|
|
18
|
+
- Added `Veri::Session.from_request` method to fetch the session from a request object
|
|
19
|
+
- Added `Veri::Session.shapeshifted` to fetch impersonating sessions
|
|
20
|
+
- Added `Veri::Session.in_original_tenant` to fetch impersonating sessions that return to a tenant
|
|
21
|
+
- Added support for binary UUID primary keys in MySQL
|
|
22
|
+
|
|
23
|
+
### Bugs
|
|
24
|
+
|
|
25
|
+
- Fixed Rails commands failing when the user model cannot be loaded
|
|
26
|
+
- Fixed migration helpers raising when the tenant has no sessions, which broke migrations on fresh databases
|
|
27
|
+
- Fixed `true_tenant` returning the impersonated tenant when the impersonator had no tenant
|
|
28
|
+
- Fixed the generated migration failing when the user table's primary key is not `id`
|
|
29
|
+
- Fixed the generated migration not working with UUID primary keys on some databases
|
|
30
|
+
|
|
31
|
+
### Misc
|
|
32
|
+
|
|
33
|
+
- The generator now takes the primary key type from the existing user table, making `--uuid` unnecessary
|
|
34
|
+
|
|
35
|
+
To upgrade to v3.0.0, please refer to the [migration guide](https://github.com/brownboxdev/veri-authentication/discussions/28)
|
|
36
|
+
|
|
1
37
|
## v2.1.0
|
|
2
38
|
|
|
3
39
|
### Features
|
|
@@ -56,7 +92,7 @@
|
|
|
56
92
|
|
|
57
93
|
- Dropped some unnecessary dependencies
|
|
58
94
|
|
|
59
|
-
To upgrade to v2.0.0, please refer to the [migration guide](https://github.com/
|
|
95
|
+
To upgrade to v2.0.0, please refer to the [migration guide](https://github.com/brownboxdev/veri-authentication/discussions/14)
|
|
60
96
|
|
|
61
97
|
## v1.1.0
|
|
62
98
|
|
data/README.md
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
[](http://badge.fury.io/rb/veri)
|
|
4
4
|
[](https://rubygems.org/gems/veri)
|
|
5
|
-
[](https://github.com/brownboxdev/veri-authentication/actions/workflows/ci.yml)
|
|
6
|
+
[](LICENSE)
|
|
7
7
|
|
|
8
8
|
Veri is a cookie-based authentication library for Ruby on Rails. It provides only essential building blocks for secure user authentication without cluttering your app with generated controllers, views, and mailers. This makes it ideal for building custom authentication flows.
|
|
9
9
|
|
|
@@ -11,7 +11,7 @@ Veri supports multi-tenancy, granular session management, multiple password hash
|
|
|
11
11
|
|
|
12
12
|
**Example of Usage:**
|
|
13
13
|
|
|
14
|
-
Consider a multi-tenant SaaS application where users need to manage their active sessions across devices and browsers, terminating specific sessions remotely when needed. Administrators require similar capabilities in their admin panel, with additional powers to
|
|
14
|
+
Consider a multi-tenant SaaS application where users need to manage their active sessions across devices and browsers, terminating specific sessions remotely when needed. Administrators require similar capabilities in their admin panel, with additional powers to suspend accounts and temporarily assume user identities for troubleshooting. You can build all this easily with Veri.
|
|
15
15
|
|
|
16
16
|
## Table of Contents
|
|
17
17
|
|
|
@@ -21,7 +21,7 @@ Consider a multi-tenant SaaS application where users need to manage their active
|
|
|
21
21
|
- [Password Management](#password-management)
|
|
22
22
|
- [Controller Integration](#controller-integration)
|
|
23
23
|
- [Authentication Sessions](#authentication-sessions)
|
|
24
|
-
- [Account
|
|
24
|
+
- [Account Suspension](#account-suspension)
|
|
25
25
|
- [User Impersonation](#user-impersonation)
|
|
26
26
|
- [Multi-Tenancy](#multi-tenancy)
|
|
27
27
|
- [View Helpers](#view-helpers)
|
|
@@ -50,13 +50,32 @@ bundle install
|
|
|
50
50
|
Generate the migration for your user model (replace `users` with your table name if different):
|
|
51
51
|
|
|
52
52
|
```shell
|
|
53
|
-
# For standard integer IDs
|
|
54
53
|
rails generate veri:authentication users
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Veri's sessions table uses the same primary key type as your users table, so the table must exist before you run the generator.
|
|
57
|
+
|
|
58
|
+
If your database can't handle your UUIDs on its own, apply your app's UUID handling to Veri's session model in an initializer. The examples below show which attributes need it; the code itself depends on how your app handles UUIDs. If your app generates UUIDs:
|
|
55
59
|
|
|
56
|
-
|
|
57
|
-
|
|
60
|
+
```rb
|
|
61
|
+
Rails.application.config.to_prepare do
|
|
62
|
+
Veri::Session.before_create { self.id ||= SecureRandom.uuid }
|
|
63
|
+
end
|
|
58
64
|
```
|
|
59
65
|
|
|
66
|
+
If it also stores them as binary, add your binary type on top of that:
|
|
67
|
+
|
|
68
|
+
```rb
|
|
69
|
+
Rails.application.config.to_prepare do
|
|
70
|
+
Veri::Session.before_create { self.id ||= SecureRandom.uuid }
|
|
71
|
+
[:id, :authenticatable_id, :original_authenticatable_id, :tenant_id, :original_tenant_id].each do |name|
|
|
72
|
+
Veri::Session.attribute name, YourBinaryUuidType.new
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
If your app sets any other kind of id itself, such as its own integer ids, do the same with your own id handling.
|
|
78
|
+
|
|
60
79
|
Run the migration:
|
|
61
80
|
|
|
62
81
|
```shell
|
|
@@ -97,11 +116,16 @@ user.verify_password("password")
|
|
|
97
116
|
# Check if a password is set
|
|
98
117
|
user.password_set?
|
|
99
118
|
```
|
|
100
|
-
|
|
119
|
+
|
|
120
|
+
Updating a password terminates all of the user's sessions, including the current one. To keep the user signed in, log them in again or opt out of session termination:
|
|
101
121
|
|
|
102
122
|
```rb
|
|
123
|
+
# Update a password and keep the user signed in
|
|
103
124
|
user.update_password(new_password)
|
|
104
|
-
user
|
|
125
|
+
log_in(user)
|
|
126
|
+
|
|
127
|
+
# Update a password without terminating sessions
|
|
128
|
+
user.update_password(new_password, terminate_sessions: false)
|
|
105
129
|
```
|
|
106
130
|
|
|
107
131
|
## Controller Integration
|
|
@@ -178,7 +202,7 @@ current_user
|
|
|
178
202
|
# Returns true if user is authenticated
|
|
179
203
|
logged_in?
|
|
180
204
|
|
|
181
|
-
# Authenticates user, returns true on success or false if account is
|
|
205
|
+
# Authenticates user, returns true on success or false if account is suspended
|
|
182
206
|
log_in(user)
|
|
183
207
|
|
|
184
208
|
# Terminates current session
|
|
@@ -209,8 +233,6 @@ class ApplicationController < ActionController::Base
|
|
|
209
233
|
end
|
|
210
234
|
```
|
|
211
235
|
|
|
212
|
-
The `when_unauthenticated` method can be overridden in any controller to provide controller-specific handling.
|
|
213
|
-
|
|
214
236
|
## Authentication Sessions
|
|
215
237
|
|
|
216
238
|
Veri stores authentication sessions in the database, providing session management capabilities:
|
|
@@ -223,6 +245,33 @@ user.sessions
|
|
|
223
245
|
|
|
224
246
|
# Get current session in controller
|
|
225
247
|
current_session
|
|
248
|
+
|
|
249
|
+
# Get session from a request
|
|
250
|
+
Veri::Session.from_request(request)
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### Sessions Outside Controllers
|
|
254
|
+
|
|
255
|
+
Code that handles a request outside of controllers, such as Action Cable connections and route constraints, can find the current session from the request:
|
|
256
|
+
|
|
257
|
+
```rb
|
|
258
|
+
module ApplicationCable
|
|
259
|
+
class Connection < ActionCable::Connection::Base
|
|
260
|
+
identified_by :current_user
|
|
261
|
+
|
|
262
|
+
# Runs only when the connection opens; disconnecting it after the session is terminated is up to your application
|
|
263
|
+
def connect
|
|
264
|
+
self.current_user = Veri::Session.from_request(request)&.identity || reject_unauthorized_connection
|
|
265
|
+
end
|
|
266
|
+
end
|
|
267
|
+
end
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
```rb
|
|
271
|
+
# Only administrators can open the jobs dashboard
|
|
272
|
+
constraints -> (request) { Veri::Session.from_request(request)&.identity&.admin? } do
|
|
273
|
+
mount Sidekiq::Web => "/jobs"
|
|
274
|
+
end
|
|
226
275
|
```
|
|
227
276
|
|
|
228
277
|
### Session Information
|
|
@@ -283,28 +332,30 @@ user.sessions.inactive.terminate_all
|
|
|
283
332
|
Veri::Session.expired.terminate_all
|
|
284
333
|
```
|
|
285
334
|
|
|
286
|
-
|
|
335
|
+
When the current session is terminated, the controller still sees it for the rest of the request, so `logged_in?` stays true until the next request unless you call `log_in` or `log_out`. The same applies when a password update or an account suspension terminates it.
|
|
287
336
|
|
|
288
|
-
|
|
337
|
+
## Account Suspension
|
|
338
|
+
|
|
339
|
+
Veri provides account suspension functionality to temporarily disable user accounts.
|
|
289
340
|
|
|
290
341
|
```rb
|
|
291
|
-
#
|
|
292
|
-
user.
|
|
342
|
+
# Suspend a user account
|
|
343
|
+
user.suspend!
|
|
293
344
|
|
|
294
|
-
#
|
|
295
|
-
user.
|
|
345
|
+
# Unsuspend a user account
|
|
346
|
+
user.unsuspend!
|
|
296
347
|
|
|
297
|
-
# Check if account is
|
|
298
|
-
user.
|
|
348
|
+
# Check if account is suspended
|
|
349
|
+
user.suspended?
|
|
299
350
|
|
|
300
|
-
# Fetch
|
|
301
|
-
User.
|
|
351
|
+
# Fetch suspended users
|
|
352
|
+
User.suspended
|
|
302
353
|
|
|
303
|
-
# Fetch
|
|
304
|
-
User.
|
|
354
|
+
# Fetch unsuspended users
|
|
355
|
+
User.unsuspended
|
|
305
356
|
```
|
|
306
357
|
|
|
307
|
-
When an account is
|
|
358
|
+
When an account is suspended, the user cannot log in. If they're already logged in, their sessions are terminated and they are treated as unauthenticated.
|
|
308
359
|
|
|
309
360
|
## User Impersonation
|
|
310
361
|
|
|
@@ -342,6 +393,16 @@ session.shapeshifted?
|
|
|
342
393
|
|
|
343
394
|
# Returns original user when shapeshifted, otherwise current user
|
|
344
395
|
session.true_identity
|
|
396
|
+
|
|
397
|
+
# Fetch the sessions in which someone is impersonating this user
|
|
398
|
+
user.sessions.shapeshifted
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
User method:
|
|
402
|
+
|
|
403
|
+
```rb
|
|
404
|
+
# Returns the sessions in which a user is impersonating someone
|
|
405
|
+
user.shapeshifter_sessions
|
|
345
406
|
```
|
|
346
407
|
|
|
347
408
|
Controller helper:
|
|
@@ -353,31 +414,40 @@ shapeshifter?
|
|
|
353
414
|
|
|
354
415
|
## Multi-Tenancy
|
|
355
416
|
|
|
356
|
-
Veri supports multi-tenancy, allowing you to
|
|
417
|
+
Veri supports multi-tenancy, allowing you to tie authentication sessions to tenants such as organizations, clients, or subdomains.
|
|
357
418
|
|
|
358
419
|
### Setup
|
|
359
420
|
|
|
360
|
-
|
|
421
|
+
Pass the tenant when logging a user in:
|
|
361
422
|
|
|
362
423
|
```rb
|
|
363
|
-
|
|
364
|
-
|
|
424
|
+
# Option 1: String-based tenancy (e.g., subdomain)
|
|
425
|
+
log_in(user, tenant: request.subdomain)
|
|
365
426
|
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
427
|
+
# Option 2: Model-based tenancy (e.g., organization)
|
|
428
|
+
log_in(user, tenant: company)
|
|
429
|
+
```
|
|
369
430
|
|
|
370
|
-
|
|
371
|
-
# Option 1: String-based tenancy (e.g., subdomain)
|
|
372
|
-
request.subdomain
|
|
431
|
+
Tenants can be represented as either a string or an instance of a model declared as a tenant:
|
|
373
432
|
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
433
|
+
```rb
|
|
434
|
+
class Company < ApplicationRecord
|
|
435
|
+
veri_tenant
|
|
377
436
|
end
|
|
378
437
|
```
|
|
379
438
|
|
|
380
|
-
|
|
439
|
+
Without the `tenant:` argument the session has no tenant, which is the single-tenant setup.
|
|
440
|
+
|
|
441
|
+
Use `current_tenant` in controllers and views to get the tenant of the current session:
|
|
442
|
+
|
|
443
|
+
```rb
|
|
444
|
+
# Returns the tenant (string, model instance, or nil if the session has no tenant)
|
|
445
|
+
current_tenant
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
Since `nil` is a valid tenant, `current_tenant` raises an error when no user is logged in.
|
|
449
|
+
|
|
450
|
+
Veri only stores the tenant in the session and returns it. The session itself is found by its cookie alone, whatever its tenant. How your application uses the tenant, if at all, is entirely up to it, so tenant management and scoping are the application's responsibility.
|
|
381
451
|
|
|
382
452
|
### Session Tenant Access
|
|
383
453
|
|
|
@@ -399,41 +469,30 @@ user.sessions.in_tenant(tenant)
|
|
|
399
469
|
|
|
400
470
|
# Terminate all sessions for a specific user within a tenant
|
|
401
471
|
user.sessions.in_tenant(tenant).terminate_all
|
|
472
|
+
|
|
473
|
+
# If you use impersonation, fetch a user's impersonating sessions that return to a given tenant
|
|
474
|
+
user.shapeshifter_sessions.in_original_tenant(tenant)
|
|
402
475
|
```
|
|
403
476
|
|
|
404
477
|
### Switching Tenants
|
|
405
478
|
|
|
406
479
|
A session belongs to one tenant, and a browser holds one session per host. A user is therefore logged in to one tenant at a time, unless your tenants live on separate subdomains, where each subdomain keeps its own session.
|
|
407
480
|
|
|
408
|
-
To move a user to another tenant, call `switch_tenant` on their session
|
|
481
|
+
To move a user to another tenant, call `switch_tenant` on their session:
|
|
409
482
|
|
|
410
483
|
```rb
|
|
411
|
-
class ApplicationController < ActionController::Base
|
|
412
|
-
include Veri::Authentication
|
|
413
|
-
|
|
414
|
-
with_authentication
|
|
415
|
-
|
|
416
|
-
private
|
|
417
|
-
|
|
418
|
-
def current_tenant
|
|
419
|
-
# This example keeps the tenant in a cookie; your app can keep it anywhere
|
|
420
|
-
Company.find_by(id: cookies.signed[:company_id])
|
|
421
|
-
end
|
|
422
|
-
end
|
|
423
|
-
|
|
424
484
|
class CompaniesController < ApplicationController
|
|
425
485
|
def switch
|
|
426
486
|
company = current_user.companies.find(params[:id])
|
|
427
487
|
|
|
428
488
|
current_session.switch_tenant(company)
|
|
429
|
-
cookies.signed[:company_id] = company.id
|
|
430
489
|
|
|
431
490
|
redirect_to root_path, notice: "Switched to #{company.name}"
|
|
432
491
|
end
|
|
433
492
|
end
|
|
434
493
|
```
|
|
435
494
|
|
|
436
|
-
The user stays logged in and the session keeps its expiration.
|
|
495
|
+
The user stays logged in and the session keeps its expiration. This also covers applications where the tenant is not known at login: log the user in without a tenant and switch once they choose one.
|
|
437
496
|
|
|
438
497
|
### User Impersonation with Tenants
|
|
439
498
|
|
|
@@ -453,9 +512,9 @@ All other session methods work the same way in multi-tenant applications as in s
|
|
|
453
512
|
|
|
454
513
|
### Orphaned Sessions
|
|
455
514
|
|
|
456
|
-
|
|
515
|
+
Destroying a record of a model declared with `veri_tenant` also deletes its sessions, including the impersonating sessions that return to it, so their users are logged out.
|
|
457
516
|
|
|
458
|
-
|
|
517
|
+
Sessions become orphaned when their tenant is removed without running Active Record callbacks, for example with `delete` or raw SQL, or is destroyed after its model stopped declaring `veri_tenant`. Users with orphaned sessions remain logged in, and looking up their tenant raises an error. Use `prune` as a fallback to delete orphaned sessions:
|
|
459
518
|
|
|
460
519
|
```rb
|
|
461
520
|
Veri::Session.prune
|
|
@@ -463,7 +522,7 @@ Veri::Session.prune
|
|
|
463
522
|
|
|
464
523
|
### Tenant Migrations
|
|
465
524
|
|
|
466
|
-
When you rename or remove models used as tenants, you need to update Veri's stored data accordingly.
|
|
525
|
+
When you rename or remove models used as tenants, you need to update Veri's stored data accordingly. Veri adds these helpers to your migrations. The changes they make are irreversible:
|
|
467
526
|
|
|
468
527
|
```rb
|
|
469
528
|
# Rename a tenant class (e.g., when you rename your Organization model to Company)
|
|
@@ -482,9 +541,9 @@ Access authentication state in your views:
|
|
|
482
541
|
<p>Welcome, <%= current_user.name %>!</p>
|
|
483
542
|
<% if shapeshifter? %>
|
|
484
543
|
<p><em>Currently viewing as <%= current_user.name %> (Original: <%= current_session.true_identity.name %>)</em></p>
|
|
485
|
-
<%=
|
|
544
|
+
<%= button_to "Return to Original Identity", revert_path, method: :patch %>
|
|
486
545
|
<% end %>
|
|
487
|
-
<%=
|
|
546
|
+
<%= button_to "Logout", logout_path, method: :delete %>
|
|
488
547
|
<% else %>
|
|
489
548
|
<%= link_to "Login", login_path %>
|
|
490
549
|
<% end %>
|
|
@@ -534,11 +593,38 @@ RSpec.configure do |config|
|
|
|
534
593
|
end
|
|
535
594
|
```
|
|
536
595
|
|
|
596
|
+
### Sessions as Test Data
|
|
597
|
+
|
|
598
|
+
Code that receives a session, such as a service object that terminates all sessions but the current one, can be tested without logging in. Create the session directly:
|
|
599
|
+
|
|
600
|
+
```rb
|
|
601
|
+
module SessionHelpers
|
|
602
|
+
def create_session_for(user, tenant: nil)
|
|
603
|
+
session = Veri::Session.create!(
|
|
604
|
+
authenticatable: user,
|
|
605
|
+
hashed_token: SecureRandom.hex,
|
|
606
|
+
expires_at: 1.day.from_now,
|
|
607
|
+
last_seen_at: Time.current
|
|
608
|
+
)
|
|
609
|
+
session.switch_tenant(tenant)
|
|
610
|
+
session
|
|
611
|
+
end
|
|
612
|
+
end
|
|
613
|
+
|
|
614
|
+
# In your spec_helper.rb
|
|
615
|
+
RSpec.configure do |config|
|
|
616
|
+
config.include SessionHelpers
|
|
617
|
+
end
|
|
618
|
+
|
|
619
|
+
# In a multi-tenant application
|
|
620
|
+
create_session_for(user, tenant: company)
|
|
621
|
+
```
|
|
622
|
+
|
|
537
623
|
## Getting Help and Contributing
|
|
538
624
|
|
|
539
625
|
### Getting Help
|
|
540
626
|
|
|
541
|
-
Have a question or need assistance? Open a discussion in the [discussions section](https://github.com/
|
|
627
|
+
Have a question or need assistance? Open a discussion in the [discussions section](https://github.com/brownboxdev/veri-authentication/discussions) for:
|
|
542
628
|
|
|
543
629
|
- Usage questions
|
|
544
630
|
- Implementation guidance
|
|
@@ -546,7 +632,7 @@ Have a question or need assistance? Open a discussion in the [discussions sectio
|
|
|
546
632
|
|
|
547
633
|
### Issues
|
|
548
634
|
|
|
549
|
-
[Issues](https://github.com/
|
|
635
|
+
[Issues](https://github.com/brownboxdev/veri-authentication/issues) track bugs, planned features, and other work. When reporting a bug, please include:
|
|
550
636
|
|
|
551
637
|
- A clear description of the problem
|
|
552
638
|
- Steps to reproduce the issue
|
|
@@ -554,20 +640,20 @@ Have a question or need assistance? Open a discussion in the [discussions sectio
|
|
|
554
640
|
|
|
555
641
|
### Contributing Code
|
|
556
642
|
|
|
557
|
-
Any open issue is free to pick up or discuss. Check the [
|
|
643
|
+
Any open issue is free to pick up or discuss. Check the [issues tab](https://github.com/brownboxdev/veri-authentication/issues).
|
|
558
644
|
|
|
559
|
-
Before contributing, please read the [contributing guidelines](https://github.com/
|
|
645
|
+
Before contributing, please read the [contributing guidelines](https://github.com/brownboxdev/veri-authentication/blob/main/CONTRIBUTING.md).
|
|
560
646
|
|
|
561
647
|
## License
|
|
562
648
|
|
|
563
|
-
The gem is available as open source under the terms of the [MIT License](https://github.com/
|
|
649
|
+
The gem is available as open source under the terms of the [MIT License](https://github.com/brownboxdev/veri-authentication/blob/main/LICENSE.txt).
|
|
564
650
|
|
|
565
651
|
## Code of Conduct
|
|
566
652
|
|
|
567
|
-
Everyone interacting in the Veri project is expected to follow the [code of conduct](https://github.com/
|
|
653
|
+
Everyone interacting in the Veri project is expected to follow the [code of conduct](https://github.com/brownboxdev/veri-authentication/blob/main/CODE_OF_CONDUCT.md).
|
|
568
654
|
|
|
569
655
|
## Old Versions
|
|
570
656
|
|
|
571
657
|
Only the latest major version is actively maintained. READMEs for older versions are available here for reference:
|
|
572
658
|
|
|
573
|
-
[v1.x.x](https://github.com/
|
|
659
|
+
[v2.x.x](https://github.com/brownboxdev/veri-authentication/blob/e6909d2689c2ae11919fc3d1f3c50c19b2995a91/README.md) | [v1.x.x](https://github.com/brownboxdev/veri-authentication/blob/9c188e16a703141b7cd89dd31d5cd49a557f143d/README.md)
|
|
@@ -5,11 +5,22 @@ module Veri
|
|
|
5
5
|
class Session < ActiveRecord::Base
|
|
6
6
|
self.table_name = "veri_sessions"
|
|
7
7
|
|
|
8
|
+
belongs_to :authenticatable, class_name: Veri::Configuration.user_model_name
|
|
9
|
+
belongs_to :original_authenticatable, class_name: Veri::Configuration.user_model_name, optional: true
|
|
8
10
|
belongs_to :tenant, polymorphic: true, optional: true
|
|
9
11
|
belongs_to :original_tenant, polymorphic: true, optional: true
|
|
10
12
|
|
|
13
|
+
scope :shapeshifted, -> { where.not(original_authenticatable_id: nil) }
|
|
11
14
|
scope :in_tenant, -> (tenant) { where(**Veri::Inputs::Tenant.new(tenant).resolve) }
|
|
12
|
-
scope :
|
|
15
|
+
scope :in_original_tenant, -> (tenant) do
|
|
16
|
+
resolved_tenant = Veri::Inputs::Tenant.new(tenant).resolve
|
|
17
|
+
shapeshifted.where(original_tenant_type: resolved_tenant[:tenant_type], original_tenant_id: resolved_tenant[:tenant_id])
|
|
18
|
+
end
|
|
19
|
+
scope :active, -> do
|
|
20
|
+
inactive_session_lifetime = Veri::Configuration.inactive_session_lifetime
|
|
21
|
+
active = where(expires_at: Time.current..)
|
|
22
|
+
inactive_session_lifetime ? active.where(last_seen_at: (Time.current - inactive_session_lifetime)..) : active
|
|
23
|
+
end
|
|
13
24
|
scope :expired, -> { where(expires_at: ...Time.current) }
|
|
14
25
|
scope :inactive, -> do
|
|
15
26
|
inactive_session_lifetime = Veri::Configuration.inactive_session_lifetime
|
|
@@ -53,23 +64,30 @@ module Veri
|
|
|
53
64
|
end
|
|
54
65
|
|
|
55
66
|
def identity = authenticatable
|
|
56
|
-
def shapeshifted? =
|
|
57
|
-
def true_identity = original_authenticatable
|
|
58
|
-
def true_tenant = original_tenant
|
|
67
|
+
def shapeshifted? = original_authenticatable_id.present?
|
|
68
|
+
def true_identity = shapeshifted? ? original_authenticatable : authenticatable
|
|
69
|
+
def true_tenant = shapeshifted? ? original_tenant : tenant
|
|
59
70
|
|
|
60
71
|
def shapeshift(user, tenant: nil)
|
|
61
72
|
raise Veri::Error, "Cannot shapeshift from a shapeshifted session" if shapeshifted?
|
|
62
73
|
|
|
74
|
+
processed_authenticatable = Veri::Inputs::Authenticatable.new(user).process
|
|
63
75
|
resolved_tenant = Veri::Inputs::Tenant.new(tenant).resolve
|
|
64
76
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
77
|
+
processed_authenticatable.transaction do
|
|
78
|
+
locked_authenticatable = processed_authenticatable.class.unscoped.lock.find(processed_authenticatable.id)
|
|
79
|
+
|
|
80
|
+
raise Veri::Error, "Cannot shapeshift into a suspended user" if locked_authenticatable.suspended?
|
|
81
|
+
|
|
82
|
+
update!(
|
|
83
|
+
shapeshifted_at: Time.current,
|
|
84
|
+
original_authenticatable: authenticatable,
|
|
85
|
+
original_tenant_type: tenant_type,
|
|
86
|
+
original_tenant_id: tenant_id,
|
|
87
|
+
**resolved_tenant,
|
|
88
|
+
authenticatable: processed_authenticatable
|
|
89
|
+
)
|
|
90
|
+
end
|
|
73
91
|
end
|
|
74
92
|
|
|
75
93
|
def to_true_identity
|
|
@@ -101,34 +119,41 @@ module Veri
|
|
|
101
119
|
token = SecureRandom.hex(32)
|
|
102
120
|
expires_at = Time.current + Veri::Configuration.total_session_lifetime
|
|
103
121
|
|
|
104
|
-
new(
|
|
122
|
+
session = new(
|
|
105
123
|
hashed_token: digest_token(token),
|
|
106
124
|
expires_at:,
|
|
107
125
|
authenticatable: user,
|
|
108
126
|
**resolved_tenant
|
|
109
|
-
)
|
|
127
|
+
)
|
|
128
|
+
session.update_info(request)
|
|
110
129
|
|
|
111
130
|
token
|
|
112
131
|
end
|
|
113
132
|
|
|
114
133
|
def prune
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
session.id
|
|
119
|
-
end
|
|
134
|
+
{ tenant_type: :tenant_id, original_tenant_type: :original_tenant_id }.each do |type_column, id_column|
|
|
135
|
+
where.not(id_column => nil).distinct.pluck(type_column).each do |tenant_type|
|
|
136
|
+
tenant_class = tenant_type.constantize
|
|
120
137
|
|
|
121
|
-
|
|
138
|
+
where(type_column => tenant_type).where.not(id_column => tenant_class.select(tenant_class.primary_key)).delete_all
|
|
139
|
+
end
|
|
140
|
+
end
|
|
122
141
|
end
|
|
123
142
|
|
|
124
143
|
alias terminate_all delete_all
|
|
125
144
|
|
|
126
|
-
def
|
|
145
|
+
def from_request(request)
|
|
146
|
+
token = Veri::Inputs::Request.new(request).process.cookie_jar.encrypted["veri_token"]
|
|
147
|
+
|
|
127
148
|
return nil if token.blank?
|
|
128
149
|
|
|
129
|
-
session = find_by(hashed_token: digest_token(token)
|
|
150
|
+
session = eager_load(:authenticatable, :original_authenticatable).find_by(hashed_token: digest_token(token))
|
|
151
|
+
|
|
152
|
+
return nil unless session&.active? && session.identity && session.true_identity
|
|
153
|
+
|
|
154
|
+
raise Veri::Error, "Session belongs to a suspended user" if session.identity.suspended? || session.true_identity.suspended?
|
|
130
155
|
|
|
131
|
-
session
|
|
156
|
+
session
|
|
132
157
|
end
|
|
133
158
|
|
|
134
159
|
private
|
|
@@ -8,9 +8,17 @@ module Veri
|
|
|
8
8
|
|
|
9
9
|
argument :table_name, type: :string, required: true
|
|
10
10
|
|
|
11
|
-
class_option :uuid, type: :boolean, default: false, desc: "Use UUIDs as primary keys"
|
|
12
|
-
|
|
13
11
|
def create_migrations
|
|
12
|
+
primary_key = ActiveRecord::Base.with_connection do |connection|
|
|
13
|
+
raise Thor::Error, "Table `#{table_name}` does not exist, run its migration first" unless connection.table_exists?(table_name)
|
|
14
|
+
|
|
15
|
+
connection.columns(table_name).find { |column| column.name == connection.primary_key(table_name) }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
@id_type = primary_key.sql_type
|
|
19
|
+
@sessions_id_type = @id_type unless primary_key.auto_incremented_by_db?
|
|
20
|
+
@sessions_id_default = primary_key.default_function if @sessions_id_type
|
|
21
|
+
|
|
14
22
|
migration_template "add_veri_authentication.rb.erb", "db/migrate/add_veri_authentication.rb"
|
|
15
23
|
end
|
|
16
24
|
|
|
@@ -2,20 +2,20 @@ class AddVeriAuthentication < ActiveRecord::Migration[<%= ActiveRecord::Migratio
|
|
|
2
2
|
def change
|
|
3
3
|
add_column <%= table_name.to_sym.inspect %>, :hashed_password, :text
|
|
4
4
|
add_column <%= table_name.to_sym.inspect %>, :password_updated_at, :datetime
|
|
5
|
-
add_column <%= table_name.to_sym.inspect %>, :
|
|
6
|
-
add_column <%= table_name.to_sym.inspect %>, :
|
|
5
|
+
add_column <%= table_name.to_sym.inspect %>, :suspended, :boolean, default: false, null: false
|
|
6
|
+
add_column <%= table_name.to_sym.inspect %>, :suspended_at, :datetime
|
|
7
7
|
|
|
8
|
-
create_table :veri_sessions<%= ", id:
|
|
8
|
+
create_table :veri_sessions<%= ", id: #{@sessions_id_type.inspect}" if @sessions_id_type %><%= ", default: -> { #{@sessions_id_default.inspect} }" if @sessions_id_default %> do |t|
|
|
9
9
|
t.string :hashed_token, null: false, index: { unique: true }
|
|
10
10
|
t.datetime :expires_at, null: false
|
|
11
|
-
t.belongs_to :authenticatable, null: false, foreign_key: { to_table: <%= table_name.to_sym.inspect %> }, index: true
|
|
12
|
-
t.belongs_to :original_authenticatable, foreign_key: { to_table: <%= table_name.to_sym.inspect %> }, index: true
|
|
13
|
-
t.belongs_to :tenant, polymorphic: true, index: true
|
|
14
|
-
t.belongs_to :original_tenant, polymorphic: true, index: true
|
|
11
|
+
t.belongs_to :authenticatable, null: false, foreign_key: { to_table: <%= table_name.to_sym.inspect %>, primary_key: primary_key(<%= table_name.to_sym.inspect %>) }, index: true, type: <%= @id_type.inspect %>
|
|
12
|
+
t.belongs_to :original_authenticatable, foreign_key: { to_table: <%= table_name.to_sym.inspect %>, primary_key: primary_key(<%= table_name.to_sym.inspect %>) }, index: true, type: <%= @id_type.inspect %>
|
|
13
|
+
t.belongs_to :tenant, polymorphic: true, index: true, type: <%= @id_type.inspect %>
|
|
14
|
+
t.belongs_to :original_tenant, polymorphic: true, index: true, type: <%= @id_type.inspect %>
|
|
15
15
|
t.datetime :shapeshifted_at
|
|
16
16
|
t.datetime :last_seen_at, null: false
|
|
17
17
|
t.string :ip_address
|
|
18
|
-
t.
|
|
18
|
+
t.text :user_agent
|
|
19
19
|
|
|
20
20
|
t.timestamps
|
|
21
21
|
end
|
|
@@ -5,7 +5,7 @@ module Veri
|
|
|
5
5
|
included do
|
|
6
6
|
include ActionController::Cookies unless self < ActionController::Cookies
|
|
7
7
|
|
|
8
|
-
helper_method(:current_user, :logged_in?, :shapeshifter?, :current_session) if respond_to?(:helper_method)
|
|
8
|
+
helper_method(:current_user, :logged_in?, :shapeshifter?, :current_session, :current_tenant) if respond_to?(:helper_method)
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
class_methods do
|
|
@@ -23,31 +23,47 @@ module Veri
|
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
def current_user
|
|
26
|
-
|
|
26
|
+
current_session&.authenticatable
|
|
27
27
|
end
|
|
28
28
|
|
|
29
29
|
def current_session
|
|
30
|
-
|
|
30
|
+
return @current_session if defined?(@current_session)
|
|
31
31
|
|
|
32
|
-
@current_session
|
|
32
|
+
@current_session = Session.from_request(request)
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
-
def
|
|
35
|
+
def current_tenant
|
|
36
|
+
raise Veri::Error, "No user is logged in" unless current_session
|
|
37
|
+
|
|
38
|
+
current_session.tenant
|
|
39
|
+
rescue ActiveRecord::RecordNotFound
|
|
40
|
+
raise Veri::Error, "Tenant not found"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def log_in(authenticatable, tenant: nil)
|
|
36
44
|
processed_authenticatable = Veri::Inputs::Authenticatable.new(authenticatable).process
|
|
45
|
+
resolved_tenant = Veri::Inputs::Tenant.new(tenant).resolve
|
|
46
|
+
|
|
47
|
+
token = processed_authenticatable.transaction do
|
|
48
|
+
locked_authenticatable = processed_authenticatable.class.unscoped.lock.find(processed_authenticatable.id)
|
|
49
|
+
|
|
50
|
+
next if locked_authenticatable.suspended? || locked_authenticatable.password_updated_at != processed_authenticatable.password_updated_at_was
|
|
37
51
|
|
|
38
|
-
|
|
52
|
+
Veri::Session.establish(processed_authenticatable, request, resolved_tenant)
|
|
53
|
+
end
|
|
39
54
|
|
|
40
|
-
|
|
55
|
+
return false unless token
|
|
41
56
|
|
|
57
|
+
current_session&.terminate
|
|
42
58
|
cookies.encrypted.permanent["veri_token"] = { value: token, httponly: true, secure: request.ssl? }
|
|
43
|
-
|
|
59
|
+
reset_memoized_session
|
|
44
60
|
true
|
|
45
61
|
end
|
|
46
62
|
|
|
47
63
|
def log_out
|
|
48
64
|
current_session&.terminate
|
|
49
65
|
cookies.delete("veri_token")
|
|
50
|
-
|
|
66
|
+
reset_memoized_session
|
|
51
67
|
end
|
|
52
68
|
|
|
53
69
|
def logged_in?
|
|
@@ -65,7 +81,7 @@ module Veri
|
|
|
65
81
|
private
|
|
66
82
|
|
|
67
83
|
def with_authentication
|
|
68
|
-
if logged_in?
|
|
84
|
+
if logged_in?
|
|
69
85
|
current_session.update_info(request)
|
|
70
86
|
return
|
|
71
87
|
end
|
|
@@ -81,14 +97,8 @@ module Veri
|
|
|
81
97
|
request.format.html? ? redirect_back_or_to(root_path) : head(:unauthorized)
|
|
82
98
|
end
|
|
83
99
|
|
|
84
|
-
def
|
|
85
|
-
|
|
86
|
-
def resolved_tenant
|
|
87
|
-
Veri::Inputs::Tenant.new(current_tenant).resolve
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
def reset_memoization
|
|
91
|
-
@current_user = @current_session = nil
|
|
100
|
+
def reset_memoized_session
|
|
101
|
+
remove_instance_variable(:@current_session) if defined?(@current_session)
|
|
92
102
|
end
|
|
93
103
|
end
|
|
94
104
|
end
|
data/lib/veri/engine.rb
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
require "rails/engine"
|
|
2
|
+
|
|
3
|
+
module Veri
|
|
4
|
+
class Engine < Rails::Engine
|
|
5
|
+
initializer "veri.include_authenticatable" do
|
|
6
|
+
Rails.autoloaders.each do |autoloader|
|
|
7
|
+
autoloader.on_load do |cpath, klass, _abspath|
|
|
8
|
+
klass.include Veri::Authenticatable if cpath == Veri::Configuration.user_model_name
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
initializer "veri.extend_tenant_declaration" do
|
|
14
|
+
ActiveSupport.on_load :active_record do
|
|
15
|
+
extend Veri::TenantDeclaration
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
initializer "veri.extend_migration_helpers" do
|
|
20
|
+
ActiveSupport.on_load :active_record do
|
|
21
|
+
ActiveRecord::Migration.include Veri::MigrationHelpers
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
module Veri
|
|
2
2
|
module MigrationHelpers
|
|
3
3
|
def migrate_authentication_tenant!(old_tenant, new_tenant)
|
|
4
|
-
|
|
4
|
+
old_tenant_type = Veri::Inputs::NonEmptyString.new(old_tenant).process
|
|
5
|
+
new_tenant_type = Veri::Inputs::NonEmptyString.new(new_tenant).process
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
Veri::Session.transaction do
|
|
8
|
+
Veri::Session.where(tenant_type: old_tenant_type).update_all(tenant_type: new_tenant_type)
|
|
9
|
+
Veri::Session.where(original_tenant_type: old_tenant_type).update_all(original_tenant_type: new_tenant_type)
|
|
10
|
+
end
|
|
10
11
|
end
|
|
11
12
|
|
|
12
13
|
def delete_authentication_tenant!(tenant)
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
raise Veri::InvalidArgumentError, "No sessions exist in tenant #{tenant.inspect}" unless sessions.exists?
|
|
14
|
+
tenant_type = Veri::Inputs::NonEmptyString.new(tenant).process
|
|
16
15
|
|
|
17
|
-
|
|
16
|
+
Veri::Session.where(tenant_type:).or(Veri::Session.where(original_tenant_type: tenant_type)).delete_all
|
|
18
17
|
end
|
|
19
18
|
end
|
|
20
19
|
end
|
|
@@ -3,9 +3,9 @@ module Veri
|
|
|
3
3
|
class Authenticatable < Veri::Inputs::Base
|
|
4
4
|
private
|
|
5
5
|
|
|
6
|
-
def default_message = "Expected
|
|
6
|
+
def default_message = "Expected a persisted instance of #{Veri::Configuration.user_model_name}, got `#{@value.inspect}`"
|
|
7
7
|
|
|
8
|
-
def
|
|
8
|
+
def validate_and_normalize = @value.is_a?(Veri::Configuration.user_model) && @value.persisted? ? @value : raise_error
|
|
9
9
|
end
|
|
10
10
|
end
|
|
11
11
|
end
|
data/lib/veri/inputs/base.rb
CHANGED
data/lib/veri/inputs/duration.rb
CHANGED
|
@@ -3,7 +3,7 @@ module Veri
|
|
|
3
3
|
class HashingAlgorithm < Veri::Inputs::Base
|
|
4
4
|
private
|
|
5
5
|
|
|
6
|
-
def
|
|
6
|
+
def validate_and_normalize = Veri::Configuration::HASHERS.key?(@value) ? @value : raise_error
|
|
7
7
|
end
|
|
8
8
|
end
|
|
9
9
|
end
|
data/lib/veri/inputs/model.rb
CHANGED
|
@@ -3,12 +3,10 @@ module Veri
|
|
|
3
3
|
class Model < Veri::Inputs::Base
|
|
4
4
|
private
|
|
5
5
|
|
|
6
|
-
def
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
model
|
|
11
|
-
}
|
|
6
|
+
def validate_and_normalize
|
|
7
|
+
model = @value.try(:safe_constantize)
|
|
8
|
+
raise_error unless model.is_a?(Class) && model < ActiveRecord::Base
|
|
9
|
+
model
|
|
12
10
|
end
|
|
13
11
|
end
|
|
14
12
|
end
|
|
@@ -5,7 +5,7 @@ module Veri
|
|
|
5
5
|
|
|
6
6
|
def default_message = "Expected a non-empty string, got `#{@value.inspect}`"
|
|
7
7
|
|
|
8
|
-
def
|
|
8
|
+
def validate_and_normalize = @value.is_a?(String) && !@value.empty? ? @value : raise_error
|
|
9
9
|
end
|
|
10
10
|
end
|
|
11
11
|
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
module Veri
|
|
2
|
+
module Inputs
|
|
3
|
+
class Request < Veri::Inputs::Base
|
|
4
|
+
private
|
|
5
|
+
|
|
6
|
+
def default_message = "Expected an instance of ActionDispatch::Request, got `#{@value.inspect}`"
|
|
7
|
+
|
|
8
|
+
def validate_and_normalize = @value.is_a?(ActionDispatch::Request) ? @value : raise_error
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
data/lib/veri/inputs/tenant.rb
CHANGED
|
@@ -7,28 +7,22 @@ module Veri
|
|
|
7
7
|
{ tenant_type: nil, tenant_id: nil }
|
|
8
8
|
when String
|
|
9
9
|
{ tenant_type: tenant, tenant_id: nil }
|
|
10
|
-
when
|
|
11
|
-
|
|
12
|
-
{ tenant_type: tenant.class.to_s, tenant_id: tenant.public_send(tenant.class.primary_key) }
|
|
13
|
-
else
|
|
14
|
-
tenant
|
|
10
|
+
when Veri::Tenantable
|
|
11
|
+
{ tenant_type: tenant.class.to_s, tenant_id: tenant.id }
|
|
15
12
|
end
|
|
16
13
|
end
|
|
17
14
|
|
|
18
15
|
private
|
|
19
16
|
|
|
20
17
|
def default_error = Veri::InvalidTenantError
|
|
21
|
-
def default_message = "Expected a string,
|
|
18
|
+
def default_message = "Expected a string, a persisted instance of a model declared with `veri_tenant`, or nil, got `#{@value.inspect}`"
|
|
22
19
|
|
|
23
|
-
def
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return @value if @value.is_a?(ActiveRecord::Base)
|
|
28
|
-
return @value if @value in { tenant_type: String | nil, tenant_id: String | Integer | nil }
|
|
20
|
+
def validate_and_normalize
|
|
21
|
+
return @value if @value.nil?
|
|
22
|
+
return @value if @value.is_a?(String) && @value.present?
|
|
23
|
+
return @value if @value.is_a?(Veri::Tenantable) && @value.persisted?
|
|
29
24
|
|
|
30
|
-
|
|
31
|
-
}
|
|
25
|
+
raise_error
|
|
32
26
|
end
|
|
33
27
|
end
|
|
34
28
|
end
|
|
@@ -7,19 +7,26 @@ module Veri
|
|
|
7
7
|
|
|
8
8
|
@@included = name
|
|
9
9
|
|
|
10
|
-
has_many :sessions, class_name: "Veri::Session", foreign_key: :authenticatable_id, dependent: :
|
|
10
|
+
has_many :sessions, class_name: "Veri::Session", foreign_key: :authenticatable_id, dependent: :delete_all
|
|
11
|
+
has_many :shapeshifter_sessions, class_name: "Veri::Session", foreign_key: :original_authenticatable_id, dependent: :delete_all
|
|
11
12
|
|
|
12
|
-
scope :
|
|
13
|
-
scope :
|
|
13
|
+
scope :suspended, -> { where(suspended: true) }
|
|
14
|
+
scope :unsuspended, -> { where(suspended: false) }
|
|
14
15
|
end
|
|
15
16
|
|
|
16
|
-
def update_password(password)
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
def update_password(password, terminate_sessions: true)
|
|
18
|
+
processed_password = Veri::Inputs::NonEmptyString.new(password).process
|
|
19
|
+
processed_terminate_sessions = Veri::Inputs::Boolean.new(terminate_sessions).process
|
|
20
|
+
|
|
21
|
+
hashed_password = hash_password(processed_password)
|
|
22
|
+
|
|
23
|
+
transaction do
|
|
24
|
+
update!(hashed_password:, password_updated_at: Time.current)
|
|
25
|
+
|
|
26
|
+
sessions.or(shapeshifter_sessions).terminate_all if processed_terminate_sessions
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
true
|
|
23
30
|
end
|
|
24
31
|
|
|
25
32
|
def password_set?
|
|
@@ -31,32 +38,41 @@ module Veri
|
|
|
31
38
|
|
|
32
39
|
return false unless password_set?
|
|
33
40
|
|
|
34
|
-
|
|
41
|
+
algorithm, hash = hashed_password.split(":", 2)
|
|
42
|
+
stored_hasher = Veri::Configuration::HASHERS[algorithm.to_sym]
|
|
35
43
|
|
|
36
44
|
raise Veri::Error, "Unrecognized password hash format" unless stored_hasher
|
|
37
45
|
|
|
38
|
-
return false unless stored_hasher.verify(processed_password,
|
|
46
|
+
return false unless stored_hasher.verify(processed_password, hash)
|
|
39
47
|
|
|
40
|
-
|
|
48
|
+
unless stored_hasher == Veri::Configuration.hasher
|
|
49
|
+
self.hashed_password = hash_password(processed_password)
|
|
50
|
+
self.class.unscoped.where(self.class.primary_key => id, hashed_password: hashed_password_was).update_all(hashed_password:)
|
|
51
|
+
clear_attribute_changes([:hashed_password])
|
|
52
|
+
end
|
|
41
53
|
|
|
42
54
|
true
|
|
43
55
|
end
|
|
44
56
|
|
|
45
|
-
def
|
|
57
|
+
def suspend!
|
|
46
58
|
transaction do
|
|
47
|
-
update!(
|
|
48
|
-
sessions.terminate_all
|
|
59
|
+
update!(suspended: true, suspended_at: Time.current)
|
|
60
|
+
sessions.or(shapeshifter_sessions).terminate_all
|
|
49
61
|
end
|
|
62
|
+
|
|
63
|
+
true
|
|
50
64
|
end
|
|
51
65
|
|
|
52
|
-
def
|
|
53
|
-
update!(
|
|
66
|
+
def unsuspend!
|
|
67
|
+
update!(suspended: false, suspended_at: nil)
|
|
68
|
+
|
|
69
|
+
true
|
|
54
70
|
end
|
|
55
71
|
|
|
56
72
|
private
|
|
57
73
|
|
|
58
|
-
def
|
|
59
|
-
|
|
74
|
+
def hash_password(password)
|
|
75
|
+
"#{Veri::Configuration.hashing_algorithm}:#{Veri::Configuration.hasher.create(password)}"
|
|
60
76
|
end
|
|
61
77
|
end
|
|
62
78
|
end
|
data/lib/veri/password/argon2.rb
CHANGED
data/lib/veri/password/bcrypt.rb
CHANGED
data/lib/veri/password/pbkdf2.rb
CHANGED
data/lib/veri/password/scrypt.rb
CHANGED
data/lib/veri/version.rb
CHANGED
data/lib/veri.rb
CHANGED
|
@@ -10,10 +10,12 @@ require_relative "veri/password/scrypt"
|
|
|
10
10
|
|
|
11
11
|
require_relative "veri/inputs/base"
|
|
12
12
|
require_relative "veri/inputs/authenticatable"
|
|
13
|
+
require_relative "veri/inputs/boolean"
|
|
13
14
|
require_relative "veri/inputs/duration"
|
|
14
15
|
require_relative "veri/inputs/hashing_algorithm"
|
|
15
16
|
require_relative "veri/inputs/model"
|
|
16
17
|
require_relative "veri/inputs/non_empty_string"
|
|
18
|
+
require_relative "veri/inputs/request"
|
|
17
19
|
require_relative "veri/inputs/tenant"
|
|
18
20
|
require_relative "veri/configuration"
|
|
19
21
|
|
|
@@ -27,9 +29,9 @@ module Veri
|
|
|
27
29
|
module_function :configure
|
|
28
30
|
end
|
|
29
31
|
|
|
30
|
-
require_relative "veri/models/session"
|
|
31
32
|
require_relative "veri/controllers/concerns/authentication"
|
|
32
33
|
require_relative "veri/models/concerns/authenticatable"
|
|
34
|
+
require_relative "veri/models/tenant_declaration"
|
|
33
35
|
require_relative "veri/helpers/migration_helpers"
|
|
34
36
|
|
|
35
|
-
require_relative "veri/
|
|
37
|
+
require_relative "veri/engine"
|
data/veri.gemspec
CHANGED
|
@@ -5,29 +5,28 @@ Gem::Specification.new do |spec|
|
|
|
5
5
|
spec.version = Veri::VERSION
|
|
6
6
|
spec.authors = ["enjaku4"]
|
|
7
7
|
spec.email = ["contact@brownbox.dev"]
|
|
8
|
-
spec.homepage = "https://github.com/
|
|
8
|
+
spec.homepage = "https://github.com/brownboxdev/veri-authentication"
|
|
9
9
|
spec.metadata["homepage_uri"] = spec.homepage
|
|
10
10
|
spec.metadata["source_code_uri"] = spec.homepage
|
|
11
11
|
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
|
|
12
12
|
spec.metadata["bug_tracker_uri"] = "#{spec.homepage}/issues"
|
|
13
13
|
spec.metadata["documentation_uri"] = "#{spec.homepage}/blob/main/README.md"
|
|
14
14
|
spec.metadata["mailing_list_uri"] = "#{spec.homepage}/discussions"
|
|
15
|
-
spec.metadata["funding_uri"] = "https://github.com/sponsors/enjaku4"
|
|
16
15
|
spec.metadata["rubygems_mfa_required"] = "true"
|
|
17
16
|
spec.summary = "Minimal cookie-based authentication library for Ruby on Rails"
|
|
18
17
|
spec.description = "Minimal cookie-based authentication for Rails applications with multi-tenancy support and granular session management"
|
|
19
18
|
spec.license = "MIT"
|
|
20
|
-
spec.required_ruby_version = ">= 3.
|
|
19
|
+
spec.required_ruby_version = ">= 3.3", "< 4.1"
|
|
21
20
|
|
|
22
21
|
spec.files = [
|
|
23
22
|
"veri.gemspec", "README.md", "CHANGELOG.md", "LICENSE.txt"
|
|
24
|
-
] + Dir.glob("lib/**/*")
|
|
23
|
+
] + Dir.glob("{app,lib}/**/*")
|
|
25
24
|
|
|
26
25
|
spec.require_paths = ["lib"]
|
|
27
26
|
|
|
28
27
|
spec.add_dependency "argon2", "~> 2.0"
|
|
29
28
|
spec.add_dependency "bcrypt", "~> 3.0"
|
|
30
|
-
spec.add_dependency "rails", ">=
|
|
29
|
+
spec.add_dependency "rails", ">= 8.1", "< 8.2"
|
|
31
30
|
spec.add_dependency "scrypt", "~> 3.0"
|
|
32
31
|
spec.add_dependency "user_agent_parser", "~> 2.0"
|
|
33
32
|
end
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: veri
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 3.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- enjaku4
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date:
|
|
10
|
+
date: 2026-11-23 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: argon2
|
|
@@ -43,7 +43,7 @@ dependencies:
|
|
|
43
43
|
requirements:
|
|
44
44
|
- - ">="
|
|
45
45
|
- !ruby/object:Gem::Version
|
|
46
|
-
version: '
|
|
46
|
+
version: '8.1'
|
|
47
47
|
- - "<"
|
|
48
48
|
- !ruby/object:Gem::Version
|
|
49
49
|
version: '8.2'
|
|
@@ -53,7 +53,7 @@ dependencies:
|
|
|
53
53
|
requirements:
|
|
54
54
|
- - ">="
|
|
55
55
|
- !ruby/object:Gem::Version
|
|
56
|
-
version: '
|
|
56
|
+
version: '8.1'
|
|
57
57
|
- - "<"
|
|
58
58
|
- !ruby/object:Gem::Version
|
|
59
59
|
version: '8.2'
|
|
@@ -96,39 +96,41 @@ files:
|
|
|
96
96
|
- CHANGELOG.md
|
|
97
97
|
- LICENSE.txt
|
|
98
98
|
- README.md
|
|
99
|
+
- app/models/veri/session.rb
|
|
99
100
|
- lib/generators/veri/authentication_generator.rb
|
|
100
101
|
- lib/generators/veri/templates/add_veri_authentication.rb.erb
|
|
101
102
|
- lib/veri.rb
|
|
102
103
|
- lib/veri/configuration.rb
|
|
103
104
|
- lib/veri/controllers/concerns/authentication.rb
|
|
105
|
+
- lib/veri/engine.rb
|
|
104
106
|
- lib/veri/helpers/migration_helpers.rb
|
|
105
107
|
- lib/veri/inputs/authenticatable.rb
|
|
106
108
|
- lib/veri/inputs/base.rb
|
|
109
|
+
- lib/veri/inputs/boolean.rb
|
|
107
110
|
- lib/veri/inputs/duration.rb
|
|
108
111
|
- lib/veri/inputs/hashing_algorithm.rb
|
|
109
112
|
- lib/veri/inputs/model.rb
|
|
110
113
|
- lib/veri/inputs/non_empty_string.rb
|
|
114
|
+
- lib/veri/inputs/request.rb
|
|
111
115
|
- lib/veri/inputs/tenant.rb
|
|
112
116
|
- lib/veri/models/concerns/authenticatable.rb
|
|
113
|
-
- lib/veri/models/
|
|
117
|
+
- lib/veri/models/tenant_declaration.rb
|
|
114
118
|
- lib/veri/password/argon2.rb
|
|
115
119
|
- lib/veri/password/bcrypt.rb
|
|
116
120
|
- lib/veri/password/pbkdf2.rb
|
|
117
121
|
- lib/veri/password/scrypt.rb
|
|
118
|
-
- lib/veri/railtie.rb
|
|
119
122
|
- lib/veri/version.rb
|
|
120
123
|
- veri.gemspec
|
|
121
|
-
homepage: https://github.com/
|
|
124
|
+
homepage: https://github.com/brownboxdev/veri-authentication
|
|
122
125
|
licenses:
|
|
123
126
|
- MIT
|
|
124
127
|
metadata:
|
|
125
|
-
homepage_uri: https://github.com/
|
|
126
|
-
source_code_uri: https://github.com/
|
|
127
|
-
changelog_uri: https://github.com/
|
|
128
|
-
bug_tracker_uri: https://github.com/
|
|
129
|
-
documentation_uri: https://github.com/
|
|
130
|
-
mailing_list_uri: https://github.com/
|
|
131
|
-
funding_uri: https://github.com/sponsors/enjaku4
|
|
128
|
+
homepage_uri: https://github.com/brownboxdev/veri-authentication
|
|
129
|
+
source_code_uri: https://github.com/brownboxdev/veri-authentication
|
|
130
|
+
changelog_uri: https://github.com/brownboxdev/veri-authentication/blob/main/CHANGELOG.md
|
|
131
|
+
bug_tracker_uri: https://github.com/brownboxdev/veri-authentication/issues
|
|
132
|
+
documentation_uri: https://github.com/brownboxdev/veri-authentication/blob/main/README.md
|
|
133
|
+
mailing_list_uri: https://github.com/brownboxdev/veri-authentication/discussions
|
|
132
134
|
rubygems_mfa_required: 'true'
|
|
133
135
|
rdoc_options: []
|
|
134
136
|
require_paths:
|
|
@@ -137,7 +139,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
137
139
|
requirements:
|
|
138
140
|
- - ">="
|
|
139
141
|
- !ruby/object:Gem::Version
|
|
140
|
-
version: '3.
|
|
142
|
+
version: '3.3'
|
|
141
143
|
- - "<"
|
|
142
144
|
- !ruby/object:Gem::Version
|
|
143
145
|
version: '4.1'
|
data/lib/veri/railtie.rb
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
require "rails/railtie"
|
|
2
|
-
|
|
3
|
-
module Veri
|
|
4
|
-
class Railtie < Rails::Railtie
|
|
5
|
-
def self.server_running?
|
|
6
|
-
!!defined?(Rails::Server)
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
def self.table_exists?
|
|
10
|
-
ActiveRecord::Base.connection.data_source_exists?("veri_sessions")
|
|
11
|
-
rescue ActiveRecord::NoDatabaseError, ActiveRecord::ConnectionNotEstablished
|
|
12
|
-
false
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
initializer "veri.to_prepare" do |app|
|
|
16
|
-
app.config.to_prepare do
|
|
17
|
-
if Veri::Railtie.server_running? && Veri::Railtie.table_exists?
|
|
18
|
-
Veri::Session.where.not(tenant_id: nil).distinct.pluck(:tenant_type).each do |tenant_class|
|
|
19
|
-
tenant_class.constantize
|
|
20
|
-
rescue NameError => e
|
|
21
|
-
raise Veri::Error, "Tenant not found: class `#{e.name}` may have been renamed or deleted"
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
begin
|
|
26
|
-
user_model = Veri::Configuration.user_model
|
|
27
|
-
user_model.include Veri::Authenticatable unless user_model < Veri::Authenticatable
|
|
28
|
-
rescue Veri::ConfigurationError
|
|
29
|
-
raise if Veri::Railtie.server_running?
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
Veri::Session.belongs_to :authenticatable, class_name: Veri::Configuration.user_model_name
|
|
33
|
-
Veri::Session.belongs_to :original_authenticatable, class_name: Veri::Configuration.user_model_name,
|
|
34
|
-
optional: true
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
initializer "veri.extend_migration_helpers" do
|
|
39
|
-
ActiveSupport.on_load :active_record do
|
|
40
|
-
ActiveRecord::Migration.include Veri::MigrationHelpers
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
end
|
|
44
|
-
end
|