mirror of
https://github.com/discourse/discourse.git
synced 2024-11-24 17:15:32 +08:00
dd6ec65061
2FA support in Discourse was added and grown gradually over the years: we first added support for TOTP for logins, then we implemented backup codes, and last but not least, security keys. 2FA usage was initially limited to logging in, but it has been expanded and we now require 2FA for risky actions such as adding a new admin to the site. As a result of this gradual growth of the 2FA system, technical debt has accumulated to the point where it has become difficult to require 2FA for more actions. We now have 5 different 2FA UI implementations and each one has to support all 3 2FA methods (TOTP, backup codes, and security keys) which makes it difficult to maintain a consistent UX for these different implementations. Moreover, there is a lot of repeated logic in the server-side code behind these 5 UI implementations which hinders maintainability even more. This commit is the first step towards repaying the technical debt: it builds a system that centralizes as much as possible of the 2FA server-side logic and UI. The 2 main components of this system are: 1. A dedicated page for 2FA with support for all 3 methods. 2. A reusable server-side class that centralizes the 2FA logic (the `SecondFactor::AuthManager` class). From a top-level view, the 2FA flow in this new system looks like this: 1. User initiates an action that requires 2FA; 2. Server is aware that 2FA is required for this action, so it redirects the user to the 2FA page if the user has a 2FA method, otherwise the action is performed. 3. User submits the 2FA form on the page; 4. Server validates the 2FA and if it's successful, the action is performed and the user is redirected to the previous page. A more technically-detailed explanation/documentation of the new system is available as a comment at the top of the `lib/second_factor/auth_manager.rb` file. Please note that the details are not set in stone and will likely change in the future, so please don't use the system in your plugins yet. Since this is a new system that needs to be tested, we've decided to migrate only the 2FA for adding a new admin to the new system at this time (in this commit). Our plan is to gradually migrate the remaining 2FA implementations to the new system. For screenshots of the 2FA page, see PR #15377 on GitHub.
62 lines
1.7 KiB
Ruby
62 lines
1.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class AdminConfirmation
|
|
attr_accessor :token
|
|
attr_reader :performed_by
|
|
attr_reader :target_user
|
|
|
|
def initialize(target_user, performed_by)
|
|
@target_user = target_user
|
|
@performed_by = performed_by
|
|
end
|
|
|
|
def create_confirmation
|
|
guardian = Guardian.new(@performed_by)
|
|
guardian.ensure_can_grant_admin!(@target_user)
|
|
|
|
@token = SecureRandom.hex
|
|
Discourse.redis.setex("admin-confirmation:#{@target_user.id}", 3.hours.to_i, @token)
|
|
|
|
payload = {
|
|
target_user_id: @target_user.id,
|
|
performed_by: @performed_by.id
|
|
}
|
|
Discourse.redis.setex("admin-confirmation-token:#{@token}", 3.hours.to_i, payload.to_json)
|
|
|
|
Jobs.enqueue(
|
|
:admin_confirmation_email,
|
|
to_address: @performed_by.email,
|
|
target_email: @target_user.email,
|
|
target_username: @target_user.username,
|
|
token: @token
|
|
)
|
|
end
|
|
|
|
def email_confirmed!
|
|
guardian = Guardian.new(@performed_by)
|
|
guardian.ensure_can_grant_admin!(@target_user)
|
|
|
|
@target_user.grant_admin!
|
|
StaffActionLogger.new(@performed_by).log_grant_admin(@target_user)
|
|
Discourse.redis.del "admin-confirmation:#{@target_user.id}"
|
|
Discourse.redis.del "admin-confirmation-token:#{@token}"
|
|
end
|
|
|
|
def self.exists_for?(user_id)
|
|
Discourse.redis.exists? "admin-confirmation:#{user_id}"
|
|
end
|
|
|
|
def self.find_by_code(token)
|
|
json = Discourse.redis.get("admin-confirmation-token:#{token}")
|
|
return nil unless json
|
|
|
|
parsed = JSON.parse(json)
|
|
target_user = User.find(parsed['target_user_id'].to_i)
|
|
performed_by = User.find(parsed['performed_by'].to_i)
|
|
|
|
ac = AdminConfirmation.new(target_user, performed_by)
|
|
ac.token = token
|
|
ac
|
|
end
|
|
end
|