Skip to content

Commit 7e86255

Browse files
PHP 8 Support (#269)
* bump JWT * Add PHP 8 build * Remove PHP CS fixer for now * Allow PHPUnit 9 * Fix static keyword * Locked to jwt 3.4|4.0, and fixed tests to use toString() Co-authored-by: Chris Tankersley <[email protected]>
1 parent 0c293b4 commit 7e86255

File tree

7 files changed

+30
-35
lines changed

7 files changed

+30
-35
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ jobs:
88
runs-on: ubuntu-latest
99
strategy:
1010
matrix:
11-
php: [ '7.2', '7.3', '7.4' ]
12-
continue-on-error: ${{ matrix.php == '8.0' }}
11+
php: [ '7.2', '7.3', '7.4', '8.0' ]
1312
name: PHP ${{ matrix.php }} Test
1413

1514
steps:

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,18 @@
2121
"ext-json": "*",
2222
"ext-mbstring": "*",
2323
"laminas/laminas-diactoros": "^2.4",
24-
"lcobucci/jwt": "^3.3",
24+
"lcobucci/jwt": "^3.4|^4.0",
2525
"composer/package-versions-deprecated": "^1.11",
2626
"psr/container": "^1.0",
2727
"psr/http-client-implementation": "^1.0",
2828
"vonage/nexmo-bridge": "^0.1.0"
2929
},
3030
"require-dev": {
31-
"friendsofphp/php-cs-fixer": "^2.16",
3231
"guzzlehttp/guzzle": ">=6",
3332
"helmich/phpunit-json-assert": "^3.3",
3433
"php-http/mock-client": "^1.4",
3534
"phpstan/phpstan": "^0.12",
36-
"phpunit/phpunit": "^8.5",
35+
"phpunit/phpunit": "^8.5|^9.4",
3736
"roave/security-advisories": "dev-latest",
3837
"squizlabs/php_codesniffer": "^3.5",
3938
"softcreatr/jsonpath": "^0.6.4"

src/Client.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,9 @@ public function __construct(CredentialsInterface $credentials, $options = [], ?C
205205
static function (
206206
int $errno,
207207
string $errstr,
208-
string $errfile,
209-
int $errline,
210-
array $errorcontext
208+
string $errfile = null,
209+
int $errline = null,
210+
array $errorcontext = null
211211
) {
212212
return true;
213213
},
@@ -478,14 +478,14 @@ public function send(RequestInterface $request): ResponseInterface
478478
if ($this->needsKeypairAuthentication($request)) {
479479
$token = $this->credentials->get(Keypair::class)->generateJwt();
480480

481-
$request = $request->withHeader('Authorization', 'Bearer ' . $token);
481+
$request = $request->withHeader('Authorization', 'Bearer ' . $token->toString());
482482
} else {
483483
$request = self::authRequest($request, $this->credentials->get(Basic::class));
484484
}
485485
} elseif ($this->credentials instanceof Keypair) {
486486
$token = $this->credentials->generateJwt();
487487

488-
$request = $request->withHeader('Authorization', 'Bearer ' . $token);
488+
$request = $request->withHeader('Authorization', 'Bearer ' . $token->toString());
489489
} elseif ($this->credentials instanceof SignatureSecret) {
490490
$request = self::signRequest($request, $this->credentials);
491491
} elseif ($this->credentials instanceof Basic) {

src/Client/Credentials/Keypair.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
namespace Vonage\Client\Credentials;
1313

14-
use Lcobucci\JWT\Builder;
14+
use Lcobucci\JWT\Configuration;
1515
use Lcobucci\JWT\Signer\Key;
16+
use Lcobucci\JWT\Signer\Key\InMemory;
1617
use Lcobucci\JWT\Signer\Rsa\Sha256;
1718
use Lcobucci\JWT\Token;
1819
use Vonage\Application\Application;
@@ -31,11 +32,6 @@ class Keypair extends AbstractCredentials
3132
*/
3233
protected $key;
3334

34-
/**
35-
* @var Sha256
36-
*/
37-
protected $signer;
38-
3935
public function __construct($privateKey, $application = null)
4036
{
4137
$this->credentials['key'] = $privateKey;
@@ -48,12 +44,13 @@ public function __construct($privateKey, $application = null)
4844
$this->credentials['application'] = $application;
4945
}
5046

51-
$this->key = new Key($privateKey);
52-
$this->signer = new Sha256();
47+
$this->key = InMemory::plainText($privateKey);
5348
}
5449

5550
public function generateJwt(array $claims = []): Token
5651
{
52+
$config = Configuration::forSymmetricSigner(new Sha256(), $this->key);
53+
5754
$exp = time() + 60;
5855
$iat = time();
5956
$jti = base64_encode((string)mt_rand());
@@ -76,13 +73,13 @@ public function generateJwt(array $claims = []): Token
7673
unset($claims['jti']);
7774
}
7875

79-
$builder = new Builder();
80-
$builder->issuedAt($iat)
81-
->expiresAt($exp)
76+
$builder = $config->builder();
77+
$builder->issuedAt((new \DateTimeImmutable())->setTimestamp($iat))
78+
->expiresAt((new \DateTimeImmutable())->setTimestamp($exp))
8279
->identifiedBy($jti);
8380

8481
if (isset($claims['nbf'])) {
85-
$builder->canOnlyBeUsedAfter($claims['nbf']);
82+
$builder->canOnlyBeUsedAfter((new \DateTimeImmutable())->setTimestamp($claims['nbf']));
8683

8784
unset($claims['nbf']);
8885
}
@@ -97,6 +94,6 @@ public function generateJwt(array $claims = []): Token
9794
}
9895
}
9996

100-
return $builder->getToken($this->signer, $this->key);
97+
return $builder->getToken($config->signer(), $config->signingKey());
10198
}
10299
}

src/Client/Signature.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function __construct(array $params, $secret, $signatureMethod)
6565
$signed = [];
6666

6767
foreach ($this->signed as $key => $value) {
68-
$signed[$key] = str_replace(["&", "="], "_", $value);
68+
$signed[$key] = str_replace(["&", "="], "_", (string) $value);
6969
}
7070

7171
//create base string

src/Entity/CollectionTrait.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ trait CollectionTrait
6969
*/
7070
protected $filter;
7171

72-
abstract public function getCollectionName(): string;
72+
abstract public static function getCollectionName(): string;
7373

74-
abstract public function getCollectionPath(): string;
74+
abstract public static function getCollectionPath(): string;
7575

7676
/**
7777
* @param $data
@@ -84,7 +84,7 @@ abstract public function hydrateEntity($data, $id);
8484
*/
8585
public function current()
8686
{
87-
return $this->hydrateEntity($this->page['_embedded'][$this->getCollectionName()][$this->current], $this->key());
87+
return $this->hydrateEntity($this->page['_embedded'][static::getCollectionName()][$this->current], $this->key());
8888
}
8989

9090
/**
@@ -103,8 +103,8 @@ public function next(): void
103103
public function key()
104104
{
105105
return
106-
$this->page['_embedded'][$this->getCollectionName()][$this->current]['id'] ??
107-
$this->page['_embedded'][$this->getCollectionName()][$this->current]['uuid'] ??
106+
$this->page['_embedded'][static::getCollectionName()][$this->current]['id'] ??
107+
$this->page['_embedded'][static::getCollectionName()][$this->current]['uuid'] ??
108108
$this->current;
109109
}
110110

@@ -119,12 +119,12 @@ public function valid(): bool
119119
}
120120

121121
//all hal collections have an `_embedded` object, we expect there to be a property matching the collection name
122-
if (!isset($this->page['_embedded'][$this->getCollectionName()])) {
122+
if (!isset($this->page['_embedded'][static::getCollectionName()])) {
123123
return false;
124124
}
125125

126126
//if we have a page with no items, we've gone beyond the end of the collection
127-
if (!count($this->page['_embedded'][$this->getCollectionName()])) {
127+
if (!count($this->page['_embedded'][static::getCollectionName()])) {
128128
return false;
129129
}
130130

@@ -134,7 +134,7 @@ public function valid(): bool
134134
}
135135

136136
//if our current index is past the current page, fetch the next page if possible and reset the index
137-
if (!isset($this->page['_embedded'][$this->getCollectionName()][$this->current])) {
137+
if (!isset($this->page['_embedded'][static::getCollectionName()][$this->current])) {
138138
if (isset($this->page['_links']['next'])) {
139139
$this->fetchPage($this->page['_links']['next']['href']);
140140
$this->current = 0;
@@ -153,7 +153,7 @@ public function valid(): bool
153153
*/
154154
public function rewind(): void
155155
{
156-
$this->fetchPage($this->getCollectionPath());
156+
$this->fetchPage(static::getCollectionPath());
157157
}
158158

159159
/**

test/Client/Credentials/KeypairTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function testDefaultJWT(): void
5757
$credentials = new Keypair($this->key, $this->application);
5858

5959
//could use the JWT object, but hope to remove as a dependency
60-
$jwt = (string)$credentials->generateJwt();
60+
$jwt = (string)$credentials->generateJwt()->toString();
6161

6262
[$header, $payload] = $this->decodeJWT($jwt);
6363

@@ -84,7 +84,7 @@ public function testAdditionalClaims(): void
8484
];
8585

8686
$jwt = $credentials->generateJwt($claims);
87-
[, $payload] = $this->decodeJWT($jwt);
87+
[, $payload] = $this->decodeJWT($jwt->toString());
8888

8989
$this->assertArrayHasKey('arbitrary', $payload);
9090
$this->assertEquals($claims['arbitrary'], $payload['arbitrary']);

0 commit comments

Comments
 (0)