Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加resource get update search delete getDownloadUrl #42

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
'school_name' => '测试网校'
));
```
* 增加工厂类`QiQiuYunSDK`,用于管理所有接口服务实例的创建。
* 增加工厂类`ESCloudSDK`,用于管理所有接口服务实例的创建。
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# QiQiuYun PHP SDK
# ESCloud PHP SDK

[![Build Status](https://travis-ci.org/codeages/qiqiuyun-php-sdk.svg?branch=master)](https://travis-ci.org/codeages/qiqiuyun-php-sdk)
[![Build Status](https://travis-ci.org/codeages/escloud-php-sdk.svg?branch=master)](https://travis-ci.org/codeages/escloud-php-sdk)

## 安装

```shell
composer require codeages/qiqiuyun-php-sdk
composer require codeages/escloud-php-sdk
```

## 使用说明

```php
$sdk = new \QiQiuYun\SDK\QiQiuYunSDK(array(
$sdk = new \ESCloud\SDK\ESCloudSDK(array(
'access_key' => 'your_access_key', // 必需
'secret_key' => 'your_secret_key', // 必需
'service' => array( // 可选,各个服务的配置项
Expand All @@ -37,7 +37,7 @@ $sdk->getDrpService();
气球云为网校接入提供了测试环境,用于发调试。通过设置`host`配置项,即可使用测试环境的服务,例如短信测试服务:

```php
$sdk = new \QiQiuYun\SDK\QiQiuYunSDK(array(
$sdk = new \ESCloud\SDK\ESCloudSDK(array(
'access_key' => 'your_access_key',
'secret_key' => 'your_secret_key',
'service' => array(
Expand Down
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name" : "codeages/qiqiuyun-php-sdk",
"name" : "codeages/escloud-php-sdk",
"type" : "library",
"description" : "QiQiuYun PHP SDK",
"description" : "ESCloud PHP SDK",
"authors": [
{
"name": "Codeages Team",
Expand All @@ -12,18 +12,21 @@
"autoload" : {
"files": ["src/functions.php"],
"psr-4": {
"QiQiuYun\\SDK\\" : "src"
"ESCloud\\SDK\\" : "src"
}
},
"autoload-dev" : {
"psr-4": {
"QiQiuYun\\SDK\\Tests\\" : "tests"
"ESCloud\\SDK\\Tests\\" : "tests"
}
},
"require": {
"php": ">=5.3.3",
"ext-json": "*",
"ext-curl": "*",
"psr/log": "^1.0"
"psr/log": "^1.0",
"firebase/php-jwt": "^5.0",
"paragonie/random_compat": ">=2"
},
"require-dev": {
"phpunit/phpunit": "^4 || ^5 || ^6",
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
bootstrap = "vendor/autoload.php" >

<testsuites>
<testsuite name="QiQiuYun PHP SDK Test Suite">
<testsuite name="ESCloud PHP SDK Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>

</phpunit>
</phpunit>
97 changes: 39 additions & 58 deletions src/Auth.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
<?php

namespace QiQiuYun\SDK;
namespace ESCloud\SDK;

use QiQiuYun\SDK;
use ESCloud\SDK;
use Firebase\JWT\JWT;

class Auth
{
protected $accessKey;

protected $secretKey;

public function __construct($accessKey, $secretKey)
protected $useJwt;

/**
* Auth constructor.
* @param $accessKey
* @param $secretKey
* @param bool $useJwt 新的服务将启用 JWT 作为鉴权的 Token
*/
public function __construct($accessKey, $secretKey, $useJwt = false)
{
$this->accessKey = $accessKey;
$this->secretKey = $secretKey;
$this->useJwt = $useJwt;
}

public function getAccessKey()
Expand All @@ -32,26 +42,37 @@ public function makeSignature($text)
{
$signature = hash_hmac('sha1', $text, $this->secretKey, true);

return str_replace(array('+', '/'), array('-', '_'), base64_encode($signature));
return str_replace(array('+', '/'), array('-', '_'), base64_encode($signature));
}

/**
* 制作API请求的授权信息
*
* @param string $uri HTTP 请求的 URI
* @param string $body HTTP 请求的 BODY
* @param int $lifetime 授权生命周期
* @param bool $useNonce 授权随机值避免重放攻击
* @param string $uri HTTP 请求的 URI
* @param string $body HTTP 请求的 BODY
* @param int $lifetime 授权生命周期
* @param bool $useNonce 授权随机值避免重放攻击
*
* @return string 授权信息
*/
public function makeRequestAuthorization($uri, $body = '', $lifetime = 600, $useNonce = true)
{
$nonce = $useNonce ? SDK\random_str('16') : 'no';
$deadline = time() + $lifetime;
$signature = $this->makeSignature("{$nonce}\n{$deadline}\n{$uri}\n{$body}");
if ($this->useJwt) {
$payload = array(
'jti' => strtolower(Sdk\random_str(16)),
'exp' => time() + $lifetime,
);

$token = JWT::encode($payload, $this->secretKey, 'HS256', $this->accessKey);

return "Bearer {$token}";
} else {
$nonce = $useNonce ? SDK\random_str('16') : 'no';
$deadline = time() + $lifetime;
$signature = $this->makeSignature("{$nonce}\n{$deadline}\n{$uri}\n{$body}");

return "Signature {$this->accessKey}:{$deadline}:{$nonce}:{$signature}";
return "Signature {$this->accessKey}:{$deadline}:{$nonce}:{$signature}";
}
}

/**
Expand All @@ -60,61 +81,21 @@ public function makeRequestAuthorization($uri, $body = '', $lifetime = 600, $use
public function makeXAPIRequestAuthorization()
{
$deadline = strtotime(date('Y-m-d H:0:0', strtotime('+2 hours')));
$signingText = $this->getAccessKey()."\n".$deadline;
$signature = $this->getAccessKey().':'.$deadline.':'.$this->makeSignature($signingText);
$signingText = $this->getAccessKey() . "\n" . $deadline;
$signature = $this->getAccessKey() . ':' . $deadline . ':' . $this->makeSignature($signingText);

return "Signature $signature";
}

/**
* 生成资源播放令牌
*
* @param string $resNo 资源编号
* @param int $lifetime 令牌的的有效时长,默认600秒
* @param bool $useNonce 是否使用随机值,防止重放攻击
*
* @return string 资源播放Token
*/
public function makePlayToken($resNo, $lifetime = 600, $useNonce = true)
{
if ($useNonce) {
$nonce = SDK\random_str('16');
} else {
$nonce = 'no';
}

$deadline = time() + $lifetime;
$signingText = "{$resNo}\n{$nonce}\n{$deadline}";
$signature = $this->makeSignature($signingText);

return "{$nonce}:{$deadline}:{$signature}";
}

/**
* 生成资源播放令牌 V2
* 生成 Jwt Token
*
* @param string $resNo 资源编号
* @param array $options 附加的选项参数
* @param int $lifetime 令牌的的有效时长,默认600秒
* @param bool $useNonce 是否使用随机值,防止重放攻击
* @param array $payload 载荷
*
* @return string 资源播放Token
*/
public function makePlayToken2($resNo, $options = array(), $lifetime = 600, $useNonce = true)
public function makeJwtToken($payload = array())
{
if ($useNonce) {
$nonce = SDK\random_str('16');
} else {
$nonce = 'no';
}

ksort($options);
$options = http_build_query($options);

$deadline = time() + $lifetime;
$signingText = "{$resNo}\n{$options}\n{$deadline}\n{$nonce}";
$signature = $this->makeSignature($signingText);

return "{$deadline}:{$nonce}:{$signature}";
return JWT::encode($payload, $this->secretKey, 'HS256');
}
}
2 changes: 1 addition & 1 deletion src/Constants/NotificationChannelTypes.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace QiQiuYun\SDK\Constants;
namespace ESCloud\SDK\Constants;

final class NotificationChannelTypes
{
Expand Down
2 changes: 1 addition & 1 deletion src/Constants/WeChatPlatformTypes.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace QiQiuYun\SDK\Constants;
namespace ESCloud\SDK\Constants;

final class WeChatPlatformTypes
{
Expand Down
2 changes: 1 addition & 1 deletion src/Constants/XAPIActivityTypes.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace QiQiuYun\SDK\Constants;
namespace ESCloud\SDK\Constants;

final class XAPIActivityTypes
{
Expand Down
2 changes: 1 addition & 1 deletion src/Constants/XAPIObjectTypes.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace QiQiuYun\SDK\Constants;
namespace ESCloud\SDK\Constants;

final class XAPIObjectTypes
{
Expand Down
2 changes: 1 addition & 1 deletion src/Constants/XAPIVerbs.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace QiQiuYun\SDK\Constants;
namespace ESCloud\SDK\Constants;

final class XAPIVerbs
{
Expand Down
Loading