Skip to content

Commit 97f8cbe

Browse files
committed
fix(files_sharing): refactor refreshing access tokens
Signed-off-by: Enrique Pérez Arnaud <enrique@cazalla.net>
1 parent 75077ec commit 97f8cbe

2 files changed

Lines changed: 110 additions & 120 deletions

File tree

apps/files_sharing/lib/External/Storage.php

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,11 @@ public function __construct($options) {
113113
}
114114

115115
/**
116-
* Refresh the access token by exchanging the refresh token.
117-
* Updates both the in-memory password and the database.
116+
* Refresh the bearer token. Extends parent to also persist to database.
118117
*
119118
* @return bool True if token was refreshed successfully
120119
*/
121-
protected function refreshAccessToken(): bool {
120+
protected function refreshBearerToken(): bool {
122121
if ($this->tokenRefreshed) {
123122
// only try to refresh once per request
124123
return false;
@@ -133,6 +132,7 @@ protected function refreshAccessToken(): bool {
133132

134133
$this->ready = false;
135134
$this->client = null;
135+
$this->init();
136136

137137
$this->logger->debug('Successfully refreshed access token', ['app' => 'files_sharing']);
138138
return true;
@@ -145,30 +145,6 @@ protected function refreshAccessToken(): bool {
145145
}
146146
}
147147

148-
/**
149-
* Execute an operation with automatic token refresh on 401 errors.
150-
*
151-
* @template T
152-
* @param callable(): T $operation The operation to execute
153-
* @return T
154-
* @throws \Exception
155-
*/
156-
protected function withTokenRefresh(callable $operation) {
157-
try {
158-
return $operation();
159-
} catch (\Sabre\HTTP\ClientHttpException $e) {
160-
if ($e->getHttpStatus() === 401 && $this->refreshAccessToken()) {
161-
return $operation();
162-
}
163-
throw $e;
164-
} catch (\Sabre\DAV\Exception\NotAuthenticated $e) {
165-
if ($this->refreshAccessToken()) {
166-
return $operation();
167-
}
168-
throw $e;
169-
}
170-
}
171-
172148
public function getWatcher(string $path = '', ?IStorage $storage = null): IWatcher {
173149
if (!$storage) {
174150
$storage = $this;
@@ -230,7 +206,7 @@ public function hasUpdated(string $path, int $time): bool {
230206
}
231207
$this->updateChecked = true;
232208
try {
233-
return $this->withTokenRefresh(fn () => parent::hasUpdated('', $time));
209+
return parent::hasUpdated('', $time);
234210
} catch (StorageInvalidException $e) {
235211
// check if it needs to be removed
236212
$this->checkStorageAvailability();
@@ -244,7 +220,7 @@ public function hasUpdated(string $path, int $time): bool {
244220

245221
public function test(): bool {
246222
try {
247-
return $this->withTokenRefresh(fn () => parent::test());
223+
return parent::test();
248224
} catch (StorageInvalidException $e) {
249225
// check if it needs to be removed
250226
$this->checkStorageAvailability();

0 commit comments

Comments
 (0)