FIX: Ensure enforce 2FA for staff satisfied by security keys (#8316)

* If a staff user created only a security key as their single 2FA option. they continued to be prompted to create a 2FA option because we only considered this condition satisfied if a TOTP was added.
* The condition is now satisfied if TOTP OR security keys are enabled.
This commit is contained in:
Martin Brennan 2019-11-08 15:11:53 +10:00 committed by GitHub
parent ba5b78a348
commit 64b4a7ba45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 4 deletions

View File

@ -39,7 +39,7 @@ class AdminDetailedUserSerializer < AdminUserSerializer
has_many :groups, embed: :object, serializer: BasicGroupSerializer
def second_factor_enabled
object.totp_enabled?
object.totp_enabled? || object.security_keys_enabled?
end
def can_disable_second_factor

View File

@ -210,6 +210,6 @@ class CurrentUserSerializer < BasicUserSerializer
end
def second_factor_enabled
object.totp_enabled?
object.totp_enabled? || object.security_keys_enabled?
end
end

View File

@ -164,7 +164,7 @@ class UserSerializer < BasicUserSerializer
end
def second_factor_enabled
object.totp_enabled?
object.totp_enabled? || object.security_keys_enabled?
end
def include_second_factor_backup_enabled?

View File

@ -68,6 +68,38 @@ RSpec.describe CurrentUserSerializer do
end
end
context "#second_factor_enabled" do
fab!(:user) { Fabricate(:user) }
let :serializer do
CurrentUserSerializer.new(user, scope: Guardian.new(user), root: false)
end
let(:json) { serializer.as_json }
it "is false by default" do
expect(json[:second_factor_enabled]).to eq(false)
end
context "when totp enabled" do
before do
User.any_instance.stubs(:totp_enabled?).returns(true)
end
it "is true" do
expect(json[:second_factor_enabled]).to eq(true)
end
end
context "when security_keys enabled" do
before do
User.any_instance.stubs(:security_keys_enabled?).returns(true)
end
it "is true" do
expect(json[:second_factor_enabled]).to eq(true)
end
end
end
context "#groups" do
fab!(:member) { Fabricate(:user) }
let :serializer do

View File

@ -40,8 +40,9 @@ describe UserSerializer do
end
context "with a user" do
let(:scope) { Guardian.new }
fab!(:user) { Fabricate(:user) }
let(:serializer) { UserSerializer.new(user, scope: Guardian.new, root: false) }
let(:serializer) { UserSerializer.new(user, scope: scope, root: false) }
let(:json) { serializer.as_json }
fab!(:upload) { Fabricate(:upload) }
fab!(:upload2) { Fabricate(:upload) }
@ -164,6 +165,33 @@ describe UserSerializer do
expect(json[:bio_cooked]).to eq 'my cooked bio'
end
end
describe "second_factor_enabled" do
let(:scope) { Guardian.new(user) }
it "is false by default" do
expect(json[:second_factor_enabled]).to eq(false)
end
context "when totp enabled" do
before do
User.any_instance.stubs(:totp_enabled?).returns(true)
end
it "is true" do
expect(json[:second_factor_enabled]).to eq(true)
end
end
context "when security_keys enabled" do
before do
User.any_instance.stubs(:security_keys_enabled?).returns(true)
end
it "is true" do
expect(json[:second_factor_enabled]).to eq(true)
end
end
end
end
context "with custom_fields" do