簡體   English   中英

Drupal 8外部/自定義身份驗證提供程序

[英]Drupal 8 external/custom authentication provider

我正在嘗試使用Drupal 8作為我們的客戶網站。 我們的客戶通過我們自己的身份驗證應用程序進行身份驗證,該應用程序與我們的文檔存儲(而不是MySQL)通信,以對用戶進行身份驗證並為他們提供唯一的會話ID(JWT最終,但那是另一天和會話)我們可以使用查詢REST API並在我們的任何其他自我應用程序中獲取用戶數據。

我們正在從一個舊的基於JSP的網站轉移到drupal,因為我們的應用程序現在用Symfony 3編寫,但希望我們的客戶網站是Drupal 8。

這是我想要解決的問題。 如果我在舊網站上進行身份驗證,我希望能夠使用我們手中的會話ID重定向到Drupal 8網站,並使用它來獲取我們登錄用戶的對象。 我有點工作正常,但我現在可以說...好的我有用戶對象,第三方服務已經說會話ID有效,所以我們知道我們已經過身份驗證。

請參考下面的流程圖。 我希望能夠手動在Drupal 8中進行身份驗證。 這是否可能(我確定是這樣),如果是這樣,有人能指出我正確的方向,我需要/應該做什么,我應該打電話給API嗎?

在此輸入圖像描述

謝謝你,好日子:)

您應該使用外部Auth模塊。

使用此模塊的一個很好的例子是SimpleSamlPHP Auth

好吧,事實證明最終並不是那么棘手。 我以為我必須擴展和實現各種類並創建我自己的提供者(這可能是最佳實踐)但是為了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');
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM