rails_simple_auth 1.0.3 → 1.0.5

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: 5fb876e2f8ec9c1f40ed2dbb8d97fdd984e7e5927e11c56be6a2ac4b2aa593a4
4
- data.tar.gz: 29a914c7c8f77a85199d6f7188ce77ab24460554ca90421b22b38646f15464f5
3
+ metadata.gz: 83fb50e708c25bcd5a7115ae8e0935e9921909580aef97ca6ec585100e410e37
4
+ data.tar.gz: 1b7a3bcef439ad6c38155404562167e487144aeeb292ea4fa9436c5252ad1e9c
5
5
  SHA512:
6
- metadata.gz: 8a7d0926e4e1e3a8c7da6ec9ab4de80768ae98a1316a32b6d9bf6aa790f266fc494598344b9a47444bfac1241ab55ea6d11c4c89a56bb2d270cff816a0bb3178
7
- data.tar.gz: b9ea5fa54ad39f51e86dae6852df477760bfb8c28a3c002cae421c40d44e5d3fb74e0ecfaa6ab6d65705cb3ba579d72e4c47061aa2b06d848d5f4639ca9f1eaf
6
+ metadata.gz: df759dcc508dca50b40cd1c85a9c8609a80b2c1a8feb71dfc2ea0a06fe48187bb5c57488a67eecce77eb9e099441bb0609524b48d14b288e318ebcf0e4fec12c
7
+ data.tar.gz: f035ac8532d07768a6a4dd5ce7df1eab2454bf4fd49c53734732b0979f68d8280d876bd174576434c7daedd9f793f029d81a6cc0b335a0f6c608bd915c71d71d
data/CHANGELOG.md CHANGED
@@ -7,6 +7,30 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [1.0.5] - 2025-01-19
11
+
12
+ ### Added
13
+
14
+ - **Secure OmniAuth by default** - Automatically restricts OAuth initiation to POST requests only (prevents CSRF attacks)
15
+
16
+ ## [1.0.4] - 2025-01-19
17
+
18
+ ### Added
19
+
20
+ - **`authenticates_with` DSL** - Cleaner model setup inspired by Devise syntax
21
+ ```ruby
22
+ # Before
23
+ include RailsSimpleAuth::Models::Concerns::Authenticatable
24
+ include RailsSimpleAuth::Models::Concerns::Confirmable
25
+
26
+ # After
27
+ authenticates_with :confirmable, :magic_linkable, :oauth, :temporary
28
+ ```
29
+ - **Devise comparison article** - Comprehensive comparison at `docs/devise-comparison.md`
30
+ - **Admin Users documentation** - Guide for implementing admin functionality
31
+ - **Rate Limiting documentation** - Default limits and customization guide
32
+ - **Session Management documentation** - Expiration, querying, and cleanup
33
+
10
34
  ## [1.0.3] - 2025-01-19
11
35
 
12
36
  ### Added
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  Simple, secure authentication for Rails 8+ applications. Built on Rails primitives with no magic.
4
4
 
5
+ **Coming from Devise?** Read our [detailed comparison](docs/devise-comparison.md).
6
+
5
7
  ## Features
6
8
 
7
9
  - [**Email/Password authentication**](#installation) - secure session-based auth
@@ -36,20 +38,31 @@ rails generate rails_simple_auth:install
36
38
  rails db:migrate
37
39
  ```
38
40
 
39
- Add concerns to your User model:
41
+ Add authentication to your User model:
40
42
 
41
43
  ```ruby
42
44
  class User < ApplicationRecord
43
- include RailsSimpleAuth::Models::Concerns::Authenticatable
44
- include RailsSimpleAuth::Models::Concerns::Confirmable # optional
45
- include RailsSimpleAuth::Models::Concerns::MagicLinkable # optional
46
- include RailsSimpleAuth::Models::Concerns::OAuthConnectable # optional
45
+ authenticates_with :confirmable, :magic_linkable, :oauth, :temporary
47
46
 
48
47
  # Your custom fields and validations
49
48
  validates :company_name, presence: true
50
49
  end
51
50
  ```
52
51
 
52
+ Available modules:
53
+ - `:confirmable` - Email confirmation for new accounts
54
+ - `:magic_linkable` - Passwordless sign-in via email
55
+ - `:oauth` - OAuth provider support (Google, GitHub, etc.)
56
+ - `:temporary` - Guest accounts that convert to permanent
57
+
58
+ For basic email/password auth only:
59
+
60
+ ```ruby
61
+ class User < ApplicationRecord
62
+ authenticates_with
63
+ end
64
+ ```
65
+
53
66
  Protect your routes:
54
67
 
55
68
  ```ruby
@@ -193,7 +206,7 @@ end
193
206
 
194
207
  ```ruby
195
208
  class User < ApplicationRecord
196
- include RailsSimpleAuth::Models::Concerns::OAuthConnectable
209
+ authenticates_with :oauth
197
210
 
198
211
  def assign_oauth_attributes(auth_hash)
199
212
  self.name = auth_hash.dig("info", "name")
@@ -228,12 +241,11 @@ rails generate rails_simple_auth:temporary_users
228
241
  rails db:migrate
229
242
  ```
230
243
 
231
- 2. Add the concern to your User model:
244
+ 2. Add the `:temporary` module to your User model:
232
245
 
233
246
  ```ruby
234
247
  class User < ApplicationRecord
235
- include RailsSimpleAuth::Models::Concerns::Authenticatable
236
- include RailsSimpleAuth::Models::Concerns::TemporaryUser # Add this
248
+ authenticates_with :confirmable, :temporary
237
249
  end
238
250
  ```
239
251
 
@@ -587,6 +599,72 @@ end
587
599
  - **Account conversion**: All sessions are invalidated when a temporary user converts to permanent
588
600
  - **Sign out**: Only the current session is destroyed (other devices stay signed in)
589
601
 
602
+ ## Admin Users
603
+
604
+ RailsSimpleAuth uses a single table with role-based access — the Rails way. No separate admin models or authentication flows needed.
605
+
606
+ ### Setup
607
+
608
+ Add an admin column to your users table:
609
+
610
+ ```ruby
611
+ # Migration
612
+ add_column :users, :admin, :boolean, default: false
613
+ ```
614
+
615
+ Add a helper method to your model:
616
+
617
+ ```ruby
618
+ class User < ApplicationRecord
619
+ authenticates_with :confirmable
620
+
621
+ def admin?
622
+ admin == true
623
+ end
624
+ end
625
+ ```
626
+
627
+ ### Protecting Admin Routes
628
+
629
+ ```ruby
630
+ class AdminController < ApplicationController
631
+ before_action :require_admin
632
+
633
+ private
634
+
635
+ def require_admin
636
+ redirect_to root_path, alert: "Not authorized" unless current_user&.admin?
637
+ end
638
+ end
639
+
640
+ # Or as a concern
641
+ module AdminAuthentication
642
+ extend ActiveSupport::Concern
643
+
644
+ included do
645
+ before_action :require_admin
646
+ end
647
+
648
+ private
649
+
650
+ def require_admin
651
+ redirect_to root_path, alert: "Not authorized" unless current_user&.admin?
652
+ end
653
+ end
654
+ ```
655
+
656
+ ### Creating Admin Users
657
+
658
+ ```ruby
659
+ # Console
660
+ User.find_by(email: "admin@example.com").update!(admin: true)
661
+
662
+ # Seeds
663
+ User.create!(email: "admin@example.com", password: "secure123", admin: true)
664
+ ```
665
+
666
+ For more complex role systems, consider adding a `role` enum or using an authorization gem like [Pundit](https://github.com/varvet/pundit).
667
+
590
668
  ## Security Features
591
669
 
592
670
  - **BCrypt password hashing** with salts
@@ -40,15 +40,14 @@ module RailsSimpleAuth
40
40
  say 'Next steps:'
41
41
  say ' 1. Review and edit the migration: db/migrate/xxx_add_rails_simple_auth.rb'
42
42
  say ' 2. Run: rails db:migrate'
43
- say " 3. Add concerns to your #{options[:user_model]} model:"
43
+ say " 3. Add authentication to your #{options[:user_model]} model:"
44
44
  say ''
45
45
  say " class #{options[:user_model]} < ApplicationRecord"
46
- say ' include RailsSimpleAuth::Models::Concerns::Authenticatable'
47
- say ' include RailsSimpleAuth::Models::Concerns::Confirmable # optional'
48
- say ' include RailsSimpleAuth::Models::Concerns::MagicLinkable # optional'
49
- say ' include RailsSimpleAuth::Models::Concerns::OAuthConnectable # optional'
46
+ say ' authenticates_with :confirmable, :magic_linkable'
50
47
  say ' end'
51
48
  say ''
49
+ say ' Available modules: :confirmable, :magic_linkable, :oauth, :temporary'
50
+ say ''
52
51
  say ' 4. Add before_action to protect routes:'
53
52
  say ''
54
53
  say ' class ApplicationController < ActionController::Base'
@@ -56,8 +55,9 @@ module RailsSimpleAuth
56
55
  say ' end'
57
56
  say ''
58
57
  say 'Optional generators:'
59
- say ' rails generate rails_simple_auth:views # Copy views for customization'
60
- say ' rails generate rails_simple_auth:css # Copy CSS for styling'
58
+ say ' rails generate rails_simple_auth:views # Copy views for customization'
59
+ say ' rails generate rails_simple_auth:css # Copy CSS for styling'
60
+ say ' rails generate rails_simple_auth:temporary_users # Add guest account support'
61
61
  say ''
62
62
  end
63
63
  end
@@ -9,8 +9,8 @@ Example:
9
9
  This will create:
10
10
  db/migrate/YYYYMMDDHHMMSS_add_temporary_to_users.rb
11
11
 
12
- After running the migration, include the concern in your User model:
13
- include RailsSimpleAuth::Models::Concerns::TemporaryUser
12
+ After running the migration, add :temporary to your User model:
13
+ authenticates_with :confirmable, :temporary
14
14
 
15
15
  And enable in your initializer:
16
16
  config.temporary_users_enabled = true
@@ -28,8 +28,8 @@ module RailsSimpleAuth
28
28
  say 'Next steps:', :yellow
29
29
  say ' 1. Run: bin/rails db:migrate'
30
30
  say ''
31
- say ' 2. Include the concern in your User model:'
32
- say ' include RailsSimpleAuth::Models::Concerns::TemporaryUser'
31
+ say ' 2. Add :temporary to your User model:'
32
+ say ' authenticates_with :confirmable, :temporary'
33
33
  say ''
34
34
  say ' 3. Enable in your initializer:'
35
35
  say ' config.temporary_users_enabled = true'
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'rails_simple_auth/model'
4
+
3
5
  module RailsSimpleAuth
4
6
  class Engine < ::Rails::Engine
5
7
  isolate_namespace RailsSimpleAuth
@@ -14,5 +16,18 @@ module RailsSimpleAuth
14
16
  include RailsSimpleAuth::Controllers::Concerns::SessionManagement
15
17
  end
16
18
  end
19
+
20
+ initializer 'rails_simple_auth.model' do
21
+ ActiveSupport.on_load(:active_record) do
22
+ include RailsSimpleAuth::Model
23
+ end
24
+ end
25
+
26
+ # Secure OmniAuth by default - only allow POST to initiate OAuth (prevents CSRF)
27
+ initializer 'rails_simple_auth.omniauth', after: :load_config_initializers do
28
+ if defined?(OmniAuth)
29
+ OmniAuth.config.allowed_request_methods = %i[post]
30
+ end
31
+ end
17
32
  end
18
33
  end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsSimpleAuth
4
+ module Model
5
+ extend ActiveSupport::Concern
6
+
7
+ MODULES = {
8
+ confirmable: 'RailsSimpleAuth::Models::Concerns::Confirmable',
9
+ magic_linkable: 'RailsSimpleAuth::Models::Concerns::MagicLinkable',
10
+ oauth: 'RailsSimpleAuth::Models::Concerns::OAuthConnectable',
11
+ temporary: 'RailsSimpleAuth::Models::Concerns::TemporaryUser'
12
+ }.freeze
13
+
14
+ class_methods do
15
+ # Configure authentication for this model
16
+ #
17
+ # @example Basic authentication only
18
+ # authenticates_with
19
+ #
20
+ # @example With optional modules
21
+ # authenticates_with :confirmable, :magic_linkable
22
+ #
23
+ # @example Full featured
24
+ # authenticates_with :confirmable, :magic_linkable, :oauth, :temporary
25
+ #
26
+ # Available modules:
27
+ # - :confirmable - Email confirmation for new accounts
28
+ # - :magic_linkable - Passwordless sign-in via email
29
+ # - :oauth - OAuth provider support (Google, GitHub, etc.)
30
+ # - :temporary - Guest accounts that convert to permanent
31
+ #
32
+ def authenticates_with(*modules)
33
+ # Always include base authentication
34
+ include RailsSimpleAuth::Models::Concerns::Authenticatable
35
+
36
+ # Include requested optional modules
37
+ modules.each do |mod|
38
+ mod_name = mod.to_sym
39
+ unless MODULES.key?(mod_name)
40
+ raise ArgumentError, "Unknown authentication module: #{mod.inspect}. " \
41
+ "Available modules: #{MODULES.keys.join(', ')}"
42
+ end
43
+
44
+ include MODULES[mod_name].constantize
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsSimpleAuth
4
- VERSION = '1.0.3'
4
+ VERSION = '1.0.5'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_simple_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Kuznetsov
@@ -82,6 +82,7 @@ files:
82
82
  - lib/rails_simple_auth/controllers/concerns/authentication.rb
83
83
  - lib/rails_simple_auth/controllers/concerns/session_management.rb
84
84
  - lib/rails_simple_auth/engine.rb
85
+ - lib/rails_simple_auth/model.rb
85
86
  - lib/rails_simple_auth/models/concerns/authenticatable.rb
86
87
  - lib/rails_simple_auth/models/concerns/confirmable.rb
87
88
  - lib/rails_simple_auth/models/concerns/magic_linkable.rb