【发布时间】:2014-12-28 20:31:43
【问题描述】:
我的代码会生成一个令牌数组,该数组按照 jwt 所需的顺序排列,但是当我运行编码脚本时,它会按照字母顺序重新排序。这会在提交给 google 时导致失败。看起来这个序列是用 assort 函数重新排序的,但不是。我预计爆炸内爆功能会导致此问题,但我无法找到解决方法!
回显的数组“$Token”是:-
Array ( [iss] => 12345605871924644272
[aud] => Google
[typ] => google/payments/inapp/subscription/v1
[exp] => 1414875564
[iat] => 1414871964
[request] => array ( [name] => Invoice Number: 106599
[description] => Supported Service
[sellerData] =>user_id:1224245,
offer_code:3098576987,affiliate:aksdfbovu9j
[initialPayment] => Array( [price] => 100
[currencyCode] => GBP
[paymentType] => prorated )
[recurrence] => Array ( [price] => 30
[currencyCode] => GBP
[startTime] => 3600 [frequency] => Monthly
[numRecurrences] => 24
)
)
)
然后调用 jwt 编码函数
$jwtToken = JWT::encode($Token, $sellerSecretKey);
调用的脚本函数是
public static function encode($payload, $key, $algo = 'HS256')
{
$header = array('typ' => 'JWT', 'alg' => $algo);
$segments = array();
$segments[] = JWT::urlsafeB64Encode(JWT::jsonEncode($header));
$segments[] = JWT::urlsafeB64Encode(JWT::jsonEncode($payload));
$signing_input = implode('.', $segments);
$signature = JWT::sign($signing_input, $key, $algo);
$segments[] = JWT::urlsafeB64Encode($signature);
return implode('.', $segments);
}
/**
* @param string $msg The message to sign
* @param string $key The secret key
* @param string $method The signing algorithm
*
* @return string An encrypted message
*/
public static function sign($msg, $key, $method = 'HS256')
{
$methods = array(
'HS256' => 'sha256',
'HS384' => 'sha384',
'HS512' => 'sha512',
);
if (empty($methods[$method])) {
throw new DomainException('Algorithm not supported');
}
return hash_hmac($methods[$method], $msg, $key, true);
}
返回的 jwt 具有正确的元素,但顺序错误。首先显示所需的顺序,然后显示我的顺序
{
"iss" : "1337133713371337",
"aud" : "Google",
"typ" : "google/payments/inapp/subscription/v1",
"exp" : "1309988959",
"iat" : "1409988959",
"request" :{
"name" : "Weekly Cake",
"description" : "Virtual chocolate cake to fill your virtual tummy every week",
"sellerData" : "user_id:1224245,offer_code:3098576987,affiliate:aksdfbovu9j",
"initialPayment" : {
"price" : "1.49",
"currencyCode" : "USD",
"paymentType" : "prorated",
},
"recurrence" : {
"price" : "4.99",
"currencyCode" : "USD",
"startTime" : "1309989638",
"frequency" : "monthly",
"numRecurrences" : "12",
}
}
}
我的
{
"aud": "Google",
"iss": "12345605871924644272",
"request": {
"initialPayment": {
"paymentType": "prorated",
"price": "100",
"currencyCode": "GBP"
},
"recurrence": {
"numRecurrences": "24",
"price": "30",
"frequency": "Monthly",
"currencyCode": "GBP",
"startTime": "3600"
},
"sellerData": "user_id:1224245,offer_code:3098576987,affiliate:aksdfbovu9j",
"name": "Invoice Number: 106599",
"description": "Supported Service"
},
"exp": 1414873724,
"iat": 1414870124,
"typ": "google/payments/inapp/subscription/v1"
}
【问题讨论】:
标签: php arrays android-pay jwt