Skip to content

Commit f114f14

Browse files
committed
Moved Authenication Exception to data()
1 parent 4462741 commit f114f14

File tree

5 files changed

+91
-14
lines changed

5 files changed

+91
-14
lines changed

src/Oauth2CredentialManagers/AuthenticatedUserStore.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,33 @@
33
namespace Webfox\Xero\Oauth2CredentialManagers;
44

55
use Illuminate\Support\Facades\Auth;
6+
use Webfox\Xero\Exceptions\XeroCredentialsNotFound;
67
use Webfox\Xero\Exceptions\XeroUserNotAuthenticated;
8+
use Webfox\Xero\Xero;
79

810
class AuthenticatedUserStore extends ModelStore
911
{
12+
public function __construct()
13+
{
14+
$auth = Auth::guard(Xero::getDefaultAuthGuard());
15+
16+
parent::__construct();
17+
18+
if($auth->check()) {
19+
$this->model = $auth->user();
20+
}
21+
}
22+
1023
/**
1124
* @throws XeroUserNotAuthenticated
25+
* @throws XeroCredentialsNotFound
1226
*/
13-
public function __construct()
27+
public function data(string $key = null)
1428
{
15-
if (! Auth::check()) {
29+
if (! Auth::guard(Xero::getDefaultAuthGuard())->check()) {
1630
throw XeroUserNotAuthenticated::make();
1731
}
1832

19-
parent::__construct();
20-
21-
$this->model = Auth::user();
33+
return parent::data($key);
2234
}
2335
}

src/Oauth2CredentialManagers/BaseCredentialManager.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Webfox\Xero\Oauth2CredentialManagers;
44

55
use Illuminate\Session\Store;
6+
use League\OAuth2\Client\Token\AccessTokenInterface;
67
use Webfox\Xero\Exceptions\XeroTenantNotFound;
78
use Webfox\Xero\Oauth2Provider;
89
use XeroAPI\XeroPHP\JWTClaims;
@@ -18,6 +19,10 @@ public function __construct()
1819
$this->oauthProvider = app(Oauth2Provider::class);
1920
}
2021

22+
abstract public function exists(): bool;
23+
24+
abstract protected function store(AccessTokenInterface $token, array $tenants = null): void;
25+
2126
abstract protected function data(string $key = null);
2227

2328
public function getAccessToken(): string

src/Oauth2CredentialManagers/ModelStore.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ class ModelStore extends BaseCredentialManager implements OauthCredentialManager
1414

1515
public function __construct()
1616
{
17-
$this->model = Xero::getModelStorage();
17+
if ($model = Xero::getModelStorage()) {
18+
$this->model = $model;
19+
}
1820

1921
parent::__construct();
2022
}
2123

2224
public function exists(): bool
2325
{
24-
return $this->model?->exists && is_array($this->model->{$this->getModelKey()});
26+
return isset($this->model) && $this->model->exists && is_array($this->model->{$this->getModelKey()});
2527
}
2628

2729
public function store(AccessTokenInterface $token, array $tenants = null): void

src/Xero.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
class Xero
88
{
9-
public static Model $modelStorage;
9+
public static ?Model $modelStorage = null;
1010

1111
public static string $modelAttribute = 'xero_credentials';
1212

13+
public static string $defaultAuthGuard = 'web';
14+
1315
public static function useModelStorage(Model $model): void
1416
{
1517
static::$modelStorage = $model;
@@ -20,7 +22,7 @@ public static function useAttributeOnModelStore(string $attribute): void
2022
static::$modelAttribute = $attribute;
2123
}
2224

23-
public static function getModelStorage(): Model
25+
public static function getModelStorage(): ?Model
2426
{
2527
return static::$modelStorage;
2628
}
@@ -29,4 +31,15 @@ public static function getModelAttribute(): string
2931
{
3032
return static::$modelAttribute;
3133
}
34+
35+
36+
public static function getDefaultAuthGuard(): string
37+
{
38+
return static::$defaultAuthGuard;
39+
}
40+
41+
public static function setDefaultAuthGuard(string $guard): void
42+
{
43+
static::$defaultAuthGuard = $guard;
44+
}
3245
}

tests/Unit/CredentialManagersTest.php

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Cache\Repository;
66
use Illuminate\Foundation\Auth\User as Authenticatable;
7+
use Illuminate\Support\Facades\Config;
78
use Illuminate\Support\Facades\Session;
89
use Illuminate\Support\Facades\Storage;
910
use Mockery\MockInterface;
@@ -25,6 +26,15 @@
2526

2627
class CredentialManagersTest extends TestCase
2728
{
29+
protected function setUp(): void
30+
{
31+
parent::setUp();
32+
33+
Xero::$modelStorage = null;
34+
Xero::$modelAttribute = 'xero_credentials';
35+
Xero::$defaultAuthGuard = 'web';
36+
}
37+
2838
#[DataProvider('credentialManagers')]
2939
public function test_you_can_get_credential_store_without_existing_data($sutClass, $setupFunction, $createExistingData)
3040
{
@@ -195,11 +205,6 @@ public function test_that_getting_a_non_existent_tenant_will_throw_an_exception(
195205
$this->assertThrows(fn () => $sut->getTenantId(1), XeroTenantNotFound::class, 'No such tenant exists');
196206
}
197207

198-
public function test_that_if_guest_it_will_throw_exception_for_authenticated_user_store()
199-
{
200-
$this->assertThrows(fn () => new AuthenticatedUserStore(), XeroUserNotAuthenticated::class, 'User is not authenticated');
201-
}
202-
203208
public function test_that_it_will_throw_exception_if_failed_to_write_file()
204209
{
205210
Storage::fake();
@@ -211,6 +216,42 @@ public function test_that_it_will_throw_exception_if_failed_to_write_file()
211216
$this->assertThrows(fn () => $sut->store(new MockAccessToken(), ['tenant' => 'tenant_id', 'expires' => 3600]), XeroFailedToWriteFile::class, 'Failed to write file: xero.json');
212217
}
213218

219+
public function test_that_it_will_throw_exception_if_not_authenticated()
220+
{
221+
$this->assertGuest();
222+
223+
$sut = new AuthenticatedUserStore();
224+
225+
$this->assertThrows(fn () => $sut->data(), XeroUserNotAuthenticated::class, 'User is not authenticated');
226+
$this->assertThrows(fn () => $sut->getAccessToken(), XeroUserNotAuthenticated::class, 'User is not authenticated');
227+
$this->assertThrows(fn () => $sut->getRefreshToken(), XeroUserNotAuthenticated::class, 'User is not authenticated');
228+
$this->assertThrows(fn () => $sut->getTenants(), XeroUserNotAuthenticated::class, 'User is not authenticated');
229+
$this->assertThrows(fn () => $sut->getTenantId(), XeroUserNotAuthenticated::class, 'User is not authenticated');
230+
$this->assertThrows(fn () => $sut->getExpires(), XeroUserNotAuthenticated::class, 'User is not authenticated');
231+
$this->assertThrows(fn () => $sut->getData(), XeroUserNotAuthenticated::class, 'User is not authenticated');
232+
$this->assertFalse($sut->exists());
233+
$this->assertThrows(fn () => $sut->isExpired(), XeroUserNotAuthenticated::class, 'User is not authenticated');
234+
$this->assertNull($sut->getUser());
235+
}
236+
237+
public function test_that_you_can_change_the_default_guard_for_users()
238+
{
239+
Config::set('auth.guards', array_merge(config('auth.guards'), [
240+
'admin' => [
241+
'driver' => 'session',
242+
'provider' => 'users',
243+
],
244+
]));
245+
246+
auth()->guard('admin')->login(User::create(['xero_credentials' => ['token' => 'foo']]));
247+
248+
$this->assertThrows(fn () => (new AuthenticatedUserStore())->data(), XeroUserNotAuthenticated::class, 'User is not authenticated');
249+
250+
Xero::setDefaultAuthGuard('admin');
251+
252+
$this->assertIsArray((new AuthenticatedUserStore())->data());
253+
}
254+
214255
public static function credentialManagers(): array
215256
{
216257
return [
@@ -253,6 +294,10 @@ public static function credentialManagers(): array
253294

254295
class User extends Authenticatable
255296
{
297+
protected $casts = [
298+
'xero_credentials' => 'array',
299+
];
300+
256301
protected function casts()
257302
{
258303
return [

0 commit comments

Comments
 (0)