Skip to content

Commit 05ac34c

Browse files
authored
API-1763: add a function to build an authentication only by app token
1 parent b50d721 commit 05ac34c

File tree

42 files changed

+191
-65
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+191
-65
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ $client->getToken();
7272
$client->getRefreshToken();
7373
```
7474

75+
If you are developing an App, authenticate with your app token.
76+
```php
77+
$client = $clientBuilder->buildAuthenticatedByAppToken('app_token');
78+
```
79+
7580
### Get a product
7681

7782
```php

spec/Search/SearchBuilderSpec.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace spec\Akeneo\Pim\ApiClient\Builder;
3+
namespace spec\Akeneo\Pim\ApiClient\Search;
44

55
use Akeneo\Pim\ApiClient\Search\Operator;
66
use Akeneo\Pim\ApiClient\Search\SearchBuilder;

spec/Security/AuthenticationSpec.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,17 @@ function it_is_initializable_from_a_token()
3131
$this->getAccessToken()->shouldReturn('token');
3232
$this->getRefreshToken()->shouldReturn('refresh_token');
3333
}
34+
35+
function it_is_initializable_from_an_app_token()
36+
{
37+
$this->beConstructedThrough('fromAppToken', ['a_token']);
38+
$this->shouldHaveType('Akeneo\Pim\ApiClient\Security\Authentication');
39+
40+
$this->getClientId()->shouldReturn(null);
41+
$this->getSecret()->shouldReturn(null);
42+
$this->getUsername()->shouldReturn(null);
43+
$this->getPassword()->shouldReturn(null);
44+
$this->getAccessToken()->shouldReturn('a_token');
45+
$this->getRefreshToken()->shouldReturn(null);
46+
}
3447
}

src/AkeneoPimClientBuilder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ public function buildAuthenticatedByPassword(string $clientId, string $secret, s
151151
return $this->buildAuthenticatedClient($authentication);
152152
}
153153

154+
public function buildAuthenticatedByAppToken(string $token): AkeneoPimClientInterface
155+
{
156+
$authentication = Authentication::fromAppToken($token);
157+
158+
return $this->buildAuthenticatedClient($authentication);
159+
}
160+
154161
/**
155162
* Build the Akeneo PIM client authenticated by token.
156163
*

src/Security/Authentication.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,20 @@ public static function fromToken(string $clientId, string $secret, string $acces
5555
return $authentication;
5656
}
5757

58-
public function getClientId(): string
58+
public static function fromAppToken(string $accessToken): Authentication
59+
{
60+
$authentication = new static();
61+
$authentication->accessToken = $accessToken;
62+
63+
return $authentication;
64+
}
65+
66+
public function getClientId(): ?string
5967
{
6068
return $this->clientId;
6169
}
6270

63-
public function getSecret(): string
71+
public function getSecret(): ?string
6472
{
6573
return $this->secret;
6674
}

tests/Api/ApiTestCase.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ protected function tearDown(): void
4747
/**
4848
* @return AkeneoPimClientInterface
4949
*/
50-
protected function createClient()
50+
protected function createClientByPassword()
5151
{
5252
$clientBuilder = new AkeneoPimClientBuilder($this->server->getServerRoot());
5353

@@ -59,6 +59,25 @@ protected function createClient()
5959
);
6060
}
6161

62+
protected function createClientByToken()
63+
{
64+
$clientBuilder = new AkeneoPimClientBuilder($this->server->getServerRoot());
65+
66+
return $clientBuilder->buildAuthenticatedByToken(
67+
'client_id',
68+
'secret',
69+
'a_token',
70+
'a_refresh_token'
71+
);
72+
}
73+
74+
protected function createClientByAppToken()
75+
{
76+
$clientBuilder = new AkeneoPimClientBuilder($this->server->getServerRoot());
77+
78+
return $clientBuilder->buildAuthenticatedByAppToken('a_token');
79+
}
80+
6281
private function getAuthenticatedJson()
6382
{
6483
return <<<JSON

tests/Api/Asset/GetAssetIntegration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function test_get_asset()
2222
)
2323
);
2424

25-
$api = $this->createClient()->getAssetManagerApi();
25+
$api = $this->createClientByPassword()->getAssetManagerApi();
2626
$asset = $api->get('packshot', 'battleship');
2727

2828
Assert::assertSame($this->server->getLastRequest()->jsonSerialize()[RequestInfo::JSON_KEY_METHOD], 'GET');
@@ -41,7 +41,7 @@ public function test_get_unknown_asset()
4141
$this->expectException(\Akeneo\Pim\ApiClient\Exception\NotFoundHttpException::class);
4242
$this->expectExceptionMessage('Asset "peace-sheep" does not exist.');
4343

44-
$api = $this->createClient()->getAssetManagerApi();
44+
$api = $this->createClientByPassword()->getAssetManagerApi();
4545
$api->get('packshot', 'peace-sheep');
4646
}
4747

tests/Api/Asset/ListAllAssetsIntegration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function test_list_per_page()
2222
)
2323
);
2424

25-
$api = $this->createClient()->getAssetManagerApi();
25+
$api = $this->createClientByPassword()->getAssetManagerApi();
2626
$assetCursor = $api->all('packshot');
2727
$assets = iterator_to_array($assetCursor);
2828

tests/Api/Asset/UpsertAssetIntegration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function test_upsert_asset()
4242
]
4343
];
4444

45-
$api = $this->createClient()->getAssetManagerApi();
45+
$api = $this->createClientByPassword()->getAssetManagerApi();
4646
$response = $api->upsert('packshot', 'sku_54628_telescope', $asset);
4747

4848
Assert::assertSame($this->server->getLastRequest()->jsonSerialize()[RequestInfo::JSON_KEY_INPUT], json_encode($asset));

tests/Api/Asset/UpsertListAssetIntegration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function test_upsert_a_list_of_assets()
8787
],
8888
];
8989

90-
$api = $this->createClient()->getAssetManagerApi();
90+
$api = $this->createClientByPassword()->getAssetManagerApi();
9191
$responses = $api->upsertList('packshot', $assets);
9292

9393
Assert::assertSame($this->server->getLastRequest()->jsonSerialize()[RequestInfo::JSON_KEY_INPUT], json_encode($assets));

0 commit comments

Comments
 (0)