所以我正在開(kāi)發(fā)一個(gè) Laravel 管理應(yīng)用程序,它使用外部 API,我們稱(chēng)之為 PlatformAPI。平臺(tái)的工作方式是,我的應(yīng)用程式的用戶(hù)在平臺(tái)上有一個(gè)帳戶(hù)。我的 Laravel 應(yīng)用程式將充當(dāng)管理儀表板,因此使用者可以查看一些從 PlatformAPI 取得的基本報(bào)告。
我的應(yīng)用程式中的每個(gè)用戶(hù)都必須添加他們的客戶(hù)端 ID 和客戶(hù)端密鑰,他們可以在平臺(tái)中創(chuàng)建這些內(nèi)容。這樣,我的應(yīng)用程式將能夠使用使用者的憑證代表他們向 PlatformAPI 執(zhí)行請(qǐng)求。
我讀過(guò)一些文章和教程,基本上介紹如何為服務(wù)設(shè)定憑證/令牌並將該服務(wù)綁定到 ServiceProvider,如下所示:
<?php namespace AppServicesPlatformAPI; class Client { protected string $clientId; protected string $clientSecret; public function __construct(string $clientId, string $clientSecret) { $this->clientId = $clientId; $this->clientSecret = $clientSecret; } public function getSales(string $month) { // ... } } <?php use AppServicesPlatformApiClient; class PlatformApiServiceProvider extends ServiceProvider { public function register() { $this->app->singleton(Client::class, function ($app) { return new Client( clientId: config('services.platform-api.client-id'), clientSecret: config('services.platform-api.client-secret'), ); }); } }
這樣,您就不必每次想要使用 PlatformApi 時(shí)都設(shè)定客戶(hù)端憑證,我可以像這樣呼叫該服務(wù):
<?php namespace AppHttpControllers; use AppServicesPlatformApiClient; class RandomController extends Controller { protected Client $client; public function __construct(Client $client) { $this->client = $client; } public function index() { $this->client->getSales(string $month); } }
但是,由於我需要代表我的應(yīng)用程式的使用者使用他們提供的憑證(並儲(chǔ)存在我的應(yīng)用程式的資料庫(kù)中)向PlatformApi 執(zhí)行請(qǐng)求,所以我不確定這種相同的方法是否有效,因?yàn)閱卫荒軐?shí)例一次?
此外,為了使用 PlatformApi,我需要使用使用者的憑證來(lái)取得存取權(quán)杖。該存取權(quán)杖也需要儲(chǔ)存在某個(gè)地方(我想在快取中)。
我有點(diǎn)不知道該如何解決這個(gè)問(wèn)題。任何指示將不勝感激。
我假設(shè)您的所有應(yīng)用程式都將使用此客戶(hù)端服務(wù)。如果是這樣,您可以繼續(xù)使用單例設(shè)計(jì)模式(以停止進(jìn)一步的 oauth 請(qǐng)求),但請(qǐng)嘗試將邏輯與提供者的 register 方法分開(kāi)。您可以在呼叫傳回有效access_token
的私有方法後實(shí)例化Client 類(lèi)別(如果存在有效令牌,則檢查DB
/ Cache
expires_in
時(shí)間戳值並傳回它,或向使用者要求一個(gè)新的client
/secret
並傳回它)
/** * Register any application services. * * @return void */ public function register(): void { $this->app->singleton(Client::class, function () { return new Client( accessToken: $this->getUserToken()->access_token ); }); } /** * Tries to get the user token from the database. * * @return ClientCredentials */ private function getUserToken(): ClientCredentials { $credentials = ClientCredentials::query() ->latest() ->find(id: auth()->user()->id); if ($credentials === null || now() > $credentials->expires_at) { $credentials = $this->requestUserToken(); } return $credentials; } /** * Requests a new token for the user & stores it in the database. * * @return ClientCredentials */ private function requestUserToken(): ClientCredentials { $tokenResponse = API::requestToken( client: auth()->user()->client, secret: auth()->user()->secret, ); return ClientCredentials::query()->create( attributes: [ 'user_id' => auth()->user()->id, 'access_token' => $tokenResponse['access_token'], 'refresh_token' => $tokenResponse['refresh_token'], 'token_type' => 'Bearer', 'expires_at' => new DateTime(datetime: '+' . $tokenResponse['expires_in'] . ' seconds') ], ); }