Skip to content

Commit

Permalink
签名校验
Browse files Browse the repository at this point in the history
  • Loading branch information
zoulux committed Dec 11, 2019
1 parent aa72adf commit 9a83c07
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"require": {
"php": ">=7.0",
"guzzlehttp/guzzle": "^6.2",
"ext-json": "*"
"ext-json": "*",
"symfony/http-foundation": "^5.0"
},
"require-dev": {
"phpunit/phpunit": "^7.5"
Expand Down
30 changes: 30 additions & 0 deletions src/BJCloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Jake\Baijiayun;

use GuzzleHttp\Client;
use Symfony\Component\HttpFoundation\Request;

class BJCloud
{
Expand Down Expand Up @@ -401,6 +402,33 @@ public function roomDataGetEvaluationStat($roomId, $page, $pageSize = 20, $date
return $this->call('/openapi/room_data/getEvaluationStat', $params);
}

/**
* 校验签名
* @param null $data
* @return array|null
* @throws BJCloudException
*/
public function verify($data = null)
{
if (is_null($data)) {
$request = Request::createFromGlobals();
$data = $request->request->count() > 0 ? $request->request->all() : $request->query->all();
}

if (!isset($data['sign'])) {
throw new BJCloudException('没有签名信息', 400001);
}

$sign = $data['sign'];

$verifySign = $this->generateSign($data, $this->partnerKey);
if (!hash_equals($verifySign, $sign)) {
throw new BJCloudException('签名不准确', 400002);
}

return $data;
}

//==================================================
// 以下为系统逻辑
//==================================================
Expand All @@ -413,6 +441,8 @@ public function roomDataGetEvaluationStat($roomId, $page, $pageSize = 20, $date
*/
private function generateSign($params, $partner_key)
{
unset($params['sign']);

ksort($params);//将参数按key进行排序
$str = '';
foreach ($params as $k => $val) {
Expand Down
23 changes: 23 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,27 @@ public function testRoomCreate()
$this->assertArrayHasKey('teacher_code', $res);
}

public function testVerify()
{
$data = [
'video_id' => '30054501',
'status' => '20',
'total_size' => '774452',
'file_md5' => '946aeebfc09dbe7c2bbc7deb9a27e796',
'room_id' => '19121181366617',
'qid' => '201912111342051110803564',
'timestamp' => '1576042925',
'sign' => 'f1d368e0ff2dbad2fcef6912965ae42a',
];

$bjcloud = new \Jake\Baijiayun\BJCloud([
'partner_idd' => '###',
'partner_key' => '###'
]);

$res = $bjcloud->verify($data);

$this->assertIsArray($res);
}

}

0 comments on commit 9a83c07

Please sign in to comment.