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

Feature/s2b2c service #41

Open
wants to merge 11 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
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
},
"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
93 changes: 37 additions & 56 deletions src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,27 @@
namespace QiQiuYun\SDK;

use QiQiuYun\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');
}
}
60 changes: 32 additions & 28 deletions src/QiQiuYunSDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Psr\Log\LoggerInterface;
use QiQiuYun\SDK\HttpClient\ClientInterface;
use QiQiuYun\SDK\Exception\SDKException;
use QiQiuYun\SDK\Service\NotificationService;

class QiQiuYunSDK
{
Expand All @@ -19,6 +18,14 @@ class QiQiuYunSDK

protected $httpClient;

/**
* QiQiuYunSDK constructor.
*
* @param array $options
* @param LoggerInterface|null $logger
* @param ClientInterface|null $httpClient
* @throws SDKException
*/
public function __construct(array $options, LoggerInterface $logger = null, ClientInterface $httpClient = null)
{
if (empty($options['access_key'])) {
Expand All @@ -29,11 +36,20 @@ public function __construct(array $options, LoggerInterface $logger = null, Clie
}

$this->options = $options;
$this->auth = $this->createAuth($options['access_key'], $options['secret_key']);
$this->logger = $logger;
$this->httpClient = $httpClient;
}

/**
* 获取云资源播放服务
*
* @return \QiQiuYun\SDK\Service\ResourceService
*/
public function getResourceService()
{
return $this->getService('Resource', true);
}

/**
* 获取短信服务
*
Expand All @@ -54,16 +70,6 @@ public function getPlayService()
return $this->getService('Play');
}

/**
* 获取云资源播放服务
*
* @return \QiQiuYun\SDK\Service\PlayV2Service
*/
public function getPlayV2Service()
{
return $this->getService('PlayV2');
}

/**
* 获取XAPI服务
*
Expand Down Expand Up @@ -117,29 +123,27 @@ public function getPushService()
}

/**
* @return NotificationService
* @return \QiQiuYun\SDK\Service\S2B2CService
*/
public function getNotificationService()
public function getS2B2CService()
{
return $this->getService('Notification');
return $this->getService('S2B2C');
}

public function getWeChatService()
/**
* @return \QiQiuYun\SDK\Service\NotificationService
*/
public function getNotificationService()
{
return $this->getService('WeChat');
return $this->getService('Notification');
}

/**
* 创建API请求认证类实例
*
* @param string $accessKey
* @param string $secretKey
*
* @return Auth
* @return \QiQiuYun\SDK\Service\WeChatService
*/
public function createAuth($accessKey, $secretKey)
public function getWeChatService()
{
return new Auth($accessKey, $secretKey);
return $this->getService('WeChat');
}

/**
Expand All @@ -149,7 +153,7 @@ public function createAuth($accessKey, $secretKey)
*
* @return mixed 服务实例
*/
protected function getService($name)
protected function getService($name, $useJwt = false)
{
if (isset($this->services[$name])) {
return $this->services[$name];
Expand All @@ -159,8 +163,8 @@ protected function getService($name)
$options = empty($this->options['service'][$lowerName]) ? array() : $this->options['service'][$lowerName];

$class = __NAMESPACE__.'\\Service\\'.$name.'Service';

$this->services[$name] = new $class($this->auth, $options, $this->logger, $this->httpClient);
$auth = new Auth($this->options['access_key'], $this->options['secret_key'], $useJwt);
$this->services[$name] = new $class($auth, $options, $this->logger, $this->httpClient);

return $this->services[$name];
}
Expand Down
28 changes: 17 additions & 11 deletions src/Service/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ abstract class BaseService
*/
protected $logger;

private static $resolversByClass = array();

public function __construct(Auth $auth, array $options = array(), LoggerInterface $logger = null, ClientInterface $client = null)
{
$this->auth = $auth;
Expand Down Expand Up @@ -88,12 +86,15 @@ protected function createClient()
/**
* 气球云 API V2 的统一请求方法
*
* @param string $method
* @param string $uri
* @param array $data
* @param array $headers
*
* @return array
* @param $method
* @param $uri
* @param array $data
* @param array $headers
* @param string $node
* @return mixed
* @throws ResponseException
* @throws SDKException
* @throws SDK\HttpClient\ClientException
*/
protected function request($method, $uri, array $data = array(), array $headers = array(), $node = 'root')
{
Expand Down Expand Up @@ -131,6 +132,9 @@ protected function request($method, $uri, array $data = array(), array $headers
* 从Response中抽取API返回结果
*
* @param Response $response
* @return mixed
* @throws ResponseException
* @throws SDKException
*/
protected function extractResultFromResponse(Response $response)
{
Expand All @@ -153,9 +157,11 @@ protected function extractResultFromResponse(Response $response)
/**
* 获得完整的请求地址
*
* @param string $uri
*
* @return string 请求地址
* @param $uri
* @param string $protocol
* @param string $node
* @return string
* @throws SDKException
*/
protected function getRequestUri($uri, $protocol = 'http', $node = 'root')
{
Expand Down
Loading