Laravel Jetstream+社交名流+ 2FA

发布于 2025-02-13 16:56:26 字数 217 浏览 0 评论 0原文

我们从Laravel Jetstream(惯性 / VUEJS)开始了一个新项目,并实施了Laravel Socialite。

当新的用户寄存器登录,启用2FA和重新连接2FA时,2FA的工作正常,但是当用户使用Socialite(Microsoft)登录时,启用2FA和RE-LOGIN,用户无法获得2FA表单,但直接转到仪表板。

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

We started a new project with Laravel Jetstream (Inertia / vuejs) and implemented Laravel Socialite.

When a new user register, login, enable 2FA and re-login the 2FA works fine, but when a user login with Socialite (Microsoft), enable 2FA and re-login the user doesn’t get the 2FA form, but go straight to the dashboard.

How can we use fortify to handle the 2fa processes for socialite users?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下凄凉 2025-02-20 16:56:26

对于登录,注册和其他流程,我们修改了现有的Sanctum auth路线,并指向强化路线,因此这些功能在成功集成强化和正确配置后将按预期工作。在“强化配置文件”中,我们必须处理其功能:

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

SocialIte用户将不会随身携带密码详细信息,因此,如果我们设置了确认通信为真,则将在处理启用/禁用2FA时显示确认密码视图。在这种情况下,我们必须将其设置为错误。

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

现在,问题发生在从社交网站回调的用户身份验证用户后发生,因为在其文档中,我们没有看到任何可以帮助调用2FA挑战的东西,因此,2FA旁路,我们无法处理。对我来说幸运的是,我找到了一种调用同一事件(Twofactorauthentication challenged)的方法,该事件让Fotify处理其余的。

使用laravel \ fortify \ events \ twofactorauthentication授予;

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));
        }
    }

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

For login, registration, and other flow, we modify our existing sanctum auth routes and point to the Fortify routes, due to which these features will work as expected after the successful integration of Fortify and proper configuration. In the Fortify config file, we have to deal with it's features:

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

Socialite users will not have their password details with them, so if we set confirmPassword is true, this will show the confirm password views while dealing with enabling/disabling 2FA. In this case, we have to set it false.

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

Now the problem occurs after authenticating the user from the socialite callback because in it's document we did not see anything that can help to invoke the 2FA challenge, and because of that, the 2FA bypass, and we can't handle it. Lucky for me, I found a way to invoke the same event(TwoFactorAuthenticationChallenged) that let the Fotify handle the rest.

use 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));
        }
    }

I hope this will sort out your issue too. Looking for a way to dynamically maintain the password confirmation so that we can perform that for other users but not for socialite user.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文