英文:
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('name', 'like', "%query%")->get()`) they will always be filtered on the logged-in user's `account_id`.
I want to provide account admins with SSO in this application, I'm currently looking into [Microsoft's SSO](https://socialiteproviders.com/Microsoft/). If one account wants to use their company's Microsoft account's SSO, and the other Account wants to use theirs, I'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()->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();
}
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()->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();
}
Which gives me:
> ErrorException: Undefined array key "redirect"
The exception seems to be thrown on 'tenant_id' => $ssoSettings->tenant_id
, which is all the more strange. config('services.microsoft.redirect')
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();
}
在我的代码中,有两件事情我需要修复:
- Microsoft 的提供程序可以在命名空间
SocialiteProviders\Microsoft\Provider
中找到。这是需要传递给Socialite::buildProvider
的类。 - 重定向 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()->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();
}
There were two things I needed to fix in my code:
- Microsoft's provider can be found in the namespace
SocialiteProviders\Microsoft\Provider
. This is the class that needs to be passed toSocialite::buildProvider
. - The redirect URI's key has to be called
'redirect'
, as can be seen on the Socialite Providers Microsoft page
I hope this helps future readers with a similar problem!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论