Skip to content

Commit 65829b4

Browse files
committed
Refactored the authentication processes in order to handle Admin API and Shop API
1 parent 7808d6b commit 65829b4

File tree

7 files changed

+135
-267
lines changed

7 files changed

+135
-267
lines changed

spec/Security/AuthenticationSpec.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ public function it_is_initializable_from_a_password()
1111
$this->beConstructedThrough('fromPassword', ['client_id', 'secret', 'Julia', 'Julia_pwd']);
1212
$this->shouldHaveType('Diglin\Sylius\ApiClient\Security\Authentication');
1313

14-
$this->getClientId()->shouldReturn('client_id');
15-
$this->getSecret()->shouldReturn('secret');
1614
$this->getUsername()->shouldReturn('Julia');
1715
$this->getPassword()->shouldReturn('Julia_pwd');
1816
$this->getAccessToken()->shouldReturn(null);

src/Api/Authentication/AdminApi.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Diglin GmbH - Switzerland.
4+
*
5+
* @author Sylvain Rayé <support at diglin.com>
6+
*
7+
* @category SyliusApiClient
8+
*
9+
* @copyright 2020 - Diglin (https://www.diglin.com)
10+
*/
11+
12+
namespace Diglin\Sylius\ApiClient\Api\Authentication;
13+
14+
use Diglin\Sylius\ApiClient\Client\HttpClient;
15+
use Diglin\Sylius\ApiClient\Routing\UriGeneratorInterface;
16+
17+
/**
18+
* API implementation to manage the authentication.
19+
*
20+
* @author Alexandre Hocquard <[email protected]>
21+
* @copyright 2017 Akeneo SAS (http://www.akeneo.com)
22+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
23+
*/
24+
class AdminApi implements AuthenticationApiInterface
25+
{
26+
public const TOKEN_URI = 'api/v2/admin/authentication-token';
27+
28+
public function __construct(
29+
private HttpClient $httpClient,
30+
private UriGeneratorInterface $uriGenerator
31+
) {}
32+
33+
public function authenticateByPassword(string $username, string $password): array
34+
{
35+
$headers = [
36+
'Content-Type' => 'application/json',
37+
];
38+
39+
$uri = $this->uriGenerator->generate(static::TOKEN_URI);
40+
41+
$response = $this->httpClient->sendRequest('POST', $uri, $headers, json_encode([
42+
'email' => $username,
43+
'password' => $password,
44+
]));
45+
46+
return json_decode($response->getBody()->getContents(), true);
47+
}
48+
}

src/Api/AuthenticationApiInterface.php renamed to src/Api/Authentication/AuthenticationApiInterface.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @copyright 2020 - Diglin (https://www.diglin.com)
1010
*/
1111

12-
namespace Diglin\Sylius\ApiClient\Api;
12+
namespace Diglin\Sylius\ApiClient\Api\Authentication;
1313

1414
/**
1515
* API to manage the authentication.
@@ -23,23 +23,7 @@ interface AuthenticationApiInterface
2323
/**
2424
* Authenticates with the password grant type.
2525
*
26-
* @param string $clientId
27-
* @param string $secret
28-
* @param string $username
29-
* @param string $password
30-
*
31-
* @return array
32-
*/
33-
public function authenticateByPassword($clientId, $secret, $username, $password);
34-
35-
/**
36-
* Authenticates with the refresh token grant type.
37-
*
38-
* @param string $clientId
39-
* @param string $secret
40-
* @param string $refreshToken
41-
*
42-
* @return array
26+
* @return array{token: string}
4327
*/
44-
public function authenticateByRefreshToken($clientId, $secret, $refreshToken);
28+
public function authenticateByPassword(string $username, string $password): array;
4529
}

src/Api/Authentication/ShopApi.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Diglin GmbH - Switzerland.
4+
*
5+
* @author Sylvain Rayé <support at diglin.com>
6+
*
7+
* @category SyliusApiClient
8+
*
9+
* @copyright 2020 - Diglin (https://www.diglin.com)
10+
*/
11+
12+
namespace Diglin\Sylius\ApiClient\Api\Authentication;
13+
14+
use Diglin\Sylius\ApiClient\Client\HttpClient;
15+
use Diglin\Sylius\ApiClient\Routing\UriGeneratorInterface;
16+
17+
/**
18+
* API implementation to manage the authentication.
19+
*
20+
* @author Alexandre Hocquard <[email protected]>
21+
* @copyright 2017 Akeneo SAS (http://www.akeneo.com)
22+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
23+
*/
24+
class ShopApi implements AuthenticationApiInterface
25+
{
26+
public const TOKEN_URI = 'api/v2/shop/authentication-token';
27+
28+
public function __construct(
29+
private HttpClient $httpClient,
30+
private UriGeneratorInterface $uriGenerator
31+
) {}
32+
33+
public function authenticateByPassword(string $username, string $password): array
34+
{
35+
$headers = [
36+
'Content-Type' => 'application/json',
37+
];
38+
39+
$uri = $this->uriGenerator->generate(static::TOKEN_URI);
40+
41+
$response = $this->httpClient->sendRequest('POST', $uri, $headers, json_encode([
42+
'email' => $username,
43+
'password' => $password,
44+
]));
45+
46+
return json_decode($response->getBody()->getContents(), true);
47+
}
48+
}

src/Api/AuthenticationApi.php

Lines changed: 0 additions & 85 deletions
This file was deleted.

src/Client/AuthenticatedHttpClient.php

Lines changed: 12 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Diglin\Sylius\ApiClient\Client;
44

5-
use Diglin\Sylius\ApiClient\Api\AuthenticationApiInterface;
5+
use Diglin\Sylius\ApiClient\Api;
66
use Diglin\Sylius\ApiClient\Exception\UnauthorizedHttpException;
77
use Diglin\Sylius\ApiClient\Security\Authentication;
8+
use Psr\Log\LoggerInterface;
89

910
/**
1011
* Http client to send an authenticated request.
@@ -20,73 +21,26 @@
2021
*/
2122
class AuthenticatedHttpClient implements HttpClientInterface
2223
{
23-
/** @var HttpClient */
24-
protected $basicHttpClient;
25-
26-
/** @var AuthenticationApiInterface */
27-
protected $authenticationApi;
28-
29-
/** @var Authentication */
30-
protected $authentication;
31-
3224
public function __construct(
33-
HttpClient $basicHttpClient,
34-
AuthenticationApiInterface $authenticationApi,
35-
Authentication $authentication
36-
) {
37-
$this->basicHttpClient = $basicHttpClient;
38-
$this->authenticationApi = $authenticationApi;
39-
$this->authentication = $authentication;
40-
}
25+
private HttpClient $basicHttpClient,
26+
private Api\Authentication\AuthenticationApiInterface $authenticationApi,
27+
private Authentication $authentication,
28+
) {}
4129

4230
/**
4331
* {@inheritdoc}
4432
*/
4533
public function sendRequest($httpMethod, $uri, array $headers = [], $body = null)
4634
{
4735
try {
48-
$xauthtokenDetected = false;
49-
foreach ((array) $this->authentication->getXauthtokenHeader() as $name => $value) {
50-
$headers[$name] = $value;
51-
$xauthtokenDetected = true;
36+
if (!$this->authentication->hasAccessToken()) {
37+
$this->authentication->authenticateByPassword($this->authenticationApi);
5238
}
5339

54-
if ($xauthtokenDetected) {
55-
return $this->basicHttpClient->sendRequest($httpMethod, $uri, $headers, $body);
56-
}
57-
} catch (UnauthorizedHttpException $e) {
58-
// Do nothing and process to standard authentication
59-
}
60-
61-
if (null === $this->authentication->getAccessToken()) {
62-
$tokens = $this->authenticationApi->authenticateByPassword(
63-
$this->authentication->getClientId(),
64-
$this->authentication->getSecret(),
65-
$this->authentication->getUsername(),
66-
$this->authentication->getPassword()
67-
);
68-
69-
$this->authentication
70-
->setAccessToken($tokens['token'])
71-
;
72-
}
73-
74-
try {
75-
$headers['Authorization'] = sprintf('Bearer %s', $this->authentication->getAccessToken());
76-
$response = $this->basicHttpClient->sendRequest($httpMethod, $uri, $headers, $body);
77-
} catch (UnauthorizedHttpException $e) {
78-
$tokens = $this->authenticationApi->authenticateByRefreshToken(
79-
$this->authentication->getClientId(),
80-
$this->authentication->getSecret(),
81-
$this->authentication->getRefreshToken()
82-
);
83-
84-
$this->authentication
85-
->setAccessToken($tokens['token'])
86-
;
87-
88-
$headers['Authorization'] = sprintf('Bearer %s', $this->authentication->getAccessToken());
89-
$response = $this->basicHttpClient->sendRequest($httpMethod, $uri, $headers, $body);
40+
$response = $this->basicHttpClient->sendRequest($httpMethod, $uri, $this->authentication->appendHeaders($headers), $body);
41+
} catch (UnauthorizedHttpException) {
42+
$this->authentication->authenticateByPassword($this->authenticationApi);
43+
$response = $this->basicHttpClient->sendRequest($httpMethod, $uri, $this->authentication->appendHeaders($headers), $body);
9044
}
9145

9246
return $response;

0 commit comments

Comments
 (0)