Drupal 8 external/custom 身份验证提供程序
Drupal 8 external/custom authentication provider
我正在尝试使用 Drupal 8 作为我们的客户网站。我们的客户目前通过我们自己的身份验证应用程序进行身份验证,该应用程序与我们的文档存储(而不是 MySQL)对话以对用户进行身份验证并为他们提供唯一的会话 ID(最终是 JWT,但那是另一天的对话)我们可以用来查询 REST API 并在我们的任何其他自助应用程序中获取用户数据。
我们正在从基于 JSP 的旧网站迁移到 drupal,因为我们的应用程序现在是用 Symfony 3 编写的,但希望我们的客户网站是 Drupal 8。
这是我正在努力解决的问题。如果我在我们的旧网站上进行身份验证,我希望能够使用我们手中的会话 ID 重定向到 Drupal 8 网站,并使用它来获取我们登录用户的对象。我的这部分工作正常,但我现在可以说...好的,我有用户对象,第 3 方服务说会话 ID 有效,所以我们知道我们已通过身份验证。
请参考以下流程图。我还希望能够在 Drupal 8 中手动进行身份验证。这可能吗(我确定是),如果可以的话,有人可以指出我 need/should 正在做的事情的正确方向,我应该打电话给 API 吗?
谢谢你,美好的一天:)
好的,结果证明最终并没有那么棘手。我以为我必须扩展和实施各种 class 并创建我自己的提供程序(这可能是最佳实践)但是为了 KISS 我找到了另一种方法。
根据我从外部服务返回的用户数据,如果用户不存在,请先创建一个用户。然后将创建的用户传递给 user_login_finalize
方法(为什么很多方法都在 Drupal 下划线...)然后对我的用户进行身份验证。
public function inbound(Request $request)
{
// Point the guzzle client to our external session service.
$client = new GuzzleHttpClient([
'base_uri' => 'https://myexternalservice.com/apps/authentication/2/',
]);
// Attempt to send to request with the session ID from the parameters.
try {
$response = $client->request('GET', 'api/v1/user/' . $request->get('session_id'));
} catch (\Exception $e) {
throw new \HttpException($e->getMessage());
}
// Convert the response to an array.
$result = json_decode((string) $response->getBody(), true);
// Convert our array to a user entity.
if ($user = $this->convertResponseToUser($result['user'])) {
try {
// Attempt to load the user. If the user does not exist then create them first.
if (!$assumeUser = user_load_by_mail($user->getEmail())) {
// Create a Drupal user object.
$assumeUser = $this->createUser([
'name' => $user->getFirstName() . ' ' . $user->getLastName(),
'mail' => $user->getEmail()
]);
$assumeUser->save();
}
// Authenticate the user.
user_login_finalize($assumeUser);
} catch (\Exception $e) {
drupal_set_message(t('An unhandled exception occurred during authentication.'), 'error');
return $this->redirect('user.login');
}
}
return $this->redirect('mymodule.route');
}
您应该使用 External Auth 模块。
如何使用此模块的一个很好的例子是 SimpleSamlPHP Auth
我正在尝试使用 Drupal 8 作为我们的客户网站。我们的客户目前通过我们自己的身份验证应用程序进行身份验证,该应用程序与我们的文档存储(而不是 MySQL)对话以对用户进行身份验证并为他们提供唯一的会话 ID(最终是 JWT,但那是另一天的对话)我们可以用来查询 REST API 并在我们的任何其他自助应用程序中获取用户数据。
我们正在从基于 JSP 的旧网站迁移到 drupal,因为我们的应用程序现在是用 Symfony 3 编写的,但希望我们的客户网站是 Drupal 8。
这是我正在努力解决的问题。如果我在我们的旧网站上进行身份验证,我希望能够使用我们手中的会话 ID 重定向到 Drupal 8 网站,并使用它来获取我们登录用户的对象。我的这部分工作正常,但我现在可以说...好的,我有用户对象,第 3 方服务说会话 ID 有效,所以我们知道我们已通过身份验证。
请参考以下流程图。我还希望能够在 Drupal 8 中手动进行身份验证。这可能吗(我确定是),如果可以的话,有人可以指出我 need/should 正在做的事情的正确方向,我应该打电话给 API 吗?
谢谢你,美好的一天:)
好的,结果证明最终并没有那么棘手。我以为我必须扩展和实施各种 class 并创建我自己的提供程序(这可能是最佳实践)但是为了 KISS 我找到了另一种方法。
根据我从外部服务返回的用户数据,如果用户不存在,请先创建一个用户。然后将创建的用户传递给 user_login_finalize
方法(为什么很多方法都在 Drupal 下划线...)然后对我的用户进行身份验证。
public function inbound(Request $request)
{
// Point the guzzle client to our external session service.
$client = new GuzzleHttpClient([
'base_uri' => 'https://myexternalservice.com/apps/authentication/2/',
]);
// Attempt to send to request with the session ID from the parameters.
try {
$response = $client->request('GET', 'api/v1/user/' . $request->get('session_id'));
} catch (\Exception $e) {
throw new \HttpException($e->getMessage());
}
// Convert the response to an array.
$result = json_decode((string) $response->getBody(), true);
// Convert our array to a user entity.
if ($user = $this->convertResponseToUser($result['user'])) {
try {
// Attempt to load the user. If the user does not exist then create them first.
if (!$assumeUser = user_load_by_mail($user->getEmail())) {
// Create a Drupal user object.
$assumeUser = $this->createUser([
'name' => $user->getFirstName() . ' ' . $user->getLastName(),
'mail' => $user->getEmail()
]);
$assumeUser->save();
}
// Authenticate the user.
user_login_finalize($assumeUser);
} catch (\Exception $e) {
drupal_set_message(t('An unhandled exception occurred during authentication.'), 'error');
return $this->redirect('user.login');
}
}
return $this->redirect('mymodule.route');
}
您应该使用 External Auth 模块。
如何使用此模块的一个很好的例子是 SimpleSamlPHP Auth