From 26b632dd88b4cb70c9110be39826278370b08fee Mon Sep 17 00:00:00 2001 From: James Freeman Date: Sat, 12 Oct 2024 21:32:51 +0100 Subject: [PATCH 1/5] Document Credential Storages and Refactored Auth User Store to support different guards --- docs/credential-storage.md | 83 +++++++++++++++---- .../AuthenticatedUserStore.php | 7 +- .../BaseCredentialManager.php | 6 ++ src/Oauth2CredentialManagers/ModelStore.php | 4 +- src/Xero.php | 16 +++- tests/Unit/CredentialManagersTest.php | 55 ++++++++---- 6 files changed, 135 insertions(+), 36 deletions(-) diff --git a/docs/credential-storage.md b/docs/credential-storage.md index 98f104f..d3b0258 100644 --- a/docs/credential-storage.md +++ b/docs/credential-storage.md @@ -1,26 +1,77 @@ -## Credential Storage +# Credential Storage +There are currently five credential storage options available: + +1. ** File Store ** (default) - Stores the credentials in a JSON file on the default disk. +2. ** Cache Store ** - Stores the credentials in the cache. +3. ** Model Store ** - Stores the credentials against a predefined model. +4. ** Authenticated User Store ** - Stores the credentials against the authenticated user. +5. ** Array Store ** - Stores the credentials in an array. + +## Changing the default storage + +If your using a default storage you can change the `xero.credential_store` config key to point to the new implementation. + +## File Store 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. To use a different disk, change the `xero.credential_disk` config item to another disk defined in `config/filesystem.php`. -You can switch out the credential store (e.g. for your own `UserStore` if you wanted to store -the credentials against your user) in one of two ways: +## Cache Store + +Credentials are stored in the cache, this is useful for applications that do not have a shared disk. + +## Model Store + +Credentials are stored against a predefined model, this is useful for applications that have a settings model. + +For the package to know which model you want to use, you will need to call the following method: + +```php +use Webfox\Xero\Xero; + +Xero::useModelStore(Settings::class); +``` + +By default, the package will use the `xero_credentials` field, Should you need to rename this field, you can do so by calling: + +```php +use Webfox\Xero\Xero; + +Xero::useAttributeOnModelStore('my_custom_field'); +``` + +## Authenticated User Store + +Credentials are stored against the authenticated user. By default, the package will use the `xero_credentials` field on the Users table. -1. If it's a simple store and Laravel can automatically resolve your bindings, simply change the `xero.credential_store` config - key to point to your new implementation. -2. If it requires more advanced logic (e.g. using the current user to retrieve the credentials) then you can rebind this - in your `AppServiceProvider` or a Middleware - e.g. +Should you need to rename this field, you can do so by calling: ```php -$this->app->bind(OauthCredentialManager::class, function(Application $app) { - return new UserStorageProvider( - \Auth::user(), // Storage Mechanism - $app->make('session.store'), // Used for storing/retrieving oauth 2 "state" for redirects - $app->make(\Webfox\Xero\Oauth2Provider::class) // Used for getting redirect url and refreshing token - ); -}); +use Webfox\Xero\Xero; + +Xero::useAttributeOnModelStore('my_custom_field'); ``` -An example UserStorageProvider [can be found here](https://github.com/webfox/laravel-xero-oauth2/issues/45#issuecomment-757552563) \ No newline at end of file +Should you need to change the authentication guard, you can do so by calling: +```php +use Webfox\Xero\Xero; + +Xero::setDefaultAuthGuard('admin'); +``` + +## Array Store + +Credentials are stored in an array, this is useful for testing purposes only. Once the application is restarted, the credentials will be lost. + +## Creating your own Storage Manager + +You can create your own storage manager by implementing the `OauthCredentialManager` interface, it is suggested that you extend the `BaseCredentialManager` class. + +You will need to implement the following methods: + +1. `exists()` - Returns a boolean indicating if the credentials exist. +2. `store(AccessTokenInterface $token, array $tenants = null)` - Creates/Updates the credentials. +3. `data(string $key = null)` - Returns the stored credentials (it is recommended you check the credentials exists, if not throw the `XeroCredentialsNotFound` exception). + +Once this has been completed, you should point `xero.credential_store` to your new implementation. \ No newline at end of file diff --git a/src/Oauth2CredentialManagers/AuthenticatedUserStore.php b/src/Oauth2CredentialManagers/AuthenticatedUserStore.php index b6862ae..3307945 100644 --- a/src/Oauth2CredentialManagers/AuthenticatedUserStore.php +++ b/src/Oauth2CredentialManagers/AuthenticatedUserStore.php @@ -4,17 +4,20 @@ use Illuminate\Support\Facades\Auth; use Webfox\Xero\Exceptions\XeroUserNotAuthenticated; +use Webfox\Xero\Xero; class AuthenticatedUserStore extends ModelStore { public function __construct() { - if (! Auth::check()) { + $auth = Auth::guard(Xero::getDefaultAuthGuard()); + + if (! $auth->check()) { throw new XeroUserNotAuthenticated('User is not authenticated'); } parent::__construct(); - $this->model = Auth::user(); + $this->model = $auth->user(); } } diff --git a/src/Oauth2CredentialManagers/BaseCredentialManager.php b/src/Oauth2CredentialManagers/BaseCredentialManager.php index 8cd39e6..e72d240 100644 --- a/src/Oauth2CredentialManagers/BaseCredentialManager.php +++ b/src/Oauth2CredentialManagers/BaseCredentialManager.php @@ -3,6 +3,7 @@ namespace Webfox\Xero\Oauth2CredentialManagers; use Illuminate\Session\Store; +use League\OAuth2\Client\Token\AccessTokenInterface; use Webfox\Xero\Exceptions\XeroTenantNotFound; use Webfox\Xero\Oauth2Provider; use XeroAPI\XeroPHP\JWTClaims; @@ -18,6 +19,11 @@ public function __construct() $this->oauthProvider = app(Oauth2Provider::class); } + + abstract public function exists(): bool; + + abstract protected function store(AccessTokenInterface $token, array $tenants = null): void; + abstract protected function data(string $key = null); public function getAccessToken(): string diff --git a/src/Oauth2CredentialManagers/ModelStore.php b/src/Oauth2CredentialManagers/ModelStore.php index a8dcc74..af6b497 100644 --- a/src/Oauth2CredentialManagers/ModelStore.php +++ b/src/Oauth2CredentialManagers/ModelStore.php @@ -14,7 +14,9 @@ class ModelStore extends BaseCredentialManager implements OauthCredentialManager public function __construct() { - $this->model = Xero::getModelStorage(); + if($model = Xero::getModelStorage()) { + $this->model = $model; + } parent::__construct(); } diff --git a/src/Xero.php b/src/Xero.php index be5c854..202801f 100644 --- a/src/Xero.php +++ b/src/Xero.php @@ -6,10 +6,12 @@ class Xero { - public static Model $modelStorage; + public static ?Model $modelStorage = null; public static string $modelAttribute = 'xero_credentials'; + public static string $defaultAuthGuard = 'web'; + public static function useModelStorage(Model $model): void { static::$modelStorage = $model; @@ -20,7 +22,7 @@ public static function useAttributeOnModelStore(string $attribute): void static::$modelAttribute = $attribute; } - public static function getModelStorage(): Model + public static function getModelStorage(): ?Model { return static::$modelStorage; } @@ -29,4 +31,14 @@ public static function getModelAttribute(): string { return static::$modelAttribute; } + + public static function getDefaultAuthGuard(): string + { + return static::$defaultAuthGuard; + } + + public static function setDefaultAuthGuard(string $guard): void + { + static::$defaultAuthGuard = $guard; + } } diff --git a/tests/Unit/CredentialManagersTest.php b/tests/Unit/CredentialManagersTest.php index eeeb147..7f50259 100644 --- a/tests/Unit/CredentialManagersTest.php +++ b/tests/Unit/CredentialManagersTest.php @@ -4,6 +4,7 @@ use Illuminate\Cache\Repository; use Illuminate\Foundation\Auth\User as Authenticatable; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Session; use Illuminate\Support\Facades\Storage; use Mockery\MockInterface; @@ -11,6 +12,7 @@ use Tests\Webfox\Xero\TestCase; use Tests\Webfox\Xero\TestSupport\Mocks\MockAccessToken; use Webfox\Xero\Exceptions\XeroCredentialsNotFound; +use Webfox\Xero\Exceptions\XeroUserNotAuthenticated; use Webfox\Xero\Oauth2CredentialManagers\ArrayStore; use Webfox\Xero\Oauth2CredentialManagers\AuthenticatedUserStore; use Webfox\Xero\Oauth2CredentialManagers\CacheStore; @@ -29,14 +31,14 @@ public function test_you_can_get_credential_store_without_existing_data($sutClas $sut = new $sutClass(); - $this->assertThrows(fn () => $sut->getAccessToken(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); - $this->assertThrows(fn () => $sut->getRefreshToken(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); - $this->assertThrows(fn () => $sut->getTenants(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); - $this->assertThrows(fn () => $sut->getTenantId(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); - $this->assertThrows(fn () => $sut->getExpires(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); - $this->assertThrows(fn () => $sut->getData(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); + $this->assertThrows(fn() => $sut->getAccessToken(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); + $this->assertThrows(fn() => $sut->getRefreshToken(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); + $this->assertThrows(fn() => $sut->getTenants(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); + $this->assertThrows(fn() => $sut->getTenantId(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); + $this->assertThrows(fn() => $sut->getExpires(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); + $this->assertThrows(fn() => $sut->getData(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); $this->assertFalse($sut->exists()); - $this->assertThrows(fn () => $sut->isExpired(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); + $this->assertThrows(fn() => $sut->isExpired(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); $this->assertNull($sut->getUser()); } @@ -178,30 +180,53 @@ public function test_you_can_get_user($sutClass, $setupFunction, $createExisting ], $sut->getUser()); } + public function test_that_if_guest_it_will_throw_exception_for_authenticated_user_store() + { + $this->assertThrows(fn() => new AuthenticatedUserStore(), XeroUserNotAuthenticated::class, 'User is not authenticated'); + } + + public function test_that_you_can_change_the_default_guard_for_users() + { + Config::set('auth.guards', array_merge(config('auth.guards'), [ + 'admin' => [ + 'driver' => 'session', + 'provider' => 'users', + ] + ])); + + auth()->guard('admin')->login(User::create(['xero_credentials' => ['token' => 'foo']])); + + $this->assertThrows(fn() => new AuthenticatedUserStore(), XeroUserNotAuthenticated::class, 'User is not authenticated'); + + Xero::setDefaultAuthGuard('admin'); + + $this->assertTrue((new AuthenticatedUserStore())->exists()); + } + public static function credentialManagers(): array { return [ 'fileStore' => [ 'sutClass' => FileStore::class, - 'setupFunction' => fn () => Storage::fake(), - 'createExistingData' => fn (OauthCredentialManager $credentialManager, $data) => Storage::put('xero.json', json_encode($data)), + 'setupFunction' => fn() => Storage::fake(), + 'createExistingData' => fn(OauthCredentialManager $credentialManager, $data) => Storage::put('xero.json', json_encode($data)), ], 'cacheStore' => [ 'sutClass' => CacheStore::class, - 'setupFunction' => fn () => null, - 'createExistingData' => fn (OauthCredentialManager $credentialManager, $data) => app(Repository::class)->put('xero_oauth', $data), + 'setupFunction' => fn() => null, + 'createExistingData' => fn(OauthCredentialManager $credentialManager, $data) => app(Repository::class)->put('xero_oauth', $data), ], 'arrayStore' => [ 'sutClass' => ArrayStore::class, - 'setupFunction' => fn () => null, - 'createExistingData' => fn (OauthCredentialManager $credentialManager, $data) => $credentialManager->dataStorage = $data, + 'setupFunction' => fn() => null, + 'createExistingData' => fn(OauthCredentialManager $credentialManager, $data) => $credentialManager->dataStorage = $data, ], 'modelStore' => [ 'sutClass' => ModelStore::class, - 'setupFunction' => fn () => Xero::useModelStorage(User::create()), + 'setupFunction' => fn() => Xero::useModelStorage(User::create()), 'createExistingData' => function (OauthCredentialManager $credentialManager, $data) { Xero::getModelStorage()->update(['xero_credentials' => $data]); }, @@ -209,7 +234,7 @@ public static function credentialManagers(): array 'authenticatedUserStore' => [ 'sutClass' => AuthenticatedUserStore::class, - 'setupFunction' => fn () => auth()->login(User::create()), + 'setupFunction' => fn() => auth()->login(User::create()), 'createExistingData' => function (OauthCredentialManager $credentialManager, $data) { $credentialManager->model->update(['xero_credentials' => $data]); }, From 04648794a7f72ac5488fa34735cb945a41693ff6 Mon Sep 17 00:00:00 2001 From: James Freeman Date: Sat, 12 Oct 2024 21:38:08 +0100 Subject: [PATCH 2/5] fix model for laravel 10 --- tests/Unit/CredentialManagersTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/Unit/CredentialManagersTest.php b/tests/Unit/CredentialManagersTest.php index 7f50259..96aef41 100644 --- a/tests/Unit/CredentialManagersTest.php +++ b/tests/Unit/CredentialManagersTest.php @@ -245,6 +245,10 @@ public static function credentialManagers(): array class User extends Authenticatable { + protected $casts = [ + 'xero_credentials' => 'array', + ]; + protected function casts() { return [ From ab7bc6be05282a885d241490d51841b5b0253f29 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 12 Oct 2024 20:43:12 +0000 Subject: [PATCH 3/5] Apply fixes from StyleCI --- .../BaseCredentialManager.php | 1 - src/Oauth2CredentialManagers/ModelStore.php | 2 +- tests/Unit/CredentialManagersTest.php | 36 +++++++++---------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/Oauth2CredentialManagers/BaseCredentialManager.php b/src/Oauth2CredentialManagers/BaseCredentialManager.php index e72d240..2bf7f1d 100644 --- a/src/Oauth2CredentialManagers/BaseCredentialManager.php +++ b/src/Oauth2CredentialManagers/BaseCredentialManager.php @@ -19,7 +19,6 @@ public function __construct() $this->oauthProvider = app(Oauth2Provider::class); } - abstract public function exists(): bool; abstract protected function store(AccessTokenInterface $token, array $tenants = null): void; diff --git a/src/Oauth2CredentialManagers/ModelStore.php b/src/Oauth2CredentialManagers/ModelStore.php index af6b497..575a268 100644 --- a/src/Oauth2CredentialManagers/ModelStore.php +++ b/src/Oauth2CredentialManagers/ModelStore.php @@ -14,7 +14,7 @@ class ModelStore extends BaseCredentialManager implements OauthCredentialManager public function __construct() { - if($model = Xero::getModelStorage()) { + if ($model = Xero::getModelStorage()) { $this->model = $model; } diff --git a/tests/Unit/CredentialManagersTest.php b/tests/Unit/CredentialManagersTest.php index 96aef41..e00e9f3 100644 --- a/tests/Unit/CredentialManagersTest.php +++ b/tests/Unit/CredentialManagersTest.php @@ -31,14 +31,14 @@ public function test_you_can_get_credential_store_without_existing_data($sutClas $sut = new $sutClass(); - $this->assertThrows(fn() => $sut->getAccessToken(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); - $this->assertThrows(fn() => $sut->getRefreshToken(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); - $this->assertThrows(fn() => $sut->getTenants(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); - $this->assertThrows(fn() => $sut->getTenantId(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); - $this->assertThrows(fn() => $sut->getExpires(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); - $this->assertThrows(fn() => $sut->getData(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); + $this->assertThrows(fn () => $sut->getAccessToken(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); + $this->assertThrows(fn () => $sut->getRefreshToken(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); + $this->assertThrows(fn () => $sut->getTenants(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); + $this->assertThrows(fn () => $sut->getTenantId(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); + $this->assertThrows(fn () => $sut->getExpires(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); + $this->assertThrows(fn () => $sut->getData(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); $this->assertFalse($sut->exists()); - $this->assertThrows(fn() => $sut->isExpired(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); + $this->assertThrows(fn () => $sut->isExpired(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); $this->assertNull($sut->getUser()); } @@ -182,7 +182,7 @@ public function test_you_can_get_user($sutClass, $setupFunction, $createExisting public function test_that_if_guest_it_will_throw_exception_for_authenticated_user_store() { - $this->assertThrows(fn() => new AuthenticatedUserStore(), XeroUserNotAuthenticated::class, 'User is not authenticated'); + $this->assertThrows(fn () => new AuthenticatedUserStore(), XeroUserNotAuthenticated::class, 'User is not authenticated'); } public function test_that_you_can_change_the_default_guard_for_users() @@ -191,12 +191,12 @@ public function test_that_you_can_change_the_default_guard_for_users() 'admin' => [ 'driver' => 'session', 'provider' => 'users', - ] + ], ])); auth()->guard('admin')->login(User::create(['xero_credentials' => ['token' => 'foo']])); - $this->assertThrows(fn() => new AuthenticatedUserStore(), XeroUserNotAuthenticated::class, 'User is not authenticated'); + $this->assertThrows(fn () => new AuthenticatedUserStore(), XeroUserNotAuthenticated::class, 'User is not authenticated'); Xero::setDefaultAuthGuard('admin'); @@ -208,25 +208,25 @@ public static function credentialManagers(): array return [ 'fileStore' => [ 'sutClass' => FileStore::class, - 'setupFunction' => fn() => Storage::fake(), - 'createExistingData' => fn(OauthCredentialManager $credentialManager, $data) => Storage::put('xero.json', json_encode($data)), + 'setupFunction' => fn () => Storage::fake(), + 'createExistingData' => fn (OauthCredentialManager $credentialManager, $data) => Storage::put('xero.json', json_encode($data)), ], 'cacheStore' => [ 'sutClass' => CacheStore::class, - 'setupFunction' => fn() => null, - 'createExistingData' => fn(OauthCredentialManager $credentialManager, $data) => app(Repository::class)->put('xero_oauth', $data), + 'setupFunction' => fn () => null, + 'createExistingData' => fn (OauthCredentialManager $credentialManager, $data) => app(Repository::class)->put('xero_oauth', $data), ], 'arrayStore' => [ 'sutClass' => ArrayStore::class, - 'setupFunction' => fn() => null, - 'createExistingData' => fn(OauthCredentialManager $credentialManager, $data) => $credentialManager->dataStorage = $data, + 'setupFunction' => fn () => null, + 'createExistingData' => fn (OauthCredentialManager $credentialManager, $data) => $credentialManager->dataStorage = $data, ], 'modelStore' => [ 'sutClass' => ModelStore::class, - 'setupFunction' => fn() => Xero::useModelStorage(User::create()), + 'setupFunction' => fn () => Xero::useModelStorage(User::create()), 'createExistingData' => function (OauthCredentialManager $credentialManager, $data) { Xero::getModelStorage()->update(['xero_credentials' => $data]); }, @@ -234,7 +234,7 @@ public static function credentialManagers(): array 'authenticatedUserStore' => [ 'sutClass' => AuthenticatedUserStore::class, - 'setupFunction' => fn() => auth()->login(User::create()), + 'setupFunction' => fn () => auth()->login(User::create()), 'createExistingData' => function (OauthCredentialManager $credentialManager, $data) { $credentialManager->model->update(['xero_credentials' => $data]); }, From 28ac3be0ec15d090252b851b6a72163773a107de Mon Sep 17 00:00:00 2001 From: James Freeman Date: Mon, 4 Nov 2024 17:36:14 +0000 Subject: [PATCH 4/5] remove all code changes --- .../AuthenticatedUserStore.php | 6 ++-- .../BaseCredentialManager.php | 6 ---- src/Oauth2CredentialManagers/ModelStore.php | 4 +-- src/Xero.php | 16 ++-------- tests/Unit/CredentialManagersTest.php | 29 ------------------- 5 files changed, 5 insertions(+), 56 deletions(-) diff --git a/src/Oauth2CredentialManagers/AuthenticatedUserStore.php b/src/Oauth2CredentialManagers/AuthenticatedUserStore.php index 3307945..838a5a2 100644 --- a/src/Oauth2CredentialManagers/AuthenticatedUserStore.php +++ b/src/Oauth2CredentialManagers/AuthenticatedUserStore.php @@ -10,14 +10,12 @@ class AuthenticatedUserStore extends ModelStore { public function __construct() { - $auth = Auth::guard(Xero::getDefaultAuthGuard()); - - if (! $auth->check()) { + if (! Auth::check()) { throw new XeroUserNotAuthenticated('User is not authenticated'); } parent::__construct(); - $this->model = $auth->user(); + $this->model = Auth::user(); } } diff --git a/src/Oauth2CredentialManagers/BaseCredentialManager.php b/src/Oauth2CredentialManagers/BaseCredentialManager.php index e72d240..8cd39e6 100644 --- a/src/Oauth2CredentialManagers/BaseCredentialManager.php +++ b/src/Oauth2CredentialManagers/BaseCredentialManager.php @@ -3,7 +3,6 @@ namespace Webfox\Xero\Oauth2CredentialManagers; use Illuminate\Session\Store; -use League\OAuth2\Client\Token\AccessTokenInterface; use Webfox\Xero\Exceptions\XeroTenantNotFound; use Webfox\Xero\Oauth2Provider; use XeroAPI\XeroPHP\JWTClaims; @@ -19,11 +18,6 @@ public function __construct() $this->oauthProvider = app(Oauth2Provider::class); } - - abstract public function exists(): bool; - - abstract protected function store(AccessTokenInterface $token, array $tenants = null): void; - abstract protected function data(string $key = null); public function getAccessToken(): string diff --git a/src/Oauth2CredentialManagers/ModelStore.php b/src/Oauth2CredentialManagers/ModelStore.php index af6b497..a8dcc74 100644 --- a/src/Oauth2CredentialManagers/ModelStore.php +++ b/src/Oauth2CredentialManagers/ModelStore.php @@ -14,9 +14,7 @@ class ModelStore extends BaseCredentialManager implements OauthCredentialManager public function __construct() { - if($model = Xero::getModelStorage()) { - $this->model = $model; - } + $this->model = Xero::getModelStorage(); parent::__construct(); } diff --git a/src/Xero.php b/src/Xero.php index 202801f..be5c854 100644 --- a/src/Xero.php +++ b/src/Xero.php @@ -6,12 +6,10 @@ class Xero { - public static ?Model $modelStorage = null; + public static Model $modelStorage; public static string $modelAttribute = 'xero_credentials'; - public static string $defaultAuthGuard = 'web'; - public static function useModelStorage(Model $model): void { static::$modelStorage = $model; @@ -22,7 +20,7 @@ public static function useAttributeOnModelStore(string $attribute): void static::$modelAttribute = $attribute; } - public static function getModelStorage(): ?Model + public static function getModelStorage(): Model { return static::$modelStorage; } @@ -31,14 +29,4 @@ public static function getModelAttribute(): string { return static::$modelAttribute; } - - public static function getDefaultAuthGuard(): string - { - return static::$defaultAuthGuard; - } - - public static function setDefaultAuthGuard(string $guard): void - { - static::$defaultAuthGuard = $guard; - } } diff --git a/tests/Unit/CredentialManagersTest.php b/tests/Unit/CredentialManagersTest.php index 96aef41..bec3b29 100644 --- a/tests/Unit/CredentialManagersTest.php +++ b/tests/Unit/CredentialManagersTest.php @@ -4,7 +4,6 @@ use Illuminate\Cache\Repository; use Illuminate\Foundation\Auth\User as Authenticatable; -use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Session; use Illuminate\Support\Facades\Storage; use Mockery\MockInterface; @@ -12,7 +11,6 @@ use Tests\Webfox\Xero\TestCase; use Tests\Webfox\Xero\TestSupport\Mocks\MockAccessToken; use Webfox\Xero\Exceptions\XeroCredentialsNotFound; -use Webfox\Xero\Exceptions\XeroUserNotAuthenticated; use Webfox\Xero\Oauth2CredentialManagers\ArrayStore; use Webfox\Xero\Oauth2CredentialManagers\AuthenticatedUserStore; use Webfox\Xero\Oauth2CredentialManagers\CacheStore; @@ -180,29 +178,6 @@ public function test_you_can_get_user($sutClass, $setupFunction, $createExisting ], $sut->getUser()); } - public function test_that_if_guest_it_will_throw_exception_for_authenticated_user_store() - { - $this->assertThrows(fn() => new AuthenticatedUserStore(), XeroUserNotAuthenticated::class, 'User is not authenticated'); - } - - public function test_that_you_can_change_the_default_guard_for_users() - { - Config::set('auth.guards', array_merge(config('auth.guards'), [ - 'admin' => [ - 'driver' => 'session', - 'provider' => 'users', - ] - ])); - - auth()->guard('admin')->login(User::create(['xero_credentials' => ['token' => 'foo']])); - - $this->assertThrows(fn() => new AuthenticatedUserStore(), XeroUserNotAuthenticated::class, 'User is not authenticated'); - - Xero::setDefaultAuthGuard('admin'); - - $this->assertTrue((new AuthenticatedUserStore())->exists()); - } - public static function credentialManagers(): array { return [ @@ -245,10 +220,6 @@ public static function credentialManagers(): array class User extends Authenticatable { - protected $casts = [ - 'xero_credentials' => 'array', - ]; - protected function casts() { return [ From 7282eced880ffffca6dcce42a5245c081a1f10d0 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Mon, 4 Nov 2024 17:38:30 +0000 Subject: [PATCH 5/5] Apply fixes from StyleCI --- .../AuthenticatedUserStore.php | 1 - tests/Unit/CredentialManagersTest.php | 30 +++++++++---------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/Oauth2CredentialManagers/AuthenticatedUserStore.php b/src/Oauth2CredentialManagers/AuthenticatedUserStore.php index 838a5a2..b6862ae 100644 --- a/src/Oauth2CredentialManagers/AuthenticatedUserStore.php +++ b/src/Oauth2CredentialManagers/AuthenticatedUserStore.php @@ -4,7 +4,6 @@ use Illuminate\Support\Facades\Auth; use Webfox\Xero\Exceptions\XeroUserNotAuthenticated; -use Webfox\Xero\Xero; class AuthenticatedUserStore extends ModelStore { diff --git a/tests/Unit/CredentialManagersTest.php b/tests/Unit/CredentialManagersTest.php index bec3b29..eeeb147 100644 --- a/tests/Unit/CredentialManagersTest.php +++ b/tests/Unit/CredentialManagersTest.php @@ -29,14 +29,14 @@ public function test_you_can_get_credential_store_without_existing_data($sutClas $sut = new $sutClass(); - $this->assertThrows(fn() => $sut->getAccessToken(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); - $this->assertThrows(fn() => $sut->getRefreshToken(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); - $this->assertThrows(fn() => $sut->getTenants(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); - $this->assertThrows(fn() => $sut->getTenantId(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); - $this->assertThrows(fn() => $sut->getExpires(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); - $this->assertThrows(fn() => $sut->getData(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); + $this->assertThrows(fn () => $sut->getAccessToken(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); + $this->assertThrows(fn () => $sut->getRefreshToken(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); + $this->assertThrows(fn () => $sut->getTenants(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); + $this->assertThrows(fn () => $sut->getTenantId(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); + $this->assertThrows(fn () => $sut->getExpires(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); + $this->assertThrows(fn () => $sut->getData(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); $this->assertFalse($sut->exists()); - $this->assertThrows(fn() => $sut->isExpired(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); + $this->assertThrows(fn () => $sut->isExpired(), XeroCredentialsNotFound::class, 'Xero oauth credentials are missing'); $this->assertNull($sut->getUser()); } @@ -183,25 +183,25 @@ public static function credentialManagers(): array return [ 'fileStore' => [ 'sutClass' => FileStore::class, - 'setupFunction' => fn() => Storage::fake(), - 'createExistingData' => fn(OauthCredentialManager $credentialManager, $data) => Storage::put('xero.json', json_encode($data)), + 'setupFunction' => fn () => Storage::fake(), + 'createExistingData' => fn (OauthCredentialManager $credentialManager, $data) => Storage::put('xero.json', json_encode($data)), ], 'cacheStore' => [ 'sutClass' => CacheStore::class, - 'setupFunction' => fn() => null, - 'createExistingData' => fn(OauthCredentialManager $credentialManager, $data) => app(Repository::class)->put('xero_oauth', $data), + 'setupFunction' => fn () => null, + 'createExistingData' => fn (OauthCredentialManager $credentialManager, $data) => app(Repository::class)->put('xero_oauth', $data), ], 'arrayStore' => [ 'sutClass' => ArrayStore::class, - 'setupFunction' => fn() => null, - 'createExistingData' => fn(OauthCredentialManager $credentialManager, $data) => $credentialManager->dataStorage = $data, + 'setupFunction' => fn () => null, + 'createExistingData' => fn (OauthCredentialManager $credentialManager, $data) => $credentialManager->dataStorage = $data, ], 'modelStore' => [ 'sutClass' => ModelStore::class, - 'setupFunction' => fn() => Xero::useModelStorage(User::create()), + 'setupFunction' => fn () => Xero::useModelStorage(User::create()), 'createExistingData' => function (OauthCredentialManager $credentialManager, $data) { Xero::getModelStorage()->update(['xero_credentials' => $data]); }, @@ -209,7 +209,7 @@ public static function credentialManagers(): array 'authenticatedUserStore' => [ 'sutClass' => AuthenticatedUserStore::class, - 'setupFunction' => fn() => auth()->login(User::create()), + 'setupFunction' => fn () => auth()->login(User::create()), 'createExistingData' => function (OauthCredentialManager $credentialManager, $data) { $credentialManager->model->update(['xero_credentials' => $data]); },