Skip to content

Commit 16a4381

Browse files
Multi-Tenant-Storage
1 parent 694d5c2 commit 16a4381

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

src/Oauth2CredentialManagers/CacheStore.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,18 @@ public function getRefreshToken(): string
4040
return $this->data('refresh_token');
4141
}
4242

43-
public function getTenantId(): string
43+
public function getTenants(): ?array
4444
{
45-
return $this->data('tenant_id');
45+
return $this->data('tenants');
46+
}
47+
48+
public function getTenantId(int $tenant =0): string
49+
{
50+
if(!isset($this->data('tenants')[$tenant]))
51+
{
52+
throw new \Exception("No such tenant exists");
53+
}
54+
return $this->data('tenants')[$tenant]['Id'];
4655
}
4756

4857
public function getExpires(): int
@@ -88,14 +97,14 @@ public function refresh(): void
8897
$this->store($newAccessToken);
8998
}
9099

91-
public function store(AccessTokenInterface $token, string $tenantId = null): void
100+
public function store(AccessTokenInterface $token, array $tenants = null): void
92101
{
93102
$this->cache->forever($this->cacheKey, [
94103
'token' => $token->getToken(),
95104
'refresh_token' => $token->getRefreshToken(),
96105
'id_token' => $token->getValues()['id_token'],
97106
'expires' => $token->getExpires(),
98-
'tenant_id' => $tenantId ?? $this->getTenantId()
107+
'tenants' => $tenants ?? $this->getTenants()
99108
]);
100109
}
101110

src/Oauth2CredentialManagers/FileStore.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,18 @@ public function getRefreshToken(): string
4242
return $this->data('refresh_token');
4343
}
4444

45-
public function getTenantId(): string
45+
public function getTenants(): ?array
4646
{
47-
return $this->data('tenant_id');
47+
return $this->data('tenants');
48+
}
49+
50+
public function getTenantId(int $tenant =0): string
51+
{
52+
if(!isset($this->data('tenants')[$tenant]))
53+
{
54+
throw new \Exception("No such tenant exists");
55+
}
56+
return $this->data('tenants')[$tenant]['Id'];
4857
}
4958

5059
public function getExpires(): int
@@ -90,14 +99,14 @@ public function refresh(): void
9099
$this->store($newAccessToken);
91100
}
92101

93-
public function store(AccessTokenInterface $token, string $tenantId = null): void
102+
public function store(AccessTokenInterface $token, array $tenants = null): void
94103
{
95104
$ret = $this->disk->put($this->filePath, json_encode([
96105
'token' => $token->getToken(),
97106
'refresh_token' => $token->getRefreshToken(),
98107
'id_token' => $token->getValues()['id_token'],
99108
'expires' => $token->getExpires(),
100-
'tenant_id' => $tenantId ?? $this->getTenantId()
109+
'tenants' => $tenants ?? $this->getTenants()
101110
]), 'private');
102111

103112
if ($ret === false) {

src/OauthCredentialManager.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ public function getRefreshToken(): string;
2121
*/
2222
public function getAuthorizationUrl(): string;
2323

24+
/**
25+
* Get all tenants available
26+
**/
27+
public function getTenants(): ?array;
28+
2429
/**
2530
* Get the current tenant ID
2631
*/
27-
public function getTenantId(): string;
32+
public function getTenantId(int $tenant =0): string;
2833

2934
/**
3035
* Get the time the current access token expires (unix timestamp)
@@ -59,13 +64,13 @@ public function refresh(): void;
5964
* 'refresh_token' => $token->getRefreshToken(),
6065
* 'id_token' => $token->getValues()['id_token'],
6166
* 'expires' => $token->getExpires(),
62-
* 'tenant_id' => $tenantId ?? $this->getTenantId(),
67+
* 'tenants' => $tenants ?? $this->getTenants(),
6368
* ]
6469
*
6570
* @param AccessTokenInterface $token
66-
* @param string|null $tenantId
71+
* @param Array|null $tenants
6772
*/
68-
public function store(AccessTokenInterface $token, string $tenantId = null): void;
73+
public function store(AccessTokenInterface $token, array $tenants = null): void;
6974

7075
/**
7176
* Get the current authenticated users details according to the id token
@@ -87,9 +92,9 @@ public function getUser(): ?array;
8792
* 'refresh_token' => 'string',
8893
* 'id_token' => 'string',
8994
* 'expires' => 000000,
90-
* 'tenant_id' => 'string',
95+
* 'tenants' => 'array',
9196
* ]
9297
*/
9398
public function getData(): array;
9499

95-
}
100+
}

0 commit comments

Comments
 (0)