我正在使用多租户设置,我正在寻找一种方法,当我尝试请求访问令牌时,可以将请求发送到正确的登录端点。
例如,在单租户设置中,当我请求访问令牌时,它看起来像这样
$http = new \GuzzleHttp\Client;
try {
$response = $http->post('http://mydomain.test/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => env('CLIENT_ID'),
'client_secret' => env('CLIENT_SECRET'),
'username' => $request->username,
'password' => $request->password,
]
]);
return response()->getBody();
}
现在我要做的是根据登录的租户用户更改登录端点$ http-> post(我的URL)。
因此,当前,当我向登录控制器发出发布请求时,路由被包裹在这样的中间件中
Route::group(['middleware' => 'tenancy.enforce'], function () {
Route::post('/login', 'PassportTokenController@login');
});
然后在我的控制器中,我可以根据请求信息的租户调用客户端密码和fqdn
因此,当我请求发出请求的fqdn和租户数据库中的client_secret时,我将获得正确的数据
$passport = DB::table('oauth_clients')->where('id', 2)->first();
$hostname = app(\Hyn\Tenancy\Environment::class)->hostname();
现在,如果我将这些变量插入登录方法,我将收到500个内部服务器错误
public function login(Request $request)
{
$passport = DB::table('oauth_clients')->where('id', 2)->first();
$hostname = app(\Hyn\Tenancy\Environment::class)->hostname();
$http = new \GuzzleHttp\Client;
try {
$response = $http->post('http://' . $hostname->fqdn . '/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => 2,
'client_secret' => $passport->secret,
'username' => $request->username,
'password' => $request->password,
]
]);
return response()->getBody();
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
if ($e->getCode() === 400) {
return response()->json('Invalid Request. Please enter a username or a password.', $e->getCode());
} else if ($e->getCode() === 401) {
return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
}
return response()->json('Something went wrong on the server.', $e->getCode());
}
}
请注意,$http->post(my url)
的更改取决于提出请求的租户用户。
我应该做些带护照的事情,以便我可以动态地请求oauth访问令牌吗?
*更新,以便在弄清楚如何获得实际的服务器错误响应后,我注意到Guzzle http正在尝试查询系统数据库而不是租户数据库
Base table or view not found: 1146 Table 'multitenant-diy.oauth_clients' doesn't exist (SQL: select * f (truncated...)
in C:\laragon\www\multitenant-diy\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:113