Skip to content

Commit

Permalink
Merge pull request #17 from srako/pr
Browse files Browse the repository at this point in the history
阿里云微服务引擎Nacos鉴权支持
  • Loading branch information
Tinywan authored Nov 10, 2023
2 parents 64e5bb3 + 3d4f6b5 commit 64701cd
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 21 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"guzzlehttp/promises": "^1.5",
"guzzlehttp/guzzle": "^7.4",
"workerman/http-client": "^1.0",
"monolog/monolog": "^2.8"
"monolog/monolog": "^2.8",
"ext-mbstring": "*"
},
"require-dev": {
"workerman/webman-framework": "^1.3.0",
Expand Down
20 changes: 11 additions & 9 deletions src/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ abstract class AbstractProvider
/** @var string|null */
protected ?string $password = null;

/** @var string|null */
protected ?string $accessKeyId = null;

/** @var string|null */
protected ?string $accessKeySecret = null;

/**
* AbstractProvider constructor.
* @param NacosClient $client
Expand All @@ -76,6 +82,8 @@ public function __construct(NacosClient $client, ?array $config = null)
isset($config['port']) && $this->port = (int) $config['port'];
isset($config['username']) && $this->username = (string) $config['username'];
isset($config['password']) && $this->password = (string) $config['password'];
isset($config['access_key_id']) && $this->accessKeyId = (string) $config['access_key_id'];
isset($config['access_key_secret']) && $this->accessKeySecret = (string) $config['access_key_secret'];
}

/**
Expand Down Expand Up @@ -138,9 +146,7 @@ public function httpClientAsync(): AsyncClient
public function request(string $method, string $uri, array $options = [])
{
try {
if($token = $this->issueToken()){
$options[RequestOptions::QUERY]['accessToken'] = $token;
}
$this->issueToken($options);
$response = $this->httpClient()->request($method, $uri, $options);
} catch (RequestException $exception) {
if ($exception->hasResponse()) {
Expand All @@ -164,9 +170,7 @@ public function request(string $method, string $uri, array $options = [])
public function requestAsync(string $method, string $uri, array $options = [])
{
try {
if($token = $this->issueToken()){
$options[RequestOptions::QUERY]['accessToken'] = $token;
}
$this->issueToken($options);
return $this->httpClient()->requestAsync($method, $uri, $options);
} catch (RequestException $exception) {
if ($exception->hasResponse()) {
Expand Down Expand Up @@ -196,9 +200,7 @@ public function requestAsyncUseEventLoop(string $method, string $uri, array $opt
{
try {
# 同步阻塞获取token
if($token = $this->issueToken()){
$options[RequestOptions::QUERY]['accessToken'] = $token;
}
$this->issueToken($options);
$queryString = http_build_query($options[RequestOptions::QUERY] ?? []);
$headers = array_merge($options[RequestOptions::HEADERS] ?? [], [
'Connection' => 'keep-alive'
Expand Down
71 changes: 64 additions & 7 deletions src/Traits/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Workbunny\WebmanNacos\Traits;

use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;

/**
* Trait Authentication
Expand All @@ -33,17 +34,17 @@ trait Authentication

/**
* 获取token
* @return string|null
* @return void
* @throws GuzzleException
*/
public function issueToken(): ?string
public function issueToken(array &$options = [])
{
if ($this->username === null || $this->password === null) {
return null;
$this->mseAuth($options);
return;
}

if (!$this->isExpired()) {
return $this->accessToken;
return;
}

$result = $this->handleResponse(
Expand All @@ -52,8 +53,7 @@ public function issueToken(): ?string

$this->accessToken = $result['accessToken'];
$this->expireTime = $result['tokenTtl'] + time();

return $this->accessToken;
$options[RequestOptions::QUERY]['accessToken'] = $this->accessToken;
}

/**
Expand All @@ -67,4 +67,61 @@ protected function isExpired(): bool
}
return true;
}

/**
* 阿里云微服务引擎MSE鉴权
* @param array $options
* @return void
*/
protected function mseAuth(array &$options = [])
{
if ($this->accessKeyId === null || $this->accessKeySecret === null) {
return;
}

$paramsToSign = $options[RequestOptions::QUERY] ?? $options[RequestOptions::FORM_PARAMS] ?? [];

$signStr = '';
$millisecondTs = (int)(microtime(true) * 1000);


// config signature
if (isset($paramsToSign['tenant'])&&$paramsToSign['tenant']) {
$signStr .= $paramsToSign['tenant'] . '+';
}
if (isset($paramsToSign['group'])&&$paramsToSign['group']) {
$signStr .= $paramsToSign['group'] . '+';
}
$signStr .= $millisecondTs;


// naming signature
if (isset($paramsToSign['serviceName'])) {
$signStr = $millisecondTs;
if (mb_strpos($paramsToSign['serviceName'], '@@') !== false
|| !isset($paramsToSign['groupName'])
|| $paramsToSign['groupName'] == '') {
$signStr .= '@@' . $paramsToSign['serviceName'];
} else {
$signStr .= '@@' . $paramsToSign['groupName'] . '@@' . $paramsToSign['serviceName'];
}
}

// 签名
$signature = base64_encode(hash_hmac('sha1', $signStr, $this->accessKeySecret, true));

// config增加header
$options[RequestOptions::HEADERS] = [
'timeStamp' => $millisecondTs,
'Spas-AccessKey' => $this->accessKeyId,
'Spas-Signature' => $signature,
] + ($options[RequestOptions::HEADERS] ?? []);

// naming增加query
$options[RequestOptions::QUERY] = [
'data' => $signStr,
'ak' => $this->accessKeyId,
'signature' => $signature,
] + ($options[RequestOptions::QUERY] ?? []);
}
}
8 changes: 6 additions & 2 deletions src/config/plugin/workbunny/webman-nacos/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

'host' => '127.0.0.1',
'port' => 8848,
'username' => '',
'password' => '',
'username' => null,
'password' => null,

// 阿里云微服务引擎MSE
'access_key_id' => null,
'access_key_secret' => null,

/** 长轮询等待时长 毫秒 @desc 当长轮询间隔不存在时,该项作为默认值使用,其余时间则不生效 */
'long_pulling_timeout' => 30000,
Expand Down
8 changes: 6 additions & 2 deletions src/config/plugin/workbunny/webman-nacos/channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
'default' => [
'host' => '127.0.0.1',
'port' => 8848,
'username' => '',
'password' => '',
'username' => null,
'password' => null,

// 阿里云微服务引擎MSE
'access_key_id' => null,
'access_key_secret' => null,
],
];

0 comments on commit 64701cd

Please sign in to comment.