fixes #3277
This commit is contained in:
@@ -2,12 +2,14 @@ class ApiKey < ApplicationRecord
|
|||||||
belongs_to :user
|
belongs_to :user
|
||||||
validates_uniqueness_of :user_id
|
validates_uniqueness_of :user_id
|
||||||
validates_uniqueness_of :key
|
validates_uniqueness_of :key
|
||||||
|
has_secure_token :key
|
||||||
|
|
||||||
def self.generate!(user)
|
def self.generate!(user)
|
||||||
create(:user_id => user.id, :key => SecureRandom.urlsafe_base64(32))
|
create(:user_id => user.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def regenerate!
|
def regenerate!
|
||||||
update!(:key => SecureRandom.urlsafe_base64(32))
|
regenerate_key
|
||||||
|
save
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
class UserPasswordResetNonce < ApplicationRecord
|
class UserPasswordResetNonce < ApplicationRecord
|
||||||
validates_presence_of :email, :key
|
has_secure_token :key
|
||||||
|
validates_presence_of :email
|
||||||
validate :validate_existence_of_email
|
validate :validate_existence_of_email
|
||||||
before_validation :initialize_key, :on => :create
|
|
||||||
after_create :deliver_notice
|
after_create :deliver_notice
|
||||||
|
|
||||||
def self.prune!
|
def self.prune!
|
||||||
@@ -12,10 +12,6 @@ class UserPasswordResetNonce < ApplicationRecord
|
|||||||
Maintenance::User::PasswordResetMailer.reset_request(user, self).deliver_now
|
Maintenance::User::PasswordResetMailer.reset_request(user, self).deliver_now
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize_key
|
|
||||||
self.key = SecureRandom.hex(16)
|
|
||||||
end
|
|
||||||
|
|
||||||
def validate_existence_of_email
|
def validate_existence_of_email
|
||||||
if !User.with_email(email).exists?
|
if !User.with_email(email).exists?
|
||||||
errors[:email] << "is invalid"
|
errors[:email] << "is invalid"
|
||||||
|
|||||||
@@ -7,6 +7,16 @@ class ApiKeyTest < ActiveSupport::TestCase
|
|||||||
@api_key = ApiKey.generate!(@user)
|
@api_key = ApiKey.generate!(@user)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
should "regenerate the key" do
|
||||||
|
assert_changes(-> { @api_key.key }) do
|
||||||
|
@api_key.regenerate!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
should "generate a unique key" do
|
||||||
|
assert_not_nil(@api_key.key)
|
||||||
|
end
|
||||||
|
|
||||||
should "authenticate via api key" do
|
should "authenticate via api key" do
|
||||||
assert_not_nil(User.authenticate_api_key(@user.name, @api_key.key))
|
assert_not_nil(User.authenticate_api_key(@user.name, @api_key.key))
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ class UserPasswordResetNonceTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
should "populate the key with a random string" do
|
should "populate the key with a random string" do
|
||||||
assert_equal(32, @nonce.key.size)
|
assert_equal(24, @nonce.key.size)
|
||||||
end
|
end
|
||||||
|
|
||||||
should "reset the password when reset" do
|
should "reset the password when reset" do
|
||||||
|
|||||||
Reference in New Issue
Block a user