Laravel Jetstream + 社交名流 + 2FA

问题描述 投票:0回答:1

我们使用 Laravel Jetstream(Inertia / vuejs)启动了一个新项目并实现了 Laravel Socialite。

当新用户注册、登录、启用 2FA 并重新登录时,2FA 工作正常,但是当用户使用 Socialite (Microsoft) 登录、启用 2FA 并重新登录时,用户不会收到 2FA 表单,而是直接进入到仪表板。

我们如何使用 fortify 来处理社交名流用户的 2fa 流程?

laravel fortify laravel-socialite
1个回答
0
投票

对于登录、注册和其他流程,我们修改现有的 sainttum auth 路由并指向 Fortify 路由,因此在成功集成 Fortify 并进行正确配置后,这些功能将按预期工作。在 Fortify 配置文件中,我们必须处理它的功能:

    'features' => [
    Features::registration(),
    Features::resetPasswords(),
    Features::emailVerification(),
    Features::updateProfileInformation(),
    Features::updatePasswords(),
    Features::twoFactorAuthentication([
        'confirm' => true,
        'confirmPassword' => true, //Socialite user can't perform it.
    ]),
],

社交名流用户不会拥有他们的密码详细信息,因此如果我们将confirmPassword设置为true,这将在处理启用/禁用2FA时显示确认密码视图。在这种情况下,我们必须将其设置为 false。

  Features::twoFactorAuthentication([
    'confirm' => true,
    'confirmPassword' => false, //we don't want password confirmation
]),

现在,问题发生在通过社交名流回调对用户进行身份验证之后,因为在其文档中我们没有看到任何可以帮助调用 2FA 挑战的内容,因此,2FA 绕过,我们无法处理它。对我来说幸运的是,我找到了一种方法来调用相同的事件(TwoFactorAuthenticationChallenged),让 Fotify 处理其余的事情。

使用 Laravel\Fortify\Events\TwoFactorAuthenticationChallenged;

if (!empty($authenticatedUser)) {
        if ($authenticatedUser->hasEnabledTwoFactorAuthentication()) {
            $request->session()->put([
                'login.id' => $authenticatedUser->getKey(),
                'login.remember' => true,
            ]);
            TwoFactorAuthenticationChallenged::dispatch($authenticatedUser);
            return redirect()->route('two-factor.login');
        } else {
            Auth::login($authenticatedUser, true);
            return redirect()->intended(route('user.dashboard', absolute: false));
        }
    }

我希望这也能解决您的问题。寻找一种动态维护密码确认的方法,以便我们可以为其他用户执行此操作,但不能为社交名流用户执行此操作。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.