Skip to content

Commit

Permalink
Use library urlSafe base64 decode
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Feb 1, 2019
1 parent 1159c3b commit c8cb6f0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
}
],
"require": {
"php": ">=7.0",
"php": ">=7.1",
"ext-json": "*",
"ext-openssl": "*",
"packaged/helpers": "~1.0"
"packaged/helpers": "^1.0||^2.0"
},
"require-dev": {
"phpunit/phpunit": "~7.0"
Expand Down
24 changes: 7 additions & 17 deletions src/Fident.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Fident\Web\Notifications\FidentNotification;
use Fident\Web\UserData\FidentJwtData;
use Packaged\Helpers\Objects;
use Packaged\Helpers\Strings;

class Fident
{
Expand All @@ -27,23 +28,23 @@ public function getConfig(): FidentConfiguration

public function verifyJwt(string $rawJwt): bool
{
list($header64, $payload64, $sig64) = explode('.', $rawJwt, 3);
$header = json_decode(self::urlsafeB64Decode($header64));
list($head64, $payload64, $sig64) = explode('.', $rawJwt, 3);
$header = json_decode(Strings::urlsafeBase64Decode($head64));
if(!$header || !isset($header->typ) || $header->typ !== 'JWT')
{
return false;
}
$key = $this->_configuration->getPublicKey();
return openssl_verify("$header64.$payload64", self::urlsafeB64Decode($sig64), $key, OPENSSL_ALGO_SHA256) === 1;
return openssl_verify("$head64.$payload64", Strings::urlsafeBase64Decode($sig64), $key, OPENSSL_ALGO_SHA256) === 1;
}

public function decodeJwtPayload(string $rawJwt): ?FidentJwtData
{
$data = new FidentJwtData();

list(, $payload64,) = explode('.', $rawJwt, 3);
$payload = json_decode(self::urlsafeB64Decode($payload64));
$payload->payload = self::urlsafeB64Decode($payload->payload);
$payload = json_decode(Strings::urlsafeBase64Decode($payload64));
$payload->payload = Strings::urlsafeBase64Decode($payload->payload);

$method = 'AES-256-CFB';
$ivlen = openssl_cipher_iv_length($method);
Expand All @@ -68,22 +69,11 @@ public function decodeNotification($requestBody): ?FidentNotification
{
$notification = json_decode($requestBody);
$data = Objects::property($notification, 'Data', '');
$sig = self::urlsafeB64Decode(Objects::property($notification, 'Signature', ''));
$sig = Strings::urlsafeBase64Decode(Objects::property($notification, 'Signature', ''));
if(openssl_verify($data, $sig, $this->getConfig()->getPublicKey(), OPENSSL_ALGO_SHA256))
{
return FidentNotification::generate(Objects::property($notification, 'DataType', 1), $data);
}
return null;
}

public static function urlsafeB64Decode($input)
{
$remainder = strlen($input) % 4;
if($remainder)
{
$padlen = 4 - $remainder;
$input .= str_repeat('=', $padlen);
}
return base64_decode(strtr($input, '-_', '+/'));
}
}

0 comments on commit c8cb6f0

Please sign in to comment.