Skip to content

Commit 9811bad

Browse files
authored
change FileStore to use FilesystemManager with disk configuration (#67)
* change FileStore to use FilesystemManager with disk configuration
1 parent 4f565c7 commit 9811bad

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ Route::get('/manage/xero', [\App\Http\Controllers\XeroController::class, 'index'
127127
```
128128

129129
## Credential Storage
130-
Version 1 of this package stored the credentials in the cache, while this worked for most, some users clear their cache
131-
on deployment of a new version of their app, this also restricted an app to only one set of credentials per codebase.
130+
Credentials are stored in a JSON file using the default disk on the Laravel Filesystem, with visibility set to private. This allows credential sharing across multiple servers using a shared disk such as S3, regardless of which server conducted the OAuth flow.
132131

133-
Version 2 swaps this out for an interface and provides a default `FileStore` implementation which stores the credentials on
134-
`/storage/framework/xero.json` you can switch out the credential store (e.g. for your own `UserStore` if you wanted to store
135-
the credentials against your user) in one of two ways
132+
To use a different disk, change the `xero.credential_disk` config item to another disk defined in `config/filesystem.php`.
133+
134+
You can switch out the credential store (e.g. for your own `UserStore` if you wanted to store
135+
the credentials against your user) in one of two ways:
136136

137137
1. If it's a simple store and Laravel can automatically resolve your bindings, simply change the `xero.credential_store` config
138138
key to point to your new implementation.

config/config.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
************************************************************************/
1313
'credential_store' => FileStore::class,
1414

15+
/************************************************************************
16+
* Disk used to store credentials.
17+
************************************************************************/
18+
'credential_disk' => env('XERO_CREDENTIAL_DISK'),
19+
1520
'oauth' => [
1621
/************************************************************************
1722
* Client ID provided by Xero when registering your application

src/Oauth2CredentialManagers/FileStore.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
namespace Webfox\Xero\Oauth2CredentialManagers;
55

66

7-
use Illuminate\Filesystem\Filesystem;
7+
use Illuminate\Filesystem\FilesystemManager;
88
use Illuminate\Session\Store;
99
use League\OAuth2\Client\Token\AccessTokenInterface;
1010
use Webfox\Xero\Oauth2Provider;
1111
use Webfox\Xero\OauthCredentialManager;
1212

1313
class FileStore implements OauthCredentialManager
1414
{
15-
/** @var Filesystem */
16-
protected $files;
15+
/** @var FilesystemManager */
16+
protected $disk;
1717

1818
/** @var Oauth2Provider */
1919
protected $oauthProvider;
@@ -24,12 +24,12 @@ class FileStore implements OauthCredentialManager
2424
/** @var string */
2525
protected $filePath;
2626

27-
public function __construct(Filesystem $files, Store $session, Oauth2Provider $oauthProvider)
27+
public function __construct(FilesystemManager $files, Store $session, Oauth2Provider $oauthProvider)
2828
{
29-
$this->files = $files;
29+
$this->disk = $files->disk(config('xero.credential_disk', config('filesystems.default')));
3030
$this->oauthProvider = $oauthProvider;
3131
$this->session = $session;
32-
$this->filePath = storage_path('framework/xero.json');
32+
$this->filePath = 'xero.json';
3333
}
3434

3535
public function getAccessToken(): string
@@ -72,7 +72,7 @@ public function getData(): array
7272

7373
public function exists(): bool
7474
{
75-
return $this->files->exists($this->filePath);
75+
return $this->disk->exists($this->filePath);
7676
}
7777

7878
public function isExpired(): bool
@@ -92,14 +92,14 @@ public function refresh(): void
9292

9393
public function store(AccessTokenInterface $token, string $tenantId = null): void
9494
{
95-
$ret = $this->files->put($this->filePath, json_encode([
95+
$ret = $this->disk->put($this->filePath, json_encode([
9696
'token' => $token->getToken(),
9797
'refresh_token' => $token->getRefreshToken(),
9898
'id_token' => $token->getValues()['id_token'],
9999
'expires' => $token->getExpires(),
100100
'tenant_id' => $tenantId ?? $this->getTenantId()
101-
]));
102-
101+
]), 'private');
102+
103103
if ($ret === false) {
104104
throw new \Exception("Failed to write to file: {$this->filePath}");
105105
}
@@ -132,7 +132,7 @@ protected function data($key = null)
132132
throw new \Exception('Xero oauth credentials are missing');
133133
}
134134

135-
$cacheData = json_decode($this->files->get($this->filePath), true);
135+
$cacheData = json_decode($this->disk->get($this->filePath), true);
136136

137137
return empty($key) ? $cacheData : ($cacheData[$key] ?? null);
138138
}

0 commit comments

Comments
 (0)