将客户端凭据作为变量传递给 Laravel Socialite

huangapple go评论76阅读模式
英文:

Pass client credentials as variable to Laravel Socialite

问题

Here's the translated code section you provided:

我有一个Laravel应用程序,其中有一个简单的多租户设置。每个用户都属于一个帐户,而用户具有帐户范围;例如,每当查询用户(`User::where('name', 'like', "%query%")->get()`)时,它们始终会根据已登录用户的`account_id`进行过滤。

我想在此应用程序中为帐户管理员提供SSO,我目前正在研究[Microsoft的SSO](https://socialiteproviders.com/Microsoft/)。如果一个帐户想要使用其公司的Microsoft帐户的SSO,而另一个帐户想要使用自己的,我将需要它们提供自己的`client_id``client_secret``tenant_id`

我正在尝试将这些值作为变量传递,而不是将它们编码在`config/services.php`中。

到目前为止,我有一个路由导向这个函数:

```php
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;

public function microsoftLogin()
{
    $ssoSettings = Auth::user()->account->ssoSettings;

    return Socialite::driver('microsoft')
        ->with([
            'client_id' => $ssoSettings->client_id,
            'client_secret' => $ssoSettings->client_secret,
            'redirect_uri' => config('services.microsoft.redirect'),
            'tenant_id' => $ssoSettings->tenant_id,
        ])
        ->redirect();
}

这仍然给我以下错误:

> SocialiteProviders\Manager\Exception\MissingConfigException: Missing services entry for microsoft.client_id

以以下方式构建提供程序:

use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;
use SocialiteProviders\Microsoft\MicrosoftExtendSocialite;

public function microsoftLogin()
{
    $ssoSettings = Auth::user()->account->ssoSettings;

    return Socialite::buildProvider(MicrosoftExtendSocialite::class, [
        'client_id' => $ssoSettings->client_id,
        'client_secret' => $ssoSettings->client_secret,
        'redirect_uri' => config('services.microsoft.redirect'),
        'tenant_id' => $ssoSettings->tenant_id,
    ])->redirect();
}

这会导致以下错误:

> ErrorException: Undefined array key "redirect"

异常似乎是在'tenant_id' => $ssoSettings->tenant_id上抛出的,这更加奇怪。config('services.microsoft.redirect')确实给了我正确的字符串。

如何传递帐户的SSO设置,覆盖.env/config中的设置?


<details>
<summary>英文:</summary>

I have a Laravel application with a simple multi-tenancy set-up. Every User belongs to an Account, and the User has an AccountScope; e.g. whenever a User is queried (`User::where(&#39;name&#39;, &#39;like&#39;, &quot;%query%&quot;)-&gt;get()`) they will always be filtered on the logged-in user&#39;s `account_id`.

I want to provide account admins with SSO in this application, I&#39;m currently looking into [Microsoft&#39;s SSO](https://socialiteproviders.com/Microsoft/). If one account wants to use their company&#39;s Microsoft account&#39;s SSO, and the other Account wants to use theirs, I&#39;ll need them to provide their own `client_id`, `client_secret` and `tenant_id`.

I am trying to pass these values as variables, rather than coding them in `config/services.php`.

What I have so far is a route leading to this function:

```php
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;

public function microsoftLogin()
{
    $ssoSettings = Auth::user()-&gt;account-&gt;ssoSettings;

    return Socialite::driver(&#39;microsoft&#39;)
        -&gt;with([
            &#39;client_id&#39; =&gt; $ssoSettings-&gt;client_id,
            &#39;client_secret&#39; =&gt; $ssoSettings-&gt;client_secret,
            &#39;redirect_uri&#39; =&gt; config(&#39;services.microsoft.redirect&#39;),
            &#39;tenant_id&#39; =&gt; $ssoSettings-&gt;tenant_id,
        ])
        -&gt;redirect();
}

Which still gives me the following error:

> SocialiteProviders\Manager\Exception\MissingConfigException: Missing services entry for microsoft.client_id

And building the provider like so:

use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;
use SocialiteProviders\Microsoft\MicrosoftExtendSocialite;

public function microsoftLogin()
{
    $ssoSettings = Auth::user()-&gt;account-&gt;ssoSettings;

    return Socialite::buildProvider(MicrosoftExtendSocialite::class, [
        &#39;client_id&#39; =&gt; $ssoSettings-&gt;client_id,
        &#39;client_secret&#39; =&gt; $ssoSettings-&gt;client_secret,
        &#39;redirect_uri&#39; =&gt; config(&#39;services.microsoft.redirect&#39;),
        &#39;tenant_id&#39; =&gt; $ssoSettings-&gt;tenant_id,
    ])-&gt;redirect();
}

Which gives me:

> ErrorException: Undefined array key "redirect"

The exception seems to be thrown on &#39;tenant_id&#39; =&gt; $ssoSettings-&gt;tenant_id, which is all the more strange. config(&#39;services.microsoft.redirect&#39;) does give me the correct string.

How can I pass the Account's SSO settings, overriding the ones from the .env/config?

答案1

得分: 0

你应该使用Socialite::buildProvider方法。你可以查看内置提供者的工作方式,例如github

英文:

You should use Socialite::buildProvider method. You can see how it works with the built-in providers, for example github

答案2

得分: 0

我找到了解决方案,使用了KejKej的答案;特别是 Microsoft 对他们的社交提供程序有一种奇怪的命名方案。以下是我的可用代码:

use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;
use SocialiteProviders\Microsoft\Provider as MicrosoftProvider;

public function microsoftLogin()
{
    $ssoSettings = Auth::user()->account->ssoSettings;

    return Socialite::buildProvider(MicrosoftProvider::class, [
        'client_id' => $ssoSettings->client_id,
        'client_secret' => $ssoSettings->client_secret,
        'redirect' => config('services.microsoft.redirect'),
        'tenant_id' => $ssoSettings->tenant_id,
    ])->redirect();
}

在我的代码中,有两件事情我需要修复:

  1. Microsoft 的提供程序可以在命名空间 SocialiteProviders\Microsoft\Provider 中找到。这是需要传递给 Socialite::buildProvider 的类。
  2. 重定向 URI 的键必须称为 'redirect',可以在Socialite Providers Microsoft 页面上看到。

希望这对未来的读者解决类似的问题有所帮助!

英文:

I found the solution with KejKej's answer; Microsoft in particular just has an odd naming scheme for their socialite provider. Here's my working code:

use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;
use SocialiteProviders\Microsoft\Provider as MicrosoftProvider;

public function microsoftLogin()
{
    $ssoSettings = Auth::user()-&gt;account-&gt;ssoSettings;

    return Socialite::buildProvider(MicrosoftProvider::class, [
        &#39;client_id&#39; =&gt; $ssoSettings-&gt;client_id,
        &#39;client_secret&#39; =&gt; $ssoSettings-&gt;client_secret,
        &#39;redirect&#39; =&gt; config(&#39;services.microsoft.redirect&#39;),
        &#39;tenant_id&#39; =&gt; $ssoSettings-&gt;tenant_id,
    ])-&gt;redirect();
}

There were two things I needed to fix in my code:

  1. Microsoft's provider can be found in the namespace SocialiteProviders\Microsoft\Provider. This is the class that needs to be passed to Socialite::buildProvider.
  2. The redirect URI's key has to be called &#39;redirect&#39;, as can be seen on the Socialite Providers Microsoft page

I hope this helps future readers with a similar problem!

huangapple
  • 本文由 发表于 2023年6月5日 23:27:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76407913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定