Merge pull request #739 from virtualtam/fix/api/jwt-signature
API: fix JWT signature verification
This commit is contained in:
commit
383cbaf2c5
4 changed files with 49 additions and 13 deletions
34
application/Base64Url.php
Normal file
34
application/Base64Url.php
Normal file
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace Shaarli;
|
||||
|
||||
|
||||
/**
|
||||
* URL-safe Base64 operations
|
||||
*
|
||||
* @see https://en.wikipedia.org/wiki/Base64#URL_applications
|
||||
*/
|
||||
class Base64Url
|
||||
{
|
||||
/**
|
||||
* Base64Url-encodes data
|
||||
*
|
||||
* @param string $data Data to encode
|
||||
*
|
||||
* @return string Base64Url-encoded data
|
||||
*/
|
||||
public static function encode($data) {
|
||||
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes Base64Url-encoded data
|
||||
*
|
||||
* @param string $data Data to decode
|
||||
*
|
||||
* @return string Decoded data
|
||||
*/
|
||||
public static function decode($data) {
|
||||
return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
|
||||
}
|
||||
}
|
|
@ -1,13 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace Shaarli\Api;
|
||||
|
||||
use Shaarli\Base64Url;
|
||||
use Shaarli\Api\Exceptions\ApiAuthorizationException;
|
||||
|
||||
/**
|
||||
* Class ApiUtils
|
||||
*
|
||||
* Utility functions for the API.
|
||||
* REST API utilities
|
||||
*/
|
||||
class ApiUtils
|
||||
{
|
||||
|
@ -26,17 +24,17 @@ public static function validateJwtToken($token, $secret)
|
|||
throw new ApiAuthorizationException('Malformed JWT token');
|
||||
}
|
||||
|
||||
$genSign = hash_hmac('sha512', $parts[0] .'.'. $parts[1], $secret);
|
||||
$genSign = Base64Url::encode(hash_hmac('sha512', $parts[0] .'.'. $parts[1], $secret, true));
|
||||
if ($parts[2] != $genSign) {
|
||||
throw new ApiAuthorizationException('Invalid JWT signature');
|
||||
}
|
||||
|
||||
$header = json_decode(base64_decode($parts[0]));
|
||||
$header = json_decode(Base64Url::decode($parts[0]));
|
||||
if ($header === null) {
|
||||
throw new ApiAuthorizationException('Invalid JWT header');
|
||||
}
|
||||
|
||||
$payload = json_decode(base64_decode($parts[1]));
|
||||
$payload = json_decode(Base64Url::decode($parts[1]));
|
||||
if ($payload === null) {
|
||||
throw new ApiAuthorizationException('Invalid JWT payload');
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Shaarli\\": "application",
|
||||
"Shaarli\\Api\\": "application/api/",
|
||||
"Shaarli\\Api\\Controllers\\": "application/api/controllers",
|
||||
"Shaarli\\Api\\Exceptions\\": "application/api/exceptions"
|
||||
|
|
|
@ -2,6 +2,9 @@
|
|||
|
||||
namespace Shaarli\Api;
|
||||
|
||||
use Shaarli\Base64Url;
|
||||
|
||||
|
||||
/**
|
||||
* Class ApiUtilsTest
|
||||
*/
|
||||
|
@ -24,14 +27,14 @@ public static function setUpBeforeClass()
|
|||
*/
|
||||
public static function generateValidJwtToken($secret)
|
||||
{
|
||||
$header = base64_encode('{
|
||||
$header = Base64Url::encode('{
|
||||
"typ": "JWT",
|
||||
"alg": "HS512"
|
||||
}');
|
||||
$payload = base64_encode('{
|
||||
$payload = Base64Url::encode('{
|
||||
"iat": '. time() .'
|
||||
}');
|
||||
$signature = hash_hmac('sha512', $header .'.'. $payload , $secret);
|
||||
$signature = Base64Url::encode(hash_hmac('sha512', $header .'.'. $payload , $secret, true));
|
||||
return $header .'.'. $payload .'.'. $signature;
|
||||
}
|
||||
|
||||
|
@ -46,9 +49,9 @@ public static function generateValidJwtToken($secret)
|
|||
*/
|
||||
public static function generateCustomJwtToken($header, $payload, $secret)
|
||||
{
|
||||
$header = base64_encode($header);
|
||||
$payload = base64_encode($payload);
|
||||
$signature = hash_hmac('sha512', $header . '.' . $payload, $secret);
|
||||
$header = Base64Url::encode($header);
|
||||
$payload = Base64Url::encode($payload);
|
||||
$signature = Base64Url::encode(hash_hmac('sha512', $header . '.' . $payload, $secret, true));
|
||||
return $header . '.' . $payload . '.' . $signature;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue