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

Commit 7b272f2

Browse files
committed
Added docs and code for BeeAuth
1 parent b37298a commit 7b272f2

File tree

4 files changed

+141
-32
lines changed

4 files changed

+141
-32
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,24 @@ file_put_contents('/path/to/storage/image.jpg', $image);
7373
Storage::put('/path/to/storage/image.jpg', $image);
7474
```
7575

76+
## Generating access token
77+
78+
To initialize Bee Editor you may need an access token. You can use `BeeAuth` to easily generate that. You can grab the Client ID and Secret by logging in into the [Developer portal](https://developers.beefree.io) and going to the "details" of your application.
79+
80+
```php
81+
use KirschbaumDevelopment\Bee\BeeAuth;
82+
83+
$beeAuth = new BeeAuth(new \GuzzleHttp\Client);
84+
$beeAuth->setClientId('your-client-id-here');
85+
$beeAuth->setClientSecret('your-client-secret-here');
86+
$token = $beeAuth->generateToken();
87+
88+
// then you can use the following methods:
89+
$token->getAccessToken();
90+
$token->getRefreshToken();
91+
$token->getExpires();
92+
```
93+
7694
### Testing
7795

7896
``` bash

src/BeeAuth.php

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace KirschbaumDevelopment\Bee;
44

55
use GuzzleHttp\Client as GuzzleClient;
6+
use KirschbaumDevelopment\Bee\Resources\AuthorizationToken;
67

78
class BeeAuth
89
{
@@ -53,25 +54,12 @@ public function setClientSecret($clientSecret)
5354
return $this;
5455
}
5556

56-
/**
57-
* Return the default headers.
58-
*
59-
* @param array $headers
60-
* @return array
61-
*/
62-
protected function formatHeaders($headers = [])
63-
{
64-
return array_merge([
65-
'Authorization' => sprintf('Bearer %s', $this->getAuthorizationHeader()),
66-
], $headers);
67-
}
68-
6957
/**
7058
* Return the 'Authorization' header value.
7159
*
7260
* @return string
7361
*/
74-
protected function getAuthorizationHeader()
62+
public function generateToken()
7563
{
7664
$response = $this->httpClient->post(static::API_AUTH_URL, [
7765
'form_params' => [
@@ -81,23 +69,6 @@ protected function getAuthorizationHeader()
8169
]
8270
]);
8371

84-
$contents = json_decode($response->getBody()->getContents(), true);
85-
86-
return $contents['access_token'];
87-
}
88-
89-
/**
90-
* Make sure JSON is encoded.
91-
*
92-
* @param mixed $json
93-
* @return string
94-
*/
95-
protected function encodeJson($json)
96-
{
97-
if (is_array($json) || is_object($json)) {
98-
return json_encode($json);
99-
}
100-
101-
return $json;
72+
return new AuthorizationToken(json_decode($response->getBody()->getContents(), true));
10273
}
10374
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace KirschbaumDevelopment\Bee\Resources;
4+
5+
class AuthorizationToken
6+
{
7+
/**
8+
* @var string
9+
*/
10+
protected $accessToken;
11+
12+
/**
13+
* @var string
14+
*/
15+
protected $tokenType;
16+
17+
/**
18+
* @var string
19+
*/
20+
protected $expiresIn;
21+
22+
/**
23+
* @var string
24+
*/
25+
protected $pageOrientation;
26+
27+
/**
28+
* @var string
29+
*/
30+
protected $refreshToken;
31+
32+
public function __construct($tokenData)
33+
{
34+
$this->accessToken = $tokenData['access_token'] ?? null;
35+
$this->tokenType = $tokenData['token_type'] ?? null;
36+
$this->expiresIn = $tokenData['expires_in'] ?? null;
37+
$this->refreshToken = $tokenData['refresh_token'] ?? null;
38+
}
39+
40+
/**
41+
* @return string
42+
*/
43+
public function getAccessToken()
44+
{
45+
return $this->accessToken;
46+
}
47+
48+
/**
49+
* @return string
50+
*/
51+
public function getTokenType()
52+
{
53+
return $this->tokenType;
54+
}
55+
56+
/**
57+
* @return string
58+
*/
59+
public function getExpiresIn()
60+
{
61+
return $this->expiresIn;
62+
}
63+
64+
/**
65+
* @return string
66+
*/
67+
public function getRefreshToken()
68+
{
69+
return $this->refreshToken;
70+
}
71+
}

tests/BeeAuthTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace KirschbaumDevelopment\Bee\Tests;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\HandlerStack;
7+
use GuzzleHttp\Psr7\Response;
8+
use PHPUnit\Framework\TestCase;
9+
use GuzzleHttp\Handler\MockHandler;
10+
use KirschbaumDevelopment\Bee\BeeAuth;
11+
use KirschbaumDevelopment\Bee\Resources\AuthorizationToken;
12+
13+
/**
14+
* @coversDefaultClass \KirschbaumDevelopment\Bee\BeeAuth
15+
*/
16+
class BeeAuthTest extends TestCase
17+
{
18+
/**
19+
* @covers ::generateToken
20+
*/
21+
public function testGenerateToken()
22+
{
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+
35+
$mock = new MockHandler([new Response(200, [], json_encode($response))]);
36+
$guzzleClient = new Client(['handler' => HandlerStack::create($mock)]);
37+
38+
$beeClient = new BeeAuth($guzzleClient);
39+
$beeClient->setClientId('fake-client-id');
40+
$beeClient->setClientSecret('fake-client-secret');
41+
$token = $beeClient->generateToken();
42+
43+
$this->assertInstanceOf(AuthorizationToken::class, $token);
44+
$this->assertEquals($response['access_token'], $token->getAccessToken());
45+
$this->assertEquals($response['token_type'], $token->getTokenType());
46+
$this->assertEquals($response['expires_in'], $token->getExpiresIn());
47+
$this->assertEquals($response['refresh_token'], $token->getRefreshToken());
48+
}
49+
}

0 commit comments

Comments
 (0)