|
|
<?php
|
|
|
|
|
|
namespace tencent\sts\credentials;
|
|
|
|
|
|
use TencentCloud\Common\Credential;
|
|
|
use TencentCloud\Common\Profile\ClientProfile;
|
|
|
use TencentCloud\Common\Profile\HttpProfile;
|
|
|
use TencentCloud\Common\Exception\TencentCloudSDKException;
|
|
|
use TencentCloud\Sts\V20180813\StsClient;
|
|
|
use TencentCloud\Sts\V20180813\Models\GetFederationTokenRequest;
|
|
|
use tencent\sts\Base;
|
|
|
|
|
|
class Credentials extends Base
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
* 获取联合身份临时访问凭证
|
|
|
* @param string $name 自定义调用方英文名称,由字母组成
|
|
|
* @param string $policy 授予该临时证书权限的CAM策略
|
|
|
* @date 2022-11-29
|
|
|
*/
|
|
|
public function getFederationToken($name, $policy)
|
|
|
{
|
|
|
|
|
|
try {
|
|
|
// 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
|
|
|
$cred = new Credential($this->config['secret_id'], $this->config['secret_key']);
|
|
|
// 实例化一个http选项,可选的,没有特殊需求可以跳过
|
|
|
$httpProfile = new HttpProfile();
|
|
|
$httpProfile->setEndpoint("sts.tencentcloudapi.com");
|
|
|
|
|
|
// 实例化一个client选项,可选的,没有特殊需求可以跳过
|
|
|
$clientProfile = new ClientProfile();
|
|
|
$clientProfile->setHttpProfile($httpProfile);
|
|
|
// 实例化要请求产品的client对象,clientProfile是可选的
|
|
|
$client = new StsClient($cred, $this->config['region'], $clientProfile);
|
|
|
|
|
|
// 实例化一个请求对象,每个接口都会对应一个request对象
|
|
|
$req = new GetFederationTokenRequest();
|
|
|
|
|
|
$params = [
|
|
|
"Name" => $name,
|
|
|
"Policy" => urlencode(json_encode($policy))
|
|
|
];
|
|
|
$req->fromJsonString(json_encode($params));
|
|
|
|
|
|
// 返回的resp是一个GetFederationTokenResponse的实例,与请求对象对应
|
|
|
$resp = $client->GetFederationToken($req);
|
|
|
// 输出json格式的字符串回包
|
|
|
$resp = $resp->toJsonString();
|
|
|
|
|
|
$result = json_decode($resp, true);
|
|
|
|
|
|
// 记录日志
|
|
|
$uid = defined('UID') ? UID : '';
|
|
|
platformLog([
|
|
|
"Name" => $name,
|
|
|
"Policy" => $policy
|
|
|
], $result, 'tencent_sts_get_federation_token_uid_' . $uid);
|
|
|
|
|
|
return $result;
|
|
|
} catch (TencentCloudSDKException $e) {
|
|
|
echo $e;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|