Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit d08bd9f

Browse files
authored
Merge pull request #3 from kirschbaum-development/add-cache
Added the ability to cache bee auth token
2 parents b8e8b86 + 4a68bd7 commit d08bd9f

File tree

7 files changed

+135
-14
lines changed

7 files changed

+135
-14
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ $token->getRefreshToken();
9393
$token->getExpires();
9494
```
9595

96+
Auth tokens are valid for 5 minutes. If you want to cache your tokens for as long as possible, you will need to pass a [PSR-16](https://www.php-fig.org/psr/psr-16/) compatible cache implementation. Laravel, Symfony and all major frameworks are already compatible with this interface. To enable the cache, you only need to call the `setCache` method passing a `Psr\SimpleCache\CacheInterface` implementation.
97+
98+
```php
99+
$beeAuth->setCache($cacheImplementation);
100+
```
101+
96102
## Usage with Laravel
97103

98104
If you use Laravel, `BeePluginServiceProvider` will be auto-registered if you use Laravel package discovery. Otherwise, you can manually register the following provider:
@@ -114,6 +120,8 @@ Client Secret: `config('services.bee.client_secret')`
114120

115121
If you don't want to use this config, you can always pass the values manually.
116122

123+
Laravel service provider will also automatically inject Laravel's cache implementation on `BeeAuth`, so your auth tokens will be cached for as long as possible.
124+
117125
### Testing
118126

119127
``` bash

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"guzzlehttp/guzzle": "^6.3"
2121
},
2222
"require-dev": {
23+
"cache/array-adapter": "^1.0",
2324
"phpunit/phpunit": "^7.0"
2425
},
2526
"autoload": {

src/BeeAuth.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace KirschbaumDevelopment\Bee;
44

5+
use Psr\SimpleCache\CacheInterface;
56
use GuzzleHttp\Client as GuzzleClient;
67
use KirschbaumDevelopment\Bee\Resources\AuthorizationToken;
78

@@ -24,6 +25,13 @@ class BeeAuth
2425
*/
2526
protected $clientSecret;
2627

28+
/**
29+
* Cache PSR compatible.
30+
*
31+
* @var \Psr\SimpleCache\CacheInterface
32+
*/
33+
protected $cache;
34+
2735
/**
2836
* @param \GuzzleHttp\Client $httpClient
2937
*/
@@ -56,13 +64,29 @@ public function setClientSecret($clientSecret)
5664
return $this;
5765
}
5866

67+
/**
68+
* Set the cache.
69+
*
70+
* @param Psr\SimpleCache\CacheInterface $cache
71+
*/
72+
public function setCache(CacheInterface $cache)
73+
{
74+
$this->cache = $cache;
75+
76+
return $this;
77+
}
78+
5979
/**
6080
* Return the 'Authorization' header value.
6181
*
6282
* @return string
6383
*/
6484
public function generateToken()
6585
{
86+
if ($this->cache && $this->cache->has($this->getCacheKey())) {
87+
return new AuthorizationToken($this->cache->get($this->getCacheKey()));
88+
}
89+
6690
$response = $this->httpClient->post(static::API_AUTH_URL, [
6791
'form_params' => [
6892
'grant_type' => 'password',
@@ -71,6 +95,17 @@ public function generateToken()
7195
],
7296
]);
7397

74-
return new AuthorizationToken(json_decode($response->getBody()->getContents(), true));
98+
$token = new AuthorizationToken(json_decode($response->getBody()->getContents(), true));
99+
100+
if ($this->cache) {
101+
$this->cache->set($this->getCacheKey(), $token->toArray(), ($token->getExpiresIn() - 10));
102+
}
103+
104+
return $token;
105+
}
106+
107+
protected function getCacheKey()
108+
{
109+
return 'bee-plugin.bee-auth.token';
75110
}
76111
}

src/Laravel/BeePluginServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function boot()
2323

2424
$this->app->bind(BeeAuth::class, function () {
2525
$client = new BeeAuth(resolve(Client::class));
26+
$client->setCache($this->app('cache.store'));
2627

2728
if (config('services.bee.client_id')) {
2829
$client->setClientId(config('services.bee.client_id'));

src/Resources/AuthorizationToken.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,17 @@ public function getRefreshToken()
6868
{
6969
return $this->refreshToken;
7070
}
71+
72+
/**
73+
* @return array
74+
*/
75+
public function toArray()
76+
{
77+
return [
78+
'access_token' => $this->accessToken,
79+
'token_type' => $this->tokenType,
80+
'expires_in' => $this->expiresIn,
81+
'refresh_token' => $this->refreshToken,
82+
];
83+
}
7184
}

tests/BeeAuthTest.php

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPUnit\Framework\TestCase;
99
use GuzzleHttp\Handler\MockHandler;
1010
use KirschbaumDevelopment\Bee\BeeAuth;
11+
use Cache\Adapter\PHPArray\ArrayCachePool;
1112
use KirschbaumDevelopment\Bee\Resources\AuthorizationToken;
1213

1314
/**
@@ -17,21 +18,10 @@ class BeeAuthTest extends TestCase
1718
{
1819
/**
1920
* @covers ::generateToken
21+
* @dataProvider data
2022
*/
21-
public function testGenerateToken()
23+
public function testGenerateToken($response)
2224
{
23-
$response = [
24-
'access_token' => 'isufgisufdhguisdfhuisdf.oasihuifghsdif79s87f9sd8f7sd98f7sd98f7dsu89f.owTLmGhqv6ZJSikUhGeLBv-JANoXEAVvMZomivT2o-g',
25-
'token_type' => 'bearer',
26-
'expires_in' => 300,
27-
'refresh_token' => '09a7sd98as7f98ds6f79sd8f.09a8s7da98sd7as98d7as9d8a7s9d8as6d9as86das98d67as.owTLmGhqv6ZJSikUhGeLBv-JANoXEAVvMZomivT2o-g',
28-
'as:client_id' => 'da91fe57-1ca6-4dba-b557-61edb96f4149',
29-
'userName' => 'Tn3KV7JYavWn',
30-
'as:region' => 'eu-west-1',
31-
'.issued' => 'Sat, 17 Aug 2019 00:08:50 GMT',
32-
'.expires' => 'Sat, 17 Aug 2019 00:13:50 GMT',
33-
];
34-
3525
$mock = new MockHandler([new Response(200, [], json_encode($response))]);
3626
$guzzleClient = new Client(['handler' => HandlerStack::create($mock)]);
3727

@@ -46,4 +36,49 @@ public function testGenerateToken()
4636
$this->assertEquals($response['expires_in'], $token->getExpiresIn());
4737
$this->assertEquals($response['refresh_token'], $token->getRefreshToken());
4838
}
39+
40+
/**
41+
* @covers ::generateToken
42+
* @dataProvider data
43+
*/
44+
public function testGenerateTokenUsingCache($response)
45+
{
46+
$cachePool = new ArrayCachePool;
47+
$mock = new MockHandler([new Response(200, [], json_encode($response))]);
48+
$guzzleClient = new Client(['handler' => HandlerStack::create($mock)]);
49+
50+
$beeClient = new BeeAuth($guzzleClient);
51+
$beeClient->setCache($cachePool);
52+
$beeClient->setClientId('fake-client-id');
53+
$beeClient->setClientSecret('fake-client-secret');
54+
55+
$token1 = $beeClient->generateToken();
56+
$token2 = $beeClient->generateToken();
57+
58+
$this->assertEquals($token1, $token2);
59+
$this->assertInstanceOf(AuthorizationToken::class, $token1);
60+
$this->assertEquals($response['access_token'], $token1->getAccessToken());
61+
$this->assertEquals($response['token_type'], $token1->getTokenType());
62+
$this->assertEquals($response['expires_in'], $token1->getExpiresIn());
63+
$this->assertEquals($response['refresh_token'], $token1->getRefreshToken());
64+
}
65+
66+
public function data()
67+
{
68+
return [
69+
[
70+
'response' => [
71+
'access_token' => 'isufgisufdhguisdfhuisdf.oasihuifghsdif79s87f9sd8f7sd98f7sd98f7dsu89f.owTLmGhqv6ZJSikUhGeLBv-JANoXEAVvMZomivT2o-g',
72+
'token_type' => 'bearer',
73+
'expires_in' => 300,
74+
'refresh_token' => '09a7sd98as7f98ds6f79sd8f.09a8s7da98sd7as98d7as9d8a7s9d8as6d9as86das98d67as.owTLmGhqv6ZJSikUhGeLBv-JANoXEAVvMZomivT2o-g',
75+
'as:client_id' => 'da91fe57-1ca6-4dba-b557-61edb96f4149',
76+
'userName' => 'Tn3KV7JYavWn',
77+
'as:region' => 'eu-west-1',
78+
'.issued' => 'Sat, 17 Aug 2019 00:08:50 GMT',
79+
'.expires' => 'Sat, 17 Aug 2019 00:13:50 GMT',
80+
],
81+
],
82+
];
83+
}
4984
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace KirschbaumDevelopment\Bee\Tests\Resources;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use KirschbaumDevelopment\Bee\Resources\AuthorizationToken;
7+
8+
/**
9+
* @coversDefaultClass \KirschbaumDevelopment\Bee\Resources\AuthorizationToken
10+
*/
11+
class AuthorizationTokenTest extends TestCase
12+
{
13+
/**
14+
* @covers ::toArray
15+
*/
16+
public function testToArray()
17+
{
18+
$response = [
19+
'access_token' => 'isufgisufdhguisdfhuisdf.oasihuifghsdif79s87f9sd8f7sd98f7sd98f7dsu89f.owTLmGhqv6ZJSikUhGeLBv-JANoXEAVvMZomivT2o-g',
20+
'token_type' => 'bearer',
21+
'expires_in' => 300,
22+
'refresh_token' => '09a7sd98as7f98ds6f79sd8f.09a8s7da98sd7as98d7as9d8a7s9d8as6d9as86das98d67as.owTLmGhqv6ZJSikUhGeLBv-JANoXEAVvMZomivT2o-g',
23+
];
24+
25+
$token = new AuthorizationToken($response);
26+
$this->assertEquals($response, $token->toArray());
27+
}
28+
}

0 commit comments

Comments
 (0)