minimalist_authentication 3.0.0 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +0 -1
- data/app/controllers/email_verifications_controller.rb +1 -1
- data/app/controllers/password_resets_controller.rb +1 -1
- data/app/mailers/minimalist_authentication_mailer.rb +9 -13
- data/config/locales/minimalist_authentication.en.yml +3 -1
- data/lib/minimalist_authentication/authenticator.rb +22 -15
- data/lib/minimalist_authentication/configuration.rb +4 -11
- data/lib/minimalist_authentication/controller.rb +1 -3
- data/lib/minimalist_authentication/sessions.rb +1 -1
- data/lib/minimalist_authentication/user.rb +40 -16
- data/lib/minimalist_authentication/version.rb +1 -1
- data/lib/minimalist_authentication.rb +6 -0
- metadata +4 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eb8cc19088eea71ab56f803fe72724911988a9dea1eaf2df45ab6eab1127fea9
|
4
|
+
data.tar.gz: 648dad4be8864624cf3bc9158c9cc207fc19da57256c363415c2c4b3cbe6b3ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fdce2aed67b60fb4adc4d6227523001c52099ea9df6458aa420ce89616467986e05e12362afba71811502d25ab406e2356d2857bc62f640ce7942dc00dc408f4
|
7
|
+
data.tar.gz: a9c0c9ac343602975b60415c0b53c13e50f748e08a63bf37a1ecb6ca64c20eea1e4e6402096c1900b45255cd13ee736a0ef664cce7a25639fccf73486ede9e12
|
data/README.md
CHANGED
@@ -73,7 +73,6 @@ MinimalistAuthentication.configure do |configuration|
|
|
73
73
|
configuration.verify_email = true # default is true
|
74
74
|
configuration.login_redirect_path = :custom_path # default is :root_path
|
75
75
|
configuration.logout_redirect_path = :custom_path # default is :new_session_path
|
76
|
-
configuration.email_prefix = '[Custom Prefix]' # default is application name
|
77
76
|
end
|
78
77
|
```
|
79
78
|
|
@@ -11,7 +11,7 @@ class EmailVerificationsController < ApplicationController
|
|
11
11
|
|
12
12
|
def create
|
13
13
|
current_user.regenerate_verification_token
|
14
|
-
MinimalistAuthenticationMailer.
|
14
|
+
MinimalistAuthenticationMailer.with(user: current_user).verify_email.deliver_now
|
15
15
|
|
16
16
|
redirect_to dashboard_path, notice: t(".notice", email: current_user.email)
|
17
17
|
end
|
@@ -14,7 +14,7 @@ class PasswordResetsController < ApplicationController
|
|
14
14
|
def create
|
15
15
|
if user
|
16
16
|
user.regenerate_verification_token
|
17
|
-
MinimalistAuthenticationMailer.
|
17
|
+
MinimalistAuthenticationMailer.with(user:).update_password.deliver_now
|
18
18
|
end
|
19
19
|
# always display notice even if the user was not found to prevent leaking user emails
|
20
20
|
redirect_to new_session_path, notice: "Password reset instructions were mailed to #{email}"
|
@@ -1,24 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class MinimalistAuthenticationMailer < ApplicationMailer
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
before_action { @user = params[:user] }
|
5
|
+
after_action :mail_user
|
6
|
+
|
7
|
+
def verify_email
|
8
|
+
@verify_email_link = email_verification_url(token: @user.verification_token)
|
7
9
|
end
|
8
10
|
|
9
|
-
def update_password
|
10
|
-
@edit_password_link = edit_user_password_url(user, token: user.verification_token)
|
11
|
-
send_to(user, "Update Password")
|
11
|
+
def update_password
|
12
|
+
@edit_password_link = edit_user_password_url(@user, token: @user.verification_token)
|
12
13
|
end
|
13
14
|
|
14
15
|
private
|
15
16
|
|
16
|
-
def
|
17
|
-
@user
|
18
|
-
mail to: @user.email, subject: prefixed_subject(subject)
|
19
|
-
end
|
20
|
-
|
21
|
-
def prefixed_subject(subject)
|
22
|
-
"#{MinimalistAuthentication.configuration.email_prefix} #{subject}"
|
17
|
+
def mail_user
|
18
|
+
mail(to: @user.email)
|
23
19
|
end
|
24
20
|
end
|
@@ -19,6 +19,8 @@ en:
|
|
19
19
|
minimalist_authentication_mailer:
|
20
20
|
update_password:
|
21
21
|
opening: Please click the link below to update your password.
|
22
|
+
subject: Update Password
|
22
23
|
verify_email:
|
23
|
-
opening: Please click the link below to complete your email verification.
|
24
24
|
closing: If you did not request email verification you can safely ignore this message.
|
25
|
+
opening: Please click the link below to complete your email verification.
|
26
|
+
subject: "Email Address Verification"
|
@@ -13,37 +13,44 @@ module MinimalistAuthentication
|
|
13
13
|
# Params examples:
|
14
14
|
# { email: 'user@example.com', password: 'abc123' }
|
15
15
|
# { username: 'user', password: 'abc123' }
|
16
|
-
|
17
|
-
def self.authenticated_user(params)
|
16
|
+
def self.authenticate(params)
|
18
17
|
hash = params.to_h.with_indifferent_access
|
18
|
+
field, value = hash.find { |key, _| LOGIN_FIELDS.include?(key) }
|
19
|
+
new(field:, value:, password: hash["password"]).authenticated_user
|
20
|
+
end
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
def self.authenticated_user(params)
|
23
|
+
MinimalistAuthentication.deprecator.warn(<<-MSG.squish)
|
24
|
+
Calling MinimalistAuthentication::Authenticator.authenticated_user is deprecated.
|
25
|
+
Use MinimalistAuthentication::Authenticator.authenticate instead.
|
26
|
+
MSG
|
27
|
+
authenticate(params)
|
25
28
|
end
|
26
29
|
|
30
|
+
# Initializes a new Authenticator instance with the provided field, value, and password.
|
27
31
|
def initialize(field:, value:, password:)
|
28
32
|
@field = field
|
29
33
|
@value = value
|
30
34
|
@password = password
|
31
35
|
end
|
32
36
|
|
33
|
-
# Returns
|
37
|
+
# Returns an authenticated and enabled user or nil.
|
34
38
|
def authenticated_user
|
35
|
-
|
39
|
+
authenticated&.enabled if valid?
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
# Attempts to authenticate a user based on the provided field, value, and password.
|
45
|
+
# Returns user upon successful authentication, otherwise returns nil.
|
46
|
+
def authenticated
|
47
|
+
MinimalistAuthentication.configuration.user_model.authenticate_by(field => value, password:)
|
36
48
|
end
|
37
49
|
|
38
50
|
# Returns true if all the authentication attributes are present.
|
51
|
+
# Otherwise returns false.
|
39
52
|
def valid?
|
40
53
|
[field, value, password].all?(&:present?)
|
41
54
|
end
|
42
|
-
|
43
|
-
private
|
44
|
-
|
45
|
-
def user
|
46
|
-
@user ||= MinimalistAuthentication.configuration.user_model.active.find_by(field => value)
|
47
|
-
end
|
48
55
|
end
|
49
56
|
end
|
@@ -51,10 +51,6 @@ module MinimalistAuthentication
|
|
51
51
|
# Defaults to :new_session_path
|
52
52
|
attr_accessor :logout_redirect_path
|
53
53
|
|
54
|
-
# Email subject prefix for MinimalistAuthenticationMailer messages
|
55
|
-
# Defaults to application name
|
56
|
-
attr_accessor :email_prefix
|
57
|
-
|
58
54
|
def initialize
|
59
55
|
self.user_model_name = "::User"
|
60
56
|
self.session_key = :user_id
|
@@ -64,7 +60,10 @@ module MinimalistAuthentication
|
|
64
60
|
self.verify_email = true
|
65
61
|
self.login_redirect_path = :root_path
|
66
62
|
self.logout_redirect_path = :new_session_path
|
67
|
-
|
63
|
+
end
|
64
|
+
|
65
|
+
def email_prefix=(_)
|
66
|
+
MinimalistAuthentication.deprecator.warn("The #email_prefix configuration setting is no longer supported.")
|
68
67
|
end
|
69
68
|
|
70
69
|
# Returns the user_model class
|
@@ -73,11 +72,5 @@ module MinimalistAuthentication
|
|
73
72
|
def user_model
|
74
73
|
user_model_name.constantize
|
75
74
|
end
|
76
|
-
|
77
|
-
private
|
78
|
-
|
79
|
-
def default_email_prefix
|
80
|
-
"[#{Rails.application.engine_name.delete_suffix('_application').titleize}]"
|
81
|
-
end
|
82
75
|
end
|
83
76
|
end
|
@@ -19,9 +19,7 @@ module MinimalistAuthentication
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def find_session_user
|
22
|
-
|
23
|
-
|
24
|
-
MinimalistAuthentication.configuration.user_model.active.find_by(id: session_user_id)
|
22
|
+
MinimalistAuthentication.configuration.user_model.find_enabled(session_user_id)
|
25
23
|
end
|
26
24
|
|
27
25
|
def session_user_id
|
@@ -37,7 +37,7 @@ module MinimalistAuthentication
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def authenticated_user
|
40
|
-
@authenticated_user ||= MinimalistAuthentication::Authenticator.
|
40
|
+
@authenticated_user ||= MinimalistAuthentication::Authenticator.authenticate(user_params)
|
41
41
|
end
|
42
42
|
|
43
43
|
def log_in_user
|
@@ -9,7 +9,7 @@ module MinimalistAuthentication
|
|
9
9
|
GUEST_USER_EMAIL = "guest"
|
10
10
|
|
11
11
|
included do
|
12
|
-
has_secure_password
|
12
|
+
has_secure_password
|
13
13
|
|
14
14
|
# Force validations for a blank password.
|
15
15
|
attribute :password_required, :boolean, default: false
|
@@ -24,37 +24,64 @@ module MinimalistAuthentication
|
|
24
24
|
validates(:email, presence: true, if: :validate_email_presence?)
|
25
25
|
|
26
26
|
# Password validations
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
presence: true,
|
32
|
-
if: :validate_password?
|
33
|
-
)
|
27
|
+
# Adds validations for minimum password length and exclusivity.
|
28
|
+
# has_secure_password adds validations for presence, maximum length, confirmation,
|
29
|
+
# and password_challenge.
|
30
|
+
validates :password, length: { minimum: :password_minimum }, if: :validate_password?
|
34
31
|
validate :password_exclusivity, if: :password?
|
35
32
|
|
36
33
|
# Active scope
|
37
|
-
scope :active,
|
38
|
-
scope :inactive, -> { active(false) }
|
34
|
+
scope :active, ->(state = true) { where(active: state) }
|
39
35
|
end
|
40
36
|
|
41
37
|
module ClassMethods
|
38
|
+
# Finds a user by their id and returns the user if they are enabled.
|
39
|
+
# Returns nil if the user is not found or not enabled.
|
40
|
+
def find_enabled(id)
|
41
|
+
find_by(id:)&.enabled if id.present?
|
42
|
+
end
|
43
|
+
|
44
|
+
def inactive
|
45
|
+
MinimalistAuthentication.deprecator.warn(<<-MSG.squish)
|
46
|
+
Calling #inactive is deprecated. Use #active(false) instead.
|
47
|
+
MSG
|
48
|
+
active(false)
|
49
|
+
end
|
50
|
+
|
42
51
|
# Returns a frozen user with the email set to GUEST_USER_EMAIL.
|
43
52
|
def guest
|
44
53
|
new(email: GUEST_USER_EMAIL).freeze
|
45
54
|
end
|
46
55
|
end
|
47
56
|
|
57
|
+
# Called after a user is authenticated to determine if the user object should be returned.
|
58
|
+
def enabled
|
59
|
+
self if enabled?
|
60
|
+
end
|
61
|
+
|
62
|
+
# Returns true if the user is enabled.
|
63
|
+
# Override this method in your user model to implement custom logic that determines if a user is eligible to log in.
|
64
|
+
def enabled?
|
65
|
+
active?
|
66
|
+
end
|
67
|
+
|
68
|
+
# Remove the has_secure_password password blank error if password is not required.
|
69
|
+
def errors
|
70
|
+
super.tap { |errors| errors.delete(:password, :blank) unless validate_password? }
|
71
|
+
end
|
72
|
+
|
48
73
|
# Returns true if the user is not active.
|
49
74
|
def inactive?
|
75
|
+
MinimalistAuthentication.deprecator.warn("Calling #inactive? is deprecated.")
|
50
76
|
!active?
|
51
77
|
end
|
52
78
|
|
53
79
|
# Returns true if password matches the hashed_password, otherwise returns false.
|
54
80
|
def authenticated?(password)
|
81
|
+
MinimalistAuthentication.deprecator.warn(<<-MSG.squish)
|
82
|
+
Calling #authenticated? is deprecated. Use #authenticate instead.
|
83
|
+
MSG
|
55
84
|
authenticate(password)
|
56
|
-
rescue ::BCrypt::Errors::InvalidHash
|
57
|
-
false
|
58
85
|
end
|
59
86
|
|
60
87
|
# Check if user is a guest based on their email attribute
|
@@ -62,17 +89,14 @@ module MinimalistAuthentication
|
|
62
89
|
email == GUEST_USER_EMAIL
|
63
90
|
end
|
64
91
|
|
92
|
+
# Sets #last_logged_in_at to the current time without updating the updated_at timestamp.
|
65
93
|
def logged_in
|
66
|
-
# Use update_column to avoid updated_on trigger
|
67
94
|
update_column(:last_logged_in_at, Time.current)
|
68
95
|
end
|
69
96
|
|
70
97
|
# Minimum password length
|
71
98
|
def password_minimum = 12
|
72
99
|
|
73
|
-
# Maximum password length
|
74
|
-
def password_maximum = 40
|
75
|
-
|
76
100
|
# Checks for password presence
|
77
101
|
def password?
|
78
102
|
password.present?
|
@@ -9,3 +9,9 @@ require "minimalist_authentication/email_verification"
|
|
9
9
|
require "minimalist_authentication/controller"
|
10
10
|
require "minimalist_authentication/sessions"
|
11
11
|
require "minimalist_authentication/test_helper"
|
12
|
+
|
13
|
+
module MinimalistAuthentication
|
14
|
+
def self.deprecator
|
15
|
+
@deprecator ||= ActiveSupport::Deprecation.new("4.0", name)
|
16
|
+
end
|
17
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: minimalist_authentication
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Baldwin
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2024-11-
|
12
|
+
date: 2024-11-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bcrypt
|
@@ -37,14 +37,14 @@ dependencies:
|
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 7.1.0
|
41
41
|
type: :runtime
|
42
42
|
prerelease: false
|
43
43
|
version_requirements: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 7.1.0
|
48
48
|
description: A Rails authentication plugin that takes a minimalist approach. It is
|
49
49
|
designed to be simple to understand, use, and modify for your application.
|
50
50
|
email:
|
@@ -94,7 +94,6 @@ licenses:
|
|
94
94
|
- MIT
|
95
95
|
metadata:
|
96
96
|
homepage_uri: https://github.com/wwidea/minimalist_authentication
|
97
|
-
source_code_uri: https://github.com/wwidea/minimalist_authentication
|
98
97
|
rubygems_mfa_required: 'true'
|
99
98
|
post_install_message:
|
100
99
|
rdoc_options: []
|