Skip to content

Commit

Permalink
Merge pull request #26 from SAP/firebase_jwt_6_compat
Browse files Browse the repository at this point in the history
Firebase jwt 6 compat
  • Loading branch information
shemma3 authored Jan 17, 2023
2 parents 4974ba4 + 3a8e621 commit 774765e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 51 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The PHP SDK, provides a PHP interface for the Gigya API.
The library makes it simple to integrate Gigya services in your PHP application.

## Requirements
[PHP 7.x.](https://www.php.net/downloads) , [PHP 8.x.](https://www.php.net/downloads)
[PHP 7.x.](https://www.php.net/downloads) , [PHP 8.1](https://www.php.net/downloads)

## Download and Installation
### Standalone
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
"version": "3.0.2",
"license": "Apache-2.0",
"require": {
"php": ">=7.0",
"firebase/php-jwt": "^5.0",
"php": ">=8.0 <8.2",
"firebase/php-jwt": "^6.0",
"ext-json": "*",
"ext-curl": "*",
"ext-openssl": "*"
},
"require-dev": {
"phpunit/phpunit": "^6.5"
"phpunit/phpunit": "^8.5"
},
"autoload": {
"psr-4": {
Expand Down
45 changes: 23 additions & 22 deletions src/JWTUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Firebase\JWT\JWK;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use InvalidArgumentException;
use stdClass;
use UnexpectedValueException;
Expand All @@ -16,15 +17,15 @@ class JWTUtils
*
* @param string $privateKey
* @param string $userKey
* @param string $nonce
* @param string|null $nonce
*
* @return string
*
* @throws Exception
*/
public static function getBearerToken(string $privateKey, string $userKey, $nonce = null)
public static function getBearerToken(string $privateKey, string $userKey, string|null $nonce = null): string
{
$jti = $nonce ?? SigUtils::currentTimeMillis() . rand(); /* PHP 7.0+ */
$jti = $nonce ?? SigUtils::currentTimeMillis() . rand();
$payload = [
'iat' => time(),
'jti' => $jti,
Expand All @@ -40,11 +41,11 @@ public static function getBearerToken(string $privateKey, string $userKey, $nonc
* @param string $apiKey
* @param string $apiDomain
*
* @return stdClass|bool
* @return stdClass|false
*
* @throws Exception
*/
public static function validateSignature(string $jwt, string $apiKey, string $apiDomain)
public static function validateSignature(string $jwt, string $apiKey, string $apiDomain): stdClass|false
{
/* Validate input and get KID */
if (!$jwt) {
Expand All @@ -68,24 +69,24 @@ public static function validateSignature(string $jwt, string $apiKey, string $ap
}

try {
$jwtInfo = JWT::decode($jwt, $jwk, ['RS256']);

return $jwtInfo ?? false; /* PHP 7.0+ */
JWT::$leeway = 5;
$jwtInfo = JWT::decode($jwt, new Key($jwk, 'RS256'));
return $jwtInfo ?? false;
} catch (UnexpectedValueException $e) {
return false;
}
}

/**
* @param $apiKey
* @param $apiDomain
* @param $kid
* @param string $apiKey
* @param string $apiDomain
* @param string $kid
*
* @return string|resource
* @return Key|false
*
* @throws GSException
*/
private static function getJWKByKid($apiKey, $apiDomain, $kid) {
private static function getJWKByKid(string $apiKey, string $apiDomain, string $kid): Key|false {
if (($jwks = self::readPublicKeyCache($apiDomain)) === false) {
$jwks = self::fetchPublicJWKs($apiKey, $apiDomain);
}
Expand All @@ -106,14 +107,14 @@ private static function getJWKByKid($apiKey, $apiDomain, $kid) {
}

/**
* @param $apiKey
* @param $apiDomain
* @param string $apiKey
* @param string $apiDomain
*
* @return array|null
* @return array<string, Key>|null
*
* @throws GSException
*/
private static function fetchPublicJWKs($apiKey, $apiDomain)
private static function fetchPublicJWKs(string $apiKey, string $apiDomain): array|null
{
$request = new GSRequest($apiKey, null, 'accounts.getJWTPublicKey');
$request->setAPIDomain($apiDomain);
Expand All @@ -138,15 +139,15 @@ private static function fetchPublicJWKs($apiKey, $apiDomain)
}

/**
* @param array $publicKeys
* @param string $apiDomain
* @param array<Key> $publicKeys
* @param string $apiDomain
*
* @return int|false Bytes written to cache file or false on failure
*/
private static function addToPublicKeyCache($publicKeys, $apiDomain)
private static function addToPublicKeyCache(array $publicKeys, string $apiDomain): int|false
{
foreach ($publicKeys as $kid => $key) {
if (!empty($pem = openssl_pkey_get_details($key)['key'])) {
if (!empty($pem = openssl_pkey_get_details($key->getKeyMaterial())['key'])) {
$publicKeys[$kid] = $pem;
} else {
return false;
Expand All @@ -163,7 +164,7 @@ private static function addToPublicKeyCache($publicKeys, $apiDomain)
*
* @return array|false Returns false if the cache file does not exist, or if reading the file or decoding the JSON array in it fails
*/
private static function readPublicKeyCache($apiDomain)
private static function readPublicKeyCache(string $apiDomain): array|false
{
$filename = __DIR__ . '/keys/' . $apiDomain . '_keys.txt';

Expand Down
36 changes: 18 additions & 18 deletions tests/GSRequestWithUserKeyPrivateKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,45 +67,45 @@ public function testGigyaCallWithIncorrectBearerToken($apiKey, $apiDomain, $user
}

/**
* @param $apiKey
* @param $apiDomain
* @param $userKey
* @param $privateKey
* @param string $apiKey
* @param string $apiDomain
* @param string $userKey
* @param string $privateKey
*
* @return GSResponse
*
* @throws GSKeyNotFoundException
*/
private function sendAccountsSearchRequest($apiKey, $apiDomain, $userKey, $privateKey) {
private function sendAccountsSearchRequest(string $apiKey, string $apiDomain, string $userKey, string $privateKey): GSResponse {
return $this->sendRequest($apiKey, $apiDomain, $userKey, $privateKey, 'accounts.search', array('query' => 'SELECT * FROM user LIMIT 1'));
}

/**
* @param $apiKey
* @param $apiDomain
* @param $userKey
* @param $privateKey
* @param $uid
* @param string $apiKey
* @param string $apiDomain
* @param string $userKey
* @param string $privateKey
* @param string $uid
*
* @return GSResponse
* @throws GSKeyNotFoundException
*/
private function sendGetAccountInfoRequest($apiKey, $apiDomain, $userKey, $privateKey, $uid) {
private function sendGetAccountInfoRequest(string $apiKey, string $apiDomain, string $userKey, string $privateKey, string $uid): GSResponse {
return $this->sendRequest($apiKey, $apiDomain, $userKey, $privateKey, 'accounts.getAccountInfo', array('uid' => $uid));
}

/**
* @param $apiKey
* @param $apiDomain
* @param $userKey
* @param $privateKey
* @param $apiMethod
* @param $params
* @param string $apiKey
* @param string $apiDomain
* @param string $userKey
* @param string $privateKey
* @param string $apiMethod
* @param array $params
*
* @return GSResponse
* @throws GSKeyNotFoundException
*/
private function sendRequest($apiKey, $apiDomain, $userKey, $privateKey, $apiMethod, $params) {
private function sendRequest(string $apiKey, string $apiDomain, string $userKey, string $privateKey, string $apiMethod, array $params): GSResponse {
$request = new GSRequest($apiKey,
null,
$apiMethod,
Expand Down
10 changes: 5 additions & 5 deletions tests/JWTUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use Exception;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Gigya\PHP\GSKeyNotFoundException;
use Gigya\PHP\GSRequest;
use PHPUnit\Framework\Error\Warning;
use PHPUnit\Framework\TestCase;
use Gigya\PHP\JWTUtils;

Expand All @@ -25,7 +25,7 @@ public function testGetBearerToken($privateKey, $publicKey)
$userKey = 'myUserKey';

$bearerToken = JWTUtils::getBearerToken($privateKey, $userKey);
$jwtDetails = JWT::decode($bearerToken, $publicKey, ['RS256']);
$jwtDetails = JWT::decode($bearerToken, new Key($publicKey, 'RS256'));

$this->assertTrue($jwtDetails instanceof \stdClass);
$this->assertObjectHasAttribute('iat', $jwtDetails);
Expand All @@ -40,7 +40,7 @@ public function testGetBearerTokenIncorrectPrivateKey()
$incorrectPrivateKey = rand();
$userKey = rand();

$this->expectException(Warning::class);
$this->expectWarning();

JWTUtils::getBearerToken($incorrectPrivateKey, $userKey);
}
Expand All @@ -56,7 +56,7 @@ public function testGetBearerTokenIncorrectPrivateKey()
*
* @throws Exception
*/
public function testValidateSignature($apiKey, $apiDomain, $userKey, $privateKey, $uid)
public function testValidateSignature(string $apiKey, string $apiDomain, string $userKey, string $privateKey, string $uid)
{
$jwt = $this->getJWT($apiKey, $apiDomain, $userKey, $privateKey, $uid);
$this->assertNotFalse($jwt);
Expand All @@ -78,7 +78,7 @@ public function testValidateSignature($apiKey, $apiDomain, $userKey, $privateKey
*
* @throws GSKeyNotFoundException
*/
private function getJWT($apiKey, $apiDomain, $userKey, $privateKey, $uid)
private function getJWT(string $apiKey, string $apiDomain, string $userKey, string $privateKey, string $uid): string|false
{
$request = new GSRequest($apiKey,
null,
Expand Down
4 changes: 2 additions & 2 deletions tests/JwtAuthDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class JwtAuthDataProvider
*
* @return array
*/
public function provideAuthDetails()
public function provideAuthDetails(): array
{
$jsonDataProvider = __DIR__ . '/' . __FUNCTION__ . '.json';

Expand All @@ -34,7 +34,7 @@ public function provideAuthDetails()
}
}

return $returnData ?? [ /* PHP 7.0+ */
return $returnData ?? [
[
'apiKey' => '',
'apiDomain' => '',
Expand Down

0 comments on commit 774765e

Please sign in to comment.