discourse/spec/lib/second_factor/actions/grant_admin_spec.rb
Osama Sayegh eb5a3cfded
FEATURE: Add 2FA support to the Discourse Connect Provider protocol (#16386)
Discourse has the Discourse Connect Provider protocol that makes it possible to
use a Discourse instance as an identity provider for external sites. As a
natural extension to this protocol, this PR adds a new feature that makes it
possible to use Discourse as a 2FA provider as well as an identity provider.

The rationale for this change is that it's very difficult to implement 2FA
support in a website and if you have multiple websites that need to have 2FA,
it's unrealistic to build and maintain a separate 2FA implementation for each
one. But with this change, you can piggyback on Discourse to take care of all
the 2FA details for you for as many sites as you wish.

To use Discourse as a 2FA provider, you'll need to follow this guide:
https://meta.discourse.org/t/-/32974. It walks you through what you need to
implement on your end/site and how to configure your Discourse instance. Once
you're done, there is only one additional thing you need to do which is to
include `require_2fa=true` in the payload that you send to Discourse.

When Discourse sees `require_2fa=true`, it'll prompt the user to confirm their
2FA using whatever methods they've enabled (TOTP or security keys), and once
they confirm they'll be redirected back to the return URL you've configured and
the payload will contain `confirmed_2fa=true`. If the user has no 2FA methods
enabled however, the payload will not contain `confirmed_2fa`, but it will
contain `no_2fa_methods=true`.

You'll need to be careful to re-run all the security checks and ensure the user
can still access the resource on your site after they return from Discourse.
This is very important because there's nothing that guarantees the user that
will come back from Discourse after they confirm 2FA is the same user that
you've redirected to Discourse.

Internal ticket: t62183.
2022-04-13 15:04:09 +03:00

93 lines
2.9 KiB
Ruby

# frozen_string_literal: true
describe SecondFactor::Actions::GrantAdmin do
fab!(:admin) { Fabricate(:admin) }
fab!(:user) { Fabricate(:user) }
def cleanup_admin_confirmation_redis_keys
keys = Discourse.redis.keys("admin-confirmation:*")
keys += Discourse.redis.keys("admin-confirmation-token:*")
Discourse.redis.del(keys)
end
after do
cleanup_admin_confirmation_redis_keys
end
def params(hash)
ActionController::Parameters.new(hash)
end
def create_request(request_method: "GET", path: "/")
ActionDispatch::TestRequest.create({
"REQUEST_METHOD" => request_method,
"PATH_INFO" => path
})
end
def create_instance(user, request = nil)
request ||= create_request
SecondFactor::Actions::GrantAdmin.new(Guardian.new(user), request)
end
describe "#no_second_factors_enabled!" do
it "sends new admin confirmation email" do
instance = create_instance(admin)
expect {
instance.no_second_factors_enabled!(params({ user_id: user.id }))
}.to change { AdminConfirmation.exists_for?(user.id) }.from(false).to(true)
end
it "ensures the acting user is admin" do
instance = create_instance(user)
expect {
instance.no_second_factors_enabled!(params({ user_id: user.id }))
}.to raise_error(Discourse::InvalidAccess)
expect(AdminConfirmation.exists_for?(user.id)).to eq(false)
end
end
describe "#second_factor_auth_required!" do
it "returns a hash with callback_params, redirect_url and a description" do
instance = create_instance(admin)
hash = instance.second_factor_auth_required!(params({ user_id: user.id }))
expect(hash[:callback_params]).to eq({ user_id: user.id })
expect(hash[:redirect_url]).to eq("/admin/users/#{user.id}/#{user.username}")
expect(hash[:description]).to eq(
I18n.t(
"second_factor_auth.actions.grant_admin.description",
username: "@#{user.username}"
)
)
end
it "ensures the acting user is admin" do
instance = create_instance(user)
expect {
instance.second_factor_auth_required!(params({ user_id: user.id }))
}.to raise_error(Discourse::InvalidAccess)
end
end
describe "#second_factor_auth_completed!" do
it "grants the target user admin access and logs to staff action logs" do
instance = create_instance(admin)
expect {
instance.second_factor_auth_completed!(user_id: user.id)
}.to change { user.reload.admin }.from(false).to(true)
expect(UserHistory.exists?(
acting_user_id: admin.id,
target_user_id: user.id,
action: UserHistory.actions[:grant_admin]
)).to eq(true)
end
it "ensures the acting user is admin" do
instance = create_instance(user)
expect {
instance.second_factor_auth_completed!(user_id: user.id)
}.to raise_error(Discourse::InvalidAccess)
end
end
end