Skip to content

Commit 60dc240

Browse files
committed
Revert the following commits that tried to implement getToken using OAuthTokenManager
Revert "add use OAuthTokenManager and create instance in constructor"This reverts commit 2a1e7c9.Revert "Add missing ) to retrieveROPCToken"This reverts commit 8df5cfc. Revert "Implement retrieveROPCToken as proposed in #1151 (comment)" This reverts commit 66cc732.
1 parent 2a1e7c9 commit 60dc240

File tree

2 files changed

+43
-26
lines changed

2 files changed

+43
-26
lines changed

src/Services/InfoProviderSystem/Providers/BuerklinProvider.php

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
use App\Services\InfoProviderSystem\DTOs\PriceDTO;
3131
use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO;
3232
use App\Services\InfoProviderSystem\DTOs\SearchResultDTO;
33-
use App\Services\OAuth\OAuthTokenManager;
3433
use App\Settings\InfoProviderSystem\BuerklinSettings;
3534
use Psr\Cache\CacheItemPoolInterface;
3635
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -51,10 +50,9 @@ class BuerklinProvider implements BatchInfoProviderInterface
5150

5251
public function __construct(
5352
private readonly HttpClientInterface $client,
54-
private readonly OAuthTokenManager $authTokenManager,
5553
private readonly CacheItemPoolInterface $partInfoCache,
5654
private readonly BuerklinSettings $settings,
57-
) {
55+
) {
5856

5957
}
6058

@@ -64,16 +62,52 @@ public function __construct(
6462
*/
6563
private function getToken(): string
6664
{
67-
//Check if we already have a token saved for this app, otherwise we have to retrieve one via OAuth
68-
if (!$this->authTokenManager->hasToken(self::OAUTH_APP_NAME)) {
69-
$this->authTokenManager->retrieveROPCToken(self::OAUTH_APP_NAME, $this->settings->username, $this->settings->password);
65+
// Cache token to avoid hammering the auth server on every request
66+
$cacheKey = 'buerklin.oauth.token';
67+
$item = $this->partInfoCache->getItem($cacheKey);
68+
69+
if ($item->isHit()) {
70+
$token = $item->get();
71+
if (is_string($token) && $token !== '') {
72+
return $token;
73+
}
74+
}
75+
76+
// Buerklin OAuth2 password grant (ROPC)
77+
$resp = $this->client->request('POST', 'https://www.buerklin.com/authorizationserver/oauth/token/', [
78+
'headers' => [
79+
'Accept' => 'application/json',
80+
'Content-Type' => 'application/x-www-form-urlencoded',
81+
],
82+
'body' => [
83+
'grant_type' => 'password',
84+
'client_id' => $this->settings->clientId,
85+
'client_secret' => $this->settings->secret,
86+
'username' => $this->settings->username,
87+
'password' => $this->settings->password,
88+
],
89+
]);
90+
91+
$data = $resp->toArray(false);
92+
93+
if (!isset($data['access_token'])) {
94+
throw new \RuntimeException(
95+
'Invalid token response from Buerklin: HTTP ' . $resp->getStatusCode() . ' body=' . $resp->getContent(false)
96+
);
7097
}
7198

72-
$token = $this->authTokenManager->getAlwaysValidTokenString(self::OAUTH_APP_NAME);
73-
if ($token === null) {
74-
throw new \RuntimeException('Could not retrieve OAuth token for Buerklin API.');
99+
$token = (string) $data['access_token'];
100+
101+
// Cache for (expires_in - 30s) if available
102+
$ttl = 300;
103+
if (isset($data['expires_in']) && is_numeric($data['expires_in'])) {
104+
$ttl = max(60, (int) $data['expires_in'] - 30);
75105
}
76106

107+
$item->set($token);
108+
$item->expiresAfter($ttl);
109+
$this->partInfoCache->save($item);
110+
77111
return $token;
78112
}
79113

src/Services/OAuth/OAuthTokenManager.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -159,21 +159,4 @@ public function retrieveClientCredentialsToken(string $app_name): OAuthToken
159159

160160
return $this->saveToken($app_name, $access_token);
161161
}
162-
163-
/**
164-
* Retrieves an access token for the given app name using the client ROPC grant (so no user flow is needed)
165-
* The app_name must be registered in the knpu_oauth2_client.yaml
166-
* The token is saved to the database, and afterwards can be used as usual
167-
* @param string $app_name
168-
* @return OAuthToken
169-
*/
170-
public function retrieveROPCToken(string $app_name, string $user, string $password): OAuthToken
171-
{
172-
$client = $this->clientRegistry->getClient($app_name);
173-
$access_token = $client->getOAuth2Provider()->getAccessToken('password', [
174-
'username' => $user,
175-
'password' => $password
176-
]);
177-
return $this->saveToken($app_name, $access_token);
178-
}
179162
}

0 commit comments

Comments
 (0)