Skip to content

Commit 9d015cd

Browse files
authored
Merge pull request #4 from dpdconnect/feature/jwt-useage
feature/jwt-useage
2 parents bdccdc7 + fb90aae commit 9d015cd

File tree

5 files changed

+45
-16
lines changed

5 files changed

+45
-16
lines changed

src/Client.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Client
1515
{
1616
const ENDPOINT = 'https://api.dpdconnect.nl';
1717

18-
const CLIENT_VERSION = '1.0.0';
18+
const CLIENT_VERSION = '1.0.3';
1919

2020
/**
2121
* @var string

src/ClientBuilder.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function setHttpClient(HttpClient $httpClient)
7373
* @param string $username Username to use for the authentication
7474
* @param string $password Password associated to the username
7575
*
76-
* @return ClientInterface
76+
* @return Client
7777
*/
7878
public function buildAuthenticatedByPassword($username, $password)
7979
{
@@ -87,7 +87,7 @@ public function buildAuthenticatedByPassword($username, $password)
8787
*
8888
* @param string $jwtToken JWT tokken for authentication
8989
*
90-
* @return ClientInterface
90+
* @return Client
9191
*/
9292
public function buildAuthenticatedByJwtToken($jwtToken)
9393
{
@@ -99,7 +99,7 @@ public function buildAuthenticatedByJwtToken($jwtToken)
9999
/**
100100
* @param Authentication $authentication
101101
*
102-
* @return ClientInterface
102+
* @return Client
103103
*/
104104
protected function buildAuthenticatedClient(Authentication $authentication)
105105
{

src/Common/AuthenticatedHttpClient.php

+22-12
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,36 @@ public function sendRequest($httpMethod, $resourceName, array $query = [], array
4949

5050
$this->authentication
5151
->setJwtToken($tokens['token']);
52+
if (is_callable($this->authentication->tokenUpdateCallback)) {
53+
call_user_func($this->authentication->tokenUpdateCallback, $this->authentication->getJwtToken());
54+
}
5255
}
5356

5457
try {
55-
$headers[] = sprintf('Authorization: Bearer %s', $this->authentication->getJwtToken());
58+
$headers[] = sprintf('Authorization: Bearer %s', $this->authentication->getJwtToken());
5659

5760
$response = $this->basicHttpClient->sendRequest($httpMethod, $resourceName, $query, $headers, $body);
5861
} catch (AuthenticateException $e) {
59-
$tokens = $this->authenticationResource->authenticateByRefreshToken(
60-
$this->authentication->getClientId(),
61-
$this->authentication->getSecret(),
62-
$this->authentication->getRefreshToken()
63-
);
6462

65-
$this->authentication
66-
->setAccessToken($tokens['access_token'])
67-
->setRefreshToken($tokens['refresh_token']);
63+
try {
64+
$token = $this->authenticationResource->authenticateByPassword(
65+
$this->authentication->getUsername(),
66+
$this->authentication->getPassword()
67+
);
68+
69+
$this->authentication
70+
->setJwtToken($token['token']);
71+
if (is_callable($this->authentication->tokenUpdateCallback)) {
72+
call_user_func($this->authentication->tokenUpdateCallback, $this->authentication->getJwtToken());
73+
}
74+
75+
// Since the Authorization Bearer always is the last added item, we overwrite it like this
76+
$headers[count($headers) - 1] = sprintf('Authorization: Bearer %s', $this->authentication->getJwtToken());
77+
$response = $this->basicHttpClient->sendRequest($httpMethod, $resourceName, $query, $headers, $body);
78+
} catch (AuthenticateException $exception) {
79+
throw $exception;
80+
}
6881

69-
$headers[] = sprintf('Authorization: Bearer %s', $this->authentication->getAccessToken());
70-
$response = $this->basicHttpClient->sendRequest($httpMethod, $resourceName, $query, $headers, $body)
71-
->setAuthentication($this->authentication);
7282
}
7383

7484
return $response;

src/Common/Authentication.php

+16
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class Authentication
1919
*/
2020
protected $jwtToken;
2121

22+
/**
23+
* @var callable
24+
*/
25+
public $tokenUpdateCallback;
26+
2227
/**
2328
* @param $username
2429
* @param $password
@@ -79,4 +84,15 @@ public function setJwtToken($jwtToken)
7984

8085
return $this;
8186
}
87+
88+
/**
89+
* @param callable $callable
90+
* @return $this
91+
*/
92+
public function setTokenUpdateCallback(callable $callable)
93+
{
94+
$this->tokenUpdateCallback = $callable;
95+
96+
return $this;
97+
}
8298
}

src/Common/HttpClient.php

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use DpdConnect\Sdk\Common;
77
use DpdConnect\Sdk\Exceptions;
88
use DpdConnect\Sdk\Exceptions\HttpException;
9+
use DpdConnect\Sdk\Exceptions\AuthenticateException;
910
use DpdConnect\Sdk\Exceptions\ServerException;
1011
use DpdConnect\Sdk\Objects\MetaData;
1112
use DpdConnect\Sdk\Objects\ObjectFactory;
@@ -244,6 +245,8 @@ public function sendRequest($method, $resourceName, $query = null, $headers = []
244245
switch ($responseStatus) {
245246
case 500:
246247
throw new ServerException($parts[1]);
248+
case 401:
249+
throw new AuthenticateException($parts[1]);
247250
}
248251

249252
if (in_array($parts[0], ['HTTP/1.1 200 OK', 'HTTP/1.1 100 Continue'])) {

0 commit comments

Comments
 (0)