Skip to content

Commit cd73903

Browse files
committed
🐛 Modern Ruby compat
1 parent 463e3ba commit cd73903

File tree

3 files changed

+73
-89
lines changed

3 files changed

+73
-89
lines changed

.rubocop_gradual.lock

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,7 @@
1313
[39, 21, 3, "Style/AndOr: Use `&&` instead of `and`.", 193409806],
1414
[49, 20, 1, "Lint/AssignmentInCondition: Wrap assignment in parentheses if intentional", 177560]
1515
],
16-
"app/models/masq/account.rb:3431296046": [
17-
[43, 5, 298, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 2464945722],
18-
[80, 5, 1035, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 2286399262],
19-
[82, 17, 3, "Style/AndOr: Use `&&` instead of `and`.", 193409806],
20-
[98, 18, 3, "Style/AndOr: Use `&&` instead of `and`.", 193409806],
21-
[98, 32, 3, "Style/AndOr: Use `&&` instead of `and`.", 193409806],
22-
[99, 39, 2, "Style/AndOr: Use `||` instead of `or`.", 5861240],
23-
[99, 88, 3, "Style/AndOr: Use `&&` instead of `and`.", 193409806],
24-
[108, 5, 97, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 4218748683],
25-
[220, 5, 3, "Lint/IneffectiveAccessModifier: `private` (on line 216) does not make singleton methods private. Use `private_class_method` or `private` inside a `class << self` block instead.", 193404514],
26-
[220, 5, 85, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 115756650],
27-
[226, 5, 3, "Lint/IneffectiveAccessModifier: `private` (on line 216) does not make singleton methods private. Use `private_class_method` or `private` inside a `class << self` block instead.", 193404514],
28-
[226, 5, 181, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 3702657420],
29-
[234, 5, 3, "Lint/IneffectiveAccessModifier: `private` (on line 216) does not make singleton methods private. Use `private_class_method` or `private` inside a `class << self` block instead.", 193404514],
30-
[234, 5, 135, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 4139970307]
31-
],
32-
"app/models/masq/open_id_request.rb:3145306176": [
16+
"app/models/masq/open_id_request.rb:2515784583": [
3317
[27, 7, 159, "Style/SafeNavigation: Use safe navigation (`&.`) instead of checking if an object exists before calling the method.", 474907089]
3418
],
3519
"app/models/masq/persona.rb:3585878101": [

app/models/masq/account.rb

Lines changed: 71 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,76 @@ def initialize(account, message = nil)
3535
end
3636
end
3737

38-
# Finds the user with the corresponding activation code, activates their account and returns the user.
39-
#
40-
# Raises:
41-
# [Account::ActivationCodeNotFound] if there is no user with the corresponding activation code
42-
# [Account::AlreadyActivated] if the user with the corresponding activation code has already activated their account
43-
def self.find_and_activate!(activation_code)
44-
raise ArgumentError if activation_code.nil?
45-
user = find_by(activation_code: activation_code)
46-
raise ActivationCodeNotFound unless user
47-
raise AlreadyActivated.new(user) if user.active?
48-
user.send(:activate!)
49-
user
38+
class << self
39+
# Finds the user with the corresponding activation code, activates their account and returns the user.
40+
#
41+
# Raises:
42+
# [Account::ActivationCodeNotFound] if there is no user with the corresponding activation code
43+
# [Account::AlreadyActivated] if the user with the corresponding activation code has already activated their account
44+
def find_and_activate!(activation_code)
45+
raise ArgumentError if activation_code.nil?
46+
user = find_by(activation_code: activation_code)
47+
raise ActivationCodeNotFound unless user
48+
raise AlreadyActivated.new(user) if user.active?
49+
user.send(:activate!)
50+
user
51+
end
52+
53+
# Authenticates a user by their login name and password.
54+
# Returns the user or nil.
55+
def authenticate(login, password, basic_auth_used = false)
56+
a = Account.find_by(login: login)
57+
if a.nil? && Masq::Engine.config.masq["create_auth_ondemand"]["enabled"]
58+
# Need to set some password - but is never used
59+
pw = if Masq::Engine.config.masq["create_auth_ondemand"]["random_password"]
60+
SecureRandom.hex(13)
61+
else
62+
password
63+
end
64+
signup = Signup.create_account!(
65+
login: login,
66+
password: pw,
67+
password_confirmation: pw,
68+
email: "#{login}@#{Masq::Engine.config.masq["create_auth_ondemand"]["default_mail_domain"]}",
69+
)
70+
a = signup.account if signup.succeeded?
71+
end
72+
73+
if !a.nil? && a.active? && a.enabled
74+
if a.authenticated?(password) || (Masq::Engine.config.masq["trust_basic_auth"] && basic_auth_used)
75+
a.last_authenticated_at, a.last_authenticated_by_yubikey = Time.now, a.authenticated_with_yubikey?
76+
a.save(validate: false)
77+
a
78+
end
79+
end
80+
end
81+
82+
# Encrypts some data with the salt.
83+
def encrypt(password, salt)
84+
Digest::SHA1.hexdigest("--#{salt}--#{password}--")
85+
end
86+
87+
# Receives a login token which consists of the users password and
88+
# a Yubico one time password (the otp is always 44 characters long)
89+
def split_password_and_yubico_otp(token)
90+
token.reverse!
91+
yubico_otp = token.slice!(0..43).reverse
92+
password = token.reverse
93+
[password, yubico_otp]
94+
end
95+
96+
# Returns the first twelve chars from the Yubico OTP,
97+
# which are used to identify a Yubikey
98+
def extract_yubico_identity_from_otp(yubico_otp)
99+
yubico_otp[0..11]
100+
end
101+
102+
# Utilizes the Yubico library to verify a one time password
103+
def verify_yubico_otp(otp)
104+
Yubikey::OTP::Verify.new(otp).valid?
105+
rescue Yubikey::OTP::InvalidOTPError
106+
false
107+
end
50108
end
51109

52110
def to_param
@@ -75,40 +133,6 @@ def has_otp_device?
75133
!yubico_identity.nil?
76134
end
77135

78-
# Authenticates a user by their login name and password.
79-
# Returns the user or nil.
80-
def self.authenticate(login, password, basic_auth_used = false)
81-
a = Account.find_by(login: login)
82-
if a.nil? and Masq::Engine.config.masq["create_auth_ondemand"]["enabled"]
83-
# Need to set some password - but is never used
84-
pw = if Masq::Engine.config.masq["create_auth_ondemand"]["random_password"]
85-
SecureRandom.hex(13)
86-
else
87-
password
88-
end
89-
signup = Signup.create_account!(
90-
login: login,
91-
password: pw,
92-
password_confirmation: pw,
93-
email: "#{login}@#{Masq::Engine.config.masq["create_auth_ondemand"]["default_mail_domain"]}",
94-
)
95-
a = signup.account if signup.succeeded?
96-
end
97-
98-
if !a.nil? and a.active? and a.enabled
99-
if a.authenticated?(password) or (Masq::Engine.config.masq["trust_basic_auth"] and basic_auth_used)
100-
a.last_authenticated_at, a.last_authenticated_by_yubikey = Time.now, a.authenticated_with_yubikey?
101-
a.save(validate: false)
102-
a
103-
end
104-
end
105-
end
106-
107-
# Encrypts some data with the salt.
108-
def self.encrypt(password, salt)
109-
Digest::SHA1.hexdigest("--#{salt}--#{password}--")
110-
end
111-
112136
# Encrypts the password with the user salt
113137
def encrypt(password)
114138
self.class.encrypt(password, salt)
@@ -121,7 +145,7 @@ def authenticated?(password)
121145
encrypt(password) == crypted_password
122146
elsif Masq::Engine.config.masq["can_use_yubikey"]
123147
password, yubico_otp = Account.split_password_and_yubico_otp(password)
124-
encrypt(password) == crypted_password && @authenticated_with_yubikey = yubikey_authenticated?(yubico_otp)
148+
@authenticated_with_yubikey = yubikey_authenticated?(yubico_otp) if encrypt(password) == crypted_password
125149
end
126150
end
127151

@@ -213,30 +237,6 @@ def make_password_reset_code
213237
self.password_reset_code = Digest::SHA1.hexdigest(Time.now.to_s.split("").sort_by { rand }.join)
214238
end
215239

216-
private
217-
218-
# Returns the first twelve chars from the Yubico OTP,
219-
# which are used to identify a Yubikey
220-
def self.extract_yubico_identity_from_otp(yubico_otp)
221-
yubico_otp[0..11]
222-
end
223-
224-
# Recieves a login token which consists of the users password and
225-
# a Yubico one time password (the otp is always 44 characters long)
226-
def self.split_password_and_yubico_otp(token)
227-
token.reverse!
228-
yubico_otp = token.slice!(0..43).reverse
229-
password = token.reverse
230-
[password, yubico_otp]
231-
end
232-
233-
# Utilizes the Yubico library to verify an one time password
234-
def self.verify_yubico_otp(otp)
235-
Yubikey::OTP::Verify.new(otp).valid?
236-
rescue Yubikey::OTP::InvalidOTPError
237-
false
238-
end
239-
240240
def deliver_forgot_password
241241
AccountMailer.forgot_password(self).deliver_now if recently_forgot_password?
242242
end

app/models/masq/open_id_request.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def from_trusted_domain?
2929
end
3030
end
3131

32-
private
32+
protected
3333

3434
def make_token
3535
self.token = Digest::SHA1.hexdigest(Time.now.to_s.split("").sort_by { rand }.join)

0 commit comments

Comments
 (0)