From 16a4381dc4f728217410453e9d693082997747de Mon Sep 17 00:00:00 2001 From: "agallop@celestra.co.uk" Date: Fri, 24 Jun 2022 17:44:35 +0100 Subject: [PATCH 1/2] Multi-Tenant-Storage --- src/Oauth2CredentialManagers/CacheStore.php | 17 +++++++++++++---- src/Oauth2CredentialManagers/FileStore.php | 17 +++++++++++++---- src/OauthCredentialManager.php | 17 +++++++++++------ 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/Oauth2CredentialManagers/CacheStore.php b/src/Oauth2CredentialManagers/CacheStore.php index bb940a2..899fd14 100755 --- a/src/Oauth2CredentialManagers/CacheStore.php +++ b/src/Oauth2CredentialManagers/CacheStore.php @@ -40,9 +40,18 @@ public function getRefreshToken(): string return $this->data('refresh_token'); } - public function getTenantId(): string + public function getTenants(): ?array { - return $this->data('tenant_id'); + return $this->data('tenants'); + } + + public function getTenantId(int $tenant =0): string + { + if(!isset($this->data('tenants')[$tenant])) + { + throw new \Exception("No such tenant exists"); + } + return $this->data('tenants')[$tenant]['Id']; } public function getExpires(): int @@ -88,14 +97,14 @@ public function refresh(): void $this->store($newAccessToken); } - public function store(AccessTokenInterface $token, string $tenantId = null): void + public function store(AccessTokenInterface $token, array $tenants = null): void { $this->cache->forever($this->cacheKey, [ 'token' => $token->getToken(), 'refresh_token' => $token->getRefreshToken(), 'id_token' => $token->getValues()['id_token'], 'expires' => $token->getExpires(), - 'tenant_id' => $tenantId ?? $this->getTenantId() + 'tenants' => $tenants ?? $this->getTenants() ]); } diff --git a/src/Oauth2CredentialManagers/FileStore.php b/src/Oauth2CredentialManagers/FileStore.php index 50e2cc4..ece72c6 100755 --- a/src/Oauth2CredentialManagers/FileStore.php +++ b/src/Oauth2CredentialManagers/FileStore.php @@ -42,9 +42,18 @@ public function getRefreshToken(): string return $this->data('refresh_token'); } - public function getTenantId(): string + public function getTenants(): ?array { - return $this->data('tenant_id'); + return $this->data('tenants'); + } + + public function getTenantId(int $tenant =0): string + { + if(!isset($this->data('tenants')[$tenant])) + { + throw new \Exception("No such tenant exists"); + } + return $this->data('tenants')[$tenant]['Id']; } public function getExpires(): int @@ -90,14 +99,14 @@ public function refresh(): void $this->store($newAccessToken); } - public function store(AccessTokenInterface $token, string $tenantId = null): void + public function store(AccessTokenInterface $token, array $tenants = null): void { $ret = $this->disk->put($this->filePath, json_encode([ 'token' => $token->getToken(), 'refresh_token' => $token->getRefreshToken(), 'id_token' => $token->getValues()['id_token'], 'expires' => $token->getExpires(), - 'tenant_id' => $tenantId ?? $this->getTenantId() + 'tenants' => $tenants ?? $this->getTenants() ]), 'private'); if ($ret === false) { diff --git a/src/OauthCredentialManager.php b/src/OauthCredentialManager.php index 4225caa..8338b44 100644 --- a/src/OauthCredentialManager.php +++ b/src/OauthCredentialManager.php @@ -21,10 +21,15 @@ public function getRefreshToken(): string; */ public function getAuthorizationUrl(): string; + /** + * Get all tenants available + **/ + public function getTenants(): ?array; + /** * Get the current tenant ID */ - public function getTenantId(): string; + public function getTenantId(int $tenant =0): string; /** * Get the time the current access token expires (unix timestamp) @@ -59,13 +64,13 @@ public function refresh(): void; * 'refresh_token' => $token->getRefreshToken(), * 'id_token' => $token->getValues()['id_token'], * 'expires' => $token->getExpires(), - * 'tenant_id' => $tenantId ?? $this->getTenantId(), + * 'tenants' => $tenants ?? $this->getTenants(), * ] * * @param AccessTokenInterface $token - * @param string|null $tenantId + * @param Array|null $tenants */ - public function store(AccessTokenInterface $token, string $tenantId = null): void; + public function store(AccessTokenInterface $token, array $tenants = null): void; /** * Get the current authenticated users details according to the id token @@ -87,9 +92,9 @@ public function getUser(): ?array; * 'refresh_token' => 'string', * 'id_token' => 'string', * 'expires' => 000000, - * 'tenant_id' => 'string', + * 'tenants' => 'array', * ] */ public function getData(): array; -} \ No newline at end of file +} From 05a16458c35d145e1ba9f4122d7c44b47c9cf0a4 Mon Sep 17 00:00:00 2001 From: Matthew Hailwood Date: Mon, 27 Jun 2022 11:20:06 +1200 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19183fb..4b53bd1 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ All notable changes to `laravel-xero-oauth2` will be documented in this file +## v4.0.0 Release +- Support multiple tenants on one connection #71 + +> **Note** +> Unless you are using a custom OauthCredentialStore then this should be an in-place update, however you may be required to +> wipe and re-enable your credential storage. + + ## v3.0.0 Release - Change FileStore to use FilesystemManager with disk configuration #67 - Drop Laravel 5 support