Merge branch 'develop' of https://gitee.com/Tyssen/yi-shi into develop

This commit is contained in:
柳清爽
2025-03-24 16:28:33 +08:00
718 changed files with 90340 additions and 26157 deletions

View File

@@ -124,7 +124,7 @@
<!-- AI智能获客 版块标题 -->
<view class="section-title">
<u-icon name="robot" size="28" color="#4080ff"></u-icon>
<image src="/static/images/icons/robot.svg" class="robot-icon"></image>
<text>AI智能获客</text>
<text class="beta-tag">Beta</text>
</view>
@@ -480,7 +480,9 @@ export default {
padding: 30rpx 20rpx 15rpx;
margin: 15rpx 7.5rpx 5rpx;
u-icon {
.robot-icon {
width: 56rpx;
height: 56rpx;
margin-right: 10rpx;
}

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg width="28" height="28" viewBox="0 0 28 28" fill="none" xmlns="http://www.w3.org/2000/svg">
<!-- 机器人头部 -->
<rect x="8" y="4" width="12" height="14" rx="2" stroke="#4080ff" stroke-width="2" fill="none"/>
<!-- 天线 -->
<path d="M14 2L14 4" stroke="#4080ff" stroke-width="2" stroke-linecap="round"/>
<!-- 眼睛 -->
<circle cx="11" cy="9" r="1.5" fill="#4080ff"/>
<circle cx="17" cy="9" r="1.5" fill="#4080ff"/>
<!-- 显示屏/嘴巴 -->
<rect x="10" y="12" width="8" height="3" rx="1" stroke="#4080ff" stroke-width="1.5" fill="none"/>
<!-- 机器人身体 -->
<rect x="6" y="18" width="16" height="6" rx="2" stroke="#4080ff" stroke-width="2" fill="none"/>
<!-- 按钮/指示灯 -->
<circle cx="11" cy="21" r="1" fill="#4080ff"/>
<circle cx="14" cy="21" r="1" fill="#4080ff"/>
<circle cx="17" cy="21" r="1" fill="#4080ff"/>
</svg>

After

Width:  |  Height:  |  Size: 913 B

147
Server/README_moments.md Normal file
View File

@@ -0,0 +1,147 @@
# 微信朋友圈数据处理功能
本模块提供了微信朋友圈数据的获取、存储和查询功能,支持保留驼峰命名结构的原始数据。
## 数据库表结构
项目包含一个数据表:
**wechat_moments** - 存储朋友圈基本信息
- `id`: 自增主键
- `wechatAccountId`: 微信账号ID
- `wechatFriendId`: 微信好友ID
- `snsId`: 朋友圈消息ID
- `commentList`: 评论列表JSON
- `createTime`: 创建时间戳
- `likeList`: 点赞列表JSON
- `content`: 朋友圈内容
- `lat`: 纬度
- `lng`: 经度
- `location`: 位置信息
- `picSize`: 图片大小
- `resUrls`: 资源URL列表
- `userName`: 用户名
- `type`: 朋友圈类型
- `create_time`: 数据创建时间
- `update_time`: 数据更新时间
## API接口
### 1. 获取朋友圈信息
```
GET/POST /api/websocket/getMoments
```
**参数:**
- `wechatAccountId`: 微信账号ID
- `wechatFriendId`: 微信好友ID
- `count`: 获取条数默认5条
获取指定账号和好友的朋友圈信息,并自动保存到数据库。
### 2. 保存单条朋友圈数据
```
POST /api/websocket/saveSingleMoment
```
**参数:**
- `commentList`: 评论列表
- `createTime`: 创建时间戳
- `likeList`: 点赞列表
- `momentEntity`: 朋友圈实体,包含以下字段:
- `content`: 朋友圈内容
- `lat`: 纬度
- `lng`: 经度
- `location`: 位置信息
- `picSize`: 图片大小
- `resUrls`: 资源URL列表
- `urls`: 媒体URL列表
- `userName`: 用户名
- `snsId`: 朋友圈ID
- `type`: 朋友圈类型
- `wechatAccountId`: 微信账号ID
- `wechatFriendId`: 微信好友ID
保存单条朋友圈数据到数据库,保持原有的驼峰数据结构。系统会将`momentEntity`中的字段提取并单独存储,不包括`objectType``createTime`字段。
### 3. 获取朋友圈数据列表
```
GET/POST /api/websocket/getMomentsList
```
**参数:**
- `wechatAccountId`: 微信账号ID (可选)
- `wechatFriendId`: 微信好友ID (可选)
- `page`: 页码默认1
- `pageSize`: 每页条数默认10
- `startTime`: 开始时间戳 (可选)
- `endTime`: 结束时间戳 (可选)
获取已保存的朋友圈数据列表,支持分页和条件筛选。返回的数据会自动构建`momentEntity`字段以保持API兼容性。
### 4. 获取朋友圈详情
```
GET/POST /api/websocket/getMomentDetail
```
**参数:**
- `snsId`: 朋友圈ID
- `wechatAccountId`: 微信账号ID
获取单条朋友圈的详细信息包括评论、点赞和资源URL等。返回的数据会自动构建`momentEntity`字段以保持API兼容性。
## 使用示例
### 保存单条朋友圈数据
```php
$data = [
'commentList' => [],
'createTime' => 1742777232,
'likeList' => [],
'momentEntity' => [
'content' => "第一位个人与Stussy联名的中国名人不是陈冠希不是葛民辉而是周杰伦",
'lat' => 0,
'lng' => 0,
'location' => "",
'picSize' => 0,
'resUrls' => [],
'snsId' => "-3827269039168736643",
'urls' => ["http://wxapp.tc.qq.com/251/20304/stodownload?encfilekey=..."],
'userName' => "wxid_afixeeh53lt012"
],
'snsId' => "-3827269039168736643",
'type' => 28,
'wechatAccountId' => 123456, // 替换为实际的微信账号ID
'wechatFriendId' => "wxid_example" // 替换为实际的微信好友ID
];
// 发送请求
$result = curl_post('/api/websocket/saveSingleMoment', $data);
```
### 查询朋友圈列表
```php
// 获取特定账号的朋友圈
$params = [
'wechatAccountId' => 123456,
'page' => 1,
'pageSize' => 20
];
// 发送请求
$result = curl_get('/api/websocket/getMomentsList', $params);
```
## 注意事项
1. 所有JSON格式的数据在保存时都会进行编码查询时会自动解码并还原为原始数据结构。
2. 数据库中的字段名保持驼峰命名格式与微信API返回的数据结构保持一致。
3. 尽管数据库中将`momentEntity`的字段拆分为独立字段存储但API接口返回时会重新构建`momentEntity`结构以保持与原始API的兼容性。
4. `objectType``createTime`字段已从`momentEntity`中移除,不再单独存储。
5. 图片或视频资源URLs直接存储在朋友圈主表中不再单独存储到资源表。

View File

@@ -50,6 +50,142 @@ class AccountController extends BaseController
}
}
/**
* 创建部门
* @return \think\response\Json
*/
public function createDepartment()
{
// 获取授权token
$authorization = trim($this->request->header('authorization', ''));
if (empty($authorization)) {
return errorJson('缺少授权信息');
}
try {
// 获取请求参数
$name = $this->request->param('name', '');
$memo = $this->request->param('memo', '');
if (empty($name)) {
return errorJson('请输入公司名称');
}
// 参数验证
if (empty($name)) {
return errorJson('部门名称不能为空');
}
// 构建请求参数设置固定的departmentIdArr和parentId
$params = [
'name' => $name,
'memo' => $memo,
'departmentIdArr' => [914],
'parentId' => 914
];
// 设置请求头
$headerData = ['client:system'];
$header = setHeader($headerData, $authorization, 'json');
// 发送请求创建部门
$result = requestCurl($this->baseUrl . 'api/Department/createDepartment', $params, 'POST', $header,'json');
// 尝试提取部门ID
if (is_int($result)) {
return successJson($result);
}else{
return errorJson($result);
}
} catch (\Exception $e) {
return response('创建部门失败:' . $e->getMessage());
}
}
/**
* 创建新账号
* @return \think\response\Json
*/
public function createAccount()
{
// 获取授权token
$authorization = trim($this->request->header('authorization', ''));
if (empty($authorization)) {
return errorJson('缺少授权信息');
}
try {
// 获取请求参数
$userName = $this->request->param('userName', '');
$password = $this->request->param('password', '');
$realName = $this->request->param('realName', '');
$nickname = $this->request->param('nickname', '');
$memo = $this->request->param('memo', '');
$departmentId = $this->request->param('departmentId', 0);
// 用户名验证
if (empty($userName)) {
return errorJson('用户名不能为空');
}
// 自定义用户名验证:只能使用英文字母或数字
if (!preg_match('/^[a-zA-Z][a-zA-Z0-9]{5,9}$/', $userName)) {
return errorJson('用户名必须以字母开头只能包含字母和数字长度6-10位');
}
// 密码验证
if (empty($password)) {
return errorJson('密码不能为空');
}
// 使用validateString验证密码添加自定义选项
$passwordValidation = validateString($password, 'password');
if (!$passwordValidation['status']) {
return errorJson($passwordValidation['message']);
}
// 真实姓名验证
if (empty($realName)) {
return errorJson('真实姓名不能为空');
}
// 部门ID验证
if (empty($departmentId)) {
return errorJson('部门ID不能为空');
}
// 构建请求参数
$params = [
'userName' => $userName,
'password' => $password,
'realName' => $realName,
'nickname' => $nickname,
'memo' => $memo,
'departmentId' => $departmentId,
'departmentIdArr' => empty($departmentId) ? [914] : [914, $departmentId]
];
// 设置请求头
$headerData = ['client:system'];
$header = setHeader($headerData, $authorization, 'json');
// 发送请求创建账号
$result = requestCurl($this->baseUrl . 'api/account/newAccount', $params, 'POST', $header, 'json');
if (is_int($result)) {
return successJson($result);
}else{
return errorJson($result);
}
} catch (\Exception $e) {
return errorJson('创建账号失败:' . $e->getMessage());
}
}
/**
* 保存账号数据到数据库
* @param array $item 账号数据

View File

@@ -14,10 +14,10 @@ class DeviceController extends BaseController
* 获取设备列表
* @return \think\response\Json
*/
public function getlist()
public function getlist($pageIndex = '',$pageSize = '',$authorization = '')
{
// 获取授权token
$authorization = trim($this->request->header('authorization', ''));
$authorization = !empty($authorization) ? $authorization : trim($this->request->header('authorization', ''));
if (empty($authorization)) {
return errorJson('缺少授权信息');
}
@@ -42,8 +42,8 @@ class DeviceController extends BaseController
'alive' => $this->request->param('alive', ''),
'hasWechat' => $this->request->param('hasWechat', ''),
'departmentId' => $this->request->param('departmentId', ''),
'pageIndex' => $this->request->param('pageIndex', 0),
'pageSize' => $this->request->param('pageSize', 20)
'pageIndex' => !empty($pageIndex) ? $pageIndex : $this->request->param('pageIndex', 0),
'pageSize' => !empty($pageSize) ? $pageSize : $this->request->param('pageSize', 20)
];
// 设置请求头

View File

@@ -50,6 +50,58 @@ class FriendTaskController extends BaseController
}
}
/**
* 添加好友任务
* @return \think\response\Json
*/
public function addFriendTask()
{
// 获取授权token
$authorization = trim($this->request->header('authorization', ''));
if (empty($authorization)) {
return errorJson('缺少授权信息');
}
try {
// 获取请求参数
$phone = $this->request->param('phone', '');
$message = $this->request->param('message', '');
$remark = $this->request->param('remark', '');
$labels = $this->request->param('labels', []);
$wechatAccountId = $this->request->param('wechatAccountId', 0);
// 参数验证
if (empty($phone)) {
return errorJson('手机号不能为空');
}
if (empty($wechatAccountId)) {
return errorJson('微信号不能为空');
}
// 构建请求参数
$params = [
'phone' => $phone,
'message' => $message,
'remark' => $remark,
'labels' => is_array($labels) ? $labels : [$labels],
'wechatAccountId' => (int)$wechatAccountId
];
// 设置请求头
$headerData = ['client:system'];
$header = setHeader($headerData, $authorization, 'json');
// 发送请求添加好友任务
$result = requestCurl($this->baseUrl . 'api/AddFriendByPhoneTask/add', $params, 'POST', $header, 'json');
// 处理响应
return successJson([], '添加好友任务创建成功');
} catch (\Exception $e) {
return errorJson('添加好友任务失败:' . $e->getMessage());
}
}
/**
* 保存添加好友记录到数据库
* @param array $item 添加好友记录数据

View File

@@ -0,0 +1,157 @@
<?php
namespace app\api\controller;
use think\facade\Request;
class MomentsController extends BaseController
{
/**
* 发布朋友圈
* @return \think\response\Json
*/
public function addJob()
{
// 获取授权token
$authorization = trim($this->request->header('authorization', ''));
if (empty($authorization)) {
return errorJson('缺少授权信息');
}
try {
// 获取请求参数
$text = $this->request->param('text', ''); // 朋友圈文本内容
$picUrlList = $this->request->param('picUrlList', []); // 图片URL列表
$videoUrl = $this->request->param('videoUrl', ''); // 视频URL
$immediately = $this->request->param('immediately', true); // 是否立即发布
$timingTime = $this->request->param('timingTime', ''); // 定时发布时间
$beginTime = $this->request->param('beginTime', ''); // 开始时间
$endTime = $this->request->param('endTime', ''); // 结束时间
$isUseLocation = $this->request->param('isUseLocation', false); // 是否使用位置信息
$poiName = $this->request->param('poiName', ''); // 位置名称
$poiAddress = $this->request->param('poiAddress', ''); // 位置地址
$lat = $this->request->param('lat', 0); // 纬度
$lng = $this->request->param('lng', 0); // 经度
$momentContentType = $this->request->param('momentContentType', 1); // 朋友圈内容类型
$publicMode = $this->request->param('publicMode', 0); // 发布模式
$altList = $this->request->param('altList', ''); // 替代列表
$link = $this->request->param('link', []); // 链接信息
$jobPublishWechatMomentsItems = $this->request->param('jobPublishWechatMomentsItems', []); // 发布账号和评论信息
// 必填参数验证
if (empty($jobPublishWechatMomentsItems) || !is_array($jobPublishWechatMomentsItems)) {
return errorJson('至少需要选择一个发布账号');
}
// 根据朋友圈类型验证必填字段
if ($momentContentType == 1 && empty($text)) { // 纯文本
return errorJson('朋友圈内容不能为空');
} else if ($momentContentType == 2 && (empty($picUrlList) || empty($text))) { // 图片+文字
return errorJson('朋友圈内容和图片不能为空');
} else if ($momentContentType == 3 && (empty($videoUrl) || empty($text))) { // 视频+文字
return errorJson('朋友圈内容和视频不能为空');
} else if ($momentContentType == 4 && (empty($link) || empty($text))) { // 链接+文字
return errorJson('朋友圈内容和链接不能为空');
}
// 构建请求参数
$params = [
'text' => $text,
'picUrlList' => $picUrlList,
'videoUrl' => $videoUrl,
'immediately' => $immediately,
'timingTime' => $timingTime,
'beginTime' => $beginTime,
'endTime' => $endTime,
'isUseLocation' => $isUseLocation,
'poiName' => $poiName,
'poiAddress' => $poiAddress,
'lat' => $lat,
'lng' => $lng,
'momentContentType' => (int)$momentContentType,
'publicMode' => (int)$publicMode,
'altList' => $altList,
'link' => $link,
'jobPublishWechatMomentsItems' => $jobPublishWechatMomentsItems
];
// 设置请求头
$headerData = ['client:system'];
$header = setHeader($headerData, $authorization, 'json');
// 发送请求发布朋友圈
$result = requestCurl($this->baseUrl . 'api/JobPublishWechatMoments/addJob', $params, 'POST', $header, 'json');
// 处理响应
if (is_numeric($result)) {
return successJson(['jobId' => $result], '朋友圈任务创建成功');
} else {
// 尝试解析JSON
$response = json_decode($result, true);
if (json_last_error() === JSON_ERROR_NONE && isset($response['id'])) {
return successJson(['jobId' => $response['id']], '朋友圈任务创建成功');
}
// 如果返回的是错误信息
return errorJson(is_string($result) ? $result : '创建朋友圈任务失败');
}
} catch (\Exception $e) {
return errorJson('发布朋友圈失败:' . $e->getMessage());
}
}
/**
* 获取朋友圈任务列表
* @return \think\response\Json
*/
public function getList()
{
// 获取授权token
$authorization = trim($this->request->header('authorization', ''));
if (empty($authorization)) {
return errorJson('缺少授权信息');
}
try {
// 获取请求参数
$keyword = $this->request->param('keyword', ''); // 关键词搜索
$jobStatus = $this->request->param('jobStatus', ''); // 任务状态筛选
$contentType = $this->request->param('contentType', ''); // 内容类型筛选
$only = $this->request->param('only', 'false'); // 是否只查看自己的
$pageIndex = $this->request->param('pageIndex', 0); // 当前页码
$pageSize = $this->request->param('pageSize', 10); // 每页数量
$from = $this->request->param('from', ''); // 开始日期
$to = $this->request->param('to', ''); // 结束日期
// 构建请求参数
$params = [
'keyword' => $keyword,
'jobStatus' => $jobStatus,
'contentType' => $contentType,
'only' => $only,
'pageIndex' => (int)$pageIndex,
'pageSize' => (int)$pageSize
];
// 添加日期筛选条件(如果有)
if (!empty($from)) {
$params['from'] = $from;
}
if (!empty($to)) {
$params['to'] = $to;
}
// 设置请求头
$headerData = ['client:system'];
$header = setHeader($headerData, $authorization, 'json');
// 发送请求获取朋友圈任务列表
$result = requestCurl($this->baseUrl . 'api/JobPublishWechatMoments/listPagination', $params, 'GET', $header, 'json');
$response = handleApiResponse($result);
return successJson($response);
} catch (\Exception $e) {
return errorJson('获取朋友圈任务列表失败:' . $e->getMessage());
}
}
}

View File

@@ -0,0 +1,410 @@
<?php
namespace app\api\controller;
use think\cache\driver\Redis;
use think\Db;
use think\Log;
use WebSocket\Client;
class WebSocketController extends BaseController
{
protected $authorized;
protected $accountId;
protected $client;
public function __construct()
{
parent::__construct();
$this->authorized = $this->request->header('authorized', '');
$this->accountId = $this->request->param('accountId', '');
if (empty($this->authorized) || empty($this->accountId)) {
$data['authorized'] = $this->authorized;
$data['accountId'] = $this->accountId;
$this->error('缺失关键参数', $data);
}
//证书
$context = stream_context_create();
stream_context_set_option($context, 'ssl', 'verify_peer', false);
stream_context_set_option($context, 'ssl', 'verify_peer_name', false);
//开启WS链接
$result = [
"accessToken" => $this->authorized,
"accountId" => $this->accountId,
"client" => "kefu-client",
"cmdType" => "CmdSignIn",
"seq" => 1,
];
$content = json_encode($result);
$this->client = new Client("wss://kf.quwanzhi.com:9993",
[
'filter' => ['text', 'binary', 'ping', 'pong', 'close','receive', 'send'],
'context' => $context,
'headers' => [
'Sec-WebSocket-Protocol' => 'soap',
'origin' => 'localhost',
],
'timeout' => 86400,
]
);
$this->client->send($content);
}
/**
* 个人消息发送
*/
public function sendPersonal()
{
if ($this->request->isPost()) {
$data = $this->request->param();
if (empty($data)) {
$this->error('参数缺失');
}
$dataArray = $data;
if (!is_array($dataArray)) {
$this->error('数据格式错误');
}
//过滤消息
if (empty($dataArray['content'])) {
$this->error('内容缺失');
}
if (empty($dataArray['wechatAccountId'])) {
$this->error('微信id不能为空');
}
if (empty($dataArray['wechatFriendId'])) {
$this->error('接收人不能为空');
}
if (empty($dataArray['msgType'])) {
$this->error('类型缺失');
}
//消息拼接 msgType(1:文本 3:图片 43:视频 47:动图表情包 49:小程序)
$result = [
"cmdType" => "CmdSendMessage",
"content" => $dataArray['content'],
"msgSubType" => 0,
"msgType" => $dataArray['msgType'],
"seq" => time(),
"wechatAccountId" => $dataArray['wechatAccountId'],
"wechatChatroomId" => 0,
"wechatFriendId" => $dataArray['wechatFriendId'],
];
$result = json_encode($result);
$this->client->send($result);
$message = $this->client->receive();
//关闭WS链接
$this->client->close();
Log::write('WS个人消息发送');
$this->success('消息成功发送', json_decode($message, 1), 200);
} else {
$this->error('非法请求');
}
}
/**
* 发送群消息
*/
public function sendCommunity()
{
if ($this->request->isPost()) {
$data = $this->request->post();
if (empty($data)) {
$this->error('参数缺失');
}
$dataArray = $data;
if (!is_array($dataArray)) {
$this->error('数据格式错误');
}
//过滤消息
if (empty($dataArray['content'])) {
$this->error('内容缺失');
}
if (empty($dataArray['wechatAccountId'])) {
$this->error('微信id不能为空');
}
if (empty($dataArray['msgType'])) {
$this->error('类型缺失');
}
if (empty($dataArray['wechatChatroomId'])) {
$this->error('群id不能为空');
}
$msg = '消息成功发送';
$message = [];
try {
//消息拼接 msgType(1:文本 3:图片 43:视频 47:动图表情包 49:小程序)
$result = [
"cmdType" => "CmdSendMessage",
"content" => htmlspecialchars_decode($dataArray['content']),
"msgSubType" => 0,
"msgType" => $dataArray['msgType'],
"seq" => time(),
"wechatAccountId" => $dataArray['wechatAccountId'],
"wechatChatroomId" => $dataArray['wechatChatroomId'],
"wechatFriendId" => 0,
];
$result = json_encode($result);
$this->client->send($result);
$message = $this->client->receive();
//关闭WS链接
$this->client->close();
Log::write('WS群消息发送');
Log::write($message);
$message = json_decode($message, 1);
} catch (\Exception $e) {
$msg = $e->getMessage();
}
$this->success($msg, $message, 200);
} else {
$this->error('非法请求');
}
}
/**
* 发送群消息
*/
public function sendCommunitys($data = [])
{
if (empty($data)) {
$this->error('参数缺失');
}
$dataArray = $data;
if (!is_array($dataArray)) {
$this->error('数据格式错误');
}
//过滤消息
if (empty($dataArray['content'])) {
$this->error('内容缺失');
}
if (empty($dataArray['wechatAccountId'])) {
$this->error('微信id不能为空');
}
if (empty($dataArray['msgType'])) {
$this->error('类型缺失');
}
if (empty($dataArray['wechatChatroomId'])) {
$this->error('群id不能为空');
}
$msg = '消息成功发送';
$message = [];
try {
//消息拼接 msgType(1:文本 3:图片 43:视频 47:动图表情包 49:小程序)
$result = [
"cmdType" => "CmdSendMessage",
"content" => $dataArray['content'],
"msgSubType" => 0,
"msgType" => $dataArray['msgType'],
"seq" => time(),
"wechatAccountId" => $dataArray['wechatAccountId'],
"wechatChatroomId" => $dataArray['wechatChatroomId'],
"wechatFriendId" => 0,
];
$result = json_encode($result);
$this->client->send($result);
$message = $this->client->receive();
//关闭WS链接
$this->client->close();
Log::write('WS群消息发送');
Log::write($message);
$message = json_decode($message, 1);
} catch (\Exception $e) {
$msg = $e->getMessage();
}
$this->success($msg, $message, 200);
}
/**
* 获取指定账号朋友圈信息
*/
public function getMoments()
{
if ($this->request->isPost()) {
$data = $this->request->param();
if (empty($data)) {
$this->error('参数缺失');
}
$dataArray = $data;
if (!is_array($dataArray)) {
$this->error('数据格式错误');
}
//获取数据条数
$count = isset($dataArray['count']) ? $dataArray['count'] : 5;
//过滤消息
if (empty($dataArray['wechatAccountId'])) {
$this->error('指定账号不能为空');
}
if (empty($dataArray['wechatFriendId'])) {
$this->error('指定好友不能为空');
}
$msg = '获取朋友圈信息成功';
$message = [];
try {
$params = [
"cmdType" => "CmdFetchMoment",
"count" => $count,
"createTimeSec" => time(),
"isTimeline" => false,
"prevSnsId" => 0,
"wechatAccountId" => $dataArray['wechatAccountId'],
"wechatFriendId" => $dataArray['wechatFriendId'],
"seq" => time(),
];
$params = json_encode($params);
Log::write('WS获取朋友圈信息参数' . json_encode($params, 256));
$this->client->send($params);
$message = $this->client->receive();
Log::write('WS获取朋友圈信息成功结果' . $message);
$message = json_decode($message, 1);
// 存储朋友圈数据到数据库
if (isset($message['momentList']) && !empty($message['momentList'])) {
$this->saveMomentsToDatabase($message['momentList'], $dataArray['wechatAccountId'], $dataArray['wechatFriendId']);
}
//关闭WS链接
$this->client->close();
} catch (\Exception $e) {
$msg = $e->getMessage();
}
$this->success($msg, $message, 200);
} else {
$this->error('非法请求');
}
}
/**
* 保存朋友圈数据到数据库
* @param array $momentList 朋友圈数据列表
* @param int $wechatAccountId 微信账号ID
* @param string $wechatFriendId 微信好友ID
* @return bool
*/
protected function saveMomentsToDatabase($momentList, $wechatAccountId, $wechatFriendId)
{
if (empty($momentList) || !is_array($momentList)) {
return false;
}
try {
foreach ($momentList as $moment) {
// 提取momentEntity中的数据
$momentEntity = $moment['momentEntity'] ?? [];
// 检查朋友圈数据是否已存在
$exists = Db::name('wechat_moments')
->where('snsId', $moment['snsId'])
->where('wechatAccountId', $wechatAccountId)
->find();
$dataToSave = [
'commentList' => json_encode($moment['commentList'] ?? [], JSON_UNESCAPED_UNICODE),
'createTime' => $moment['createTime'] ?? 0,
'likeList' => json_encode($moment['likeList'] ?? [], JSON_UNESCAPED_UNICODE),
'content' => $momentEntity['content'] ?? '',
'lat' => $momentEntity['lat'] ?? 0,
'lng' => $momentEntity['lng'] ?? 0,
'location' => $momentEntity['location'] ?? '',
'picSize' => $momentEntity['picSize'] ?? 0,
'resUrls' => json_encode($momentEntity['resUrls'] ?? [], JSON_UNESCAPED_UNICODE),
'userName' => $momentEntity['userName'] ?? '',
'snsId' => $moment['snsId'] ?? '',
'type' => $moment['type'] ?? 0,
'update_time' => time()
];
if ($exists) {
// 如果已存在,则更新数据
Db::name('wechat_moments')->where('id', $exists['id'])->update($dataToSave);
} else {
// 如果不存在,则插入新数据
$dataToSave['wechatAccountId'] = $wechatAccountId;
$dataToSave['wechatFriendId'] = $wechatFriendId;
$dataToSave['create_time'] = time();
Db::name('wechat_moments')->insert($dataToSave);
}
}
Log::write('朋友圈数据已存入数据库,共' . count($momentList) . '条');
return true;
} catch (\Exception $e) {
Log::write('保存朋友圈数据失败:' . $e->getMessage(), 'error');
return false;
}
}
/**
* 获取指定账号朋友圈图片地址
*/
public function getMomentSourceRealUrl()
{
if ($this->request->isPost()) {
$data = $this->request->param();
if (empty($data)) {
$this->error('参数缺失');
}
$dataArray = $data;
if (!is_array($dataArray)) {
$this->error('数据格式错误');
}
//获取数据条数
// $count = isset($dataArray['count']) ? $dataArray['count'] : 10;
//过滤消息
if (empty($dataArray['wechatAccountId'])) {
$this->error('指定账号不能为空');
}
if (empty($dataArray['snsId'])) {
$this->error('指定消息ID不能为空');
}
if (empty($dataArray['snsUrls'])) {
$this->error('资源信息不能为空');
}
$msg = '获取朋友圈资源链接成功';
$message = [];
try {
$params = [
"cmdType" => $dataArray['type'],
"snsId" => $dataArray['snsId'],
"urls" => $dataArray['snsUrls'],
"wechatAccountId" => $dataArray['wechatAccountId'],
"seq" => time(),
];
$params = json_encode($params);
$this->client->send($params);
$message = $this->client->receive();
Log::write('WS获取朋友圈图片/视频链接成功,结果:' . json_encode($message, 256));
//关闭WS链接
$this->client->close();
} catch (\Exception $e) {
$msg = $e->getMessage();
}
$this->success($msg, $message, 200);
} else {
$this->error('非法请求');
}
}
}

View File

@@ -11,4 +11,5 @@
return [
'test' => 'app\common\command\TestCommand',
'device:list' => 'app\common\command\DeviceListCommand',
];

View File

@@ -113,12 +113,12 @@ if (!function_exists('errorJson')) {
if (!function_exists('successJson')) {
function successJson($data = [] ,$error = '操作成功', $code = 200)
function successJson($data = [] ,$msg = '操作成功', $code = 200)
{
return json([
'data' => $data,
'code' => $code,
'msg' => $error,
'msg' => $msg,
]);
}
}
@@ -137,6 +137,7 @@ if (!function_exists('validateString')) {
$config = [
'password' => [
'min_length' => 6,
'max_length' => 20,
'pattern' => '/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z0-9]+$/',
'error' => '密码必须包含英文和数字,不能包含特殊符号'
],

View File

@@ -0,0 +1,57 @@
<?php
namespace app\common\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Log;
use think\Queue;
use app\job\DeviceListJob;
class DeviceListCommand extends BaseCommand
{
protected function configure()
{
$this->setName('device:list')
->setDescription('获取设备列表,并根据分页自动处理下一页');
}
protected function execute(Input $input, Output $output)
{
$output->writeln('开始处理设备列表任务...');
try {
// 初始页码
$pageIndex = 0;
$pageSize = 100; // 每页获取100条记录
// 将第一页任务添加到队列
$this->addToQueue($pageIndex, $pageSize);
$output->writeln('设备列表任务已添加到队列');
} catch (\Exception $e) {
Log::error('设备列表任务添加失败:' . $e->getMessage());
$output->writeln('设备列表任务添加失败:' . $e->getMessage());
return false;
}
return true;
}
/**
* 添加任务到队列
* @param int $pageIndex 页码
* @param int $pageSize 每页大小
*/
protected function addToQueue($pageIndex, $pageSize)
{
$data = [
'pageIndex' => $pageIndex,
'pageSize' => $pageSize
];
// 添加到队列,设置任务名为 device_list
Queue::push(DeviceListJob::class, $data, 'device_list');
}
}

View File

@@ -4,6 +4,9 @@ namespace app\common\service;
use app\common\model\User;
use app\common\util\JwtUtil;
use think\facade\Log;
use think\facade\Cache;
use think\facade\Config;
use think\facade\Env;
class AuthService
{
@@ -154,4 +157,71 @@ class AuthService
'token_expired' => $expireTime
];
}
/**
* 获取系统授权信息使用缓存存储10分钟
* @return string
*/
public static function getSystemAuthorization()
{
// 定义缓存键名
$cacheKey = 'system_authorization_token';
// 尝试从缓存获取授权信息
$authorization = Cache::get($cacheKey);
// 如果缓存中没有或已过期,则重新获取
if (empty($authorization)) {
try {
// 从环境变量中获取API用户名和密码
$username = Env::get('api.username', '');
$password = Env::get('api.password', '');
if (empty($username) || empty($password)) {
Log::error('缺少API用户名或密码配置');
return '';
}
// 构建登录参数
$params = [
'grant_type' => 'password',
'username' => $username,
'password' => $password
];
// 获取API基础URL
$baseUrl = Env::get('api.wechat_url', '');
if (empty($baseUrl)) {
Log::error('缺少API基础URL配置');
return '';
}
// 调用登录接口获取token
// 设置请求头
$headerData = ['client:system'];
$header = setHeader($headerData, '', 'plain');
$result = requestCurl($baseUrl . 'token', $params, 'POST',$header);
$result_array = handleApiResponse($result);
if (isset($result_array['access_token']) && !empty($result_array['access_token'])) {
$authorization = $result_array['access_token'];
// 存入缓存有效期10分钟600秒
Cache::set($cacheKey, $authorization, 600);
Cache::set('system_refresh_token', $result_array['refresh_token'], 600);
Log::info('已重新获取系统授权信息并缓存');
return $authorization;
} else {
Log::error('获取系统授权信息失败:' . ($response['message'] ?? '未知错误'));
return '';
}
} catch (\Exception $e) {
Log::error('获取系统授权信息异常:' . $e->getMessage());
return '';
}
}
return $authorization;
}
}

View File

@@ -0,0 +1,123 @@
<?php
namespace app\job;
use think\queue\Job;
use think\facade\Log;
use think\Queue;
use think\facade\Config;
use app\api\controller\DeviceController;
use app\common\service\AuthService;
class DeviceListJob
{
/**
* 队列任务处理
* @param Job $job 队列任务
* @param array $data 任务数据
* @return void
*/
public function fire(Job $job, $data)
{
try {
// 如果任务执行成功后删除任务
if ($this->processDeviceList($data, $job->attempts())) {
$job->delete();
Log::info('设备列表任务执行成功,页码:' . $data['pageIndex']);
} else {
if ($job->attempts() > 3) {
// 超过重试次数,删除任务
Log::error('设备列表任务执行失败,已超过重试次数,页码:' . $data['pageIndex']);
$job->delete();
} else {
// 任务失败,重新放回队列
Log::warning('设备列表任务执行失败,重试次数:' . $job->attempts() . ',页码:' . $data['pageIndex']);
$job->release(Config::get('queue.failed_delay', 10));
}
}
} catch (\Exception $e) {
// 出现异常,记录日志
Log::error('设备列表任务异常:' . $e->getMessage());
if ($job->attempts() > 3) {
$job->delete();
} else {
$job->release(Config::get('queue.failed_delay', 10));
}
}
}
/**
* 处理设备列表获取
* @param array $data 任务数据
* @param int $attempts 重试次数
* @return bool
*/
protected function processDeviceList($data, $attempts)
{
// 获取参数
$pageIndex = isset($data['pageIndex']) ? $data['pageIndex'] : 0;
$pageSize = isset($data['pageSize']) ? $data['pageSize'] : 100;
Log::info('开始获取设备列表,页码:' . $pageIndex . ',页大小:' . $pageSize);
// 实例化控制器
$deviceController = new DeviceController();
// 构建请求参数
$params = [
'pageIndex' => $pageIndex,
'pageSize' => $pageSize
];
// 设置请求信息
$request = request();
$request->withGet($params);
// 获取系统授权信息
$authorization = AuthService::getSystemAuthorization();
if (empty($authorization)) {
Log::error('获取系统授权信息失败');
return false;
}
// 调用设备列表获取方法
$result = $deviceController->getlist($pageIndex,$pageSize,$authorization);
$response = json_decode($result,true);
// 判断是否成功
if ($response['code'] == 200) {
$data = $response['data'];
// 判断是否有下一页
if (!empty($data) && count($data['results']) > 0) {
// 有下一页,将下一页任务添加到队列
$nextPageIndex = $pageIndex + 1;
$this->addNextPageToQueue($nextPageIndex, $pageSize);
Log::info('添加下一页任务到队列,页码:' . $nextPageIndex);
}
return true;
} else {
$errorMsg = isset($response['msg']) ? $response['msg'] : '未知错误';
Log::error('获取设备列表失败:' . $errorMsg);
return false;
}
}
/**
* 添加下一页任务到队列
* @param int $pageIndex 页码
* @param int $pageSize 每页大小
*/
protected function addNextPageToQueue($pageIndex, $pageSize)
{
$data = [
'pageIndex' => $pageIndex,
'pageSize' => $pageSize
];
// 添加到队列,设置任务名为 device_list
Queue::push(self::class, $data, 'device_list');
}
}

View File

@@ -0,0 +1,23 @@
-- 朋友圈表
CREATE TABLE IF NOT EXISTS `wechat_moments` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`wechatAccountId` int(11) NOT NULL COMMENT '微信账号ID',
`wechatFriendId` varchar(64) NOT NULL COMMENT '微信好友ID',
`snsId` varchar(64) NOT NULL COMMENT '朋友圈消息ID',
`commentList` text COMMENT '评论列表JSON',
`createTime` bigint(20) DEFAULT '0' COMMENT '创建时间戳',
`likeList` text COMMENT '点赞列表JSON',
`content` text COMMENT '朋友圈内容',
`lat` decimal(10,6) DEFAULT '0.000000' COMMENT '纬度',
`lng` decimal(10,6) DEFAULT '0.000000' COMMENT '经度',
`location` varchar(255) DEFAULT '' COMMENT '位置信息',
`picSize` int(11) DEFAULT '0' COMMENT '图片大小',
`resUrls` text COMMENT '资源URL列表',
`userName` varchar(64) DEFAULT '' COMMENT '用户名',
`type` int(11) DEFAULT '0' COMMENT '朋友圈类型',
`create_time` int(11) DEFAULT NULL COMMENT '数据创建时间',
`update_time` int(11) DEFAULT NULL COMMENT '数据更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `idx_sns_account` (`snsId`,`wechatAccountId`),
KEY `idx_account_friend` (`wechatAccountId`,`wechatFriendId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='微信朋友圈数据表';

View File

@@ -23,7 +23,9 @@
"alibabacloud/dysmsapi-20170525": "2.0.16",
"hashids/hashids": "^3.0",
"aliyuncs/oss-sdk-php": "^2.7",
"topthink/think-worker": "2.0.*"
"topthink/think-worker": "2.0.*",
"topthink/think-queue": "2.0.*",
"textalk/websocket": "^1.5"
},
"autoload": {
"psr-4": {

33
Server/config/queue.php Normal file
View File

@@ -0,0 +1,33 @@
<?php
// +----------------------------------------------------------------------
// | 队列设置
// +----------------------------------------------------------------------
return [
'default' => 'redis',
'connections' => [
'sync' => [
'type' => 'sync',
],
'database' => [
'type' => 'database',
'queue' => 'default',
'table' => 'jobs',
],
'redis' => [
'type' => 'redis',
'queue' => 'default',
'host' => '127.0.0.1',
'port' => 6379,
'password' => '',
'select' => 0,
'timeout' => 0,
'persistent' => false,
],
],
'failed' => [
'type' => 'redis',
'table' => 'failed_jobs',
],
'failed_delay' => 30, // 失败重试延迟时间(秒)
];

180
Server/scripts/README.md Normal file
View File

@@ -0,0 +1,180 @@
# 定时任务和队列使用说明
## 一、环境准备
### 1. 安装Redis
确保服务器已安装Redis并正常运行。
```bash
# Ubuntu/Debian系统
sudo apt-get update
sudo apt-get install redis-server
# CentOS系统
sudo yum install redis
sudo systemctl start redis
```
### 2. 安装PHP的Redis扩展
```bash
sudo pecl install redis
```
### 3. 修改队列配置
编辑 `config/queue.php` 文件根据实际环境修改Redis连接信息。
## 二、系统组件说明
### 1. 命令行任务
已实现的命令行任务:
- `device:list`: 获取设备列表命令
### 2. 队列任务
已实现的队列任务:
- `DeviceListJob`: 处理设备列表获取并自动翻页的任务
## 三、配置步骤
### 1. 确保API连接信息正确
`.env` 文件中确保以下配置正确:
- `api.wechat_url`: API基础URL地址
- `api.username`: API登录用户名
- `api.password`: API登录密码
## 四、系统架构说明
### 1. 授权服务
系统使用了公共授权服务 `app\common\service\AuthService`用于管理API授权信息
- 提供静态方法 `getSystemAuthorization()`,可被所有定时任务和控制器复用
- 从环境变量(.env文件中读取API连接信息
- 自动缓存授权Token有效期为10分钟避免频繁请求API
- 缓存失效后自动重新获取授权信息
### 2. 队列任务
队列任务统一放置在 `application/job` 目录中:
- 目前已实现 `DeviceListJob` 用于获取设备列表
- 每个任务类需实现 `fire` 方法来处理队列任务
- 任务可以通过 `think queue:work` 命令处理
- 失败任务会自动重试最多3次
### 3. 定时命令
定时命令统一放置在 `application/common/command` 目录中:
- 继承自 `BaseCommand` 基类
- 需要在 `application/command.php` 中注册命令
- 可通过 `php think` 命令调用
## 五、配置定时任务
### 1. 编辑crontab配置
```bash
crontab -e
```
### 2. 直接配置PHP命令执行定时任务
```
# 每5分钟执行一次设备列表获取任务
*/5 * * * * cd /www/wwwroot/yi.54iis.com && php think device:list >> /www/wwwroot/yi.54iis.com/logs/device_list.log 2>&1
```
说明:
- `cd /www/wwwroot/yi.54iis.com`: 切换到项目目录
- `php think device:list`: 执行设备列表命令
- `>> /www/wwwroot/yi.54iis.com/logs/device_list.log 2>&1`: 将输出和错误信息追加到日志文件
### 3. 创建日志目录
```bash
# 确保日志目录存在
mkdir -p /www/wwwroot/yi.54iis.com/logs
chmod 755 /www/wwwroot/yi.54iis.com/logs
```
## 六、配置队列处理进程
### 1. 使用crontab监控队列进程
```
# 每分钟检查队列进程,如果不存在则启动
* * * * * ps aux | grep "php think queue:work" | grep -v grep > /dev/null || (cd /www/wwwroot/yi.54iis.com && nohup php think queue:work --queue device_list --tries 3 --sleep 3 >> /www/wwwroot/yi.54iis.com/logs/queue_worker.log 2>&1 &)
```
说明:
- `ps aux | grep "php think queue:work" | grep -v grep > /dev/null`: 检查队列进程是否存在
- `||`: 如果前面的命令失败(进程不存在),则执行后面的命令
- `(cd /www/wwwroot/yi.54iis.com && nohup...)`: 进入项目目录并启动队列处理进程
### 2. 或者使用supervisor管理队列进程推荐
如果服务器上安装了supervisor可以创建配置文件 `/etc/supervisor/conf.d/device_queue.conf`
```ini
[program:device_queue]
process_name=%(program_name)s_%(process_num)02d
command=php /www/wwwroot/yi.54iis.com/think queue:work --queue device_list --tries 3 --sleep 3
autostart=true
autorestart=true
user=www
numprocs=1
redirect_stderr=true
stdout_logfile=/www/wwwroot/yi.54iis.com/logs/queue_worker.log
```
然后重新加载supervisor配置
```bash
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start device_queue:*
```
## 七、测试
### 1. 手动执行命令
```bash
# 进入项目目录
cd /www/wwwroot/yi.54iis.com
# 执行设备列表获取命令
php think device:list
```
### 2. 查看日志
```bash
# 查看定时任务日志
cat /www/wwwroot/yi.54iis.com/logs/device_list.log
# 查看队列处理日志
cat /www/wwwroot/yi.54iis.com/logs/queue_worker.log
```
## 八、新增定时任务流程
如需添加新的定时任务,请按照以下步骤操作:
1.`application/common/command` 目录下创建新的命令类
2.`application/job` 目录下创建对应的队列任务处理类
3.`application/command.php` 文件中注册新命令
4. 更新crontab配置添加新的命令执行计划
示例添加一个每天凌晨2点执行的数据备份任务
```
0 2 * * * cd /www/wwwroot/yi.54iis.com && php think backup:data >> /www/wwwroot/yi.54iis.com/logs/backup_data.log 2>&1
```
新增的定时任务可直接使用 `AuthService::getSystemAuthorization()` 获取授权信息,无需重复实现授权逻辑。
## 九、注意事项
1. 确保PHP命令可以正常执行如果默认PHP版本不匹配可能需要使用完整路径例如 `/www/server/php/74/bin/php`
2. 确保Redis服务正常运行
3. 确保API连接信息配置正确
4. 确保日志目录存在且有写入权限
5. 定时任务执行用户需要有项目目录的读写权限
6. 如果使用宝塔面板可以在【计划任务】中配置上述crontab任务

View File

@@ -217,6 +217,12 @@ class Lang
}
}
if (preg_match('/^([a-z\d\-]+)/i', $langSet, $matches)) {
$langSet = strtolower($matches[1]);
} else {
$langSet = $this->range;
}
if (empty($this->allowLangList) || in_array($langSet, $this->allowLangList)) {
// 合法的语言
$this->range = $langSet ?: $this->range;

View File

@@ -37,24 +37,24 @@ use think\db\Query;
* @method $this limit(mixed $offset, integer $length = null) static 查询LIMIT
* @method $this order(mixed $field, string $order = null) static 查询ORDER
* @method $this orderRaw(string $field, array $bind = []) static 查询ORDER
* @method $this cache(mixed $key = null , integer|\DateTime $expire = null, string $tag = null) static 设置查询缓存
* @method $this cache(mixed $key = null, integer|\DateTime $expire = null, string $tag = null) static 设置查询缓存
* @method mixed value(string $field, mixed $default = null) static 获取某个字段的值
* @method array column(string $field, string $key = '') static 获取某个列的值
* @method $this find(mixed $data = null) static 查询单个记录
* @method $this findOrFail(mixed $data = null) 查询单个记录
* @method Collection|$this[] select(mixed $data = null) static 查询多个记录
* @method $this get(mixed $data = null,mixed $with = [],bool $cache = false, bool $failException = false) static 查询单个记录 支持关联预载入
* @method $this getOrFail(mixed $data = null,mixed $with = [],bool $cache = false) static 查询单个记录 不存在则抛出异常
* @method $this get(mixed $data = null, mixed $with = [], bool $cache = false, bool $failException = false) static 查询单个记录 支持关联预载入
* @method $this getOrFail(mixed $data = null, mixed $with = [], bool $cache = false) static 查询单个记录 不存在则抛出异常
* @method $this findOrEmpty(mixed $data = null) static 查询单个记录 不存在则返回空模型
* @method Collection|$this[] all(mixed $data = null,mixed $with = [],bool $cache = false) static 查询多个记录 支持关联预载入
* @method $this withAttr(array $name,\Closure $closure = null) static 动态定义获取器
* @method Collection|$this[] all(mixed $data = null, mixed $with = [], bool $cache = false) static 查询多个记录 支持关联预载入
* @method $this withAttr(array $name, \Closure $closure = null) static 动态定义获取器
* @method $this withJoin(string|array $with, string $joinType = '') static
* @method $this withCount(string|array $relation, bool $subQuery = true) static 关联统计
* @method $this withSum(string|array $relation, string $field, bool $subQuery = true) static 关联SUM统计
* @method $this withMax(string|array $relation, string $field, bool $subQuery = true) static 关联MAX统计
* @method $this withMin(string|array $relation, string $field, bool $subQuery = true) static 关联Min统计
* @method $this withAvg(string|array $relation, string $field, bool $subQuery = true) static 关联Avg统计
* @method Paginator|$this paginate() static 分页
* @method Paginator|$this paginate(int|array $listRows = null, int|bool $simple = false, array $config = []) static 分页
*/
abstract class Model implements \JsonSerializable, \ArrayAccess
{

View File

@@ -684,7 +684,7 @@ class Request
unset($_GET[$this->config['var_pathinfo']]);
unset($this->get[$this->config['var_pathinfo']]);
} elseif ($this->isCli()) {
// CLI模式下 view.php module/controller/action/params/...
// CLI模式下 index.php module/controller/action/params/...
$pathinfo = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
} elseif ('cli-server' == PHP_SAPI) {
$pathinfo = strpos($this->server('REQUEST_URI'), '?') ? strstr($this->server('REQUEST_URI'), '?', true) : $this->server('REQUEST_URI');

View File

@@ -489,7 +489,7 @@
var err_line = $('.line-' + LINE, ol[0])[0];
err_line.className = err_line.className + ' line-error';
$.getScript('//cdn.bootcss.com/prettify/r298/prettify.min.js', function(){
$.getScript('//cdn.bootcdn.net/ajax/libs/prettify/r298/prettify.min.js', function(){
prettyPrint();
// Firefox浏览器一个很诡异的问题

View File

@@ -29,14 +29,25 @@ class Dot implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
*/
protected $items = [];
/**
* The delimiter (alternative to a '.') to be used.
*
* @var string
*/
protected $delimiter = '.';
/**
* Create a new Dot instance
*
* @param mixed $items
* @param string $delimiter
*/
public function __construct($items = [])
public function __construct($items = [], $delimiter = '.')
{
$this->items = $this->getArrayItems($items);
$this->delimiter = strlen($delimiter) ? $delimiter : '.';
}
/**
@@ -104,7 +115,7 @@ class Dot implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
}
$items = &$this->items;
$segments = explode('.', $key);
$segments = explode($this->delimiter, $key);
$lastSegment = array_pop($segments);
foreach ($segments as $segment) {
@@ -148,6 +159,10 @@ class Dot implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
$items = $this->items;
}
if (!func_num_args()) {
$delimiter = $this->delimiter;
}
foreach ($items as $key => $value) {
if (is_array($value) && !empty($value)) {
$flatten = array_merge(
@@ -179,13 +194,13 @@ class Dot implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
return $this->items[$key];
}
if (strpos($key, '.') === false) {
if (strpos($key, $this->delimiter) === false) {
return $default;
}
$items = $this->items;
foreach (explode('.', $key) as $segment) {
foreach (explode($this->delimiter, $key) as $segment) {
if (!is_array($items) || !$this->exists($items, $segment)) {
return $default;
}
@@ -234,7 +249,7 @@ class Dot implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
continue;
}
foreach (explode('.', $key) as $segment) {
foreach (explode($this->delimiter, $key) as $segment) {
if (!is_array($items) || !$this->exists($items, $segment)) {
return false;
}
@@ -446,7 +461,7 @@ class Dot implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
$items = &$this->items;
foreach (explode('.', $keys) as $key) {
foreach (explode($this->delimiter, $keys) as $key) {
if (!isset($items[$key]) || !is_array($items[$key])) {
$items[$key] = [];
}
@@ -600,6 +615,7 @@ class Dot implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
*
* @return array
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return $this->items;

View File

@@ -11,13 +11,14 @@ use Adbar\Dot;
if (! function_exists('dot')) {
/**
* Create a new Dot object with the given items
* Create a new Dot object with the given items and optional delimiter
*
* @param mixed $items
* @param mixed $items
* @param string $delimiter
* @return \Adbar\Dot
*/
function dot($items)
function dot($items, $delimiter = '.')
{
return new Dot($items);
return new Dot($items, $delimiter);
}
}

View File

@@ -1,5 +1,9 @@
# CHANGELOG
## 1.2.0 - 2024-10-17
- Refactor all credentials providers.
## 1.1.3 - 2020-12-24
- Require guzzle ^6.3|^7.0

View File

@@ -1,41 +1,57 @@
[English](/README.md) | 简体中文
![](https://aliyunsdk-pages.alicdn.com/icons/AlibabaCloud.svg)
# Alibaba Cloud Credentials for PHP
[![PHP CI](https://github.com/aliyun/credentials-php/actions/workflows/ci.yml/badge.svg)](https://github.com/aliyun/credentials-php/actions/workflows/ci.yml)
[![codecov](https://codecov.io/gh/aliyun/credentials-php/graph/badge.svg?token=YIkSjtfKbB)](https://codecov.io/gh/aliyun/credentials-php)
[![Latest Stable Version](https://poser.pugx.org/alibabacloud/credentials/v/stable)](https://packagist.org/packages/alibabacloud/credentials)
[![composer.lock](https://poser.pugx.org/alibabacloud/credentials/composerlock)](https://packagist.org/packages/alibabacloud/credentials)
[![Total Downloads](https://poser.pugx.org/alibabacloud/credentials/downloads)](https://packagist.org/packages/alibabacloud/credentials)
[![License](https://poser.pugx.org/alibabacloud/credentials/license)](https://packagist.org/packages/alibabacloud/credentials)
[![codecov](https://codecov.io/gh/aliyun/credentials-php/branch/master/graph/badge.svg)](https://codecov.io/gh/aliyun/credentials-php)
[![Travis Build Status](https://travis-ci.org/aliyun/credentials-php.svg?branch=master)](https://travis-ci.org/aliyun/credentials-php)
[![Appveyor Build Status](https://ci.appveyor.com/api/projects/status/6jxpwmhyfipagtge/branch/master?svg=true)](https://ci.appveyor.com/project/aliyun/credentials-php)
![](https://aliyunsdk-pages.alicdn.com/icons/AlibabaCloud.svg)
Alibaba Cloud Credentials for PHP 是帮助 PHP 开发者管理凭据的工具。
## 先决条件
您的系统需要满足[先决条件](/docs/zh-CN/0-Prerequisites.md),包括 PHP> = 5.6。 我们强烈建议使用cURL扩展并使用TLS后端编译cURL 7.16.2+。
## 安装依赖
如果已在系统上[全局安装 Composer](https://getcomposer.org/doc/00-intro.md#globally),请直接在项目目录中运行以下内容来安装 Alibaba Cloud Credentials for PHP 作为依赖项:
```
```sh
composer require alibabacloud/credentials
```
> 一些用户可能由于网络问题无法安装,可以使用[阿里云 Composer 全量镜像](https://developer.aliyun.com/composer)。
请看[安装](/docs/zh-CN/1-Installation.md)有关通过 Composer 和其他方式安装的详细信息。
## 快速使用
在您开始之前,您需要注册阿里云帐户并获取您的[凭证](https://usercenter.console.aliyun.com/#/manage/ak)。
### 凭证类型
#### 使用默认凭据链
当您在初始化凭据客户端不传入任何参数时Credentials工具会使用默认凭据链方式初始化客户端。默认凭据的读取逻辑请参见[默认凭据链](#默认凭证提供程序链)。
```php
<?php
use AlibabaCloud\Credentials\Credential;
// Chain Provider if no Parameter
$client = new Credential();
$credential = $client->getCredential();
$credential->getAccessKeyId();
$credential->getAccessKeySecret();
$credential->getSecurityToken();
```
#### AccessKey
通过[用户信息管理][ak]设置 access_key它们具有该账户完全的权限请妥善保管。有时出于安全考虑您不能把具有完全访问权限的主账户 AccessKey 交于一个项目的开发者使用,您可以[创建RAM子账户][ram]并为子账户[授权][permissions]使用RAM子用户的 AccessKey 来进行API调用。
@@ -44,20 +60,19 @@ composer require alibabacloud/credentials
<?php
use AlibabaCloud\Credentials\Credential;
// Chain Provider if no Parameter
$credential = new Credential();
$credential->getAccessKeyId();
$credential->getAccessKeySecret();
use AlibabaCloud\Credentials\Credential\Config;
// Access Key
$ak = new Credential([
'type' => 'access_key',
'access_key_id' => '<access_key_id>',
'access_key_secret' => '<access_key_secret>',
$config = new Config([
'type' => 'access_key',
'accessKeyId' => '<access_key_id>',
'accessKeySecret' => '<access_key_secret>',
]);
$ak->getAccessKeyId();
$ak->getAccessKeySecret();
$client = new Credential($config);
$credential = $client->getCredential();
$credential->getAccessKeyId();
$credential->getAccessKeySecret();
```
#### STS
@@ -68,102 +83,256 @@ $ak->getAccessKeySecret();
<?php
use AlibabaCloud\Credentials\Credential;
use AlibabaCloud\Credentials\Credential\Config;
$sts = new Credential([
'type' => 'sts',
'access_key_id' => '<access_key_id>',
'accessKey_secret' => '<accessKey_secret>',
'security_token' => '<security_token>',
$config = new Config([
'type' => 'sts',
'accessKeyId' => '<access_key_id>',
'accessKeySecret' => '<access_key_secret>',
'securityToken' => '<security_token>',
]);
$sts->getAccessKeyId();
$sts->getAccessKeySecret();
$sts->getSecurityToken();
$client = new Credential($config);
$credential = $client->getCredential();
$credential->getAccessKeyId();
$credential->getAccessKeySecret();
$credential->getSecurityToken();
```
#### RamRoleArn
通过指定[RAM角色][RAM Role],让凭证自动申请维护 STS Token。可以通过为 `Policy` 赋值来限制获取到的 STS Token 的权限。
通过指定RAM角色的ARNAlibabacloud Resource NameCredentials工具可以帮助开发者前往STS换取STS Token。您也可以通过为 `Policy` 赋值来限制RAM角色到一个更小的权限集合
```php
<?php
use AlibabaCloud\Credentials\Credential;
use AlibabaCloud\Credentials\Credential\Config;
$ramRoleArn = new Credential([
'type' => 'ram_role_arn',
'access_key_id' => '<access_key_id>',
'access_key_secret' => '<access_key_secret>',
'role_arn' => '<role_arn>',
'role_session_name' => '<role_session_name>',
'policy' => '',
$config = new Config([
'type' => 'ram_role_arn',
'accessKeyId' => '<access_key_id>',
'accessKeySecret' => '<access_key_secret>',
// 要扮演的RAM角色ARN示例值acs:ram::123456789012****:role/adminrole可以通过环境变量ALIBABA_CLOUD_ROLE_ARN设置role_arn
'roleArn' => '<role_arn>',
// 角色会话名称可以通过环境变量ALIBABA_CLOUD_ROLE_SESSION_NAME设置role_session_name
'roleSessionName' => '<role_session_name>',
// 设置更小的权限策略,非必填。示例值:{"Statement": [{"Action": ["*"],"Effect": "Allow","Resource": ["*"]}],"Version":"1"}
'policy' => '',
// 设置session过期时间非必填。
'roleSessionExpiration' => 3600,
]);
$ramRoleArn->getAccessKeyId();
$ramRoleArn->getAccessKeySecret();
$ramRoleArn->getRoleArn();
$ramRoleArn->getRoleSessionName();
$ramRoleArn->getPolicy();
$client = new Credential($config);
$credential = $client->getCredential();
$credential->getAccessKeyId();
$credential->getAccessKeySecret();
$credential->getSecurityToken();
```
#### EcsRamRole
通过指定角色名称,让凭证自动申请维护 STS Token
ECS和ECI实例均支持绑定实例RAM角色当在实例中使用Credentials工具时将自动获取实例绑定的RAM角色并通过访问元数据服务获取RAM角色的STS Token以完成凭据客户端的初始化。
实例元数据服务器支持加固模式和普通模式两种访问方式Credentials工具默认使用加固模式IMDSv2获取访问凭据。若使用加固模式时发生异常您可以通过设置disableIMDSv1来执行不同的异常处理逻辑
- 当值为false默认值会使用普通模式继续获取访问凭据。
- 当值为true时表示只能使用加固模式获取访问凭据会抛出异常。
服务端是否支持IMDSv2取决于您在服务器的配置。
```php
<?php
use AlibabaCloud\Credentials\Credential;
use AlibabaCloud\Credentials\Credential\Config;
$ecsRamRole = new Credential([
'type' => 'ecs_ram_role',
'role_name' => '<role_name>',
$config = new Config([
'type' => 'ecs_ram_role',
// 选填该ECS角色的角色名称不填会自动获取但是建议加上以减少请求次数可以通过环境变量ALIBABA_CLOUD_ECS_METADATA设置role_name
'roleName' => '<role_name>',
// 选填是否强制关闭IMDSv1即必须使用IMDSv2加固模式可以通过环境变量ALIBABA_CLOUD_IMDSV1_DISABLED设置
'disableIMDSv1' => true,
]);
$ecsRamRole->getRoleName();
// Note: `role_name` is optional. It will be retrieved automatically if not set. It is highly recommended to set it up to reduce requests.
$client = new Credential($config);
$credential = $client->getCredential();
$credential->getAccessKeyId();
$credential->getAccessKeySecret();
$credential->getSecurityToken();
```
#### RsaKeyPair
#### OIDCRoleArn
通过指定公钥Id和私钥文件让凭证自动申请维护 AccessKey。仅支持日本站
在容器服务 Kubernetes 版中设置了Worker节点RAM角色后对应节点内的Pod中的应用也就可以像ECS上部署的应用一样通过元数据服务Meta Data Server获取关联角色的STS Token。但如果容器集群上部署的是不可信的应用比如部署您的客户提交的应用代码也没有对您开放您可能并不希望它们能通过元数据服务获取Worker节点关联实例RAM角色的STS Token。为了避免影响云上资源的安全同时又能让这些不可信的应用安全地获取所需的 STS Token实现应用级别的权限最小化您可以使用RRSARAM Roles for Service Account功能。阿里云容器集群会为不同的应用Pod创建和挂载相应的服务账户OIDC Token文件并将相关配置信息注入到环境变量中Credentials工具通过获取环境变量的配置信息调用STS服务的AssumeRoleWithOIDC - OIDC角色SSO时获取扮演角色的临时身份凭证接口换取绑定角色的STS Token。详情请参见[通过RRSA配置ServiceAccount的RAM权限实现Pod权限隔离](https://help.aliyun.com/zh/ack/ack-managed-and-ack-dedicated/user-guide/use-rrsa-to-authorize-pods-to-access-different-cloud-services#task-2142941)
```php
<?php
use AlibabaCloud\Credentials\Credential;
use AlibabaCloud\Credentials\Credential\Config;
$rsaKeyPair = new Credential([
'type' => 'rsa_key_pair',
'public_key_id' => '<public_key_id>',
'private_key_file' => '<private_key_file>',
$config = new Config([
'type' => 'oidc_role_arn',
// OIDC提供商ARN可以通过环境变量ALIBABA_CLOUD_OIDC_PROVIDER_ARN设置oidc_provider_arn
'oidcProviderArn' => '<oidc_provider_arn>',
// OIDC Token文件路径可以通过环境变量ALIBABA_CLOUD_OIDC_TOKEN_FILE设置oidc_token_file_path
'oidcTokenFilePath' => '<oidc_token_file_path>',
// 要扮演的RAM角色ARN示例值acs:ram::123456789012****:role/adminrole可以通过环境变量ALIBABA_CLOUD_ROLE_ARN设置role_arn
'roleArn' => '<role_arn>',
// 角色会话名称可以通过环境变量ALIBABA_CLOUD_ROLE_SESSION_NAME设置role_session_name
'roleSessionName' => '<role_session_name>',
// 设置更小的权限策略,非必填。示例值:{"Statement": [{"Action": ["*"],"Effect": "Allow","Resource": ["*"]}],"Version":"1"}
'policy' => '',
# 设置session过期时间
'roleSessionExpiration' => 3600,
]);
$rsaKeyPair->getPublicKeyId();
$rsaKeyPair->getPrivateKey();
$client = new Credential($config);
$credential = $client->getCredential();
$credential->getAccessKeyId();
$credential->getAccessKeySecret();
$credential->getSecurityToken();
```
#### Credentials URI
通过指定提供凭证的自定义网络服务地址,让凭证自动申请维护 STS Token。
```php
<?php
use AlibabaCloud\Credentials\Credential;
use AlibabaCloud\Credentials\Credential\Config;
$config = new Config([
'type' => 'credentials_uri',
// 凭证的 URI格式为http://local_or_remote_uri/可以通过环境变量ALIBABA_CLOUD_CREDENTIALS_URI设置credentials_uri
'credentialsURI' => '<credentials_uri>',
]);
$client = new Credential($config);
$credential = $client->getCredential();
$credential->getAccessKeyId();
$credential->getAccessKeySecret();
$credential->getSecurityToken();
```
#### Bearer Token
呼叫中心(CCC)需用此凭证,请自行申请维护 Bearer Token。
目前只有云呼叫中心 CCC 这款产品支持 Bearer Token 的凭据初始化方式
```php
<?php
use AlibabaCloud\Credentials\Credential;
use AlibabaCloud\Credentials\Credential\Config;
$bearerToken = new Credential([
'type' => 'bearer_token',
'bearer_token' => '<bearer_token>',
$config = new Config([
'type' => 'bearer',
// 填入您的Bearer Token
'bearerToken' => '<bearer_token>',
]);
$bearerToken->getBearerToken();
$bearerToken->getSignature();
$client = new Credential($config);
$credential = $client->getCredential();
$credential->getBearerToken();
```
## 默认凭证提供程序链
默认凭证提供程序链查找可用的凭证,寻找顺序如下:
### 1. 环境凭证
程序首先会在环境变量里寻找环境凭证,如果定义了 `ALIBABA_CLOUD_ACCESS_KEY_ID``ALIBABA_CLOUD_ACCESS_KEY_SECRET` 环境变量且不为空,程序将使用他们创建默认凭证。
当您的程序开发环境和生产环境采用不同的凭据类型常见做法是在代码中获取当前环境信息编写获取不同凭据的分支代码。借助Credentials工具的默认凭据链您可以用同一套代码通过程序之外的配置来控制不同环境下的凭据获取方式。当您在不传入参数的情况下直接使用$credential = new Credential();初始化凭据客户端时阿里云SDK将会尝试按照如下顺序查找相关凭据信息。
### 2. 配置文件
> 如果用户主目录存在默认文件 `~/.alibabacloud/credentials` Windows 为 `C:\Users\USER_NAME\.alibabacloud\credentials`),程序会自动创建指定类型和名称的凭证。默认文件可以不存在,但解析错误会抛出异常。 凭证名称不分大小写若凭证同名后者会覆盖前者。不同的项目、工具之间可以共用这个配置文件因为超出项目之外也不会被意外提交到版本控制。Windows 上可以使用环境变量引用到主目录 %UserProfile%。类 Unix 的系统可以使用环境变量 $HOME 或 ~ (tilde)。 可以通过定义 `ALIBABA_CLOUD_CREDENTIALS_FILE` 环境变量修改默认文件的路径。
### 1. 使用环境变量
Credentials工具会优先在环境变量中获取凭据信息。
- 如果系统环境变量 `ALIBABA_CLOUD_ACCESS_KEY_ID`密钥Key`ALIBABA_CLOUD_ACCESS_KEY_SECRET`密钥Value 不为空Credentials工具会优先使用它们作为默认凭据。
- 如果系统环境变量 `ALIBABA_CLOUD_ACCESS_KEY_ID`密钥Key`ALIBABA_CLOUD_ACCESS_KEY_SECRET`密钥Value`ALIBABA_CLOUD_SECURITY_TOKEN`Token均不为空Credentials工具会优先使用STS Token作为默认凭据。
### 2. 使用OIDC RAM角色
若不存在优先级更高的凭据信息Credentials工具会在环境变量中获取如下内容
`ALIBABA_CLOUD_ROLE_ARN`RAM角色名称ARN
`ALIBABA_CLOUD_OIDC_PROVIDER_ARN`OIDC提供商ARN
`ALIBABA_CLOUD_OIDC_TOKEN_FILE`OIDC Token文件路径
若以上三个环境变量都已设置内容Credentials将会使用变量内容调用STS服务的[AssumeRoleWithOIDC - OIDC角色SSO时获取扮演角色的临时身份凭证](https://help.aliyun.com/zh/ram/developer-reference/api-sts-2015-04-01-assumerolewithoidc)接口换取STS Token作为默认凭据。
### 3. 使用 Aliyun CLI 工具的 config.json 配置文件
若不存在优先级更高的凭据信息Credentials工具会优先在如下位置查找 `config.json` 文件是否存在:
Linux系统`~/.aliyun/config.json`
Windows系统 `C:\Users\USER_NAME\.aliyun\config.json`
如果文件存在,程序将会使用配置文件中 `current` 指定的凭据信息初始化凭据客户端。当然,您也可以通过环境变量 `ALIBABA_CLOUD_PROFILE` 来指定凭据信息,例如设置 `ALIBABA_CLOUD_PROFILE` 的值为 `AK`
在config.json配置文件中每个module的值代表了不同的凭据信息获取方式
- AK使用用户的Access Key作为凭据信息
- RamRoleArn使用RAM角色的ARN来获取凭据信息
- EcsRamRole利用ECS绑定的RAM角色来获取凭据信息
- OIDC通过OIDC ARN和OIDC Token来获取凭据信息
- ChainableRamRoleArn采用角色链的方式通过指定JSON文件中的其他凭据以重新获取新的凭据信息。
配置示例信息如下:
```json
{
"current": "AK",
"profiles": [
{
"name": "AK",
"mode": "AK",
"access_key_id": "access_key_id",
"access_key_secret": "access_key_secret"
},
{
"name": "RamRoleArn",
"mode": "RamRoleArn",
"access_key_id": "access_key_id",
"access_key_secret": "access_key_secret",
"ram_role_arn": "ram_role_arn",
"ram_session_name": "ram_session_name",
"expired_seconds": 3600,
"sts_region": "cn-hangzhou"
},
{
"name": "EcsRamRole",
"mode": "EcsRamRole",
"ram_role_name": "ram_role_name"
},
{
"name": "OIDC",
"mode": "OIDC",
"ram_role_arn": "ram_role_arn",
"oidc_token_file": "path/to/oidc/file",
"oidc_provider_arn": "oidc_provider_arn",
"ram_session_name": "ram_session_name",
"expired_seconds": 3600,
"sts_region": "cn-hangzhou"
},
{
"name": "ChainableRamRoleArn",
"mode": "ChainableRamRoleArn",
"source_profile": "AK",
"ram_role_arn": "ram_role_arn",
"ram_session_name": "ram_session_name",
"expired_seconds": 3600,
"sts_region": "cn-hangzhou"
}
]
}
```
### 4. 使用配置文件
>
> 如果用户主目录存在默认文件 `~/.alibabacloud/credentials` Windows 为 `C:\Users\USER_NAME\.alibabacloud\credentials`),程序会自动创建指定类型和名称的凭证。您也可通过环境变量 `ALIBABA_CLOUD_CREDENTIALS_FILE` 指定配置文件路径。如果文件存在,程序将会使用配置文件中 default 指定的凭据信息初始化凭据客户端。当然,您也可以通过环境变量 `ALIBABA_CLOUD_PROFILE` 来指定凭据信息,例如设置 `ALIBABA_CLOUD_PROFILE` 的值为 `client1`。
配置示例信息如下:
```ini
[default]
@@ -183,68 +352,72 @@ role_arn = role_arn
role_session_name = session_name
[project3]
type = rsa_key_pair # 认证方式为 rsa_key_pair
public_key_id = publicKeyId # Public Key ID
private_key_file = /your/pk.pem # Private Key 文件
type=oidc_role_arn # 认证方式为 oidc_role_arn
oidc_provider_arn=oidc_provider_arn
oidc_token_file_path=oidc_token_file_path
role_arn=role_arn
role_session_name=session_name
```
### 3. 实例 RAM 角色
如果定义了环境变量 `ALIBABA_CLOUD_ECS_METADATA` 且不为空,程序会将该环境变量的值作为角色名称,请求 `http://100.100.100.200/latest/meta-data/ram/security-credentials/` 获取临时安全凭证作为默认凭证。
### 5. 使用 ECS 实例RAM角色
### 自定义凭证提供程序链
可通过自定义程序链代替默认程序链的寻找顺序,也可以自行编写闭包传入提供者。
```php
<?php
若不存在优先级更高的凭据信息Credentials工具将通过环境变量获取ALIBABA_CLOUD_ECS_METADATAECS实例RAM角色名称的值。若该变量的值存在程序将采用加固模式IMDSv2访问ECS的元数据服务Meta Data Server以获取ECS实例RAM角色的STS Token作为默认凭据信息。在使用加固模式时若发生异常将使用普通模式兜底来获取访问凭据。您也可以通过设置环境变量ALIBABA_CLOUD_IMDSV1_DISABLED执行不同的异常处理逻辑
use AlibabaCloud\Credentials\Providers\ChainProvider;
- 当值为false时会使用普通模式继续获取访问凭据。
ChainProvider::set(
ChainProvider::ini(),
ChainProvider::env(),
ChainProvider::instance()
);
- 当值为true时表示只能使用加固模式获取访问凭据会抛出异常。
服务端是否支持IMDSv2取决于您在服务器的配置。
### 6. 使用外部服务 Credentials URI
若不存在优先级更高的凭据信息Credentials工具会在环境变量中获取ALIBABA_CLOUD_CREDENTIALS_URI若存在程序将请求该URI地址获取临时安全凭证作为默认凭据信息。
外部服务响应结构应如下:
```json
{
"Code": "Success",
"AccessKeyId": "AccessKeyId",
"AccessKeySecret": "AccessKeySecret",
"SecurityToken": "SecurityToken",
"Expiration": "2024-10-26T03:46:38Z"
}
```
## 文档
* [先决条件](/docs/zh-CN/0-Prerequisites.md)
* [安装](/docs/zh-CN/1-Installation.md)
## 问题
[提交 Issue](https://github.com/aliyun/credentials-php/issues/new/choose),不符合指南的问题可能会立即关闭。
## 发行说明
每个版本的详细更改记录在[发行说明](/CHANGELOG.md)中。
## 贡献
提交 Pull Request 之前请阅读[贡献指南](/CONTRIBUTING.md)。
## 相关
* [OpenAPI 开发者门户][open-api]
* [Packagist][packagist]
* [Composer][composer]
* [Guzzle中文文档][guzzle-docs]
* [最新源码][latest-release]
## 许可证
[Apache-2.0](/LICENSE.md)
Copyright (c) 2009-present, Alibaba Cloud All rights reserved.
[open-api]: https://next.api.aliyun.com
[open-api]: https://api.aliyun.com
[latest-release]: https://github.com/aliyun/credentials-php
[guzzle-docs]: https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html
[composer]: https://getcomposer.org
[packagist]: https://packagist.org/packages/alibabacloud/credentials
[home]: https://home.console.aliyun.com
[aliyun]: https://www.aliyun.com
[cURL]: http://php.net/manual/zh/book.curl.php
[OPCache]: http://php.net/manual/zh/book.opcache.php
[xdebug]: http://xdebug.org
[OpenSSL]: http://php.net/manual/zh/book.openssl.php

View File

@@ -1,41 +1,59 @@
English | [简体中文](/README-zh-CN.md)
![](https://aliyunsdk-pages.alicdn.com/icons/AlibabaCloud.svg)
# Alibaba Cloud Credentials for PHP
[![PHP CI](https://github.com/aliyun/credentials-php/actions/workflows/ci.yml/badge.svg)](https://github.com/aliyun/credentials-php/actions/workflows/ci.yml)
[![codecov](https://codecov.io/gh/aliyun/credentials-php/graph/badge.svg?token=YIkSjtfKbB)](https://codecov.io/gh/aliyun/credentials-php)
[![Latest Stable Version](https://poser.pugx.org/alibabacloud/credentials/v/stable)](https://packagist.org/packages/alibabacloud/credentials)
[![composer.lock](https://poser.pugx.org/alibabacloud/credentials/composerlock)](https://packagist.org/packages/alibabacloud/credentials)
[![Total Downloads](https://poser.pugx.org/alibabacloud/credentials/downloads)](https://packagist.org/packages/alibabacloud/credentials)
[![License](https://poser.pugx.org/alibabacloud/credentials/license)](https://packagist.org/packages/alibabacloud/credentials)
[![codecov](https://codecov.io/gh/aliyun/credentials-php/branch/master/graph/badge.svg)](https://codecov.io/gh/aliyun/credentials-php)
[![Travis Build Status](https://travis-ci.org/aliyun/credentials-php.svg?branch=master)](https://travis-ci.org/aliyun/credentials-php)
[![Appveyor Build Status](https://ci.appveyor.com/api/projects/status/6jxpwmhyfipagtge/branch/master?svg=true)](https://ci.appveyor.com/project/aliyun/credentials-php)
![](https://aliyunsdk-pages.alicdn.com/icons/AlibabaCloud.svg)
Alibaba Cloud Credentials for PHP is a tool that helps PHP developers manage their credentials.
## Prerequisites
Your system needs to meet [Prerequisites](/docs/zh-CN/0-Prerequisites.md), including PHP> = 5.6. We strongly recommend using the cURL extension and compiling cURL 7.16.2+ using the TLS backend.
## Installation
If you have [Globally Install Composer](https://getcomposer.org/doc/00-intro.md#globally) on your system, install Alibaba Cloud Credentials for PHP as a dependency by running the following directly in the project directory:
```
```sh
composer require alibabacloud/credentials
```
> Some users may not be able to install due to network problems, you can switch to the [Alibaba Cloud Composer Mirror](https://developer.aliyun.com/composer).
See [Installation](/docs/zh-CN/1-Installation.md) for details on installing through Composer and other means.
## Quick Examples
Before you begin, you need to sign up for an Alibaba Cloud account and retrieve your [Credentials](https://usercenter.console.aliyun.com/#/manage/ak).
### Credential Type
#### Default credential provider chain
If you do not specify a method to initialize a Credentials client, the default credential provider chain is used. For more information, see the Default credential provider chain section of this topic.
```php
<?php
use AlibabaCloud\Credentials\Credential;
// Chain Provider if no Parameter
// Do not specify a method to initialize a Credentials client.
$client = new Credential();
$credential = $client->getCredential();
$credential->getAccessKeyId();
$credential->getAccessKeySecret();
$credential->getSecurityToken();
```
#### AccessKey
Setup access_key credential through [User Information Management][ak], it have full authority over the account, please keep it safe. Sometimes for security reasons, you cannot hand over a primary account AccessKey with full access to the developer of a project. You may create a sub-account [RAM Sub-account][ram] , grant its [authorization][permissions]and use the AccessKey of RAM Sub-account.
@@ -44,20 +62,19 @@ Setup access_key credential through [User Information Management][ak], it have f
<?php
use AlibabaCloud\Credentials\Credential;
// Chain Provider if no Parameter
$credential = new Credential();
$credential->getAccessKeyId();
$credential->getAccessKeySecret();
use AlibabaCloud\Credentials\Credential\Config;
// Access Key
$ak = new Credential([
'type' => 'access_key',
'access_key_id' => '<access_key_id>',
'access_key_secret' => '<access_key_secret>',
$config = new Config([
'type' => 'access_key',
'accessKeyId' => '<access_key_id>',
'accessKeySecret' => '<access_key_secret>',
]);
$ak->getAccessKeyId();
$ak->getAccessKeySecret();
$client = new Credential($config);
$credential = $client->getCredential();
$credential->getAccessKeyId();
$credential->getAccessKeySecret();
```
#### STS
@@ -68,16 +85,20 @@ Create a temporary security credential by applying Temporary Security Credential
<?php
use AlibabaCloud\Credentials\Credential;
use AlibabaCloud\Credentials\Credential\Config;
$sts = new Credential([
'type' => 'sts',
'access_key_id' => '<access_key_id>',
'accessKey_secret' => '<accessKey_secret>',
'security_token' => '<security_token>',
$config = new Config([
'type' => 'sts',
'accessKeyId' => '<access_key_id>',
'accessKeySecret' => '<access_key_secret>',
'securityToken' => '<security_token>',
]);
$sts->getAccessKeyId();
$sts->getAccessKeySecret();
$sts->getSecurityToken();
$client = new Credential($config);
$credential = $client->getCredential();
$credential->getAccessKeyId();
$credential->getAccessKeySecret();
$credential->getSecurityToken();
```
#### RamRoleArn
@@ -88,56 +109,116 @@ By specifying [RAM Role][RAM Role], the credential will be able to automatically
<?php
use AlibabaCloud\Credentials\Credential;
use AlibabaCloud\Credentials\Credential\Config;
$ramRoleArn = new Credential([
'type' => 'ram_role_arn',
'access_key_id' => '<access_key_id>',
'access_key_secret' => '<access_key_secret>',
'role_arn' => '<role_arn>',
'role_session_name' => '<role_session_name>',
'policy' => '',
$config = new Config([
'type' => 'ram_role_arn',
'accessKeyId' => '<access_key_id>',
'accessKeySecret' => '<access_key_secret>',
// Specify the ARN of the RAM role to be assumed. Example: acs:ram::123456789012****:role/adminrole.
'roleArn' => '<role_arn>',
// Specify the name of the role session.
'roleSessionName' => '<role_session_name>',
// Optional. Specify limited permissions for the RAM role. Example: {"Statement": [{"Action": ["*"],"Effect": "Allow","Resource": ["*"]}],"Version":"1"}.
'policy' => '',
// Optional. Specify the expiration of the session
'roleSessionExpiration' => 3600,
]);
$ramRoleArn->getAccessKeyId();
$ramRoleArn->getAccessKeySecret();
$ramRoleArn->getRoleArn();
$ramRoleArn->getRoleSessionName();
$ramRoleArn->getPolicy();
$client = new Credential($config);
$credential = $client->getCredential();
$credential->getAccessKeyId();
$credential->getAccessKeySecret();
$credential->getSecurityToken();
```
#### EcsRamRole
By specifying the role name, the credential will be able to automatically request maintenance of STS Token.
Both ECS and ECI instances support binding instance RAM roles. When the Credentials tool is used in an instance, the RAM role bound to the instance will be automatically obtained, and the STS Token of the RAM role will be obtained by accessing the metadata service to complete the initialization of the credential client.
The instance metadata server supports two access modes: hardened mode and normal mode. The Credentials tool uses hardened mode (IMDSv2) by default to obtain access credentials. If an exception occurs when using hardened mode, you can set disableIMDSv1 to perform different exception handling logic:
- When the value is false (default value), the normal mode will continue to be used to obtain access credentials.
- When the value is true, it means that only hardened mode can be used to obtain access credentials, and an exception will be thrown.
Whether the server supports IMDSv2 depends on your configuration on the server.
```php
<?php
use AlibabaCloud\Credentials\Credential;
use AlibabaCloud\Credentials\Credential\Config;
$ecsRamRole = new Credential([
'type' => 'ecs_ram_role',
'role_name' => '<role_name>',
$config = new Config([
'type' => 'ecs_ram_role',
// Optional. Specify the name of the RAM role of the ECS instance. If you do not specify this parameter, its value is automatically obtained. To reduce the number of requests, we recommend that you specify this parameter.
'roleName' => '<role_name>',
//Optional, whether to forcibly disable IMDSv1, that is, to use IMDSv2 hardening mode, which can be set by the environment variable ALIBABA_CLOUD_IMDSV1_DISABLED
'disableIMDSv1' => true,
]);
$ecsRamRole->getRoleName();
// Note: `role_name` is optional. It will be retrieved automatically if not set. It is highly recommended to set it up to reduce requests.
$client = new Credential($config);
$credential = $client->getCredential();
$credential->getAccessKeyId();
$credential->getAccessKeySecret();
$credential->getSecurityToken();
```
#### RsaKeyPair
By specifying the public key Id and the private key file, the credential will be able to automatically request maintenance of the AccessKey before sending the request. Only Japan station is supported.
#### OIDCRoleArn
After you attach a RAM role to a worker node in an Container Service for Kubernetes, applications in the pods on the worker node can use the metadata server to obtain an STS token the same way in which applications on ECS instances do. However, if an untrusted application is deployed on the worker node, such as an application that is submitted by your customer and whose code is unavailable to you, you may not want the application to use the metadata server to obtain an STS token of the RAM role attached to the worker node. To ensure the security of cloud resources and enable untrusted applications to securely obtain required STS tokens, you can use the RAM Roles for Service Accounts (RRSA) feature to grant minimum necessary permissions to an application. In this case, the ACK cluster creates a service account OpenID Connect (OIDC) token file, associates the token file with a pod, and then injects relevant environment variables into the pod. Then, the Credentials tool uses the environment variables to call the AssumeRoleWithOIDC operation of STS and obtains an STS token of the RAM role. For more information about the RRSA feature, see [Use RRSA to authorize different pods to access different cloud services](https://www.alibabacloud.com/help/en/ack/ack-managed-and-ack-dedicated/user-guide/use-rrsa-to-authorize-pods-to-access-different-cloud-services#task-2142941).
```php
<?php
use AlibabaCloud\Credentials\Credential;
use AlibabaCloud\Credentials\Credential\Config;
$rsaKeyPair = new Credential([
'type' => 'rsa_key_pair',
'public_key_id' => '<public_key_id>',
'private_key_file' => '<private_key_file>',
$config = new Config([
'type' => 'oidc_role_arn',
// Specify the ARN of the OIDC IdP by specifying the ALIBABA_CLOUD_OIDC_PROVIDER_ARN environment variable.
'oidcProviderArn' => '<oidc_provider_arn>',
// Specify the path of the OIDC token file by specifying the ALIBABA_CLOUD_OIDC_TOKEN_FILE environment variable.
'oidcTokenFilePath' => '<oidc_token_file_path>',
// Specify the ARN of the RAM role by specifying the ALIBABA_CLOUD_ROLE_ARN environment variable.
'roleArn' => '<role_arn>',
// Specify the role session name by specifying the ALIBABA_CLOUD_ROLE_SESSION_NAME environment variable.
'roleSessionName' => '<role_session_name>',
// Optional. Specify limited permissions for the RAM role. Example: {"Statement": [{"Action": ["*"],"Effect": "Allow","Resource": ["*"]}],"Version":"1"}.
'policy' => '',
// Optional. Specify the validity period of the session.
'roleSessionExpiration' => 3600,
]);
$rsaKeyPair->getPublicKeyId();
$rsaKeyPair->getPrivateKey();
$client = new Credential($config);
$credential = $client->getCredential();
$credential->getAccessKeyId();
$credential->getAccessKeySecret();
$credential->getSecurityToken();
```
#### Credentials URI
By specifying the url, the credential will be able to automatically request maintenance of STS Token.
```php
<?php
use AlibabaCloud\Credentials\Credential;
use AlibabaCloud\Credentials\Credential\Config;
$config = new Config([
'type' => 'credentials_uri',
// Format: http url. `credentialsURI` can be replaced by setting environment variable: ALIBABA_CLOUD_CREDENTIALS_URI
'credentialsURI' => '<credentials_uri>',
]);
$client = new Credential($config);
$credential = $client->getCredential();
$credential->getAccessKeyId();
$credential->getAccessKeySecret();
$credential->getSecurityToken();
```
#### Bearer Token
@@ -148,24 +229,110 @@ If credential is required by the Cloud Call Centre (CCC), please apply for Beare
<?php
use AlibabaCloud\Credentials\Credential;
use AlibabaCloud\Credentials\Credential\Config;
$bearerToken = new Credential([
'type' => 'bearer_token',
'bearer_token' => '<bearer_token>',
$config = new Config([
'type' => 'bearer',
'bearerToken' => '<bearer_token>',
]);
$bearerToken->getBearerToken();
$bearerToken->getSignature();
$client = new Credential($config);
$credential = $client->getCredential();
$credential->getBearerToken();
```
## Default credential provider chain
The default credential provider chain looks for available credentials, looking in the following order:
If you want to use different types of credentials in the development and production environments of your application, you generally need to obtain the environment information from the code and write code branches to obtain different credentials for the development and production environments. The default credential provider chain of the Credentials tool allows you to use the same code to obtain credentials for different environments based on configurations independent of the application. If you use $credential = new Credential(); to initialize a Credentials client without specifying an initialization method, the Credentials tool obtains the credential information in the following order:
### 1. Environmental certificate
The program first looks for environment credentials in the environment variable. If the `ALIBABA_CLOUD_ACCESS_KEY_ID` and `ALIBABA_CLOUD_ACCESS_KEY_SECRET` environment variables are defined and not empty, the program will use them to create default credentials.
### 2. Configuration file
> If the user's home directory has the default file `~/.alibabacloud/credentials` (Windows is `C:\Users\USER_NAME\.alibabacloud\credentials`), the program will automatically create credentials with the specified type and name. The default file may not exist, but parsing errors will throw an exception. The voucher name is not case sensitive. If the voucher has the same name, the latter will overwrite the former. This configuration file can be shared between different projects and tools, and it will not be accidentally submitted to version control because it is outside the project. Environment variables can be referenced to the home directory %UserProfile% on Windows. Unix-like systems can use the environment variable $HOME or ~ (tilde). The path to the default file can be modified by defining the `ALIBABA_CLOUD_CREDENTIALS_FILE` environment variable.
Look for environment credentials in environment variable.
- If the `ALIBABA_CLOUD_ACCESS_KEY_ID` and `ALIBABA_CLOUD_ACCESS_KEY_SECRET` environment variables are defined and are not empty, the program will use them to create default credentials.
- If the `ALIBABA_CLOUD_ACCESS_KEY_ID`, `ALIBABA_CLOUD_ACCESS_KEY_SECRET` and `ALIBABA_CLOUD_SECURITY_TOKEN` environment variables are defined and are not empty, the program will use them to create temporary security credentials(STS). Note: This token has an expiration time, it is recommended to use it in a temporary environment.
### 2. The RAM role of an OIDC IdP
If no credentials are found in the previous step, the Credentials tool obtains the values of the following environment variables:
`ALIBABA_CLOUD_ROLE_ARN`: the ARN of the RAM role.
`ALIBABA_CLOUD_OIDC_PROVIDER_ARN`: the ARN of the OIDC IdP.
`ALIBABA_CLOUD_OIDC_TOKEN_FILE`: the path of the OIDC token file.
If the preceding three environment variables are specified, the Credentials tool uses the environment variables to call the [AssumeRoleWithOIDC](https://www.alibabacloud.com/help/en/ram/developer-reference/api-sts-2015-04-01-assumerolewithoidc) operation of STS to obtain an STS token as the default credential.
### 3. Using the config.json Configuration File of Aliyun CLI Tool
If there is no higher-priority credential information, the Credentials tool will first check the following locations to see if the config.json file exists:
Linux system: `~/.aliyun/config.json`
Windows system: `C:\Users\USER_NAME\.aliyun\config.json`
If the file exists, the program will use the credential information specified by `current` in the configuration file to initialize the credentials client. Of course, you can also use the environment variable `ALIBABA_CLOUD_PROFILE` to specify the credential information, for example by setting the value of `ALIBABA_CLOUD_PROFILE` to `AK`.
In the config.json configuration file, the value of each module represents different ways to obtain credential information:
- AK: Use the Access Key of the user as credential information;
- RamRoleArn: Use the ARN of the RAM role to obtain credential information;
- EcsRamRole: Use the RAM role bound to the ECS to obtain credential information;
- OIDC: Obtain credential information through OIDC ARN and OIDC Token;
- ChainableRamRoleArn: Use the role chaining method to obtain new credential information by specifying other credentials in the JSON file.
The configuration example information is as follows:
```json
{
"current": "AK",
"profiles": [
{
"name": "AK",
"mode": "AK",
"access_key_id": "access_key_id",
"access_key_secret": "access_key_secret"
},
{
"name": "RamRoleArn",
"mode": "RamRoleArn",
"access_key_id": "access_key_id",
"access_key_secret": "access_key_secret",
"ram_role_arn": "ram_role_arn",
"ram_session_name": "ram_session_name",
"expired_seconds": 3600,
"sts_region": "cn-hangzhou"
},
{
"name": "EcsRamRole",
"mode": "EcsRamRole",
"ram_role_name": "ram_role_name"
},
{
"name": "OIDC",
"mode": "OIDC",
"ram_role_arn": "ram_role_arn",
"oidc_token_file": "path/to/oidc/file",
"oidc_provider_arn": "oidc_provider_arn",
"ram_session_name": "ram_session_name",
"expired_seconds": 3600,
"sts_region": "cn-hangzhou"
},
{
"name": "ChainableRamRoleArn",
"mode": "ChainableRamRoleArn",
"source_profile": "AK",
"ram_role_arn": "ram_role_arn",
"ram_session_name": "ram_session_name",
"expired_seconds": 3600,
"sts_region": "cn-hangzhou"
}
]
}
```
### 4. Configuration file
>
> If the user's home directory has the default file `~/.alibabacloud/credentials` (Windows is `C:\Users\USER_NAME\.alibabacloud\credentials`), the program will automatically create credentials with the specified type and name. You can also specify the configuration file path by configuring the `ALIBABA_CLOUD_CREDENTIALS_FILE` environment variable. If the configuration file exists, the application initializes a Credentials client by using the credential information that is specified by default in the configuration file. You can also configure the `ALIBABA_CLOUD_PROFILE` environment variable to modify the default credential information that is read.
Configuration example:
```ini
[default]
type = access_key # Authentication method is access_key
@@ -184,68 +351,72 @@ role_arn = role_arn
role_session_name = session_name
[project3]
type = rsa_key_pair # Authentication method is rsa_key_pair
public_key_id = publicKeyId # Public Key ID
private_key_file = /your/pk.pem # Private Key File
type=oidc_role_arn # Authentication method is oidc_role_arn
oidc_provider_arn=oidc_provider_arn
oidc_token_file_path=oidc_token_file_path
role_arn=role_arn
role_session_name=session_name
```
### 3. Instance RAM role
If the environment variable `ALIBABA_CLOUD_ECS_METADATA` is defined and not empty, the program will take the value of the environment variable as the role name and request `http://100.100.100.200/latest/meta-data/ram/security-credentials/` to get the temporary Security credentials are used as default credentials.
### 5. Instance RAM role
### Custom credential provider chain
You can replace the default order of the program chain by customizing the program chain, or you can write the closure to the provider.
```php
<?php
If there is no credential information with a higher priority, the Credentials tool will obtain the value of ALIBABA_CLOUD_ECS_METADATA (ECS instance RAM role name) through the environment variable. If the value of this variable exists, the program will use the hardened mode (IMDSv2) to access the metadata service (Meta Data Server) of ECS to obtain the STS Token of the ECS instance RAM role as the default credential information. If an exception occurs when using the hardened mode, the normal mode will be used as a fallback to obtain access credentials. You can also set the environment variable ALIBABA_CLOUD_IMDSV1_DISABLED to perform different exception handling logic:
use AlibabaCloud\Credentials\Providers\ChainProvider;
- When the value is false, the normal mode will continue to obtain access credentials.
ChainProvider::set(
ChainProvider::ini(),
ChainProvider::env(),
ChainProvider::instance()
);
- When the value is true, it means that only the hardened mode can be used to obtain access credentials, and an exception will be thrown.
Whether the server supports IMDSv2 depends on your configuration on the server.
### 6. Using External Service Credentials URI
If there are no higher-priority credential information, the Credentials tool will obtain the `ALIBABA_CLOUD_CREDENTIALS_URI` from the environment variables. If it exists, the program will request the URI address to obtain temporary security credentials as the default credential information.
The external service response structure should be as follows:
```json
{
"Code": "Success",
"AccessKeyId": "AccessKeyId",
"AccessKeySecret": "AccessKeySecret",
"SecurityToken": "SecurityToken",
"Expiration": "2024-10-26T03:46:38Z"
}
```
## Documentation
* [Prerequisites](/docs/zh-CN/0-Prerequisites.md)
* [Installation](/docs/zh-CN/1-Installation.md)
## Issue
[Submit Issue](https://github.com/aliyun/credentials-php/issues/new/choose), Problems that do not meet the guidelines may close immediately.
## Release notes
Detailed changes for each version are recorded in the [Release Notes](/CHANGELOG.md).
## Contribution
Please read the [Contribution Guide](/CONTRIBUTING.md) before submitting a Pull Request.
## Related
* [OpenAPI Developer Portal][open-api]
* [Packagist][packagist]
* [Composer][composer]
* [Guzzle Doc][guzzle-docs]
* [Latest Release][latest-release]
## License
[Apache-2.0](/LICENSE.md)
Copyright (c) 2009-present, Alibaba Cloud All rights reserved.
[open-api]: https://next.api.aliyun.com
[open-api]: https://api.alibabacloud.com
[latest-release]: https://github.com/aliyun/credentials-php
[guzzle-docs]: http://docs.guzzlephp.org/en/stable/request-options.html
[composer]: https://getcomposer.org
[packagist]: https://packagist.org/packages/alibabacloud/credentials
[home]: https://home.console.aliyun.com
[aliyun]: https://www.aliyun.com
[cURL]: https://www.php.net/manual/en/book.curl.php
[OPCache]: http://php.net/manual/en/book.opcache.php
[xdebug]: http://xdebug.org
[OpenSSL]: http://php.net/manual/en/book.openssl.php

View File

@@ -47,7 +47,7 @@
"ext-sockets": "*",
"drupal/coder": "^8.3",
"symfony/dotenv": "^3.4",
"phpunit/phpunit": "^4.8.35|^5.4.3",
"phpunit/phpunit": "^5.7|^6.6|^9.3",
"monolog/monolog": "^1.24",
"composer/composer": "^1.8",
"mikey179/vfsstream": "^1.6",
@@ -68,7 +68,10 @@
},
"config": {
"preferred-install": "dist",
"optimize-autoloader": true
"optimize-autoloader": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"minimum-stability": "dev",
"prefer-stable": true,

View File

@@ -2,9 +2,12 @@
namespace AlibabaCloud\Credentials;
use AlibabaCloud\Credentials\Utils\Filter;
use AlibabaCloud\Credentials\Credential\CredentialModel;
use AlibabaCloud\Credentials\Signature\ShaHmac1Signature;
/**
* @deprecated
* Use the AccessKey to complete the authentication.
*/
class AccessKeyCredential implements CredentialsInterface
@@ -29,7 +32,7 @@ class AccessKeyCredential implements CredentialsInterface
{
Filter::accessKey($access_key_id, $access_key_secret);
$this->accessKeyId = $access_key_id;
$this->accessKeyId = $access_key_id;
$this->accessKeySecret = $access_key_secret;
}
@@ -69,4 +72,15 @@ class AccessKeyCredential implements CredentialsInterface
{
return '';
}
/**
* @inheritDoc
*/
public function getCredential()
{
return new CredentialModel([
'accessKeyId' => $this->accessKeyId,
'accessKeySecret' => $this->accessKeySecret,
'type' => 'access_key',
]);
}
}

View File

@@ -2,6 +2,8 @@
namespace AlibabaCloud\Credentials;
use AlibabaCloud\Credentials\Utils\Filter;
use AlibabaCloud\Credentials\Credential\CredentialModel;
use AlibabaCloud\Credentials\Signature\BearerTokenSignature;
/**
@@ -18,13 +20,13 @@ class BearerTokenCredential implements CredentialsInterface
/**
* BearerTokenCredential constructor.
*
* @param $bearerToken
* @param $bearer_token
*/
public function __construct($bearerToken)
public function __construct($bearer_token)
{
Filter::bearerToken($bearerToken);
Filter::bearerToken($bearer_token);
$this->bearerToken = $bearerToken;
$this->bearerToken = $bearer_token;
}
/**
@@ -50,4 +52,16 @@ class BearerTokenCredential implements CredentialsInterface
{
return new BearerTokenSignature();
}
/**
* @inheritDoc
*/
public function getCredential()
{
return new CredentialModel([
'bearerToken' => $this->bearerToken,
'type' => 'bearer',
]);
}
}

View File

@@ -3,153 +3,188 @@
namespace AlibabaCloud\Credentials;
use AlibabaCloud\Credentials\Credential\Config;
use AlibabaCloud\Credentials\Credential\CredentialModel;
use AlibabaCloud\Credentials\Providers\DefaultCredentialsProvider;
use AlibabaCloud\Credentials\Providers\EcsRamRoleCredentialsProvider;
use AlibabaCloud\Credentials\Providers\OIDCRoleArnCredentialsProvider;
use AlibabaCloud\Credentials\Providers\RamRoleArnCredentialsProvider;
use AlibabaCloud\Credentials\Providers\RsaKeyPairCredentialsProvider;
use AlibabaCloud\Credentials\Providers\StaticAKCredentialsProvider;
use AlibabaCloud\Credentials\Providers\StaticSTSCredentialsProvider;
use AlibabaCloud\Credentials\Providers\URLCredentialsProvider;
use AlibabaCloud\Credentials\Utils\Helper;
use GuzzleHttp\Exception\GuzzleException;
use InvalidArgumentException;
use ReflectionClass;
use ReflectionException;
use ReflectionParameter;
use RuntimeException;
/**
* Class Credential
*
* @package AlibabaCloud\Credentials
*
* @mixin AccessKeyCredential
* @mixin BearerTokenCredential
* @mixin EcsRamRoleCredential
* @mixin RamRoleArnCredential
* @mixin RsaKeyPairCredential
*/
class Credential
{
/**
* @var array
*/
protected $config = [];
/**
* @var array
* Version of the Client
*/
protected $types = [
'access_key' => AccessKeyCredential::class,
'sts' => StsCredential::class,
'ecs_ram_role' => EcsRamRoleCredential::class,
'ram_role_arn' => RamRoleArnCredential::class,
'rsa_key_pair' => RsaKeyPairCredential::class,
];
const VERSION = '1.1.5';
/**
* @var AccessKeyCredential|BearerTokenCredential|EcsRamRoleCredential|RamRoleArnCredential|RsaKeyPairCredential
* @var Config
*/
protected $config;
/**
* @var CredentialsInterface
*/
protected $credential;
/**
* @var string
*/
protected $type;
/**
* Credential constructor.
*
* @param array|Config $config
*
* @throws ReflectionException
*/
public function __construct($config = [])
{
if ($config instanceof Config) {
$config = $this->parse($config);
}
if ($config !== []) {
$this->config = array_change_key_case($config);
$this->parseConfig();
if (\is_array($config)) {
if (empty($config)) {
$this->config = null;
} else {
$this->config = new Config($this->parseConfig($config));
}
} else {
$this->credential = Credentials::get()->getCredential();
$this->config = $config;
}
$this->credential = $this->getCredentials($this->config);
}
/**
* @param Config $config
* @param array $config
*
* @return array
*/
private function parse($config)
private function parseConfig($config)
{
$config = get_object_vars($config);
$res = [];
foreach ($config as $key => $value) {
$res[$this->toUnderScore($key)] = $value;
$res = [];
foreach (\array_change_key_case($config) as $key => $value) {
$res[Helper::snakeToCamelCase($key)] = $value;
}
return $res;
}
private function toUnderScore($str)
{
$dstr = preg_replace_callback('/([A-Z]+)/', function ($matchs) {
return '_' . strtolower($matchs[0]);
}, $str);
return trim(preg_replace('/_{2,}/', '_', $dstr), '_');
}
/**
* @throws ReflectionException
*/
private function parseConfig()
{
if (!isset($this->config['type'])) {
throw new InvalidArgumentException('Missing required type option');
}
$this->type = $this->config['type'];
if (!isset($this->types[$this->type])) {
throw new InvalidArgumentException(
'Invalid type option, support: ' .
implode(', ', array_keys($this->types))
);
}
$class = new ReflectionClass($this->types[$this->type]);
$parameters = [];
/**
* @var $parameter ReflectionParameter
*/
foreach ($class->getConstructor()->getParameters() as $parameter) {
$parameters[] = $this->getValue($parameter);
}
$this->credential = $class->newInstance(...$parameters);
}
/**
* @param ReflectionParameter $parameter
* Credentials getter.
*
* @param Config $config
* @return CredentialsInterface
*
* @return string|array
* @throws ReflectionException
*/
protected function getValue(ReflectionParameter $parameter)
private function getCredentials($config)
{
if ($parameter->name === 'config' || $parameter->name === 'credential') {
return $this->config;
if (is_null($config)) {
return new CredentialsProviderWrap('default', new DefaultCredentialsProvider());
}
foreach ($this->config as $key => $value) {
if (strtolower($parameter->name) === $key) {
return $value;
}
switch ($config->type) {
case 'access_key':
$provider = new StaticAKCredentialsProvider([
'accessKeyId' => $config->accessKeyId,
'accessKeySecret' => $config->accessKeySecret,
]);
return new CredentialsProviderWrap('access_key', $provider);
case 'sts':
$provider = new StaticSTSCredentialsProvider([
'accessKeyId' => $config->accessKeyId,
'accessKeySecret' => $config->accessKeySecret,
'securityToken' => $config->securityToken,
]);
return new CredentialsProviderWrap('sts', $provider);
case 'bearer':
return new BearerTokenCredential($config->bearerToken);
case 'ram_role_arn':
if (!is_null($config->securityToken) && $config->securityToken !== '') {
$innerProvider = new StaticSTSCredentialsProvider([
'accessKeyId' => $config->accessKeyId,
'accessKeySecret' => $config->accessKeySecret,
'securityToken' => $config->securityToken,
]);
} else {
$innerProvider = new StaticAKCredentialsProvider([
'accessKeyId' => $config->accessKeyId,
'accessKeySecret' => $config->accessKeySecret,
]);
}
$provider = new RamRoleArnCredentialsProvider([
'credentialsProvider' => $innerProvider,
'roleArn' => $config->roleArn,
'roleSessionName' => $config->roleSessionName,
'policy' => $config->policy,
'durationSeconds' => $config->roleSessionExpiration,
'externalId' => $config->externalId,
'stsEndpoint' => $config->STSEndpoint,
], [
'connectTimeout' => $config->connectTimeout,
'readTimeout' => $config->readTimeout,
]);
return new CredentialsProviderWrap('ram_role_arn', $provider);
case 'rsa_key_pair':
$provider = new RsaKeyPairCredentialsProvider([
'publicKeyId' => $config->publicKeyId,
'privateKeyFile' => $config->privateKeyFile,
'durationSeconds' => $config->roleSessionExpiration,
'stsEndpoint' => $config->STSEndpoint,
], [
'connectTimeout' => $config->connectTimeout,
'readTimeout' => $config->readTimeout,
]);
return new CredentialsProviderWrap('rsa_key_pair', $provider);
case 'ecs_ram_role':
$provider = new EcsRamRoleCredentialsProvider([
'roleName' => $config->roleName,
'disableIMDSv1' => $config->disableIMDSv1,
], [
'connectTimeout' => $config->connectTimeout,
'readTimeout' => $config->readTimeout,
]);
return new CredentialsProviderWrap('ecs_ram_role', $provider);
case 'oidc_role_arn':
$provider = new OIDCRoleArnCredentialsProvider([
'roleArn' => $config->roleArn,
'oidcProviderArn' => $config->oidcProviderArn,
'oidcTokenFilePath' => $config->oidcTokenFilePath,
'roleSessionName' => $config->roleSessionName,
'policy' => $config->policy,
'durationSeconds' => $config->roleSessionExpiration,
'stsEndpoint' => $config->STSEndpoint,
], [
'connectTimeout' => $config->connectTimeout,
'readTimeout' => $config->readTimeout,
]);
return new CredentialsProviderWrap('oidc_role_arn', $provider);
case "credentials_uri":
$provider = new URLCredentialsProvider([
'credentialsURI' => $config->credentialsURI,
], [
'connectTimeout' => $config->connectTimeout,
'readTimeout' => $config->readTimeout,
]);
return new CredentialsProviderWrap('credentials_uri', $provider);
default:
throw new InvalidArgumentException('Unsupported credential type option: ' . $config->type . ', support: access_key, sts, bearer, ecs_ram_role, ram_role_arn, rsa_key_pair, oidc_role_arn, credentials_uri');
}
if ($parameter->isDefaultValueAvailable()) {
return $parameter->getDefaultValue();
}
throw new InvalidArgumentException("Missing required {$parameter->name} option in config for {$this->type}");
}
/**
* @return AccessKeyCredential|BearerTokenCredential|EcsRamRoleCredential|RamRoleArnCredential|RsaKeyPairCredential
* @return CredentialModel
* @throws RuntimeException
* @throws GuzzleException
*/
public function getCredential()
{
return $this->credential;
return $this->credential->getCredential();
}
/**
@@ -157,17 +192,68 @@ class Credential
*/
public function getConfig()
{
return $this->config;
return $this->config->toMap();
}
/**
* @deprecated use getCredential() instead
*
* @return string
* @throws RuntimeException
* @throws GuzzleException
*/
public function getType()
{
return $this->type;
return $this->credential->getCredential()->getType();
}
/**
* @deprecated use getCredential() instead
*
* @return string
* @throws RuntimeException
* @throws GuzzleException
*/
public function getAccessKeyId()
{
return $this->credential->getCredential()->getAccessKeyId();
}
/**
* @deprecated use getCredential() instead
*
* @return string
* @throws RuntimeException
* @throws GuzzleException
*/
public function getAccessKeySecret()
{
return $this->credential->getCredential()->getAccessKeySecret();
}
/**
* @deprecated use getCredential() instead
*
* @return string
* @throws RuntimeException
* @throws GuzzleException
*/
public function getSecurityToken()
{
return $this->credential->getCredential()->getSecurityToken();
}
/**
* @deprecated use getCredential() instead
*
* @return string
* @throws RuntimeException
* @throws GuzzleException
*/
public function getBearerToken()
{
return $this->credential->getCredential()->getBearerToken();
}
/**
* @param string $name

View File

@@ -2,49 +2,269 @@
namespace AlibabaCloud\Credentials\Credential;
class Config
use AlibabaCloud\Tea\Model;
class Config extends Model
{
public function validate()
{
}
public function toMap()
{
$res = [];
if (null !== $this->accessKeyId) {
$res['accessKeyId'] = $this->accessKeyId;
}
if (null !== $this->accessKeySecret) {
$res['accessKeySecret'] = $this->accessKeySecret;
}
if (null !== $this->securityToken) {
$res['securityToken'] = $this->securityToken;
}
if (null !== $this->bearerToken) {
$res['bearerToken'] = $this->bearerToken;
}
if (null !== $this->durationSeconds) {
$res['durationSeconds'] = $this->durationSeconds;
}
if (null !== $this->roleArn) {
$res['roleArn'] = $this->roleArn;
}
if (null !== $this->policy) {
$res['policy'] = $this->policy;
}
if (null !== $this->roleSessionExpiration) {
$res['roleSessionExpiration'] = $this->roleSessionExpiration;
}
if (null !== $this->roleSessionName) {
$res['roleSessionName'] = $this->roleSessionName;
}
if (null !== $this->publicKeyId) {
$res['publicKeyId'] = $this->publicKeyId;
}
if (null !== $this->privateKeyFile) {
$res['privateKeyFile'] = $this->privateKeyFile;
}
if (null !== $this->roleName) {
$res['roleName'] = $this->roleName;
}
if (null !== $this->credentialsURI) {
$res['credentialsURI'] = $this->credentialsURI;
}
if (null !== $this->type) {
$res['type'] = $this->type;
}
if (null !== $this->STSEndpoint) {
$res['STSEndpoint'] = $this->STSEndpoint;
}
if (null !== $this->externalId) {
$res['externalId'] = $this->externalId;
}
return $res;
}
/**
* @param array $map
* @return Config
*/
public static function fromMap($map = [])
{
$model = new self();
if (isset($map['accessKeyId'])) {
$model->accessKeyId = $map['accessKeyId'];
}
if (isset($map['accessKeySecret'])) {
$model->accessKeySecret = $map['accessKeySecret'];
}
if (isset($map['securityToken'])) {
$model->securityToken = $map['securityToken'];
}
if (isset($map['bearerToken'])) {
$model->bearerToken = $map['bearerToken'];
}
if (isset($map['durationSeconds'])) {
$model->durationSeconds = $map['durationSeconds'];
}
if (isset($map['roleArn'])) {
$model->roleArn = $map['roleArn'];
}
if (isset($map['policy'])) {
$model->policy = $map['policy'];
}
if (isset($map['roleSessionExpiration'])) {
$model->roleSessionExpiration = $map['roleSessionExpiration'];
}
if (isset($map['roleSessionName'])) {
$model->roleSessionName = $map['roleSessionName'];
}
if (isset($map['publicKeyId'])) {
$model->publicKeyId = $map['publicKeyId'];
}
if (isset($map['privateKeyFile'])) {
$model->privateKeyFile = $map['privateKeyFile'];
}
if (isset($map['roleName'])) {
$model->roleName = $map['roleName'];
}
if (isset($map['credentialsURI'])) {
$model->credentialsURI = $map['credentialsURI'];
}
if (isset($map['type'])) {
$model->type = $map['type'];
}
if (isset($map['STSEndpoint'])) {
$model->STSEndpoint = $map['STSEndpoint'];
}
if (isset($map['externalId'])) {
$model->externalId = $map['externalId'];
}
return $model;
}
/**
* @description credential type
* @example access_key
* @var string
*/
public $type = 'default';
public $accessKeyId = "";
/**
* @description accesskey id
* @var string
*/
public $accessKeyId;
public $accessKeySecret = "";
/**
* @description accesskey secret
* @var string
*/
public $accessKeySecret;
public $securityToken = "";
/**
* @description security token
* @var string
*/
public $securityToken;
public $bearerToken = "";
/**
* @description bearer token
* @var string
*/
public $bearerToken;
public $roleName = "";
/**
* @description role name
* @var string
*/
public $roleName;
public $roleArn = "";
/**
* @description role arn
* @var string
*/
public $roleArn;
public $roleSessionName = "";
/**
* @description oidc provider arn
* @var string
*/
public $oidcProviderArn;
public $host = "";
/**
* @description oidc token file path
* @var string
*/
public $oidcTokenFilePath;
public $publicKeyId = "";
/**
* @description role session expiration
* @example 3600
* @var int
*/
public $roleSessionExpiration;
public $privateKeyFile = "";
/**
* @description role session name
* @var string
*/
public $roleSessionName;
public $readTimeout = 0;
/**
* @description role arn policy
* @var string
*/
public $policy;
public $connectTimeout = 0;
/**
* @description external id for ram role arn
* @var string
*/
public $externalId;
/**
* @description sts endpoint
* @var string
*/
public $STSEndpoint;
public $publicKeyId;
public $privateKeyFile;
/**
* @description read timeout
* @var int
*/
public $readTimeout;
/**
* @description connection timeout
* @var int
*/
public $connectTimeout;
/**
* @description disable IMDS v1
* @var bool
*/
public $disableIMDSv1;
/**
* @description credentials URI
* @var string
*/
public $credentialsURI;
/**
* @deprecated
*/
public $metadataTokenDuration;
/**
* @deprecated
*/
public $durationSeconds;
/**
* @deprecated
*/
public $host;
/**
* @deprecated
*/
public $expiration;
/**
* @deprecated
*/
public $certFile = "";
/**
* @deprecated
*/
public $certPassword = "";
public $proxy = "";
public $expiration = 0;
public function __construct($config)
{
foreach ($config as $k => $v) {
$this->{$k} = $v;
}
}
/**
* @internal
*/
public $proxy;
}

View File

@@ -0,0 +1,143 @@
<?php
// This file is auto-generated, don't edit it. Thanks.
namespace AlibabaCloud\Credentials\Credential;
use AlibabaCloud\Tea\Model;
class CredentialModel extends Model
{
public function validate()
{
}
public function toMap()
{
$res = [];
if (null !== $this->accessKeyId) {
$res['accessKeyId'] = $this->accessKeyId;
}
if (null !== $this->accessKeySecret) {
$res['accessKeySecret'] = $this->accessKeySecret;
}
if (null !== $this->securityToken) {
$res['securityToken'] = $this->securityToken;
}
if (null !== $this->bearerToken) {
$res['bearerToken'] = $this->bearerToken;
}
if (null !== $this->type) {
$res['type'] = $this->type;
}
if (null !== $this->providerName) {
$res['providerName'] = $this->providerName;
}
return $res;
}
/**
* @param array $map
* @return CredentialModel
*/
public static function fromMap($map = [])
{
$model = new self();
if (isset($map['accessKeyId'])) {
$model->accessKeyId = $map['accessKeyId'];
}
if (isset($map['accessKeySecret'])) {
$model->accessKeySecret = $map['accessKeySecret'];
}
if (isset($map['securityToken'])) {
$model->securityToken = $map['securityToken'];
}
if (isset($map['bearerToken'])) {
$model->bearerToken = $map['bearerToken'];
}
if (isset($map['type'])) {
$model->type = $map['type'];
}
if(isset($map['providerName'])){
$model->providerName = $map['providerName'];
}
return $model;
}
/**
* @description accesskey id
* @var string
*/
public $accessKeyId;
/**
* @description accesskey secret
* @var string
*/
public $accessKeySecret;
/**
* @description security token
* @var string
*/
public $securityToken;
/**
* @description bearer token
* @var string
*/
public $bearerToken;
/**
* @description type
* @example access_key
* @var string
*/
public $type;
/**
* @description provider name
* @example cli_profile/static_ak
* @var string
*/
public $providerName;
/**
* @return string
*/
public function getAccessKeyId()
{
return $this->accessKeyId;
}
/**
* @return string
*/
public function getAccessKeySecret()
{
return $this->accessKeySecret;
}
/**
* @return string
*/
public function getSecurityToken()
{
return $this->securityToken;
}
/**
* @return string
*/
public function getBearerToken()
{
return $this->bearerToken;
}
public function getType()
{
return $this->type;
}
public function getProviderName()
{
return $this->providerName;
}
}

View File

@@ -0,0 +1,99 @@
<?php
namespace AlibabaCloud\Credentials\Credential;
use AlibabaCloud\Credentials\Providers\Credentials;
use function PHPUnit\Framework\isNull;
class RefreshResult
{
/**
* RefreshResult constructor.
* @param Credentials $params
* @param int $staleTime
* @param int $prefetchTime
*/
public function __construct($credentials = null, $staleTime = PHP_INT_MAX, $prefetchTime = PHP_INT_MAX)
{
$this->credentials = $credentials;
$this->staleTime = $staleTime;
$this->prefetchTime = $prefetchTime;
}
public function validate() {}
public function toMap()
{
$res = [];
if (null !== $this->staleTime) {
$res['staleTime'] = $this->staleTime;
}
if (null !== $this->prefetchTime) {
$res['prefetchTime'] = $this->prefetchTime;
}
if (null !== $this->credentials) {
$res['credentials'] = $this->credentials;
}
return $res;
}
/**
* @param array $map
* @return RefreshResult
*/
public static function fromMap($map = [])
{
$model = new self();
if (isset($map['staleTime'])) {
$model->staleTime = $map['staleTime'];
}
if (isset($map['prefetchTime'])) {
$model->staleTime = $map['prefetchTime'];
}
if (isset($map['credentials'])) {
$model->staleTime = $map['credentials'];
}
return $model;
}
/**
* @description staleTime
* @var int
*/
public $staleTime;
/**
* @description prefetchTime
* @var int
*/
public $prefetchTime;
/**
* @description credentials
* @var Credentials
*/
public $credentials;
/**
* @return Credentials
*/
public function credentials()
{
return $this->credentials;
}
/**
* @var int
*/
public function staleTime()
{
return $this->staleTime;
}
/**
* @var int
*/
public function prefetchTime()
{
return $this->prefetchTime;
}
}

View File

@@ -3,6 +3,8 @@
namespace AlibabaCloud\Credentials;
use AlibabaCloud\Credentials\Providers\ChainProvider;
use AlibabaCloud\Credentials\Utils\Filter;
use AlibabaCloud\Credentials\Utils\MockTrait;
use ReflectionException;
use RuntimeException;
@@ -99,4 +101,4 @@ class Credentials
self::$credentials[\strtolower($name)] = \array_change_key_case($credential);
}
}
}

View File

@@ -2,9 +2,11 @@
namespace AlibabaCloud\Credentials;
use AlibabaCloud\Credentials\Credential\CredentialModel;
use AlibabaCloud\Credentials\Signature\SignatureInterface;
/**
* @internal This class is intended for internal use within the package.
* Interface CredentialsInterface
*
* @codeCoverageIgnore
@@ -12,12 +14,19 @@ use AlibabaCloud\Credentials\Signature\SignatureInterface;
interface CredentialsInterface
{
/**
* @deprecated
* @return string
*/
public function __toString();
/**
* @deprecated
* @return SignatureInterface
*/
public function getSignature();
/**
* @return CredentialModel
*/
public function getCredential();
}

View File

@@ -0,0 +1,76 @@
<?php
namespace AlibabaCloud\Credentials;
use AlibabaCloud\Credentials\Credential\CredentialModel;
use AlibabaCloud\Credentials\Providers\CredentialsProvider;
/**
* @internal This class is intended for internal use within the package.
* Class CredentialsProviderWrap
*
* @package AlibabaCloud\Credentials
*/
class CredentialsProviderWrap implements CredentialsInterface
{
/**
* @var string
*/
private $typeName;
/**
* @var CredentialsProvider
*/
private $credentialsProvider;
/**
* CLIProfileCredentialsProvider constructor.
*
* @param string $typeName
* @param CredentialsProvider $credentialsProvider
*/
public function __construct($typeName, $credentialsProvider)
{
$this->typeName = $typeName;
$this->credentialsProvider = $credentialsProvider;
}
/**
* @inheritDoc
*/
public function getCredential()
{
$credentials = $this->credentialsProvider->getCredentials();
return new CredentialModel([
'accessKeyId' => $credentials->getAccessKeyId(),
'accessKeySecret' => $credentials->getAccessKeySecret(),
'securityToken' => $credentials->getSecurityToken(),
'type' => $this->typeName,
'providerName' => $credentials->getProviderName(),
]);
}
/**
* @param string $name
* @param array $arguments
*
* @return mixed
*/
public function __call($name, $arguments)
{
return $this->credentialsProvider->$name($arguments);
}
public function __toString()
{
return "credentialsProviderWrap#$this->typeName";
}
/**
* @return ShaHmac1Signature
*/
public function getSignature()
{
return null;
}
}

View File

@@ -2,15 +2,18 @@
namespace AlibabaCloud\Credentials;
use AlibabaCloud\Credentials\Providers\EcsRamRoleProvider;
use AlibabaCloud\Credentials\Request\Request;
use AlibabaCloud\Credentials\Providers\EcsRamRoleCredentialsProvider;
use AlibabaCloud\Credentials\Credential\CredentialModel;
use AlibabaCloud\Credentials\Signature\ShaHmac1Signature;
use AlibabaCloud\Credentials\Request\Request;
use AlibabaCloud\Credentials\Utils\Filter;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use InvalidArgumentException;
use RuntimeException;
/**
* @deprecated
* Use the RAM role of an ECS instance to complete the authentication.
*/
class EcsRamRoleCredential implements CredentialsInterface
@@ -21,16 +24,33 @@ class EcsRamRoleCredential implements CredentialsInterface
*/
private $roleName;
/**
* @var boolean
*/
private $disableIMDSv1;
/**
* @var int
*/
private $metadataTokenDuration;
/**
* EcsRamRoleCredential constructor.
*
* @param $role_name
*/
public function __construct($role_name = null)
public function __construct($role_name = null, $disable_imdsv1 = false, $metadata_token_duration = 21600)
{
Filter::roleName($role_name);
$this->roleName = $role_name;
Filter::disableIMDSv1($disable_imdsv1);
$this->disableIMDSv1 = $disable_imdsv1;
$this->metadataTokenDuration = $metadata_token_duration;
}
/**
@@ -56,8 +76,8 @@ class EcsRamRoleCredential implements CredentialsInterface
public function getRoleNameFromMeta()
{
$options = [
'http_errors' => false,
'timeout' => 1,
'http_errors' => false,
'timeout' => 1,
'connect_timeout' => 1,
];
@@ -75,7 +95,7 @@ class EcsRamRoleCredential implements CredentialsInterface
throw new RuntimeException('Error retrieving credentials from result: ' . $result->getBody());
}
$role_name = (string)$result;
$role_name = (string) $result;
if (!$role_name) {
throw new RuntimeException('Error retrieving credentials from result is empty');
}
@@ -110,13 +130,18 @@ class EcsRamRoleCredential implements CredentialsInterface
}
/**
* @return StsCredential
* @return AlibabaCloud\Credentials\Providers\Credentials
* @throws Exception
* @throws GuzzleException
*/
protected function getSessionCredential()
{
return (new EcsRamRoleProvider($this))->get();
$params = [
"roleName" => $this->roleName,
'disableIMDSv1' => $this->disableIMDSv1,
'metadataTokenDuration' => $this->metadataTokenDuration,
];
return (new EcsRamRoleCredentialsProvider($params))->getCredentials();
}
/**
@@ -148,4 +173,27 @@ class EcsRamRoleCredential implements CredentialsInterface
{
return $this->getSessionCredential()->getExpiration();
}
/**
* @return bool
*/
public function isDisableIMDSv1()
{
return $this->disableIMDSv1;
}
/**
* @inheritDoc
*/
public function getCredential()
{
$credentials = $this->getSessionCredential();
return new CredentialModel([
'accessKeyId' => $credentials->getAccessKeyId(),
'accessKeySecret' => $credentials->getAccessKeySecret(),
'securityToken' => $credentials->getSecurityToken(),
'type' => 'ecs_ram_role',
]);
}
}

View File

@@ -1,134 +0,0 @@
<?php
namespace AlibabaCloud\Credentials;
use InvalidArgumentException;
class Filter
{
/**
* @param $name
*
* @codeCoverageIgnore
* @return string
*/
public static function credentialName($name)
{
if (!is_string($name)) {
throw new InvalidArgumentException('Name must be a string');
}
if ($name === '') {
throw new InvalidArgumentException('Name cannot be empty');
}
return $name;
}
/**
* @param $bearerToken
*
* @return mixed
* @throws InvalidArgumentException
*/
public static function bearerToken($bearerToken)
{
if (!is_string($bearerToken)) {
throw new InvalidArgumentException('Bearer Token must be a string');
}
if ($bearerToken === '') {
throw new InvalidArgumentException('Bearer Token cannot be empty');
}
return $bearerToken;
}
/**
* @param $publicKeyId
*
* @return mixed
*/
public static function publicKeyId($publicKeyId)
{
if (!is_string($publicKeyId)) {
throw new InvalidArgumentException('public_key_id must be a string');
}
if ($publicKeyId === '') {
throw new InvalidArgumentException('public_key_id cannot be empty');
}
return $publicKeyId;
}
/**
* @param $privateKeyFile
*
* @return mixed
*/
public static function privateKeyFile($privateKeyFile)
{
if (!is_string($privateKeyFile)) {
throw new InvalidArgumentException('private_key_file must be a string');
}
if ($privateKeyFile === '') {
throw new InvalidArgumentException('private_key_file cannot be empty');
}
return $privateKeyFile;
}
/**
* @param string|null $role_name
*/
public static function roleName($role_name)
{
if ($role_name === null) {
return;
}
if (!is_string($role_name)) {
throw new InvalidArgumentException('role_name must be a string');
}
if ($role_name === '') {
throw new InvalidArgumentException('role_name cannot be empty');
}
}
/**
* @param string $accessKeyId
* @param string $accessKeySecret
*/
public static function accessKey($accessKeyId, $accessKeySecret)
{
if (!is_string($accessKeyId)) {
throw new InvalidArgumentException('access_key_id must be a string');
}
if ($accessKeyId === '') {
throw new InvalidArgumentException('access_key_id cannot be empty');
}
if (!is_string($accessKeySecret)) {
throw new InvalidArgumentException('access_key_secret must be a string');
}
if ($accessKeySecret === '') {
throw new InvalidArgumentException('access_key_secret cannot be empty');
}
}
/**
* @param int $expiration
*/
public static function expiration($expiration)
{
if (!is_int($expiration)) {
throw new InvalidArgumentException('expiration must be a int');
}
}
}

View File

@@ -0,0 +1,187 @@
<?php
namespace AlibabaCloud\Credentials\Providers;
use AlibabaCloud\Credentials\Utils\Helper;
use RuntimeException;
/**
* @internal This class is intended for internal use within the package.
* Class CLIProfileCredentialsProvider
*
* @package AlibabaCloud\Credentials\Providers
*/
class CLIProfileCredentialsProvider implements CredentialsProvider
{
/**
* @var string
*/
private $profileName;
/**
* @var CredentialsProvider
*/
private $credentialsProvider;
/**
* CLIProfileCredentialsProvider constructor.
*
* @param array $params
*/
public function __construct(array $params = [])
{
$this->filterProfileName($params);
}
private function filterProfileName(array $params)
{
if (Helper::envNotEmpty('ALIBABA_CLOUD_PROFILE')) {
$this->profileName = Helper::env('ALIBABA_CLOUD_PROFILE');
}
if (isset($params['profileName'])) {
$this->profileName = $params['profileName'];
}
}
/**
* @return bool
*/
private function shouldReloadCredentialsProvider()
{
if (is_null($this->credentialsProvider)) {
return true;
}
return false;
}
/**
* @return CredentialsProvider
*/
protected function reloadCredentialsProvider($profileFile, $profileName)
{
if (!Helper::inOpenBasedir($profileFile)) {
throw new RuntimeException('Unable to open credentials file: ' . $profileFile);
}
if (!\is_readable($profileFile) || !\is_file($profileFile)) {
throw new RuntimeException('Credentials file is not readable: ' . $profileFile);
}
$jsonContent = \file_get_contents($profileFile);
$fileArray = json_decode($jsonContent, true);
if (\is_array($fileArray) && !empty($fileArray)) {
if (is_null($profileName) || $profileName === '') {
$profileName = $fileArray['current'];
}
if (isset($fileArray['profiles'])) {
foreach ($fileArray['profiles'] as $profile) {
if (Helper::unsetReturnNull($profile, 'name') === $profileName) {
switch (Helper::unsetReturnNull($profile, 'mode')) {
case 'AK':
return new StaticAKCredentialsProvider([
'accessKeyId' => Helper::unsetReturnNull($profile, 'access_key_id'),
'accessKeySecret' => Helper::unsetReturnNull($profile, 'access_key_secret'),
]);
case 'RamRoleArn':
$innerProvider = new StaticAKCredentialsProvider([
'accessKeyId' => Helper::unsetReturnNull($profile, 'access_key_id'),
'accessKeySecret' => Helper::unsetReturnNull($profile, 'access_key_secret'),
]);
return new RamRoleArnCredentialsProvider([
'credentialsProvider' => $innerProvider,
'roleArn' => Helper::unsetReturnNull($profile, 'ram_role_arn'),
'roleSessionName' => Helper::unsetReturnNull($profile, 'ram_session_name'),
'durationSeconds' => Helper::unsetReturnNull($profile, 'expired_seconds'),
'policy' => Helper::unsetReturnNull($profile, 'policy'),
'externalId' => Helper::unsetReturnNull($profile, 'external_id'),
'stsRegionId' => Helper::unsetReturnNull($profile, 'sts_region'),
'enableVpc' => Helper::unsetReturnNull($profile, 'enable_vpc'),
]);
case 'EcsRamRole':
return new EcsRamRoleCredentialsProvider([
'roleName' => Helper::unsetReturnNull($profile, 'ram_role_name'),
]);
case 'OIDC':
return new OIDCRoleArnCredentialsProvider([
'roleArn' => Helper::unsetReturnNull($profile, 'ram_role_arn'),
'oidcProviderArn' => Helper::unsetReturnNull($profile, 'oidc_provider_arn'),
'oidcTokenFilePath' => Helper::unsetReturnNull($profile, 'oidc_token_file'),
'roleSessionName' => Helper::unsetReturnNull($profile, 'ram_session_name'),
'durationSeconds' => Helper::unsetReturnNull($profile, 'expired_seconds'),
'policy' => Helper::unsetReturnNull($profile, 'policy'),
'stsRegionId' => Helper::unsetReturnNull($profile, 'sts_region'),
'enableVpc' => Helper::unsetReturnNull($profile, 'enable_vpc'),
]);
case 'ChainableRamRoleArn':
$previousProvider = $this->reloadCredentialsProvider($profileFile, Helper::unsetReturnNull($profile, 'source_profile'));
return new RamRoleArnCredentialsProvider([
'credentialsProvider' => $previousProvider,
'roleArn' => Helper::unsetReturnNull($profile, 'ram_role_arn'),
'roleSessionName' => Helper::unsetReturnNull($profile, 'ram_session_name'),
'durationSeconds' => Helper::unsetReturnNull($profile, 'expired_seconds'),
'policy' => Helper::unsetReturnNull($profile, 'policy'),
'externalId' => Helper::unsetReturnNull($profile, 'external_id'),
'stsRegionId' => Helper::unsetReturnNull($profile, 'sts_region'),
'enableVpc' => Helper::unsetReturnNull($profile, 'enable_vpc'),
]);
default:
throw new RuntimeException('Unsupported credential mode from CLI credentials file: ' . Helper::unsetReturnNull($profile, 'mode'));
}
}
}
}
}
throw new RuntimeException('Failed to get credential from CLI credentials file: ' . $profileFile);
}
/**
* Get credential.
*
* @return Credentials
* @throws RuntimeException
*/
public function getCredentials()
{
if (Helper::envNotEmpty('ALIBABA_CLOUD_CLI_PROFILE_DISABLED') && Helper::env('ALIBABA_CLOUD_CLI_PROFILE_DISABLED') === true) {
throw new RuntimeException('CLI credentials file is disabled');
}
$cliProfileFile = self::getDefaultFile();
if ($this->shouldReloadCredentialsProvider()) {
$this->credentialsProvider = $this->reloadCredentialsProvider($cliProfileFile, $this->profileName);
}
$credentials = $this->credentialsProvider->getCredentials();
return new Credentials([
'accessKeyId' => $credentials->getAccessKeyId(),
'accessKeySecret' => $credentials->getAccessKeySecret(),
'securityToken' => $credentials->getSecurityToken(),
'providerName' => $this->getProviderName() . '/' . $this->credentialsProvider->getProviderName(),
]);
}
/**
* Get the default credential file.
*
* @return string
*/
private function getDefaultFile()
{
return Helper::getHomeDirectory() .
DIRECTORY_SEPARATOR .
'.aliyun' .
DIRECTORY_SEPARATOR .
'config.json';
}
/**
* @return string
*/
public function getProviderName()
{
return 'cli_profile';
}
}

View File

@@ -3,12 +3,13 @@
namespace AlibabaCloud\Credentials\Providers;
use AlibabaCloud\Credentials\Credentials;
use AlibabaCloud\Credentials\Helper;
use AlibabaCloud\Credentials\Utils\Helper;
use Closure;
use InvalidArgumentException;
use RuntimeException;
/**
* @deprecated
* Class ChainProvider
*
* @package AlibabaCloud\Credentials\Providers
@@ -184,4 +185,4 @@ class ChainProvider
}
};
}
}
}

View File

@@ -0,0 +1,87 @@
<?php
namespace AlibabaCloud\Credentials\Providers;
/**
* @internal This class is intended for internal use within the package.
* Class Credentials
*
* @package AlibabaCloud\Credentials\Providers
*/
class Credentials
{
/**
* @var string
*/
private $accessKeyId;
/**
* @var string
*/
private $accessKeySecret;
/**
* @var string
*/
private $securityToken;
/**
* @var int
*/
private $expiration;
/**
* @var int
*/
private $providerName;
public function __construct($config = [])
{
if (!empty($config)) {
foreach ($config as $k => $v) {
$this->{$k} = $v;
}
}
}
/**
* @return string
*/
public function getAccessKeyId()
{
return $this->accessKeyId;
}
/**
* @return string
*/
public function getAccessKeySecret()
{
return $this->accessKeySecret;
}
/**
* @return string
*/
public function getSecurityToken()
{
return $this->securityToken;
}
/**
* @return int
*/
public function getExpiration()
{
return $this->expiration;
}
/**
* @return string
*/
public function getProviderName()
{
return $this->providerName;
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace AlibabaCloud\Credentials\Providers;
/**
* @internal This class is intended for internal use within the package.
* Interface CredentialsInterface
*
* @codeCoverageIgnore
*/
interface CredentialsProvider
{
/**
* @return Credentials
*/
public function getCredentials();
/**
* @return string
*/
public function getProviderName();
}

View File

@@ -0,0 +1,175 @@
<?php
namespace AlibabaCloud\Credentials\Providers;
use AlibabaCloud\Credentials\Utils\Filter;
use AlibabaCloud\Credentials\Utils\Helper;
use InvalidArgumentException;
use RuntimeException;
use Exception;
/**
* @internal This class is intended for internal use within the package.
* Class DefaultCredentialsProvider
*
* @package AlibabaCloud\Credentials\Providers
*/
class DefaultCredentialsProvider implements CredentialsProvider
{
/**
* @var array
*/
private static $defaultProviders = [];
/**
* @var bool
*/
private $reuseLastProviderEnabled;
/**
* @var CredentialsProvider
*/
private $lastUsedCredentialsProvider;
/**
* @var array
*/
private static $customChain = [];
/**
* DefaultCredentialsProvider constructor.
* @param array $params
*/
public function __construct(array $params = [])
{
$this->filterReuseLastProviderEnabled($params);
$this->createDefaultChain();
Filter::reuseLastProviderEnabled($this->reuseLastProviderEnabled);
}
private function filterReuseLastProviderEnabled(array $params)
{
$this->reuseLastProviderEnabled = true;
if (isset($params['reuseLastProviderEnabled'])) {
$this->reuseLastProviderEnabled = $params['reuseLastProviderEnabled'];
}
}
private function createDefaultChain()
{
self::$defaultProviders = [
new EnvironmentVariableCredentialsProvider(),
];
if (
Helper::envNotEmpty('ALIBABA_CLOUD_ROLE_ARN')
&& Helper::envNotEmpty('ALIBABA_CLOUD_OIDC_PROVIDER_ARN')
&& Helper::envNotEmpty('ALIBABA_CLOUD_OIDC_TOKEN_FILE')
) {
array_push(
self::$defaultProviders,
new OIDCRoleArnCredentialsProvider()
);
}
array_push(
self::$defaultProviders,
new CLIProfileCredentialsProvider()
);
array_push(
self::$defaultProviders,
new ProfileCredentialsProvider()
);
array_push(
self::$defaultProviders,
new EcsRamRoleCredentialsProvider()
);
if (Helper::envNotEmpty('ALIBABA_CLOUD_CREDENTIALS_URI')) {
array_push(
self::$defaultProviders,
new URLCredentialsProvider()
);
}
}
/**
* @param CredentialsProvider ...$providers
*/
public static function set(...$providers)
{
if (empty($providers)) {
throw new InvalidArgumentException('No providers in chain');
}
foreach ($providers as $provider) {
if (!$provider instanceof CredentialsProvider) {
throw new InvalidArgumentException('Providers must all be CredentialsProvider');
}
}
self::$customChain = $providers;
}
/**
* @return bool
*/
public static function hasCustomChain()
{
return (bool) self::$customChain;
}
public static function flush()
{
self::$customChain = [];
}
/**
* Get credential.
*
* @return Credentials
* @throws RuntimeException
*/
public function getCredentials()
{
if ($this->reuseLastProviderEnabled && !is_null($this->lastUsedCredentialsProvider)) {
$credentials = $this->lastUsedCredentialsProvider->getCredentials();
return new Credentials([
'accessKeyId' => $credentials->getAccessKeyId(),
'accessKeySecret' => $credentials->getAccessKeySecret(),
'securityToken' => $credentials->getSecurityToken(),
'providerName' => $this->getProviderName() . '/' . $this->lastUsedCredentialsProvider->getProviderName(),
]);
}
$providerChain = array_merge(
self::$customChain,
self::$defaultProviders
);
$exceptionMessages = [];
foreach ($providerChain as $provider) {
try {
$credentials = $provider->getCredentials();
$this->lastUsedCredentialsProvider = $provider;
return new Credentials([
'accessKeyId' => $credentials->getAccessKeyId(),
'accessKeySecret' => $credentials->getAccessKeySecret(),
'securityToken' => $credentials->getSecurityToken(),
'providerName' => $this->getProviderName() . '/' . $provider->getProviderName(),
]);
} catch (Exception $exception) {
array_push($exceptionMessages, basename(str_replace('\\', '/', get_class($provider))) . ': ' . $exception->getMessage());
}
}
throw new RuntimeException('Unable to load credentials from any of the providers in the chain: ' . implode(', ', $exceptionMessages));
}
/**
* @inheritDoc
*/
public function getProviderName()
{
return "default";
}
}

View File

@@ -0,0 +1,276 @@
<?php
namespace AlibabaCloud\Credentials\Providers;
use AlibabaCloud\Credentials\Utils\Helper;
use AlibabaCloud\Credentials\Utils\Filter;
use AlibabaCloud\Credentials\Request\Request;
use GuzzleHttp\Exception\GuzzleException;
use InvalidArgumentException;
use RuntimeException;
use AlibabaCloud\Credentials\Credential\RefreshResult;
/**
* @internal This class is intended for internal use within the package.
* Class EcsRamRoleCredentialsProvider
*
* @package AlibabaCloud\Credentials\Providers
*/
class EcsRamRoleCredentialsProvider extends SessionCredentialsProvider
{
/**
* @var string
*/
private $metadataHost = 'http://100.100.100.200';
/**
* @var string
*/
private $ecsUri = '/latest/meta-data/ram/security-credentials/';
/**
* @var string
*/
private $metadataTokenUri = '/latest/api/token';
/**
* @var string
*/
private $roleName;
/**
* @var boolean
*/
private $disableIMDSv1 = false;
/**
* @var int
*/
private $metadataTokenDuration = 21600;
/**
* @var int
*/
private $connectTimeout = 1;
/**
* @var int
*/
private $readTimeout = 1;
/**
* EcsRamRoleCredentialsProvider constructor.
*
* @param array $params
* @param array $options
*/
public function __construct(array $params = [], array $options = [])
{
$this->filterOptions($options);
$this->filterRoleName($params);
$this->filterDisableECSIMDSv1($params);
Filter::roleName($this->roleName);
Filter::disableIMDSv1($this->disableIMDSv1);
}
private function filterOptions(array $options)
{
if (isset($options['connectTimeout'])) {
$this->connectTimeout = $options['connectTimeout'];
}
if (isset($options['readTimeout'])) {
$this->readTimeout = $options['readTimeout'];
}
Filter::timeout($this->connectTimeout, $this->readTimeout);
}
private function filterRoleName(array $params)
{
if (Helper::envNotEmpty('ALIBABA_CLOUD_ECS_METADATA')) {
$this->roleName = Helper::env('ALIBABA_CLOUD_ECS_METADATA');
}
if (isset($params['roleName'])) {
$this->roleName = $params['roleName'];
}
}
private function filterDisableECSIMDSv1($params)
{
if (Helper::envNotEmpty('ALIBABA_CLOUD_IMDSV1_DISABLED')) {
$this->disableIMDSv1 = Helper::env('ALIBABA_CLOUD_IMDSV1_DISABLED') === true ? true : false;
}
if (isset($params['disableIMDSv1'])) {
$this->disableIMDSv1 = $params['disableIMDSv1'];
}
}
/**
* Get credentials by request.
*
* @return RefreshResult
* @throws InvalidArgumentException
* @throws RuntimeException
* @throws GuzzleException
*/
public function refreshCredentials()
{
if (Helper::envNotEmpty('ALIBABA_CLOUD_ECS_METADATA_DISABLED') && Helper::env('ALIBABA_CLOUD_ECS_METADATA_DISABLED') === true) {
throw new RuntimeException('IMDS credentials is disabled');
}
if (is_null($this->roleName) || $this->roleName === '') {
$this->roleName = $this->getRoleNameFromMeta();
}
$url = $this->metadataHost . $this->ecsUri . $this->roleName;
$options = Request::commonOptions();
$options['read_timeout'] = $this->readTimeout;
$options['connect_timeout'] = $this->connectTimeout;
$metadataToken = $this->getMetadataToken();
if (!is_null($metadataToken)) {
$options['headers']['X-aliyun-ecs-metadata-token'] = $metadataToken;
}
$result = Request::createClient()->request('GET', $url, $options);
if ($result->getStatusCode() === 404) {
throw new InvalidArgumentException('The role was not found in the instance' . (string) $result);
}
if ($result->getStatusCode() !== 200) {
throw new RuntimeException('Error refreshing credentials from IMDS, statusCode: ' . $result->getStatusCode() . ', result: ' . (string) $result);
}
$credentials = $result->toArray();
if (!isset($credentials['AccessKeyId']) || !isset($credentials['AccessKeySecret']) || !isset($credentials['SecurityToken'])) {
throw new RuntimeException('Error retrieving credentials from IMDS result:' . $result->toJson());
}
if (!isset($credentials['Code']) || $credentials['Code'] !== 'Success') {
throw new RuntimeException('Error retrieving credentials from IMDS result, Code is not Success:' . $result->toJson());
}
return new RefreshResult(new Credentials([
'accessKeyId' => $credentials['AccessKeyId'],
'accessKeySecret' => $credentials['AccessKeySecret'],
'securityToken' => $credentials['SecurityToken'],
'expiration' => \strtotime($credentials['Expiration']),
'providerName' => $this->getProviderName(),
]), $this->getStaleTime(strtotime($credentials["Expiration"])), $this->getPrefetchTime(strtotime($credentials["Expiration"])));
}
/**
* @return string
* @throws InvalidArgumentException
* @throws RuntimeException
* @throws GuzzleException
*/
private function getRoleNameFromMeta()
{
$options = Request::commonOptions();
$options['read_timeout'] = $this->readTimeout;
$options['connect_timeout'] = $this->connectTimeout;
$metadataToken = $this->getMetadataToken();
if (!is_null($metadataToken)) {
$options['headers']['X-aliyun-ecs-metadata-token'] = $metadataToken;
}
$result = Request::createClient()->request(
'GET',
'http://100.100.100.200/latest/meta-data/ram/security-credentials/',
$options
);
if ($result->getStatusCode() === 404) {
throw new InvalidArgumentException('The role name was not found in the instance' . (string) $result);
}
if ($result->getStatusCode() !== 200) {
throw new RuntimeException('Error retrieving role name from result: ' . (string) $result);
}
$role_name = (string) $result;
if (!$role_name) {
throw new RuntimeException('Error retrieving role name from result is empty');
}
return $role_name;
}
/**
* Get metadata token by request.
*
* @return string
* @throws RuntimeException
* @throws GuzzleException
*/
private function getMetadataToken()
{
$url = $this->metadataHost . $this->metadataTokenUri;
$options = Request::commonOptions();
$options['read_timeout'] = $this->readTimeout;
$options['connect_timeout'] = $this->connectTimeout;
$options['headers']['X-aliyun-ecs-metadata-token-ttl-seconds'] = $this->metadataTokenDuration;
$result = Request::createClient()->request('PUT', $url, $options);
if ($result->getStatusCode() != 200) {
if ($this->disableIMDSv1) {
throw new RuntimeException('Failed to get token from ECS Metadata Service. HttpCode= ' . $result->getStatusCode());
}
return null;
}
return (string) $result;
}
/**
* @var int
*/
public function getPrefetchTime($expiration)
{
return $expiration <= 0 ?
time() + (5 * 60) :
time() + (60 * 60);
}
/**
* @return string
*/
public function key()
{
return 'ecs_ram_role#roleName#' . $this->roleName;
}
/**
* @return string
*/
public function getProviderName()
{
return 'ecs_ram_role';
}
/**
* @return string
*/
public function getRoleName()
{
return $this->roleName;
}
/**
* @return bool
*/
public function isDisableIMDSv1()
{
return $this->disableIMDSv1;
}
}

View File

@@ -1,94 +0,0 @@
<?php
namespace AlibabaCloud\Credentials\Providers;
use AlibabaCloud\Credentials\Request\Request;
use AlibabaCloud\Credentials\StsCredential;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use AlibabaCloud\Tea\Response;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
/**
* Class EcsRamRoleProvider
*
* @package AlibabaCloud\Credentials\Providers
*/
class EcsRamRoleProvider extends Provider
{
/**
* Expiration time slot for temporary security credentials.
*
* @var int
*/
protected $expirationSlot = 10;
/**
* @var string
*/
private $uri = 'http://100.100.100.200/latest/meta-data/ram/security-credentials/';
/**
* Get credential.
*
* @return StsCredential
* @throws Exception
* @throws GuzzleException
*/
public function get()
{
$result = $this->getCredentialsInCache();
if ($result === null) {
$result = $this->request();
if (!isset($result['AccessKeyId'], $result['AccessKeySecret'], $result['SecurityToken'])) {
throw new RuntimeException($this->error);
}
$this->cache($result->toArray());
}
return new StsCredential(
$result['AccessKeyId'],
$result['AccessKeySecret'],
strtotime($result['Expiration']),
$result['SecurityToken']
);
}
/**
* Get credentials by request.
*
* @return ResponseInterface
* @throws Exception
* @throws GuzzleException
*/
public function request()
{
$credential = $this->credential;
$url = $this->uri . $credential->getRoleName();
$options = [
'http_errors' => false,
'timeout' => 1,
'connect_timeout' => 1,
];
$result = Request::createClient()->request('GET', $url, $options);
if ($result->getStatusCode() === 404) {
$message = 'The role was not found in the instance';
throw new InvalidArgumentException($message);
}
if ($result->getStatusCode() !== 200) {
throw new RuntimeException('Error retrieving credentials from result: ' . $result->toJson());
}
return $result;
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace AlibabaCloud\Credentials\Providers;
use AlibabaCloud\Credentials\Utils\Helper;
use InvalidArgumentException;
/**
* @internal This class is intended for internal use within the package.
* Class EnvironmentVariableCredentialsProvider
*
* @package AlibabaCloud\Credentials\Providers
*/
class EnvironmentVariableCredentialsProvider implements CredentialsProvider
{
/**
* EnvironmentVariableCredentialsProvider constructor.
*/
public function __construct() {}
/**
* Get credential.
*
* @return Credentials
* @throws InvalidArgumentException
*/
public function getCredentials()
{
if (Helper::envNotEmpty('ALIBABA_CLOUD_ACCESS_KEY_ID')) {
$accessKeyId = Helper::env('ALIBABA_CLOUD_ACCESS_KEY_ID');
} else {
throw new InvalidArgumentException('Access key ID must be specified via environment variable (ALIBABA_CLOUD_ACCESS_KEY_ID)');
}
if (Helper::envNotEmpty('ALIBABA_CLOUD_ACCESS_KEY_SECRET')) {
$accessKeySecret = Helper::env('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
} else {
throw new InvalidArgumentException('Access key Secret must be specified via environment variable (ALIBABA_CLOUD_ACCESS_KEY_SECRET)');
}
if (Helper::envNotEmpty('ALIBABA_CLOUD_SECURITY_TOKEN')) {
$securityToken = Helper::env('ALIBABA_CLOUD_SECURITY_TOKEN');
return new Credentials([
'accessKeyId' => $accessKeyId,
'accessKeySecret' => $accessKeySecret,
'securityToken' => $securityToken,
'providerName' => $this->getProviderName(),
]);
}
return new Credentials([
'accessKeyId' => $accessKeyId,
'accessKeySecret' => $accessKeySecret,
'providerName' => $this->getProviderName(),
]);
}
/**
* @inheritDoc
*/
public function getProviderName()
{
return "env";
}
}

View File

@@ -0,0 +1,264 @@
<?php
namespace AlibabaCloud\Credentials\Providers;
use AlibabaCloud\Credentials\Utils\Helper;
use AlibabaCloud\Credentials\Utils\Filter;
use AlibabaCloud\Credentials\Request\Request;
use GuzzleHttp\Psr7\Uri;
use GuzzleHttp\Exception\GuzzleException;
use InvalidArgumentException;
use RuntimeException;
use Exception;
use AlibabaCloud\Credentials\Credential\RefreshResult;
/**
* @internal This class is intended for internal use within the package.
* Class OIDCRoleArnCredentialsProvider
*
* @package AlibabaCloud\Credentials\Providers
*/
class OIDCRoleArnCredentialsProvider extends SessionCredentialsProvider
{
/**
* @var string
*/
private $roleArn;
/**
* @var string
*/
private $oidcProviderArn;
/**
* @var string
*/
private $oidcTokenFilePath;
/**
* @var string
*/
private $roleSessionName;
/**
* @description role session expiration
* @example 3600
* @var int
*/
private $durationSeconds = 3600;
/**
* @var string
*/
private $policy;
/**
* @var string
*/
private $stsEndpoint;
/**
* @var int
*/
private $connectTimeout = 5;
/**
* @var int
*/
private $readTimeout = 5;
/**
* OIDCRoleArnCredentialsProvider constructor.
*
* @param array $params
* @param array $options
*/
public function __construct(array $params = [], array $options = [])
{
$this->filterOptions($options);
$this->filterRoleArn($params);
$this->filterOIDCProviderArn($params);
$this->filterOIDCTokenFilePath($params);
$this->filterRoleSessionName($params);
$this->filterDurationSeconds($params);
$this->filterPolicy($params);
$this->filterSTSEndpoint($params);
}
private function filterRoleArn(array $params)
{
if (Helper::envNotEmpty('ALIBABA_CLOUD_ROLE_ARN')) {
$this->roleArn = Helper::env('ALIBABA_CLOUD_ROLE_ARN');
}
if (isset($params['roleArn'])) {
$this->roleArn = $params['roleArn'];
}
Filter::roleArn($this->roleArn);
}
private function filterOIDCProviderArn(array $params)
{
if (Helper::envNotEmpty('ALIBABA_CLOUD_OIDC_PROVIDER_ARN')) {
$this->oidcProviderArn = Helper::env('ALIBABA_CLOUD_OIDC_PROVIDER_ARN');
}
if (isset($params['oidcProviderArn'])) {
$this->oidcProviderArn = $params['oidcProviderArn'];
}
Filter::oidcProviderArn($this->oidcProviderArn);
}
private function filterOIDCTokenFilePath(array $params)
{
if (Helper::envNotEmpty('ALIBABA_CLOUD_OIDC_TOKEN_FILE')) {
$this->oidcTokenFilePath = Helper::env('ALIBABA_CLOUD_OIDC_TOKEN_FILE');
}
if (isset($params['oidcTokenFilePath'])) {
$this->oidcTokenFilePath = $params['oidcTokenFilePath'];
}
Filter::oidcTokenFilePath($this->oidcTokenFilePath);
}
private function filterRoleSessionName(array $params)
{
if (Helper::envNotEmpty('ALIBABA_CLOUD_ROLE_SESSION_NAME')) {
$this->roleSessionName = Helper::env('ALIBABA_CLOUD_ROLE_SESSION_NAME');
}
if (isset($params['roleSessionName'])) {
$this->roleSessionName = $params['roleSessionName'];
}
if (is_null($this->roleSessionName) || $this->roleSessionName === '') {
$this->roleSessionName = 'phpSdkRoleSessionName';
}
}
private function filterDurationSeconds(array $params)
{
if (isset($params['durationSeconds'])) {
if (is_int($params['durationSeconds'])) {
$this->durationSeconds = $params['durationSeconds'];
}
}
if ($this->durationSeconds < 900) {
throw new InvalidArgumentException('Role session expiration should be in the range of 900s - max session duration');
}
}
private function filterPolicy(array $params)
{
if (isset($params['policy'])) {
if (is_string($params['policy'])) {
$this->policy = $params['policy'];
}
if (is_array($params['policy'])) {
$this->policy = json_encode($params['policy']);
}
}
}
private function filterSTSEndpoint(array $params)
{
if (Helper::envNotEmpty('ALIBABA_CLOUD_STS_REGION')) {
$this->stsEndpoint = 'sts' . Helper::env('ALIBABA_CLOUD_STS_REGION') . '.aliyuncs.com';
}
if (isset($params['stsRegionId'])) {
$this->stsEndpoint = 'sts' . $params['stsRegionId'] . '.aliyuncs.com';
}
if (isset($params['stsEndpoint'])) {
$this->stsEndpoint = $params['stsEndpoint'];
}
if (is_null($this->stsEndpoint) || $this->stsEndpoint === '') {
$this->stsEndpoint = 'sts.aliyuncs.com';
}
}
private function filterOptions(array $options)
{
if (isset($options['connectTimeout'])) {
$this->connectTimeout = $options['connectTimeout'];
}
if (isset($options['readTimeout'])) {
$this->readTimeout = $options['readTimeout'];
}
Filter::timeout($this->connectTimeout, $this->readTimeout);
}
/**
* Get credentials by request.
*
* @return RefreshResult
* @throws RuntimeException
* @throws GuzzleException
*/
public function refreshCredentials()
{
$options = Request::commonOptions();
$options['read_timeout'] = $this->readTimeout;
$options['connect_timeout'] = $this->connectTimeout;
$options['query']['Action'] = 'AssumeRoleWithOIDC';
$options['query']['Version'] = '2015-04-01';
$options['query']['Format'] = 'JSON';
$options['query']['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z');
$options['query']['RoleArn'] = $this->roleArn;
$options['query']['OIDCProviderArn'] = $this->oidcProviderArn;
try {
$oidcToken = file_get_contents($this->oidcTokenFilePath);
$options['query']['OIDCToken'] = $oidcToken;
} catch (Exception $exception) {
throw new InvalidArgumentException($exception->getMessage());
}
$options['query']['RoleSessionName'] = $this->roleSessionName;
$options['query']['DurationSeconds'] = (string) $this->durationSeconds;
if (!is_null($this->policy)) {
$options['query']['Policy'] = $this->policy;
}
$url = (new Uri())->withScheme('https')->withHost($this->stsEndpoint);
$result = Request::createClient()->request('POST', $url, $options);
if ($result->getStatusCode() !== 200) {
throw new RuntimeException('Error refreshing credentials from OIDC, statusCode: ' . $result->getStatusCode() . ', result: ' . (string) $result);
}
$json = $result->toArray();
$credentials = $json['Credentials'];
if (!isset($credentials['AccessKeyId']) || !isset($credentials['AccessKeySecret']) || !isset($credentials['SecurityToken'])) {
throw new RuntimeException('Error retrieving credentials from OIDC result:' . $result->toJson());
}
return new RefreshResult(new Credentials([
'accessKeyId' => $credentials['AccessKeyId'],
'accessKeySecret' => $credentials['AccessKeySecret'],
'securityToken' => $credentials['SecurityToken'],
'expiration' => \strtotime($credentials['Expiration']),
'providerName' => $this->getProviderName(),
]), $this->getStaleTime(strtotime($credentials['Expiration'])) );
}
public function key()
{
return 'oidc_role_arn#roleArn#' . $this->roleArn . '#oidcProviderArn#' . $this->oidcProviderArn . '#roleSessionName#' . $this->roleSessionName;
}
public function getProviderName()
{
return 'oidc_role_arn';
}
}

View File

@@ -0,0 +1,188 @@
<?php
namespace AlibabaCloud\Credentials\Providers;
use AlibabaCloud\Credentials\Utils\Helper;
use RuntimeException;
/**
* @internal This class is intended for internal use within the package.
* Class ProfileCredentialsProvider
*
* @package AlibabaCloud\Credentials\Providers
*/
class ProfileCredentialsProvider implements CredentialsProvider
{
/**
* @var string
*/
private $profileName;
/**
* @var string
*/
private $profileFile;
/**
* @var CredentialsProvider
*/
private $credentialsProvider;
/**
* ProfileCredentialsProvider constructor.
*
* @param array $params
*/
public function __construct(array $params = [])
{
$this->filterProfileName($params);
$this->filterProfileFile();
}
private function filterProfileName(array $params)
{
if (Helper::envNotEmpty('ALIBABA_CLOUD_PROFILE')) {
$this->profileName = Helper::env('ALIBABA_CLOUD_PROFILE');
}
if (isset($params['profileName'])) {
$this->profileName = $params['profileName'];
}
if (is_null($this->profileName) || $this->profileName === '') {
$this->profileName = 'default';
}
}
private function filterProfileFile()
{
$this->profileFile = Helper::envNotEmpty('ALIBABA_CLOUD_CREDENTIALS_FILE');
if (!$this->profileFile) {
$this->profileFile = self::getDefaultFile();
}
}
/**
* @return bool
*/
private function shouldReloadCredentialsProvider()
{
if (is_null($this->credentialsProvider)) {
return true;
}
return false;
}
/**
* @return CredentialsProvider
*/
private function reloadCredentialsProvider($profileFile, $profileName)
{
if (!Helper::inOpenBasedir($profileFile)) {
throw new RuntimeException('Unable to open credentials file: ' . $profileFile);
}
if (!\is_readable($profileFile) || !\is_file($profileFile)) {
throw new RuntimeException('Credentials file is not readable: ' . $profileFile);
}
$fileArray = \parse_ini_file($profileFile, true);
if (\is_array($fileArray) && !empty($fileArray)) {
$credentialsConfigures = [];
foreach (\array_change_key_case($fileArray) as $name => $configures) {
if ($name === $profileName) {
$credentialsConfigures = $configures;
break;
}
}
if (\is_array($credentialsConfigures) && !empty($credentialsConfigures)) {
switch (Helper::unsetReturnNull($credentialsConfigures, 'type')) {
case 'access_key':
return new StaticAKCredentialsProvider([
'accessKeyId' => Helper::unsetReturnNull($credentialsConfigures, 'access_key_id'),
'accessKeySecret' => Helper::unsetReturnNull($credentialsConfigures, 'access_key_secret'),
]);
case 'ram_role_arn':
$innerProvider = new StaticAKCredentialsProvider([
'accessKeyId' => Helper::unsetReturnNull($credentialsConfigures, 'access_key_id'),
'accessKeySecret' => Helper::unsetReturnNull($credentialsConfigures, 'access_key_secret'),
]);
return new RamRoleArnCredentialsProvider([
'credentialsProvider' => $innerProvider,
'roleArn' => Helper::unsetReturnNull($credentialsConfigures, 'role_arn'),
'roleSessionName' => Helper::unsetReturnNull($credentialsConfigures, 'role_session_name'),
'policy' => Helper::unsetReturnNull($credentialsConfigures, 'policy'),
]);
case 'ecs_ram_role':
return new EcsRamRoleCredentialsProvider([
'roleName' => Helper::unsetReturnNull($credentialsConfigures, 'role_name'),
]);
case 'oidc_role_arn':
return new OIDCRoleArnCredentialsProvider([
'roleArn' => Helper::unsetReturnNull($credentialsConfigures, 'role_arn'),
'oidcProviderArn' => Helper::unsetReturnNull($credentialsConfigures, 'oidc_provider_arn'),
'oidcTokenFilePath' => Helper::unsetReturnNull($credentialsConfigures, 'oidc_token_file_path'),
'roleSessionName' => Helper::unsetReturnNull($credentialsConfigures, 'role_session_name'),
'policy' => Helper::unsetReturnNull($credentialsConfigures, 'policy'),
]);
case 'rsa_key_pair':
return new RsaKeyPairCredentialsProvider([
'publicKeyId' => Helper::unsetReturnNull($credentialsConfigures, 'public_key_id'),
'privateKeyFile' => Helper::unsetReturnNull($credentialsConfigures, 'private_key_file'),
]);
default:
throw new RuntimeException('Unsupported credential type from credentials file: ' . Helper::unsetReturnNull($credentialsConfigures, 'type'));
}
}
}
throw new RuntimeException('Failed to get credential from credentials file: ' . $profileFile);
}
/**
* Get credential.
*
* @return Credentials
* @throws RuntimeException
*/
public function getCredentials()
{
if ($this->shouldReloadCredentialsProvider()) {
$this->credentialsProvider = $this->reloadCredentialsProvider($this->profileFile, $this->profileName);
}
$credentials = $this->credentialsProvider->getCredentials();
return new Credentials([
'accessKeyId' => $credentials->getAccessKeyId(),
'accessKeySecret' => $credentials->getAccessKeySecret(),
'securityToken' => $credentials->getSecurityToken(),
'providerName' => $this->getProviderName() . '/' . $this->credentialsProvider->getProviderName(),
]);
}
/**
* Get the default credential file.
*
* @return string
*/
private function getDefaultFile()
{
return Helper::getHomeDirectory() .
DIRECTORY_SEPARATOR .
'.alibabacloud' .
DIRECTORY_SEPARATOR .
'credentials';
}
/**
* @return string
*/
public function getProviderName()
{
return 'profile';
}
}

View File

@@ -1,82 +0,0 @@
<?php
namespace AlibabaCloud\Credentials\Providers;
use AlibabaCloud\Credentials\CredentialsInterface;
use AlibabaCloud\Credentials\EcsRamRoleCredential;
use AlibabaCloud\Credentials\RamRoleArnCredential;
use AlibabaCloud\Credentials\RsaKeyPairCredential;
abstract class Provider
{
/**
* For TSC Duration Seconds
*/
const DURATION_SECONDS = 3600;
/**
* @var array
*/
protected static $credentialsCache = [];
/**
* Expiration time slot for temporary security credentials.
*
* @var int
*/
protected $expirationSlot = 180;
/**
* @var RamRoleArnCredential|RsaKeyPairCredential|EcsRamRoleCredential
*/
protected $credential;
/**
* @var string
*/
protected $error = 'Result contains no credentials';
/**
* @var array
*/
protected $config = [];
/**
* CredentialTrait constructor.
*
* @param CredentialsInterface $credential
* @param array $config
*/
public function __construct(CredentialsInterface $credential, $config = [])
{
$this->credential = $credential;
$this->config = $config;
}
/**
* Get the credentials from the cache in the validity period.
*
* @return array|null
*/
public function getCredentialsInCache()
{
if (isset(self::$credentialsCache[(string)$this->credential])) {
$result = self::$credentialsCache[(string)$this->credential];
if (\strtotime($result['Expiration']) - \time() >= $this->expirationSlot) {
return $result;
}
}
return null;
}
/**
* Cache credentials.
*
* @param array $credential
*/
protected function cache(array $credential)
{
self::$credentialsCache[(string)$this->credential] = $credential;
}
}

View File

@@ -0,0 +1,317 @@
<?php
namespace AlibabaCloud\Credentials\Providers;
use AlibabaCloud\Credentials\Utils\Helper;
use AlibabaCloud\Credentials\Utils\Filter;
use AlibabaCloud\Credentials\Request\Request;
use GuzzleHttp\Psr7\Uri;
use GuzzleHttp\Exception\GuzzleException;
use InvalidArgumentException;
use RuntimeException;
use AlibabaCloud\Credentials\Credential\RefreshResult;
/**
* @internal This class is intended for internal use within the package.
* Class RamRoleArnCredentialsProvider
*
* @package AlibabaCloud\Credentials\Providers
*/
class RamRoleArnCredentialsProvider extends SessionCredentialsProvider
{
/**
* @var CredentialsProvider
*/
private $credentialsProvider;
/**
* @var string
*/
private $roleArn;
/**
* @var string
*/
private $roleSessionName;
/**
* @description role session expiration
* @example 3600
* @var int
*/
private $durationSeconds = 3600;
/**
* @var string
*/
private $externalId;
/**
* @var string
*/
private $policy;
/**
* @var string
*/
private $stsEndpoint;
/**
* @var int
*/
private $connectTimeout = 5;
/**
* @var int
*/
private $readTimeout = 5;
/**
* RamRoleArnCredentialsProvider constructor.
*
* @param array $params
* @param array $options
*/
public function __construct(array $params = [], array $options = [])
{
$this->filterOptions($options);
$this->filterCredentials($params);
$this->filterRoleArn($params);
$this->filterRoleSessionName($params);
$this->filterDurationSeconds($params);
$this->filterPolicy($params);
$this->filterExternalId($params);
$this->filterSTSEndpoint($params);
}
private function filterRoleArn(array $params)
{
if (Helper::envNotEmpty('ALIBABA_CLOUD_ROLE_ARN')) {
$this->roleArn = Helper::env('ALIBABA_CLOUD_ROLE_ARN');
}
if (isset($params['roleArn'])) {
$this->roleArn = $params['roleArn'];
}
Filter::roleArn($this->roleArn);
}
private function filterRoleSessionName(array $params)
{
if (Helper::envNotEmpty('ALIBABA_CLOUD_ROLE_SESSION_NAME')) {
$this->roleSessionName = Helper::env('ALIBABA_CLOUD_ROLE_SESSION_NAME');
}
if (isset($params['roleSessionName'])) {
$this->roleSessionName = $params['roleSessionName'];
}
if (is_null($this->roleSessionName) || $this->roleSessionName === '') {
$this->roleSessionName = 'phpSdkRoleSessionName';
}
}
private function filterDurationSeconds(array $params)
{
if (isset($params['durationSeconds'])) {
if (is_int($params['durationSeconds'])) {
$this->durationSeconds = $params['durationSeconds'];
}
}
if ($this->durationSeconds < 900) {
throw new InvalidArgumentException('Role session expiration should be in the range of 900s - max session duration');
}
}
private function filterPolicy(array $params)
{
if (isset($params['policy'])) {
if (is_string($params['policy'])) {
$this->policy = $params['policy'];
}
if (is_array($params['policy'])) {
$this->policy = json_encode($params['policy']);
}
}
}
private function filterExternalId(array $params)
{
if (isset($params['externalId'])) {
if (is_string($params['externalId'])) {
$this->externalId = $params['externalId'];
}
}
}
private function filterSTSEndpoint(array $params)
{
if (Helper::envNotEmpty('ALIBABA_CLOUD_STS_REGION')) {
$this->stsEndpoint = 'sts.' . Helper::env('ALIBABA_CLOUD_STS_REGION') . '.aliyuncs.com';
}
if (isset($params['stsRegionId'])) {
$this->stsEndpoint = 'sts.' . $params['stsRegionId'] . '.aliyuncs.com';
}
if (isset($params['stsEndpoint'])) {
$this->stsEndpoint = $params['stsEndpoint'];
}
if (is_null($this->stsEndpoint) || $this->stsEndpoint === '') {
$this->stsEndpoint = 'sts.aliyuncs.com';
}
}
private function filterCredentials(array $params)
{
if (isset($params['credentialsProvider'])) {
if (!($params['credentialsProvider'] instanceof CredentialsProvider)) {
throw new InvalidArgumentException('Invalid credentialsProvider option for ram_role_arn');
}
$this->credentialsProvider = $params['credentialsProvider'];
} else if (isset($params['accessKeyId']) && isset($params['accessKeySecret']) && isset($params['securityToken'])) {
Filter::accessKey($params['accessKeyId'], $params['accessKeySecret']);
Filter::securityToken($params['securityToken']);
$this->credentialsProvider = new StaticSTSCredentialsProvider($params);
} else if (isset($params['accessKeyId']) && isset($params['accessKeySecret'])) {
Filter::accessKey($params['accessKeyId'], $params['accessKeySecret']);
$this->credentialsProvider = new StaticAKCredentialsProvider($params);
} else {
throw new InvalidArgumentException('Missing required credentials option for ram_role_arn');
}
}
private function filterOptions(array $options)
{
if (isset($options['connectTimeout'])) {
$this->connectTimeout = $options['connectTimeout'];
}
if (isset($options['readTimeout'])) {
$this->readTimeout = $options['readTimeout'];
}
Filter::timeout($this->connectTimeout, $this->readTimeout);
}
/**
* Get credentials by request.
*
* @return RefreshResult
* @throws RuntimeException
* @throws GuzzleException
*/
public function refreshCredentials()
{
$options = Request::commonOptions();
$options['read_timeout'] = $this->readTimeout;
$options['connect_timeout'] = $this->connectTimeout;
$options['query']['Action'] = 'AssumeRole';
$options['query']['Version'] = '2015-04-01';
$options['query']['Format'] = 'JSON';
$options['query']['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z');
$options['query']['SignatureMethod'] = 'HMAC-SHA1';
$options['query']['SignatureVersion'] = '1.0';
$options['query']['SignatureNonce'] = Request::uuid(json_encode($options['query']));
$options['query']['RoleArn'] = $this->roleArn;
$options['query']['RoleSessionName'] = $this->roleSessionName;
$options['query']['DurationSeconds'] = (string) $this->durationSeconds;
if (!is_null($this->policy) && $this->policy !== '') {
$options['query']['Policy'] = $this->policy;
}
if (!is_null($this->externalId) && $this->externalId !== '') {
$options['query']['ExternalId'] = $this->externalId;
}
$sessionCredentials = $this->credentialsProvider->getCredentials();
$options['query']['AccessKeyId'] = $sessionCredentials->getAccessKeyId();
if (!is_null($sessionCredentials->getSecurityToken())) {
$options['query']['SecurityToken'] = $sessionCredentials->getSecurityToken();
}
$options['query']['Signature'] = Request::shaHmac1sign(
Request::signString('GET', $options['query']),
$sessionCredentials->getAccessKeySecret() . '&'
);
$url = (new Uri())->withScheme('https')->withHost($this->stsEndpoint);
$result = Request::createClient()->request('GET', $url, $options);
if ($result->getStatusCode() !== 200) {
throw new RuntimeException('Error refreshing credentials from RamRoleArn, statusCode: ' . $result->getStatusCode() . ', result: ' . (string) $result);
}
$json = $result->toArray();
$credentials = $json['Credentials'];
if (!isset($credentials['AccessKeyId']) || !isset($credentials['AccessKeySecret']) || !isset($credentials['SecurityToken'])) {
throw new RuntimeException('Error retrieving credentials from RamRoleArn result:' . $result->toJson());
}
return new RefreshResult(new Credentials([
'accessKeyId' => $credentials['AccessKeyId'],
'accessKeySecret' => $credentials['AccessKeySecret'],
'securityToken' => $credentials['SecurityToken'],
'expiration' => \strtotime($credentials['Expiration']),
'providerName' => $this->getProviderName(),
]), $this->getStaleTime(strtotime($credentials['Expiration'])));
}
public function key()
{
$credentials = $this->credentialsProvider->getCredentials();
return 'ram_role_arn#credential#' . $credentials->getAccessKeyId() . '#roleArn#' . $this->roleArn . '#roleSessionName#' . $this->roleSessionName;
}
public function getProviderName()
{
return 'ram_role_arn/' . $this->credentialsProvider->getProviderName();
}
/**
* @return string
*/
public function getRoleArn()
{
return $this->roleArn;
}
/**
* @return string
*/
public function getRoleSessionName()
{
return $this->roleSessionName;
}
/**
* @return string
*/
public function getPolicy()
{
return $this->policy;
}
/**
* @deprecated
* @return string
*/
public function getOriginalAccessKeyId()
{
return $this->credentialsProvider->getCredentials()->getAccessKeyId();
}
/**
* @deprecated
* @return string
*/
public function getOriginalAccessKeySecret()
{
return $this->credentialsProvider->getCredentials()->getAccessKeySecret();
}
}

View File

@@ -1,49 +0,0 @@
<?php
namespace AlibabaCloud\Credentials\Providers;
use AlibabaCloud\Credentials\Request\AssumeRole;
use AlibabaCloud\Credentials\StsCredential;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use RuntimeException;
class RamRoleArnProvider extends Provider
{
/**
* Get credential.
*
* @return StsCredential
* @throws Exception
* @throws GuzzleException
*/
public function get()
{
$credential = $this->getCredentialsInCache();
if (null === $credential) {
$result = (new AssumeRole($this->credential))->request();
if ($result->getStatusCode() !== 200) {
throw new RuntimeException(isset($result['Message']) ? $result['Message'] : (string)$result->getBody());
}
if (!isset($result['Credentials']['AccessKeyId'],
$result['Credentials']['AccessKeySecret'],
$result['Credentials']['SecurityToken'])) {
throw new RuntimeException($this->error);
}
$credential = $result['Credentials'];
$this->cache($credential);
}
return new StsCredential(
$credential['AccessKeyId'],
$credential['AccessKeySecret'],
strtotime($credential['Expiration']),
$credential['SecurityToken']
);
}
}

View File

@@ -0,0 +1,200 @@
<?php
namespace AlibabaCloud\Credentials\Providers;
use AlibabaCloud\Credentials\Utils\Helper;
use AlibabaCloud\Credentials\Utils\Filter;
use AlibabaCloud\Credentials\Request\Request;
use GuzzleHttp\Psr7\Uri;
use GuzzleHttp\Exception\GuzzleException;
use AlibabaCloud\Credentials\Credential\RefreshResult;
use InvalidArgumentException;
use RuntimeException;
use Exception;
/**
* @internal This class is intended for internal use within the package.
* Class RsaKeyPairCredentialsProvider
*
* @package AlibabaCloud\Credentials\Providers
*/
class RsaKeyPairCredentialsProvider extends SessionCredentialsProvider
{
/**
* @var string
*/
private $publicKeyId;
/**
* @var string
*/
private $privateKey;
/**
* @description role session expiration
* @example 3600
* @var int
*/
private $durationSeconds = 3600;
/**
* @var string
*/
private $stsEndpoint;
/**
* @var int
*/
private $connectTimeout = 5;
/**
* @var int
*/
private $readTimeout = 5;
/**
* RsaKeyPairCredentialsProvider constructor.
*
* @param array $params
* @param array $options
*/
public function __construct(array $params = [], array $options = [])
{
$this->filterOptions($options);
$this->filterDurationSeconds($params);
$this->filterSTSEndpoint($params);
$this->publicKeyId = isset($params['publicKeyId']) ? $params['publicKeyId'] : null;
$privateKeyFile = isset($params['privateKeyFile']) ? $params['privateKeyFile'] : null;
Filter::publicKeyId($this->publicKeyId);
Filter::privateKeyFile($privateKeyFile);
try {
$this->privateKey = file_get_contents($privateKeyFile);
} catch (Exception $exception) {
throw new InvalidArgumentException($exception->getMessage());
}
}
private function filterOptions(array $options)
{
if (isset($options['connectTimeout'])) {
$this->connectTimeout = $options['connectTimeout'];
}
if (isset($options['readTimeout'])) {
$this->readTimeout = $options['readTimeout'];
}
Filter::timeout($this->connectTimeout, $this->readTimeout);
}
private function filterDurationSeconds(array $params)
{
if (isset($params['durationSeconds'])) {
if (is_int($params['durationSeconds'])) {
$this->durationSeconds = $params['durationSeconds'];
}
}
if ($this->durationSeconds < 900) {
throw new InvalidArgumentException('Role session expiration should be in the range of 900s - max session duration');
}
}
private function filterSTSEndpoint(array $params)
{
if (isset($params['stsEndpoint'])) {
$this->stsEndpoint = $params['stsEndpoint'];
}
if (is_null($this->stsEndpoint) || $this->stsEndpoint === '') {
$this->stsEndpoint = 'sts.ap-northeast-1.aliyuncs.com';
}
}
/**
* Get credentials by request.
*
* @return RefreshResult
* @throws RuntimeException
* @throws GuzzleException
*/
public function refreshCredentials()
{
$options = Request::commonOptions();
$options['read_timeout'] = $this->readTimeout;
$options['connect_timeout'] = $this->connectTimeout;
$options['query']['Action'] = 'GenerateSessionAccessKey';
$options['query']['Version'] = '2015-04-01';
$options['query']['Format'] = 'JSON';
$options['query']['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z');
$options['query']['SignatureMethod'] = 'SHA256withRSA';
$options['query']['SignatureType'] = 'PRIVATEKEY';
$options['query']['SignatureVersion'] = '1.0';
$options['query']['SignatureNonce'] = Request::uuid(json_encode($options['query']));
$options['query']['DurationSeconds'] = (string) $this->durationSeconds;
$options['query']['AccessKeyId'] = $this->publicKeyId;
$options['query']['Signature'] = Request::shaHmac256WithRsasign(
Request::signString('GET', $options['query']),
$this->privateKey
);
$url = (new Uri())->withScheme('https')->withHost($this->stsEndpoint);
$result = Request::createClient()->request('GET', $url, $options);
if ($result->getStatusCode() !== 200) {
throw new RuntimeException('Error refreshing credentials from RsaKeyPair, statusCode: ' . $result->getStatusCode() . ', result: ' . (string) $result);
}
$json = $result->toArray();
if (!isset($json['SessionAccessKey']['SessionAccessKeyId']) || !isset($json['SessionAccessKey']['SessionAccessKeySecret'])) {
throw new RuntimeException('Error retrieving credentials from RsaKeyPair result:' . $result->toJson());
}
$credentials = [];
$credentials['AccessKeyId'] = $json['SessionAccessKey']['SessionAccessKeyId'];
$credentials['AccessKeySecret'] = $json['SessionAccessKey']['SessionAccessKeySecret'];
$credentials['Expiration'] = $json['SessionAccessKey']['Expiration'];
$credentials['SecurityToken'] = null;
return new RefreshResult(new Credentials([
'accessKeyId' => $credentials['AccessKeyId'],
'accessKeySecret' => $credentials['AccessKeySecret'],
'securityToken' => $credentials['SecurityToken'],
'expiration' => \strtotime($credentials['Expiration']),
'providerName' => $this->getProviderName(),
]), $this->getStaleTime(strtotime($credentials['Expiration'])));
}
public function key()
{
return 'rsa_key_pair#publicKeyId#' . $this->publicKeyId;
}
public function getProviderName()
{
return 'rsa_key_pair';
}
/**
* @return string
*/
public function getPublicKeyId()
{
return $this->publicKeyId;
}
/**
* @return mixed
*/
public function getPrivateKey()
{
return $this->privateKey;
}
}

View File

@@ -1,53 +0,0 @@
<?php
namespace AlibabaCloud\Credentials\Providers;
use AlibabaCloud\Credentials\Request\GenerateSessionAccessKey;
use AlibabaCloud\Credentials\StsCredential;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use RuntimeException;
/**
* Class RsaKeyPairProvider
*
* @package AlibabaCloud\Credentials\Providers
*/
class RsaKeyPairProvider extends Provider
{
/**
* Get credential.
*
*
* @return StsCredential
* @throws Exception
* @throws GuzzleException
*/
public function get()
{
$credential = $this->getCredentialsInCache();
if ($credential === null) {
$result = (new GenerateSessionAccessKey($this->credential))->request();
if ($result->getStatusCode() !== 200) {
throw new RuntimeException(isset($result['Message']) ? $result['Message'] : (string)$result->getBody());
}
if (!isset($result['SessionAccessKey']['SessionAccessKeyId'],
$result['SessionAccessKey']['SessionAccessKeySecret'])) {
throw new RuntimeException($this->error);
}
$credential = $result['SessionAccessKey'];
$this->cache($credential);
}
return new StsCredential(
$credential['SessionAccessKeyId'],
$credential['SessionAccessKeySecret'],
strtotime($credential['Expiration'])
);
}
}

View File

@@ -0,0 +1,161 @@
<?php
namespace AlibabaCloud\Credentials\Providers;
use AlibabaCloud\Credentials\Credential\RefreshResult;
abstract class SessionCredentialsProvider implements CredentialsProvider
{
/**
* @var array
*/
protected static $credentialsCache = [];
/**
* Expiration time slot for temporary security credentials.
*
* @var int
*/
protected $expirationSlot = 180;
/**
* @var string
*/
protected $error = 'Result contains no credentials';
/**
* Get the credentials from the cache in the validity period.
*
* @return RefreshResult|null
*/
protected function getCredentialsInCache()
{
if (isset(self::$credentialsCache[$this->key()])) {
$result = self::$credentialsCache[$this->key()];
return $result;
}
return null;
}
/**
* Cache credentials.
*
* @param RefreshResult $credential
*/
protected function cache(RefreshResult $credential)
{
self::$credentialsCache[$this->key()] = $credential;
}
/**
* Get credential.
*
* @return Credentials
*/
public function getCredentials()
{
if ($this->cacheIsStale() || $this->shouldInitiateCachePrefetch()) {
$result = $this->refreshCache();
$this->cache($result);
}
$result = $this->getCredentialsInCache();
return $result->credentials();
}
/**
* @return RefreshResult
*/
protected function refreshCache()
{
try {
return $this->handleFetchedSuccess($this->refreshCredentials());
} catch (\Exception $e) {
return $this->handleFetchedFailure($e);
}
}
/**
* @return RefreshResult
* @throws \Exception
*/
protected function handleFetchedFailure(\Exception $e)
{
$currentCachedValue = $this->getCredentialsInCache();
if (is_null($currentCachedValue)) {
throw $e;
}
if (time() < $currentCachedValue->staleTime()) {
return $currentCachedValue;
}
throw $e;
}
/**
* @return RefreshResult
*/
protected function handleFetchedSuccess(RefreshResult $value)
{
$now = time();
// 过期时间大于15分钟不用管
if ($now < $value->staleTime()) {
return $value;
}
// 不足或等于15分钟但未过期下次会再次刷新
if ($now < $value->staleTime() + 15 * 60) {
$value->staleTime = $now;
return $value;
}
// 已过期看缓存缓存若大于15分钟返回缓存若小于15分钟则稍后重试
if (is_null($this->getCredentialsInCache())) {
throw new \Exception("The fetched credentials have expired and no cache is available.");
} else if ($now < $this->getCredentialsInCache()->staleTime()) {
return $this->getCredentialsInCache();
} else {
// 返回成功,延长有效期 1 分钟
$expectation = mt_rand(50, 70);
$value->staleTime = time() + $expectation;
return $value;
}
}
/**
* @return bool
*/
protected function cacheIsStale()
{
return is_null($this->getCredentialsInCache()) || time() >= $this->getCredentialsInCache()->staleTime();
}
/**
* @return bool
*/
protected function shouldInitiateCachePrefetch()
{
return is_null($this->getCredentialsInCache()) || time() >= $this->getCredentialsInCache()->prefetchTime();
}
/**
* @return int
*/
public function getStaleTime($expiration)
{
return $expiration <= 0 ?
time() + (60 * 60) :
$expiration - (15 * 60);
}
/**
* @return RefreshResult
*/
abstract function refreshCredentials();
/**
* Get the toString of the credentials provider as the key.
*
* @return string
*/
abstract function key();
}

View File

@@ -0,0 +1,78 @@
<?php
namespace AlibabaCloud\Credentials\Providers;
use AlibabaCloud\Credentials\Utils\Helper;
use AlibabaCloud\Credentials\Utils\Filter;
/**
* @internal This class is intended for internal use within the package.
* Class StaticAKCredentialsProvider
*
* @package AlibabaCloud\Credentials\Providers
*/
class StaticAKCredentialsProvider implements CredentialsProvider
{
/**
* @var string
*/
private $accessKeyId;
/**
* @var string
*/
private $accessKeySecret;
/**
* StaticAKCredentialsProvider constructor.
*
* @param array $params
*/
public function __construct(array $params = [])
{
$this->filterAK($params);
}
private function filterAK(array $params)
{
if (Helper::envNotEmpty('ALIBABA_CLOUD_ACCESS_KEY_ID')) {
$this->accessKeyId = Helper::env('ALIBABA_CLOUD_ACCESS_KEY_ID');
}
if (Helper::envNotEmpty('ALIBABA_CLOUD_ACCESS_KEY_SECRET')) {
$this->accessKeySecret = Helper::env('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
}
if (isset($params['accessKeyId'])) {
$this->accessKeyId = $params['accessKeyId'];
}
if (isset($params['accessKeySecret'])) {
$this->accessKeySecret = $params['accessKeySecret'];
}
Filter::accessKey($this->accessKeyId, $this->accessKeySecret);
}
/**
* Get credential.
*
* @return Credentials
*/
public function getCredentials()
{
return new Credentials([
'accessKeyId' => $this->accessKeyId,
'accessKeySecret' => $this->accessKeySecret,
'providerName' => $this->getProviderName(),
]);
}
/**
* @inheritDoc
*/
public function getProviderName()
{
return "static_ak";
}
}

View File

@@ -0,0 +1,92 @@
<?php
namespace AlibabaCloud\Credentials\Providers;
use AlibabaCloud\Credentials\Utils\Helper;
use AlibabaCloud\Credentials\Utils\Filter;
/**
* @internal This class is intended for internal use within the package.
* Class StaticSTSCredentialsProvider
*
* @package AlibabaCloud\Credentials\Providers
*/
class StaticSTSCredentialsProvider implements CredentialsProvider
{
/**
* @var string
*/
private $accessKeyId;
/**
* @var string
*/
private $accessKeySecret;
/**
* @var string
*/
private $securityToken;
/**
* StaticSTSCredentialsProvider constructor.
*
* @param array $params
*/
public function __construct(array $params = [])
{
$this->filterSTS($params);
}
private function filterSTS(array $params)
{
if (Helper::envNotEmpty('ALIBABA_CLOUD_ACCESS_KEY_ID')) {
$this->accessKeyId = Helper::env('ALIBABA_CLOUD_ACCESS_KEY_ID');
}
if (Helper::envNotEmpty('ALIBABA_CLOUD_ACCESS_KEY_SECRET')) {
$this->accessKeySecret = Helper::env('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
}
if (Helper::envNotEmpty('ALIBABA_CLOUD_SECURITY_TOKEN')) {
$this->securityToken = Helper::env('ALIBABA_CLOUD_SECURITY_TOKEN');
}
if (isset($params['accessKeyId'])) {
$this->accessKeyId = $params['accessKeyId'];
}
if (isset($params['accessKeySecret'])) {
$this->accessKeySecret = $params['accessKeySecret'];
}
if (isset($params['securityToken'])) {
$this->securityToken = $params['securityToken'];
}
Filter::accessKey($this->accessKeyId, $this->accessKeySecret);
Filter::securityToken($this->securityToken);
}
/**
* Get credential.
*
* @return Credentials
*/
public function getCredentials()
{
return new Credentials([
'accessKeyId' => $this->accessKeyId,
'accessKeySecret' => $this->accessKeySecret,
'securityToken' => $this->securityToken,
'providerName' => $this->getProviderName(),
]);
}
/**
* @inheritDoc
*/
public function getProviderName()
{
return "static_sts";
}
}

View File

@@ -0,0 +1,126 @@
<?php
namespace AlibabaCloud\Credentials\Providers;
use AlibabaCloud\Credentials\Utils\Helper;
use AlibabaCloud\Credentials\Utils\Filter;
use AlibabaCloud\Credentials\Request\Request;
use GuzzleHttp\Exception\GuzzleException;
use InvalidArgumentException;
use RuntimeException;
use AlibabaCloud\Credentials\Credential\RefreshResult;
/**
* @internal This class is intended for internal use within the package.
* Class URLCredentialsProvider
*
* @package AlibabaCloud\Credentials\Providers
*/
class URLCredentialsProvider extends SessionCredentialsProvider
{
/**
* @var string
*/
private $credentialsURI;
/**
* @var int
*/
private $connectTimeout = 5;
/**
* @var int
*/
private $readTimeout = 5;
/**
* URLCredentialsProvider constructor.
*
* @param array $params
* @param array $options
*/
public function __construct(array $params = [], array $options = [])
{
$this->filterOptions($options);
$this->filterCredentialsURI($params);
}
private function filterOptions(array $options)
{
if (isset($options['connectTimeout'])) {
$this->connectTimeout = $options['connectTimeout'];
}
if (isset($options['readTimeout'])) {
$this->readTimeout = $options['readTimeout'];
}
Filter::timeout($this->connectTimeout, $this->readTimeout);
}
private function filterCredentialsURI(array $params)
{
if (Helper::envNotEmpty('ALIBABA_CLOUD_CREDENTIALS_URI')) {
$this->credentialsURI = Helper::env('ALIBABA_CLOUD_CREDENTIALS_URI');
}
if (isset($params['credentialsURI'])) {
$this->credentialsURI = $params['credentialsURI'];
}
Filter::credentialsURI($this->credentialsURI);
}
/**
* Get credentials by request.
*
* @return RefreshResult
* @throws InvalidArgumentException
* @throws RuntimeException
* @throws GuzzleException
*/
public function refreshCredentials()
{
$options = Request::commonOptions();
$options['read_timeout'] = $this->readTimeout;
$options['connect_timeout'] = $this->connectTimeout;
$result = Request::createClient()->request('GET', $this->credentialsURI, $options);
if ($result->getStatusCode() !== 200) {
throw new RuntimeException('Error refreshing credentials from credentialsURI, statusCode: ' . $result->getStatusCode() . ', result: ' . (string) $result);
}
$credentials = $result->toArray();
if (!isset($credentials['AccessKeyId']) || !isset($credentials['AccessKeySecret']) || !isset($credentials['SecurityToken']) || !isset($credentials['Expiration'])) {
throw new RuntimeException('Error retrieving credentials from credentialsURI result:' . $result->toJson());
}
return new RefreshResult(new Credentials([
'accessKeyId' => $credentials['AccessKeyId'],
'accessKeySecret' => $credentials['AccessKeySecret'],
'securityToken' => $credentials['SecurityToken'],
'expiration' => \strtotime($credentials['Expiration']),
'providerName' => $this->getProviderName(),
]), $this->getStaleTime(strtotime($credentials['Expiration'])));
}
/**
* @return string
*/
public function key()
{
return 'credential_uri#' . $this->credentialsURI;
}
/**
* @return string
*/
public function getProviderName()
{
return 'credential_uri';
}
}

View File

@@ -2,13 +2,16 @@
namespace AlibabaCloud\Credentials;
use AlibabaCloud\Credentials\Providers\RamRoleArnProvider;
use AlibabaCloud\Credentials\Providers\RamRoleArnCredentialsProvider;
use AlibabaCloud\Credentials\Credential\CredentialModel;
use AlibabaCloud\Credentials\Signature\ShaHmac1Signature;
use AlibabaCloud\Credentials\Utils\Filter;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use InvalidArgumentException;
/**
* @deprecated
* Use the AssumeRole of the RAM account to complete the authentication.
*/
class RamRoleArnCredential implements CredentialsInterface
@@ -57,10 +60,10 @@ class RamRoleArnCredential implements CredentialsInterface
Filter::accessKey($credential['access_key_id'], $credential['access_key_secret']);
$this->config = $config;
$this->accessKeyId = $credential['access_key_id'];
$this->config = $config;
$this->accessKeyId = $credential['access_key_id'];
$this->accessKeySecret = $credential['access_key_secret'];
$this->roleArn = $credential['role_arn'];
$this->roleArn = $credential['role_arn'];
$this->roleSessionName = $credential['role_session_name'];
}
@@ -177,13 +180,20 @@ class RamRoleArnCredential implements CredentialsInterface
}
/**
* @return StsCredential
* @return AlibabaCloud\Credentials\Providers\Credentials
* @throws Exception
* @throws GuzzleException
*/
protected function getSessionCredential()
{
return (new RamRoleArnProvider($this))->get();
$params = [
'accessKeyId' => $this->accessKeyId,
'accessKeySecret' => $this->accessKeyId,
'roleArn' => $this->roleArn,
'roleSessionName' => $this->roleSessionName,
'policy' => $this->policy,
];
return (new RamRoleArnCredentialsProvider($params))->getCredentials();
}
/**
@@ -215,4 +225,18 @@ class RamRoleArnCredential implements CredentialsInterface
{
return $this->getSessionCredential()->getExpiration();
}
/**
* @inheritDoc
*/
public function getCredential()
{
$credentials = $this->getSessionCredential();
return new CredentialModel([
'accessKeyId' => $credentials->getAccessKeyId(),
'accessKeySecret' => $credentials->getAccessKeySecret(),
'securityToken' => $credentials->getSecurityToken(),
'type' => 'ram_role_arn',
]);
}
}

View File

@@ -1,37 +0,0 @@
<?php
namespace AlibabaCloud\Credentials\Request;
use AlibabaCloud\Credentials\Providers\Provider;
use AlibabaCloud\Credentials\RamRoleArnCredential;
use AlibabaCloud\Credentials\Signature\ShaHmac1Signature;
/**
* Retrieving assume role credentials.
*/
class AssumeRole extends Request
{
/**
* AssumeRole constructor.
*
* @param RamRoleArnCredential $arnCredential
*/
public function __construct(RamRoleArnCredential $arnCredential)
{
parent::__construct();
$this->signature = new ShaHmac1Signature();
$this->credential = $arnCredential;
$this->uri = $this->uri->withHost('sts.aliyuncs.com');
$this->options['verify'] = false;
$this->options['query']['RoleArn'] = $arnCredential->getRoleArn();
$this->options['query']['RoleSessionName'] = $arnCredential->getRoleSessionName();
$this->options['query']['DurationSeconds'] = Provider::DURATION_SECONDS;
$this->options['query']['AccessKeyId'] = $this->credential->getOriginalAccessKeyId();
$this->options['query']['Version'] = '2015-04-01';
$this->options['query']['Action'] = 'AssumeRole';
$this->options['query']['RegionId'] = 'cn-hangzhou';
if ($arnCredential->getPolicy()) {
$this->options['query']['Policy'] = $arnCredential->getPolicy();
}
}
}

View File

@@ -1,33 +0,0 @@
<?php
namespace AlibabaCloud\Credentials\Request;
use AlibabaCloud\Credentials\Providers\Provider;
use AlibabaCloud\Credentials\RsaKeyPairCredential;
use AlibabaCloud\Credentials\Signature\ShaHmac256WithRsaSignature;
/**
* Use the RSA key pair to complete the authentication (supported only on Japanese site)
*/
class GenerateSessionAccessKey extends Request
{
/**
* GenerateSessionAccessKey constructor.
*
* @param RsaKeyPairCredential $credential
*/
public function __construct(RsaKeyPairCredential $credential)
{
parent::__construct();
$this->signature = new ShaHmac256WithRsaSignature();
$this->credential = $credential;
$this->uri = $this->uri->withHost('sts.ap-northeast-1.aliyuncs.com');
$this->options['verify'] = false;
$this->options['query']['Version'] = '2015-04-01';
$this->options['query']['Action'] = 'GenerateSessionAccessKey';
$this->options['query']['RegionId'] = 'cn-hangzhou';
$this->options['query']['AccessKeyId'] = $credential->getPublicKeyId();
$this->options['query']['PublicKeyId'] = $credential->getPublicKeyId();
$this->options['query']['DurationSeconds'] = Provider::DURATION_SECONDS;
}
}

View File

@@ -3,19 +3,16 @@
namespace AlibabaCloud\Credentials\Request;
use AlibabaCloud\Credentials\Credentials;
use AlibabaCloud\Credentials\EcsRamRoleCredential;
use AlibabaCloud\Credentials\Helper;
use AlibabaCloud\Credentials\RamRoleArnCredential;
use AlibabaCloud\Credentials\Signature\ShaHmac1Signature;
use AlibabaCloud\Credentials\Signature\ShaHmac256WithRsaSignature;
use Exception;
use AlibabaCloud\Credentials\Utils\Helper;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Uri;
use AlibabaCloud\Tea\Response;
use Psr\Http\Message\ResponseInterface;
use Exception;
use InvalidArgumentException;
/**
* RESTful RPC Request.
*/
@@ -28,67 +25,33 @@ class Request
const CONNECT_TIMEOUT = 5;
/**
* Request Timeout
* Request Read Timeout
*/
const TIMEOUT = 10;
const READ_TIMEOUT = 5;
/**
* @var array
*/
private static $config = [];
/**
* @var array
*/
public $options = [];
/**
* @var Uri
*
* @return array
*/
public $uri;
/**
* @var EcsRamRoleCredential|RamRoleArnCredential
*/
protected $credential;
/**
* @var ShaHmac256WithRsaSignature|ShaHmac1Signature
*/
protected $signature;
/**
* Request constructor.
*/
public function __construct()
public static function commonOptions()
{
$this->uri = (new Uri())->withScheme('https');
$this->options['http_errors'] = false;
$this->options['connect_timeout'] = self::CONNECT_TIMEOUT;
$this->options['timeout'] = self::TIMEOUT;
$options = [];
$options['http_errors'] = false;
$options['connect_timeout'] = self::CONNECT_TIMEOUT;
$options['read_timeout'] = self::READ_TIMEOUT;
$options['headers']['User-Agent'] = Helper::getUserAgent();
// Turn on debug mode based on environment variable.
if (strtolower(Helper::env('DEBUG')) === 'sdk') {
$this->options['debug'] = true;
$options['debug'] = true;
}
}
/**
* @return ResponseInterface
* @throws Exception
*/
public function request()
{
$this->options['query']['Format'] = 'JSON';
$this->options['query']['SignatureMethod'] = $this->signature->getMethod();
$this->options['query']['SignatureVersion'] = $this->signature->getVersion();
$this->options['query']['SignatureNonce'] = self::uuid(json_encode($this->options['query']));
$this->options['query']['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z');
$this->options['query']['Signature'] = $this->signature->sign(
self::signString('GET', $this->options['query']),
$this->credential->getOriginalAccessKeySecret() . '&'
);
return self::createClient()->request('GET', (string)$this->uri, $this->options);
return $options;
}
/**
@@ -118,6 +81,53 @@ class Request
return $method . '&%2F&' . self::percentEncode(substr($canonicalized, 1));
}
/**
* @param string $string
* @param string $accessKeySecret
*
* @return string
*/
public static function shaHmac1sign($string, $accessKeySecret)
{
return base64_encode(hash_hmac('sha1', $string, $accessKeySecret, true));
}
/**
* @param string $string
* @param string $accessKeySecret
*
* @return string
*/
public static function shaHmac256sign($string, $accessKeySecret)
{
return base64_encode(hash_hmac('sha256', $string, $accessKeySecret, true));
}
/**
* @param string $string
* @param string $privateKey
*
* @return string
*/
public static function shaHmac256WithRsasign($string, $privateKey)
{
$binarySignature = '';
try {
openssl_sign(
$string,
$binarySignature,
$privateKey,
\OPENSSL_ALGO_SHA256
);
} catch (Exception $exception) {
throw new InvalidArgumentException(
$exception->getMessage()
);
}
return base64_encode($binarySignature);
}
/**
* @param string $string
*
@@ -140,6 +150,8 @@ class Request
{
if (Credentials::hasMock()) {
$stack = HandlerStack::create(Credentials::getMock());
$history = Credentials::getHandlerHistory();
$stack->push($history);
} else {
$stack = HandlerStack::create();
}

View File

@@ -2,13 +2,16 @@
namespace AlibabaCloud\Credentials;
use AlibabaCloud\Credentials\Providers\RsaKeyPairProvider;
use AlibabaCloud\Credentials\Providers\RsaKeyPairCredentialsProvider;
use AlibabaCloud\Credentials\Credential\CredentialModel;
use AlibabaCloud\Credentials\Signature\ShaHmac1Signature;
use AlibabaCloud\Credentials\Utils\Filter;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use InvalidArgumentException;
/**
* @deprecated
* Use the RSA key pair to complete the authentication (supported only on Japanese site)
*/
class RsaKeyPairCredential implements CredentialsInterface
@@ -19,6 +22,11 @@ class RsaKeyPairCredential implements CredentialsInterface
*/
private $publicKeyId;
/**
* @var string
*/
private $privateKeyFile;
/**
* @var string
*/
@@ -42,7 +50,8 @@ class RsaKeyPairCredential implements CredentialsInterface
Filter::privateKeyFile($private_key_file);
$this->publicKeyId = $public_key_id;
$this->config = $config;
$this->privateKeyFile = $private_key_file;
$this->config = $config;
try {
$this->privateKey = file_get_contents($private_key_file);
} catch (Exception $exception) {
@@ -117,13 +126,17 @@ class RsaKeyPairCredential implements CredentialsInterface
}
/**
* @return StsCredential
* @return AlibabaCloud\Credentials\Providers\Credentials
* @throws Exception
* @throws GuzzleException
*/
protected function getSessionCredential()
{
return (new RsaKeyPairProvider($this))->get();
$params = [
'publicKeyId' => $this->publicKeyId,
'privateKeyFile' => $this->privateKeyFile,
];
return (new RsaKeyPairCredentialsProvider($params))->getCredentials();
}
/**
@@ -155,4 +168,18 @@ class RsaKeyPairCredential implements CredentialsInterface
{
return $this->getSessionCredential()->getExpiration();
}
/**
* @inheritDoc
*/
public function getCredential()
{
$credentials = $this->getSessionCredential();
return new CredentialModel([
'accessKeyId' => $credentials->getAccessKeyId(),
'accessKeySecret' => $credentials->getAccessKeySecret(),
'securityToken' => $credentials->getSecurityToken(),
'type' => 'rsa_key_pair',
]);
}
}

View File

@@ -2,9 +2,12 @@
namespace AlibabaCloud\Credentials;
use AlibabaCloud\Credentials\Utils\Filter;
use AlibabaCloud\Credentials\Credential\CredentialModel;
use AlibabaCloud\Credentials\Signature\ShaHmac1Signature;
/**
* @deprecated
* Use the STS Token to complete the authentication.
*/
class StsCredential implements CredentialsInterface
@@ -42,10 +45,10 @@ class StsCredential implements CredentialsInterface
{
Filter::accessKey($access_key_id, $access_key_secret);
Filter::expiration($expiration);
$this->accessKeyId = $access_key_id;
$this->accessKeyId = $access_key_id;
$this->accessKeySecret = $access_key_secret;
$this->expiration = $expiration;
$this->securityToken = $security_token;
$this->expiration = $expiration;
$this->securityToken = $security_token;
}
/**
@@ -95,4 +98,18 @@ class StsCredential implements CredentialsInterface
{
return new ShaHmac1Signature();
}
/**
* @inheritDoc
*/
public function getCredential()
{
return new CredentialModel([
'accessKeyId' => $this->accessKeyId,
'accessKeySecret' => $this->accessKeySecret,
'securityToken' => $this->securityToken,
'type' => 'sts',
]);
}
}

View File

@@ -0,0 +1,233 @@
<?php
namespace AlibabaCloud\Credentials\Utils;
use InvalidArgumentException;
/**
* Class Filter
*
* @package AlibabaCloud\Credentials\Utils
*/
class Filter
{
/**
* @param $name
*
* @codeCoverageIgnore
* @return string
*/
public static function credentialName($name)
{
if (!is_string($name)) {
throw new InvalidArgumentException('Name must be a string');
}
if ($name === '') {
throw new InvalidArgumentException('Name cannot be empty');
}
return $name;
}
/**
* @param $bearerToken
*
* @return mixed
* @throws InvalidArgumentException
*/
public static function bearerToken($bearerToken)
{
if (!is_string($bearerToken)) {
throw new InvalidArgumentException('bearerToken must be a string');
}
if ($bearerToken === '') {
throw new InvalidArgumentException('bearerToken cannot be empty');
}
return $bearerToken;
}
/**
* @param $publicKeyId
*
* @return mixed
*/
public static function publicKeyId($publicKeyId)
{
if (!is_string($publicKeyId)) {
throw new InvalidArgumentException('publicKeyId must be a string');
}
if ($publicKeyId === '') {
throw new InvalidArgumentException('publicKeyId cannot be empty');
}
return $publicKeyId;
}
/**
* @param $privateKeyFile
*
* @return mixed
*/
public static function privateKeyFile($privateKeyFile)
{
if (!is_string($privateKeyFile)) {
throw new InvalidArgumentException('privateKeyFile must be a string');
}
if ($privateKeyFile === '') {
throw new InvalidArgumentException('privateKeyFile cannot be empty');
}
return $privateKeyFile;
}
/**
* @param string|null $roleName
*/
public static function roleName($roleName)
{
if ($roleName === null) {
return;
}
if (!is_string($roleName)) {
throw new InvalidArgumentException('roleName must be a string');
}
if ($roleName === '') {
throw new InvalidArgumentException('roleName cannot be empty');
}
}
/**
* @param boolean|null $disableIMDSv1
*/
public static function disableIMDSv1($disableIMDSv1)
{
if (!is_bool($disableIMDSv1)) {
throw new InvalidArgumentException('disableIMDSv1 must be a boolean');
}
}
/**
* @param string|null $roleArn
*/
public static function roleArn($roleArn)
{
if (is_null($roleArn) || $roleArn === '') {
throw new InvalidArgumentException('roleArn cannot be empty');
}
}
/**
* @param string|null $roleArn
*/
public static function oidcProviderArn($oidcProviderArn)
{
if (is_null($oidcProviderArn) || $oidcProviderArn === '') {
throw new InvalidArgumentException('oidcProviderArn cannot be empty');
}
}
/**
* @param string|null $roleArn
*/
public static function oidcTokenFilePath($oidcTokenFilePath)
{
if (is_null($oidcTokenFilePath) || $oidcTokenFilePath === '') {
throw new InvalidArgumentException('oidcTokenFilePath cannot be empty');
}
}
/**
* @param string $accessKeyId
* @param string $accessKeySecret
*/
public static function accessKey($accessKeyId, $accessKeySecret)
{
if (!is_string($accessKeyId)) {
throw new InvalidArgumentException('accessKeyId must be a string');
}
if ($accessKeyId === '') {
throw new InvalidArgumentException('accessKeyId cannot be empty');
}
if (!is_string($accessKeySecret)) {
throw new InvalidArgumentException('accessKeySecret must be a string');
}
if ($accessKeySecret === '') {
throw new InvalidArgumentException('accessKeySecret cannot be empty');
}
}
/**
* @param string $securityToken
*/
public static function securityToken($securityToken)
{
if (!is_string($securityToken)) {
throw new InvalidArgumentException('securityToken must be a string');
}
if ($securityToken === '') {
throw new InvalidArgumentException('securityToken cannot be empty');
}
}
/**
* @param int $expiration
*/
public static function expiration($expiration)
{
if (!is_int($expiration)) {
throw new InvalidArgumentException('expiration must be a int');
}
}
/**
* @param int $connectTimeout
* @param int $readTimeout
*/
public static function timeout($connectTimeout, $readTimeout)
{
if (!is_int($connectTimeout)) {
throw new InvalidArgumentException('connectTimeout must be a int');
}
if (!is_int($readTimeout)) {
throw new InvalidArgumentException('readTimeout must be a int');
}
}
/**
* @param string|null $credentialsURI
*/
public static function credentialsURI($credentialsURI)
{
if (!is_string($credentialsURI)) {
throw new InvalidArgumentException('credentialsURI must be a string');
}
if ($credentialsURI === '') {
throw new InvalidArgumentException('credentialsURI cannot be empty');
}
}
/**
* @param boolean|null $reuseLastProviderEnabled
*/
public static function reuseLastProviderEnabled($reuseLastProviderEnabled)
{
if (!is_bool($reuseLastProviderEnabled)) {
throw new InvalidArgumentException('reuseLastProviderEnabled must be a boolean');
}
}
}

View File

@@ -1,13 +1,15 @@
<?php
namespace AlibabaCloud\Credentials;
namespace AlibabaCloud\Credentials\Utils;
use AlibabaCloud\Credentials\Credential;
use org\bovigo\vfs\vfsStream;
use Closure;
/**
* Class Helper
*
* @package AlibabaCloud\Credentials
* @package AlibabaCloud\Credentials\Utils
*/
class Helper
{
@@ -51,6 +53,10 @@ class Helper
if (!$open_basedir) {
return true;
}
if (0 === strpos($filename, vfsStream::SCHEME)) {
// 虚拟文件忽略
return true;
}
$dirs = explode(PATH_SEPARATOR, $open_basedir);
@@ -199,4 +205,47 @@ class Helper
dump(...$parameters);
exit;
}
/**
* Snake to camel case.
*
* @param string $str
*
* @return string
*/
public static function snakeToCamelCase($str)
{
$components = explode('_', $str);
$camelCaseStr = $components[0];
for ($i = 1; $i < count($components); $i++) {
$camelCaseStr .= ucfirst($components[$i]);
}
return $camelCaseStr;
}
/**
* Get user agent.
*
* @param string $userAgent
*
* @return string
*/
public static function getUserAgent()
{
return sprintf('AlibabaCloud (%s; %s) PHP/%s Credentials/%s TeaDSL/1', PHP_OS, \PHP_SAPI, PHP_VERSION, Credential::VERSION);
}
/**
* @param array $arrays
* @param string $key
*
* @return mix
*/
public static function unsetReturnNull(array $arrays, $key)
{
if(isset($arrays[$key])) {
return $arrays[$key];
}
return null;
}
}

View File

@@ -1,18 +1,19 @@
<?php
namespace AlibabaCloud\Credentials;
namespace AlibabaCloud\Credentials\Utils;
use Exception;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Middleware;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Trait MockTrait
*
* @package AlibabaCloud\Credentials
* @package AlibabaCloud\Credentials\Utils
*/
trait MockTrait
{
@@ -21,6 +22,11 @@ trait MockTrait
*/
private static $mockQueue = [];
/**
* @var array
*/
private static $history = [];
/**
* @var MockHandler
*/
@@ -46,6 +52,14 @@ trait MockTrait
self::$mock = new MockHandler(self::$mockQueue);
}
/**
* @return MockHandler
*/
public static function getHandlerHistory()
{
return Middleware::history(self::$history);
}
/**
* @param string $message
* @param RequestInterface $request
@@ -95,4 +109,12 @@ trait MockTrait
{
return self::$mock;
}
/**
* @return array
*/
public static function getHistroy()
{
return self::$history;
}
}

View File

@@ -11,9 +11,9 @@
],
"require": {
"php": ">5.5",
"alibabacloud/tea-utils": "^0.2.0",
"alibabacloud/tea-utils": "^0.2.21",
"alibabacloud/credentials": "^1.1",
"alibabacloud/openapi-util": "^0.1.10",
"alibabacloud/openapi-util": "^0.1.10|^0.2.1",
"alibabacloud/gateway-spi": "^1",
"alibabacloud/tea-xml": "^0.2"
},

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./tests/bootstrap.php" colors="true" processIsolation="false" stopOnFailure="false"
convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true"
testSuiteLoaderFile="phpunit/src/Runner/StandardTestSuiteLoader.php">
<testsuites>
<testsuite name="All">
<directory>tests</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<groups>
<exclude>
<group>integration</group>
</exclude>
</groups>
<logging>
<log type="coverage-html" target="cache/coverage" lowUpperBound="35" highLowerBound="70"/>
<log type="coverage-clover" target="cache/coverage.clover"/>
</logging>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>

View File

@@ -1,14 +1,15 @@
<?php
// This file is auto-generated, don't edit it. Thanks.
namespace Darabonba\OpenApi\Models;
use AlibabaCloud\Credentials\Credential;
use AlibabaCloud\Tea\Model;
use AlibabaCloud\Credentials\Credential;
use Darabonba\OpenApi\Models\GlobalParameters;
/**
* Model for initing client.
* Model for initing client
*/
class Config extends Model
{
@@ -16,6 +17,7 @@ class Config extends Model
'accessKeyId' => '',
'accessKeySecret' => '',
'securityToken' => '',
'bearerToken' => '',
'protocol' => 'http',
'method' => '',
'regionId' => '',
@@ -37,12 +39,13 @@ class Config extends Model
'type' => '',
'signatureVersion' => '',
'signatureAlgorithm' => '',
'key' => '',
'cert' => '',
'ca' => '',
];
public function validate()
{
}
public function toMap()
{
$res = [];
@@ -55,6 +58,9 @@ class Config extends Model
if (null !== $this->securityToken) {
$res['securityToken'] = $this->securityToken;
}
if (null !== $this->bearerToken) {
$res['bearerToken'] = $this->bearerToken;
}
if (null !== $this->protocol) {
$res['protocol'] = $this->protocol;
}
@@ -121,13 +127,22 @@ class Config extends Model
if (null !== $this->globalParameters) {
$res['globalParameters'] = null !== $this->globalParameters ? $this->globalParameters->toMap() : null;
}
if (null !== $this->key) {
$res['key'] = $this->key;
}
if (null !== $this->cert) {
$res['cert'] = $this->cert;
}
if (null !== $this->ca) {
$res['ca'] = $this->ca;
}
if (null !== $this->disableHttp2) {
$res['disableHttp2'] = $this->disableHttp2;
}
return $res;
}
/**
* @param array $map
*
* @return Config
*/
public static function fromMap($map = [])
@@ -142,6 +157,9 @@ class Config extends Model
if (isset($map['securityToken'])) {
$model->securityToken = $map['securityToken'];
}
if (isset($map['bearerToken'])) {
$model->bearerToken = $map['bearerToken'];
}
if (isset($map['protocol'])) {
$model->protocol = $map['protocol'];
}
@@ -208,226 +226,225 @@ class Config extends Model
if (isset($map['globalParameters'])) {
$model->globalParameters = GlobalParameters::fromMap($map['globalParameters']);
}
if (isset($map['key'])) {
$model->key = $map['key'];
}
if (isset($map['cert'])) {
$model->cert = $map['cert'];
}
if (isset($map['ca'])) {
$model->ca = $map['ca'];
}
if (isset($map['disableHttp2'])) {
$model->disableHttp2 = $map['disableHttp2'];
}
return $model;
}
/**
* @description accesskey id
*
* @var string
*/
public $accessKeyId;
/**
* @description accesskey secret
*
* @var string
*/
public $accessKeySecret;
/**
* @description security token
*
* @example a.txt
*
* @var string
*/
public $securityToken;
/**
* @description bearer token
* @example the-bearer-token
* @var string
*/
public $bearerToken;
/**
* @description http protocol
*
* @example http
*
* @var string
*/
public $protocol;
/**
* @description http method
*
* @example GET
*
* @var string
*/
public $method;
/**
* @description region id
*
* @example cn-hangzhou
*
* @var string
*/
public $regionId;
/**
* @description read timeout
*
* @example 10
*
* @var int
*/
public $readTimeout;
/**
* @description connect timeout
*
* @example 10
*
* @var int
*/
public $connectTimeout;
/**
* @description http proxy
*
* @example http://localhost
*
* @var string
*/
public $httpProxy;
/**
* @description https proxy
*
* @example https://localhost
*
* @var string
*/
public $httpsProxy;
/**
* @description credential
*
* @example
*
* @example
* @var Credential
*/
public $credential;
/**
* @description endpoint
*
* @example cs.aliyuncs.com
*
* @var string
*/
public $endpoint;
/**
* @description proxy white list
*
* @example http://localhost
*
* @var string
*/
public $noProxy;
/**
* @description max idle conns
*
* @example 3
*
* @var int
*/
public $maxIdleConns;
/**
* @description network for endpoint
*
* @example public
*
* @var string
*/
public $network;
/**
* @description user agent
*
* @example Alibabacloud/1
*
* @var string
*/
public $userAgent;
/**
* @description suffix for endpoint
*
* @example aliyun
*
* @var string
*/
public $suffix;
/**
* @description socks5 proxy
*
* @var string
*/
public $socks5Proxy;
/**
* @description socks5 network
*
* @example TCP
*
* @var string
*/
public $socks5NetWork;
/**
* @description endpoint type
*
* @example internal
*
* @var string
*/
public $endpointType;
/**
* @description OpenPlatform endpoint
*
* @example openplatform.aliyuncs.com
*
* @var string
*/
public $openPlatformEndpoint;
/**
* @description credential type
*
* @example access_key
*
* @deprecated
*
* @var string
*/
public $type;
/**
* @description Signature Version
*
* @example v1
*
* @var string
*/
public $signatureVersion;
/**
* @description Signature Algorithm
*
* @example ACS3-HMAC-SHA256
*
* @var string
*/
public $signatureAlgorithm;
/**
* @description Global Parameters
*
* @var GlobalParameters
*/
public $globalParameters;
/**
* @description privite key for client certificate
* @example MIIEvQ
* @var string
*/
public $key;
/**
* @description client certificate
* @example -----BEGIN CERTIFICATE-----xxx-----END CERTIFICATE-----
* @var string
*/
public $cert;
/**
* @description server certificate
* @example -----BEGIN CERTIFICATE-----xxx-----END CERTIFICATE-----
* @var string
*/
public $ca;
/**
* @description disable HTTP/2
* @example false
* @var bool
*/
public $disableHttp2;
}

View File

@@ -1,7 +1,6 @@
<?php
// This file is auto-generated, don't edit it. Thanks.
namespace Darabonba\OpenApi\Models;
use AlibabaCloud\Tea\Model;
@@ -11,7 +10,6 @@ class GlobalParameters extends Model
public function validate()
{
}
public function toMap()
{
$res = [];
@@ -21,13 +19,10 @@ class GlobalParameters extends Model
if (null !== $this->queries) {
$res['queries'] = $this->queries;
}
return $res;
}
/**
* @param array $map
*
* @return GlobalParameters
*/
public static function fromMap($map = [])
@@ -39,11 +34,10 @@ class GlobalParameters extends Model
if (isset($map['queries'])) {
$model->queries = $map['queries'];
}
return $model;
}
public $headers;
public $queries;
}

View File

@@ -1,7 +1,6 @@
<?php
// This file is auto-generated, don't edit it. Thanks.
namespace Darabonba\OpenApi\Models;
use AlibabaCloud\Tea\Model;
@@ -11,7 +10,6 @@ class OpenApiRequest extends Model
public function validate()
{
}
public function toMap()
{
$res = [];
@@ -33,13 +31,10 @@ class OpenApiRequest extends Model
if (null !== $this->endpointOverride) {
$res['endpointOverride'] = $this->endpointOverride;
}
return $res;
}
/**
* @param array $map
*
* @return OpenApiRequest
*/
public static function fromMap($map = [])
@@ -63,10 +58,8 @@ class OpenApiRequest extends Model
if (isset($map['endpointOverride'])) {
$model->endpointOverride = $map['endpointOverride'];
}
return $model;
}
public $headers;
public $query;
@@ -78,4 +71,5 @@ class OpenApiRequest extends Model
public $hostMap;
public $endpointOverride;
}

View File

@@ -1,7 +1,6 @@
<?php
// This file is auto-generated, don't edit it. Thanks.
namespace Darabonba\OpenApi\Models;
use AlibabaCloud\Tea\Model;
@@ -19,7 +18,6 @@ class Params extends Model
Model::validateRequired('bodyType', $this->bodyType, true);
Model::validateRequired('reqBodyType', $this->reqBodyType, true);
}
public function toMap()
{
$res = [];
@@ -50,13 +48,10 @@ class Params extends Model
if (null !== $this->style) {
$res['style'] = $this->style;
}
return $res;
}
/**
* @param array $map
*
* @return Params
*/
public static function fromMap($map = [])
@@ -89,10 +84,8 @@ class Params extends Model
if (isset($map['style'])) {
$model->style = $map['style'];
}
return $model;
}
/**
* @var string
*/
@@ -134,4 +127,5 @@ class Params extends Model
public $reqBodyType;
public $style;
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,328 @@
<?php
namespace Darabonba\OpenApi\Tests;
use Darabonba\OpenApi\OpenApiClient;
use AlibabaCloud\Tea\Model;
use AlibabaCloud\Tea\Request;
use AlibabaCloud\Tea\Utils\Utils;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
class OpenApiClientTest extends TestCase
{
public function testConfig(){
$globalParameters = new GlobalParameters([
"headers" => [
"global-key" => "global-value"
],
"queries" => [
"global-query" => "global-value"
]
]);
$config = new Config([
"endpoint" => "config.endpoint",
"endpointType" => "regional",
"network" => "config.network",
"suffix" => "config.suffix",
"protocol" => "config.protocol",
"method" => "config.method",
"regionId" => "config.regionId",
"userAgent" => "config.userAgent",
"readTimeout" => 3000,
"connectTimeout" => 3000,
"httpProxy" => "config.httpProxy",
"httpsProxy" => "config.httpsProxy",
"noProxy" => "config.noProxy",
"socks5Proxy" => "config.socks5Proxy",
"socks5NetWork" => "config.socks5NetWork",
"maxIdleConns" => 128,
"signatureVersion" => "config.signatureVersion",
"signatureAlgorithm" => "config.signatureAlgorithm",
"globalParameters" => $globalParameters
]);
$creConfig = new \AlibabaCloud\Credentials\Credential\Config([
"accessKeyId" => "accessKeyId",
"accessKeySecret" => "accessKeySecret",
"securityToken" => "securityToken",
"type" => "sts"
]);
$credential = new Credential($creConfig);
$config->credential = $credential;
$client = new OpenApiClient($config);
$config->accessKeyId = "ak";
$config->accessKeySecret = "secret";
$config->securityToken = "token";
$config->type = "sts";
$client = new OpenApiClient($config);
}
/**
* @return Config
*/
public static function createConfig(){
$globalParameters = new GlobalParameters([
"headers" => [
"global-key" => "global-value"
],
"queries" => [
"global-query" => "global-value"
]
]);
$config = new Config([
"accessKeyId" => "ak",
"accessKeySecret" => "secret",
"securityToken" => "token",
"type" => "sts",
"userAgent" => "config.userAgent",
"readTimeout" => 3000,
"connectTimeout" => 3000,
"maxIdleConns" => 128,
"signatureVersion" => "config.signatureVersion",
"signatureAlgorithm" => "ACS3-HMAC-SHA256",
"globalParameters" => $globalParameters
]);
return $config;
}
/**
* @return RuntimeOptions
*/
public static function createRuntimeOptions(){
$runtime = new RuntimeOptions([
"readTimeout" => 4000,
"connectTimeout" => 4000,
"maxIdleConns" => 100,
"autoretry" => true,
"maxAttempts" => 1,
"backoffPolicy" => "no",
"backoffPeriod" => 1,
"ignoreSSL" => true
]);
return $runtime;
}
/**
* @return OpenApiRequest
*/
public static function createOpenApiRequest(){
$query = [];
$query["key1"] = "value";
$query["key2"] = 1;
$query["key3"] = true;
$body = [];
$body["key1"] = "value";
$body["key2"] = 1;
$body["key3"] = true;
$headers = [
"for-test" => "sdk"
];
$req = new OpenApiRequest([
"headers" => $headers,
"query" => OpenApiUtilClient::query($query),
"body" => OpenApiUtilClient::parseToMap($body)
]);
return $req;
}
public function testCallApiForRPCWithV2Sign_AK_Form(){
$config = self::createConfig();
$runtime = self::createRuntimeOptions();
$config->protocol = "HTTP";
$config->signatureAlgorithm = "v2";
$config->endpoint = "test.aliyuncs.com";
$client = new OpenApiClient($config);
$request = self::createOpenApiRequest();
$params = new Params([
"action" => "TestAPI",
"version" => "2022-06-01",
"protocol" => "HTTPS",
"pathname" => "/",
"method" => "POST",
"authType" => "AK",
"style" => "RPC",
"reqBodyType" => "formData",
"bodyType" => "json"
]);
$client->callApi($params, $request, $runtime);
}
public function testCallApiForRPCWithV2Sign_Anonymous_JSON(){
$config = self::createConfig();
$runtime = self::createRuntimeOptions();
$config->protocol = "HTTP";
$config->signatureAlgorithm = "v2";
$config->endpoint = "test.aliyuncs.com";
$client = new OpenApiClient($config);
$request = self::createOpenApiRequest();
$params = new Params([
"action" => "TestAPI",
"version" => "2022-06-01",
"protocol" => "HTTPS",
"pathname" => "/",
"method" => "POST",
"authType" => "Anonymous",
"style" => "RPC",
"reqBodyType" => "json",
"bodyType" => "json"
]);
$client->callApi($params, $request, $runtime);
}
public function testCallApiForROAWithV2Sign_HTTPS_AK_Form(){
$config = self::createConfig();
$runtime = self::createRuntimeOptions();
$config->signatureAlgorithm = "v2";
$config->endpoint = "test.aliyuncs.com";
$client = new OpenApiClient($config);
$request = self::createOpenApiRequest();
$params = new Params([
"action" => "TestAPI",
"version" => "2022-06-01",
"protocol" => "HTTPS",
"pathname" => "/test",
"method" => "POST",
"authType" => "AK",
"style" => "ROA",
"reqBodyType" => "formData",
"bodyType" => "json"
]);
$client->callApi($params, $request, $runtime);
}
public function testCallApiForROAWithV2Sign_Anonymous_JSON(){
$config = self::createConfig();
$runtime = self::createRuntimeOptions();
$config->protocol = "HTTP";
$config->signatureAlgorithm = "v2";
$config->endpoint = "test.aliyuncs.com";
$client = new OpenApiClient($config);
$request = self::createOpenApiRequest();
$params = new Params([
"action" => "TestAPI",
"version" => "2022-06-01",
"protocol" => "HTTPS",
"pathname" => "/test",
"method" => "POST",
"authType" => "Anonymous",
"style" => "ROA",
"reqBodyType" => "json",
"bodyType" => "json"
]);
$client->callApi($params, $request, $runtime);
}
public function testCallApiForRPCWithV3Sign_AK_Form(){
$config = self::createConfig();
$runtime = self::createRuntimeOptions();
$config->protocol = "HTTP";
$config->endpoint = "test.aliyuncs.com";
$client = new OpenApiClient($config);
$request = self::createOpenApiRequest();
$params = new Params([
"action" => "TestAPI",
"version" => "2022-06-01",
"protocol" => "HTTPS",
"pathname" => "/",
"method" => "POST",
"authType" => "AK",
"style" => "RPC",
"reqBodyType" => "formData",
"bodyType" => "json"
]);
$client->callApi($params, $request, $runtime);
}
public function testCallApiForRPCWithV3Sign_Anonymous_JSON(){
$config = self::createConfig();
$runtime = self::createRuntimeOptions();
$config->protocol = "HTTP";
$config->endpoint = "test.aliyuncs.com";
$client = new OpenApiClient($config);
$request = self::createOpenApiRequest();
$params = new Params([
"action" => "TestAPI",
"version" => "2022-06-01",
"protocol" => "HTTPS",
"pathname" => "/",
"method" => "POST",
"authType" => "Anonymous",
"style" => "RPC",
"reqBodyType" => "json",
"bodyType" => "json"
]);
$client->callApi($params, $request, $runtime);
}
public function testCallApiForROAWithV3Sign_AK_Form(){
$config = self::createConfig();
$runtime = self::createRuntimeOptions();
$config->protocol = "HTTP";
$config->endpoint = "test.aliyuncs.com";
$client = new OpenApiClient($config);
$request = self::createOpenApiRequest();
$params = new Params([
"action" => "TestAPI",
"version" => "2022-06-01",
"protocol" => "HTTPS",
"pathname" => "/test",
"method" => "POST",
"authType" => "AK",
"style" => "ROA",
"reqBodyType" => "formData",
"bodyType" => "json"
]);
$client->callApi($params, $request, $runtime);
}
public function testCallApiForROAWithV3Sign_Anonymous_JSON(){
$config = self::createConfig();
$runtime = self::createRuntimeOptions();
$config->protocol = "HTTP";
$config->endpoint = "test.aliyuncs.com";
$client = new OpenApiClient($config);
$request = self::createOpenApiRequest();
$params = new Params([
"action" => "TestAPI",
"version" => "2022-06-01",
"protocol" => "HTTPS",
"pathname" => "/test",
"method" => "POST",
"authType" => "Anonymous",
"style" => "ROA",
"reqBodyType" => "json",
"bodyType" => "json"
]);
$client->callApi($params, $request, $runtime);
}
public function testResponseBodyType(){
$config = self::createConfig();
$runtime = self::createRuntimeOptions();
$config->protocol = "HTTP";
$config->endpoint = "test.aliyuncs.com";
$client = new OpenApiClient($config);
$request = self::createOpenApiRequest();
$params = new Params([
"action" => "TestAPI",
"version" => "2022-06-01",
"protocol" => "HTTPS",
"pathname" => "/test",
"method" => "POST",
"authType" => "AK",
"style" => "ROA",
"reqBodyType" => "formData",
"bodyType" => "json"
]);
$client->callApi($params, $request, $runtime);
$params->bodyType = "array";
$client->callApi($params, $request, $runtime);
$params->bodyType = "string";
$client->callApi($params, $request, $runtime);
$params->bodyType = "byte";
$client->callApi($params, $request, $runtime);
}
}

View File

@@ -0,0 +1,3 @@
<?php
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';

View File

@@ -16,7 +16,7 @@
"lizhichao/one-sm": "^1.5"
},
"require-dev": {
"phpunit/phpunit": "^4.8.35|^5.4.3"
"phpunit/phpunit": "*"
},
"autoload": {
"psr-4": {

View File

@@ -8,7 +8,7 @@
<directory>tests</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>

View File

@@ -160,26 +160,26 @@ class OpenApiUtilClient
}
/**
* Parse array into a string with specified style.
* Parse object into a string with specified style.
*
* @style specified style e.g. repeatList
*
* @param mixed $array the array
* @param mixed $object the object
* @param string $prefix the prefix string
* @param string $style
*
* @return string the string
*/
public static function arrayToStringWithSpecifiedStyle($array, $prefix, $style)
public static function arrayToStringWithSpecifiedStyle($object, $prefix, $style)
{
if (null === $array) {
if (null === $object) {
return '';
}
if ('repeatList' === $style) {
return self::toForm([$prefix => $array]);
return self::toForm([$prefix => $object]);
}
if ('simple' == $style || 'spaceDelimited' == $style || 'pipeDelimited' == $style) {
$strs = self::flatten($array);
$strs = self::flatten($object);
switch ($style) {
case 'spaceDelimited':
@@ -192,7 +192,8 @@ class OpenApiUtilClient
return implode(',', $strs);
}
} elseif ('json' === $style) {
return json_encode($array);
self::parse($object, $parsed);
return json_encode($parsed);
}
return '';
@@ -423,7 +424,7 @@ class OpenApiUtilClient
foreach ($items as $key => $value) {
$pos = \is_int($key) ? $key + 1 : $key;
if ($value instanceof Model) {
$value = $value->toMap();
} elseif (\is_object($value)) {
@@ -436,6 +437,9 @@ class OpenApiUtilClient
self::flatten($value, $delimiter, $prepend . $pos . $delimiter)
);
} else {
if (\is_bool($value)) {
$value = true === $value ? 'true' : 'false';
}
$flatten[$prepend . $pos] = $value;
}
}

View File

@@ -63,12 +63,14 @@ class OpenApiUtilClientTest extends TestCase
public function testToForm()
{
$this->assertEquals('client=test&strs.1=str1&strs.2=str2&tag.key=value', OpenApiUtilClient::toForm([
$this->assertEquals('bool=true&client=test&strs.1=str1&strs.2=str2&strs.3=false&tag.key=value', OpenApiUtilClient::toForm([
'client' => 'test',
'tag' => [
'key' => 'value',
],
'strs' => ['str1', 'str2'],
'strs' => ['str1', 'str2', false],
'bool' => true,
'null' => null,
]));
}
@@ -83,6 +85,7 @@ class OpenApiUtilClientTest extends TestCase
$model = new MockModel();
$model->a = 'foo';
$model->c = 'boo';
$model->r = true;
$array = [
'a' => 'a',
@@ -95,7 +98,9 @@ class OpenApiUtilClientTest extends TestCase
'c' => ['x', 'y', 'z'],
'd' => [
$model
]
],
'e' => true,
'f' => null,
];
$this->assertEquals([
'a' => 'a',
@@ -107,6 +112,10 @@ class OpenApiUtilClientTest extends TestCase
'd.1.A' => 'foo',
'd.1.b' => '',
'd.1.c' => 'boo',
'd.1.c' => 'boo',
'd.1.r' => 'true',
'e' => 'true',
'f' => null
], OpenApiUtilClient::query($array));
}
@@ -142,6 +151,48 @@ class OpenApiUtilClientTest extends TestCase
)
);
$test = new ParseModel([
'str' => 'A',
'model' => new ParseModel(['str' => 'sub model']),
'array' => [1, 2, 3],
]);
$this->assertEquals(
'{"str":"A","model":{"str":"sub model","model":null,"array":null},"array":[1,2,3]}',
OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
$test,
'instance',
'json'
)
);
// model item in array
$test = [
new ParseModel([
'str' => 'A',
]),
];
$this->assertEquals(
'[{"str":"A","model":null,"array":null}]',
OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
$test,
'instance',
'json'
)
);
// model item in map
$test = [
'model' => new ParseModel([
'str' => 'A',
]),
];
$this->assertEquals(
'{"model":{"str":"A","model":null,"array":null}}',
OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
$test,
'instance',
'json'
)
);
$this->assertEquals(
'ok,test,2,3',
OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
@@ -295,7 +346,8 @@ class OpenApiUtilClientTest extends TestCase
'b9ff646822f41ef647c1416fa2b8408923828abc0464af6706e18db3e8553da8',
OpenApiUtilClient::hexEncode(OpenApiUtilClient::sign('secret', 'source', 'ACS3-HMAC-SM3'))
);
$this->assertEquals('1d93c62698a1c26427265668e79fac099aa26c1df873669599a2fb2f272e64c9',
$this->assertEquals(
'1d93c62698a1c26427265668e79fac099aa26c1df873669599a2fb2f272e64c9',
OpenApiUtilClient::hexEncode(OpenApiUtilClient::sign('secret', 'source', 'ACS3-HMAC-SHA256'))
);
}
@@ -311,14 +363,14 @@ class OpenApiUtilClientTest extends TestCase
'array' => [1, 2, 3],
]),
[ // model item in array
new ParseModel([
'str' => 'A',
]),
new ParseModel([
'str' => 'A',
]),
],
[ // model item in map
'model' => new ParseModel([
'str' => 'A',
]),
'model' => new ParseModel([
'str' => 'A',
]),
],
],
'expected' => [
@@ -347,6 +399,12 @@ class OpenApiUtilClientTest extends TestCase
],
],
],
'expectedJsonStr' => [
'["NotArray"]',
'NotArray',
'NotArray',
'NotArray',
],
];
}
}

View File

@@ -1,15 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="tests/bootstrap.php" colors="true"
convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true"
executionOrder="depends,defects" processIsolation="false" stopOnFailure="false">
<logging>
<log type="coverage-html" target="cache/coverage" lowUpperBound="35" highLowerBound="70"/>
<log type="coverage-clover" target="cache/coverage.clover"/>
</logging>
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="tests/bootstrap.php" colors="true" executionOrder="depends,defects" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
<report>
<clover outputFile="cache/coverage.clover"/>
<html outputDirectory="cache/coverage" lowUpperBound="35" highLowerBound="70"/>
</report>
</coverage>
<logging/>
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
</phpunit>

View File

@@ -19,6 +19,9 @@ class Utils
*/
public static function toBytes($string)
{
if (self::is_bytes($string)) {
return $string;
}
$bytes = [];
for ($i = 0; $i < \strlen($string); ++$i) {
$bytes[] = \ord($string[$i]);
@@ -36,6 +39,9 @@ class Utils
*/
public static function toString($bytes)
{
if (\is_string($bytes)) {
return $bytes;
}
$str = '';
foreach ($bytes as $ch) {
$str .= \chr($ch);
@@ -185,7 +191,7 @@ class Utils
$object = $object->toMap();
}
return json_encode($object);
return json_encode($object, JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES);
}
/**
@@ -380,7 +386,7 @@ class Utils
*
* @param mixed $value
*
* @return bool the number value
* @return int the number value
*/
public static function assertAsNumber($value)
{
@@ -391,6 +397,19 @@ class Utils
throw new \InvalidArgumentException('It is not a number value.');
}
/**
* Assert a value, if it is a integer, return it, otherwise throws
* @param mixed $value
* @return int the integer value
*/
public static function assertAsInteger($value){
if (\is_int($value)) {
return $value;
}
throw new \InvalidArgumentException('It is not a int value.');
}
/**
* Assert a value, if it is a map, return it, otherwise throws.
*

View File

@@ -0,0 +1,38 @@
<?php
// This file is auto-generated, don't edit it. Thanks.
namespace AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Model;
class ExtendsParameters extends Model {
public function validate() {}
public function toMap() {
$res = [];
if (null !== $this->headers) {
$res['headers'] = $this->headers;
}
if (null !== $this->queries) {
$res['queries'] = $this->queries;
}
return $res;
}
/**
* @param array $map
* @return ExtendsParameters
*/
public static function fromMap($map = []) {
$model = new self();
if(isset($map['headers'])){
$model->headers = $map['headers'];
}
if(isset($map['queries'])){
$model->queries = $map['queries'];
}
return $model;
}
public $headers;
public $queries;
}

View File

@@ -1,41 +1,273 @@
<?php
// This file is auto-generated, don't edit it. Thanks.
namespace AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Model;
class RuntimeOptions extends Model
{
/**
* The common runtime options model
*/
class RuntimeOptions extends Model {
protected $_name = [
'autoretry' => 'autoretry',
'ignoreSSL' => 'ignoreSSL',
'key' => 'key',
'cert' => 'cert',
'ca' => 'ca',
'maxAttempts' => 'max_attempts',
'backoffPolicy' => 'backoff_policy',
'backoffPeriod' => 'backoff_period',
'readTimeout' => 'readTimeout',
'connectTimeout' => 'connectTimeout',
'httpProxy' => 'httpProxy',
'httpsProxy' => 'httpsProxy',
'noProxy' => 'noProxy',
'maxIdleConns' => 'maxIdleConns',
'localAddr' => 'localAddr',
'socks5Proxy' => 'socks5Proxy',
'socks5NetWork' => 'socks5NetWork',
'keepAlive' => 'keepAlive',
];
public function validate() {}
public function toMap() {
$res = [];
if (null !== $this->autoretry) {
$res['autoretry'] = $this->autoretry;
}
if (null !== $this->ignoreSSL) {
$res['ignoreSSL'] = $this->ignoreSSL;
}
if (null !== $this->key) {
$res['key'] = $this->key;
}
if (null !== $this->cert) {
$res['cert'] = $this->cert;
}
if (null !== $this->ca) {
$res['ca'] = $this->ca;
}
if (null !== $this->maxAttempts) {
$res['max_attempts'] = $this->maxAttempts;
}
if (null !== $this->backoffPolicy) {
$res['backoff_policy'] = $this->backoffPolicy;
}
if (null !== $this->backoffPeriod) {
$res['backoff_period'] = $this->backoffPeriod;
}
if (null !== $this->readTimeout) {
$res['readTimeout'] = $this->readTimeout;
}
if (null !== $this->connectTimeout) {
$res['connectTimeout'] = $this->connectTimeout;
}
if (null !== $this->httpProxy) {
$res['httpProxy'] = $this->httpProxy;
}
if (null !== $this->httpsProxy) {
$res['httpsProxy'] = $this->httpsProxy;
}
if (null !== $this->noProxy) {
$res['noProxy'] = $this->noProxy;
}
if (null !== $this->maxIdleConns) {
$res['maxIdleConns'] = $this->maxIdleConns;
}
if (null !== $this->localAddr) {
$res['localAddr'] = $this->localAddr;
}
if (null !== $this->socks5Proxy) {
$res['socks5Proxy'] = $this->socks5Proxy;
}
if (null !== $this->socks5NetWork) {
$res['socks5NetWork'] = $this->socks5NetWork;
}
if (null !== $this->keepAlive) {
$res['keepAlive'] = $this->keepAlive;
}
if (null !== $this->extendsParameters) {
$res['extendsParameters'] = null !== $this->extendsParameters ? $this->extendsParameters->toMap() : null;
}
return $res;
}
/**
* @param array $map
* @return RuntimeOptions
*/
public static function fromMap($map = []) {
$model = new self();
if(isset($map['autoretry'])){
$model->autoretry = $map['autoretry'];
}
if(isset($map['ignoreSSL'])){
$model->ignoreSSL = $map['ignoreSSL'];
}
if(isset($map['key'])){
$model->key = $map['key'];
}
if(isset($map['cert'])){
$model->cert = $map['cert'];
}
if(isset($map['ca'])){
$model->ca = $map['ca'];
}
if(isset($map['max_attempts'])){
$model->maxAttempts = $map['max_attempts'];
}
if(isset($map['backoff_policy'])){
$model->backoffPolicy = $map['backoff_policy'];
}
if(isset($map['backoff_period'])){
$model->backoffPeriod = $map['backoff_period'];
}
if(isset($map['readTimeout'])){
$model->readTimeout = $map['readTimeout'];
}
if(isset($map['connectTimeout'])){
$model->connectTimeout = $map['connectTimeout'];
}
if(isset($map['httpProxy'])){
$model->httpProxy = $map['httpProxy'];
}
if(isset($map['httpsProxy'])){
$model->httpsProxy = $map['httpsProxy'];
}
if(isset($map['noProxy'])){
$model->noProxy = $map['noProxy'];
}
if(isset($map['maxIdleConns'])){
$model->maxIdleConns = $map['maxIdleConns'];
}
if(isset($map['localAddr'])){
$model->localAddr = $map['localAddr'];
}
if(isset($map['socks5Proxy'])){
$model->socks5Proxy = $map['socks5Proxy'];
}
if(isset($map['socks5NetWork'])){
$model->socks5NetWork = $map['socks5NetWork'];
}
if(isset($map['keepAlive'])){
$model->keepAlive = $map['keepAlive'];
}
if(isset($map['extendsParameters'])){
$model->extendsParameters = ExtendsParameters::fromMap($map['extendsParameters']);
}
return $model;
}
/**
* @description whether to try again
* @var bool
*/
public $autoretry;
/**
* @description ignore SSL validation
* @var bool
*/
public $ignoreSSL;
/**
* @description privite key for client certificate
* @var string
*/
public $key;
/**
* @description client certificate
* @var string
*/
public $cert;
/**
* @description server certificate
* @var string
*/
public $ca;
/**
* @description maximum number of retries
* @var int
*/
public $maxAttempts;
/**
* @description backoff policy
* @var string
*/
public $backoffPolicy;
/**
* @description backoff period
* @var int
*/
public $backoffPeriod;
/**
* @description read timeout
* @var int
*/
public $readTimeout;
/**
* @description connect timeout
* @var int
*/
public $connectTimeout;
/**
* @description http proxy url
* @var string
*/
public $httpProxy;
/**
* @description https Proxy url
* @var string
*/
public $httpsProxy;
/**
* @description agent blacklist
* @var string
*/
public $noProxy;
/**
* @description maximum number of connections
* @var int
*/
public $maxIdleConns;
/**
* @description local addr
* @var string
*/
public $localAddr;
/**
* @description SOCKS5 proxy
* @var string
*/
public $socks5Proxy;
/**
* @description SOCKS5 netWork
* @var string
*/
public $socks5NetWork;
/**
* @description whether to enable keep-alive
* @var bool
*/
public $keepAlive;
protected $_name = [];
/**
* @description Extends Parameters
* @var ExtendsParameters
*/
public $extendsParameters;
}

View File

@@ -4,6 +4,8 @@ namespace AlibabaCloud\Tea\Utils\Tests;
use AlibabaCloud\Tea\Model;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Utils\Utils\ExtendsParameters;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use GuzzleHttp\Psr7\Stream;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\StreamInterface;
@@ -24,6 +26,11 @@ final class UtilsTest extends TestCase
$this->assertEquals([
115, 116, 114, 105, 110, 103,
], Utils::toBytes('string'));
$this->assertEquals([
115, 116, 114, 105, 110, 103,
], Utils::toBytes([
115, 116, 114, 105, 110, 103,
]));
}
public function testToString()
@@ -31,6 +38,7 @@ final class UtilsTest extends TestCase
$this->assertEquals('string', Utils::toString([
115, 116, 114, 105, 110, 103,
]));
$this->assertEquals('string', Utils::toString('string'));
}
public function testParseJSON()
@@ -108,12 +116,17 @@ final class UtilsTest extends TestCase
$this->assertJson(Utils::toJSONString($object));
$this->assertEquals('[]', Utils::toJSONString([]));
$this->assertEquals('["foo"]', Utils::toJSONString(['foo']));
$this->assertEquals('{"str":"test","number":1,"bool":false,"null":null}', Utils::toJSONString([
'str' => 'test',
'number' => 1,
'bool' => FALSE,
'null' => null,
]));
$this->assertEquals(
'{"str":"test","number":1,"bool":false,"null":null,"chinese":"中文","http":"https://aliyun.com:8080/zh/中文.html"}',
Utils::toJSONString([
'str' => 'test',
'number' => 1,
'bool' => FALSE,
'null' => null,
'chinese' => '中文',
'http' => 'https://aliyun.com:8080/zh/中文.html',
])
);
$this->assertEquals('1', Utils::toJSONString(1));
$this->assertEquals('true', Utils::toJSONString(TRUE));
$this->assertEquals('null', Utils::toJSONString(null));
@@ -250,6 +263,21 @@ final class UtilsTest extends TestCase
}
}
public function testAssertAsInteger()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('It is not a int value.');
Utils::assertAsInteger('is not int');
try {
$map = 123;
$this->assertEquals($map, Utils::assertAsInteger($map));
} catch (\Exception $e) {
// should not be here
$this->assertTrue(false);
}
}
public function testAssertAsMap()
{
$this->expectException(\InvalidArgumentException::class);
@@ -371,6 +399,48 @@ final class UtilsTest extends TestCase
Utils::assertAsReadable(0);
}
public function testRuntimeOptions()
{
$opts = new RuntimeOptions([
"autoretry" => false,
"ignoreSSL" => false,
"key" => "key",
"cert" => "cert",
"ca" => "ca",
"maxAttempts" => 3,
"backoffPolicy" => "backoffPolicy",
"backoffPeriod" => 10,
"readTimeout" => 3000,
"connectTimeout" => 3000,
"httpProxy" => "httpProxy",
"httpsProxy" => "httpsProxy",
"noProxy" => "noProxy",
"maxIdleConns" => 300,
"keepAlive" => true,
"extendsParameters" => new ExtendsParameters([
"headers" => ['key' => 'value'],
"queries" => ['key' => 'value'],
]),
]);
$this->assertEquals(false, $opts->autoretry);
$this->assertEquals(false, $opts->ignoreSSL);
$this->assertEquals("key", $opts->key);
$this->assertEquals("cert", $opts->cert);
$this->assertEquals("ca", $opts->ca);
$this->assertEquals(3, $opts->maxAttempts);
$this->assertEquals("backoffPolicy", $opts->backoffPolicy);
$this->assertEquals(10, $opts->backoffPeriod);
$this->assertEquals(3000, $opts->readTimeout);
$this->assertEquals(3000, $opts->connectTimeout);
$this->assertEquals("httpProxy", $opts->httpProxy);
$this->assertEquals("httpsProxy", $opts->httpsProxy);
$this->assertEquals("noProxy", $opts->noProxy);
$this->assertEquals(300, $opts->maxIdleConns);
$this->assertEquals(true, $opts->keepAlive);
$this->assertEquals('value', $opts->extendsParameters->headers['key']);
$this->assertEquals('value', $opts->extendsParameters->queries['key']);
}
private function convert($body, &$content)
{
$class = new \ReflectionClass($body);

View File

@@ -13,7 +13,7 @@
"php": ">5.5"
},
"require-dev": {
"phpunit/phpunit": "^4.8.35|^5.4.3",
"phpunit/phpunit": "*",
"symfony/var-dumper": "*"
},
"autoload": {

View File

@@ -8,7 +8,7 @@
<directory>tests</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>

View File

@@ -8,14 +8,14 @@ class XML
{
$res = self::parse($xmlStr);
if ($response === null) {
return $ref;
return $res;
} else {
if (\is_string($response)) {
$response = new $response();
}
$prop = get_object_vars($response);
$target = [];
foreach ($res as $k => $v) {
if (isset($prop[$k])) {
$target[$k] = $v;
@@ -45,7 +45,9 @@ class XML
private static function parse($xml)
{
libxml_disable_entity_loader(true);
if (\PHP_VERSION_ID < 80000) {
libxml_disable_entity_loader(true);
}
return json_decode(
json_encode(

View File

@@ -23,13 +23,13 @@ class RpcUtilsTest extends TestCase
$name = $res['name'];
$value = $res['value'];
$this->assertEquals('test', $name);
$this->assertEquals('1', $value);
$this->assertEquals(1, $value);
$res = XML::parseXml($this->xmlStr, null);
$name = $res['name'];
$value = $res['value'];
$this->assertEquals('test', $name);
$this->assertEquals('1', $value);
$this->assertEquals(1, $value);
}
public function testArrayToXML()

View File

@@ -31,7 +31,7 @@
"ext-simplexml": "*",
"ext-xmlwriter": "*",
"guzzlehttp/guzzle": "^6.3|^7.0",
"adbario/php-dot-notation": "^2.3"
"adbario/php-dot-notation": "^2.4"
},
"require-dev": {
"symfony/dotenv": "^3.4",

View File

@@ -13,6 +13,9 @@ class TeaError extends RuntimeException
public $code = 0;
public $data;
public $name = '';
public $statusCode;
public $description;
public $accessDeniedDetail;
private $errorInfo;
/**
@@ -28,10 +31,13 @@ class TeaError extends RuntimeException
parent::__construct((string) $message, (int) $code, $previous);
$this->errorInfo = $errorInfo;
if (!empty($errorInfo)) {
$properties = ['name', 'message', 'code', 'data'];
$properties = ['name', 'message', 'code', 'data', 'description', 'accessDeniedDetail'];
foreach ($properties as $property) {
if (isset($errorInfo[$property])) {
$this->{$property} = $errorInfo[$property];
if ($property === 'data' && isset($errorInfo['data']['statusCode'])) {
$this->statusCode = $errorInfo['data']['statusCode'];
}
}
}
}

View File

@@ -37,6 +37,50 @@ class Helper
return \JSON_ERROR_NONE == json_last_error();
}
/**
* @param mixed $value
*
* @return bool
*/
public static function isBytes($value)
{
if (!\is_array($value)) {
return false;
}
$i = 0;
foreach ($value as $k => $ord) {
if ($k !== $i) {
return false;
}
if (!\is_int($ord)) {
return false;
}
if ($ord < 0 || $ord > 255) {
return false;
}
++$i;
}
return true;
}
/**
* Convert a bytes to string(utf8).
*
* @param array $bytes
*
* @return string the return string
*/
public static function toString($bytes)
{
$str = '';
foreach ($bytes as $ch) {
$str .= \chr($ch);
}
return $str;
}
/**
* @return array
*/

View File

@@ -89,11 +89,15 @@ class Request extends PsrRequest
if ($this->body instanceof StreamInterface) {
$request = $request->withBody($this->body);
} else {
$body = $this->body;
if (Helper::isBytes($this->body)) {
$body = Helper::toString($this->body);
}
if (\function_exists('\GuzzleHttp\Psr7\stream_for')) {
// @deprecated stream_for will be removed in guzzlehttp/psr7:2.0
$request = $request->withBody(\GuzzleHttp\Psr7\stream_for($this->body));
$request = $request->withBody(\GuzzleHttp\Psr7\stream_for($body));
} else {
$request = $request->withBody(\GuzzleHttp\Psr7\Utils::streamFor($this->body));
$request = $request->withBody(\GuzzleHttp\Psr7\Utils::streamFor($body));
}
}
}

View File

@@ -273,6 +273,9 @@ class Tea
if (isset($config['noProxy']) && !empty($config['noProxy'])) {
$options->set('proxy.no', $config['noProxy']);
}
if (isset($config['ignoreSSL']) && !empty($config['ignoreSSL'])) {
$options->set('verify',!((bool)$config['ignoreSSL']));
}
// readTimeout&connectTimeout unit is millisecond
$read_timeout = isset($config['readTimeout']) && !empty($config['readTimeout']) ? (int) $config['readTimeout'] : 0;
$con_timeout = isset($config['connectTimeout']) && !empty($config['connectTimeout']) ? (int) $config['connectTimeout'] : 0;

View File

@@ -1,5 +1,9 @@
# ChangeLog - Aliyun OSS SDK for PHP
## v2.7.2 / 2024-10-28
* Added: presign supports response-* parameters
* Added: forcePathStyle option.
## v2.7.1 / 2024-02-28
* Fixed: fix deprecated

View File

@@ -169,6 +169,11 @@ class OssClient
throw new OssException("endpoint is empty");
}
$this->hostname = $this->checkEndpoint($endpoint, $isCName);
if (isset($config['forcePathStyle'])) {
if ($config['forcePathStyle'] === true) {
$this->hostType = self::OSS_HOST_TYPE_PATH_STYLE;
}
}
$this->requestProxy = $requestProxy;
if (!$provider instanceof CredentialsProvider) {
throw new OssException("provider must be an instance of CredentialsProvider");
@@ -2372,7 +2377,7 @@ class OssClient
$options[self::OSS_OBJECT] = $object;
$options[self::OSS_SUB_RESOURCE] = 'x-oss-async-process';
$options[self::OSS_CONTENT_TYPE] = 'application/octet-stream';
$options[self::OSS_CONTENT] = 'x-oss-async-process='.$asyncProcess;
$options[self::OSS_CONTENT] = 'x-oss-async-process=' . $asyncProcess;
$response = $this->auth($options);
$result = new BodyResult($response);
return $result->getData();
@@ -2999,7 +3004,7 @@ class OssClient
return $this->getValue($options, self::OSS_CHECK_MD5, false, true, true);
}
/**
/**
* Gets value of the specified key from the options
*
* @param array $options
@@ -3269,7 +3274,7 @@ class OssClient
try {
$tmp_object = $options[self::OSS_OBJECT];
$encoding = array('UTF-8','GB2312', 'GBK');
$encoding = array('UTF-8', 'GB2312', 'GBK');
$encode = mb_detect_encoding($tmp_object, $encoding);
if ($encode === 'UTF-8' || $encode === false) {
return;
@@ -3333,6 +3338,9 @@ class OssClient
if ('' !== $bucket) {
if ($this->hostType === self::OSS_HOST_TYPE_IP || $this->hostType === self::OSS_HOST_TYPE_PATH_STYLE) {
$paths[] = $bucket;
if ('' === $object) {
$paths[] = '';
}
}
}
// + object
@@ -3354,6 +3362,12 @@ class OssClient
$query = array();
$queryList = array(
self::OSS_PART_NUM,
'response-content-type',
'response-content-language',
'response-cache-control',
'response-content-encoding',
'response-expires',
'response-content-disposition',
self::OSS_UPLOAD_ID,
self::OSS_COMP,
self::OSS_LIVE_CHANNEL_STATUS,
@@ -3381,6 +3395,7 @@ class OssClient
if (isset($options[self::OSS_SUB_RESOURCE])) {
$query[$options[self::OSS_SUB_RESOURCE]] = '';
}
return OssUtil::toQueryString($query);
}
@@ -3519,7 +3534,7 @@ class OssClient
}
try {
$encoding = array('UTF-8','GB2312', 'GBK');
$encoding = array('UTF-8', 'GB2312', 'GBK');
$encode = mb_detect_encoding($filepath, $encoding);
if ($encode !== 'UTF-8') {
return $filepath;
@@ -3534,7 +3549,7 @@ class OssClient
return $filepath;
}
/**
/**
* Decodes the file path from GBK to UTF-8.
*
* @param $filepath
@@ -3550,7 +3565,7 @@ class OssClient
}
try {
$encoding = array('UTF-8','GB2312', 'GBK');
$encoding = array('UTF-8', 'GB2312', 'GBK');
$encode = mb_detect_encoding($filepath, $encoding);
if ($encode === 'UTF-8' || $encode === false) {
return $filepath;
@@ -3745,8 +3760,8 @@ class OssClient
);
// OssClient version information
const OSS_NAME = "aliyun-sdk-php";
const OSS_VERSION = "2.7.1";
const OSS_BUILD = "20240228";
const OSS_VERSION = "2.7.2";
const OSS_BUILD = "20241028";
const OSS_AUTHOR = "";
const OSS_OPTIONS_ORIGIN = 'Origin';
const OSS_OPTIONS_REQUEST_METHOD = 'Access-Control-Request-Method';

View File

@@ -133,6 +133,11 @@ class Common
return OssClient::OSS_SIGNATURE_VERSION_V1;
}
public static function getPathStyleBucket()
{
return getenv('OSS_TEST_PATHSTYLE_BUCKET');
}
/**
* Tool method, create a bucket
*/

View File

@@ -3,10 +3,7 @@
namespace OSS\Tests;
use OSS\Core\OssException;
use OSS\Credentials\StaticCredentialsProvider;
use OSS\Model\LifecycleConfig;
use OSS\Model\LifecycleRule;
use OSS\Model\LifecycleAction;
use OSS\Http\RequestCore;
use OSS\OssClient;
require_once __DIR__ . DIRECTORY_SEPARATOR . 'TestOssClientBase.php';
@@ -18,12 +15,14 @@ class OssClientForcePathStyleTest extends TestOssClientBase
{
$config = array(
'signatureVersion' => OssClient::OSS_SIGNATURE_VERSION_V4,
'hostType' => OssClient::OSS_HOST_TYPE_PATH_STYLE,
'forcePathStyle' => true,
);
$this->ossClient = Common::getOssClient($config);
$pathStyleClient = Common::getOssClient($config);
try {
$this->ossClient->getBucketInfo($this->bucket);
$pathStyleClient->getBucketInfo($this->bucket);
$this->assertTrue(false, "should not here");
} catch (OssException $e) {
$this->assertEquals($e->getErrorCode(), "SecondLevelDomainForbidden");
$this->assertTrue(true);
@@ -31,7 +30,8 @@ class OssClientForcePathStyleTest extends TestOssClientBase
try {
$object = "oss-php-sdk-test/upload-test-object-name.txt";
$this->ossClient->putObject($this->bucket, $object, 'hi oss');
$pathStyleClient->putObject($this->bucket, $object, 'hi oss');
$this->assertTrue(false, "should not here");
} catch (OssException $e) {
$this->assertEquals($e->getErrorCode(), "SecondLevelDomainForbidden");
$this->assertTrue(true);
@@ -40,11 +40,85 @@ class OssClientForcePathStyleTest extends TestOssClientBase
try {
$endpoint = Common::getEndpoint();
$endpoint = str_replace(array('http://', 'https://'), '', $endpoint);
$strUrl = $this->bucket . '.' . $endpoint . "/" . $object;
$signUrl = $this->ossClient->signUrl($this->bucket, $object, 3600);
$strUrl = $endpoint . "/" . $this->bucket . '/' . $object;
$signUrl = $pathStyleClient->signUrl($this->bucket, $object, 3600);
$this->assertTrue(strpos($signUrl, $strUrl) !== false);
} catch (OssException $e) {
$this->assertFalse(true);
}
}
public function testForcePathStyleOKV1()
{
$bucket = Common::getPathStyleBucket();
$this->assertFalse(empty($bucket), "path style bucket is not set.");
$config = array(
'signatureVersion' => OssClient::OSS_SIGNATURE_VERSION_V1,
'forcePathStyle' => true,
);
$pathStyleClient = Common::getOssClient($config);
// bucket
$info = $pathStyleClient->getBucketInfo($bucket);
$this->assertEquals($bucket, $info->getName());
// object
$object = "upload-test-object-name.txt";
$pathStyleClient->putObject($bucket, $object, 'hi oss');
$res = $pathStyleClient->getObject($bucket, $object);
$this->assertEquals($res, 'hi oss');
//presign
$signUrl = $pathStyleClient->signUrl($bucket, $object, 3600);
$httpCore = new RequestCore($signUrl);
$httpCore->set_body("");
$httpCore->set_method("GET");
$httpCore->connect_timeout = 10;
$httpCore->timeout = 10;
$httpCore->add_header("Content-Type", "");
$httpCore->send_request();
$this->assertEquals(200, $httpCore->response_code);
}
public function testForcePathStyleOKV4()
{
$bucket = Common::getPathStyleBucket();
$this->assertFalse(empty($bucket), "path style bucket is not set.");
$config = array(
'signatureVersion' => OssClient::OSS_SIGNATURE_VERSION_V4,
'forcePathStyle' => true,
);
$pathStyleClient = Common::getOssClient($config);
// bucket
$info = $pathStyleClient->getBucketInfo($bucket);
$this->assertEquals($bucket, $info->getName());
// object
$object = "upload-test-object-name.txt";
$pathStyleClient->putObject($bucket, $object, 'hi oss');
$res = $pathStyleClient->getObject($bucket, $object);
$this->assertEquals($res, 'hi oss');
//presign
$signUrl = $pathStyleClient->signUrl($bucket, $object, 3600);
#print("signUrl" . $signUrl . "\n");
$httpCore = new RequestCore($signUrl);
$httpCore->set_body("");
$httpCore->set_method("GET");
$httpCore->connect_timeout = 10;
$httpCore->timeout = 10;
$httpCore->add_header("Content-Type", "");
$httpCore->send_request();
$this->assertEquals(200, $httpCore->response_code);
}
}

View File

@@ -0,0 +1,70 @@
<?php
namespace OSS\Tests;
use OSS\Core\OssException;
use OSS\Credentials\StaticCredentialsProvider;
use OSS\Http\RequestCore;
use OSS\Http\ResponseCore;
use OSS\OssClient;
require_once __DIR__ . DIRECTORY_SEPARATOR . 'TestOssClientBase.php';
class OssClientPresignTest extends TestOssClientBase
{
protected $stsOssClient;
public function testObjectWithSignV1()
{
$config = array(
'signatureVersion' => OssClient::OSS_SIGNATURE_VERSION_V1
);
$this->bucket = Common::getBucketName() . '-' . time();
$this->ossClient = Common::getOssClient($config);
$this->ossClient->createBucket($this->bucket);
Common::waitMetaSync();
$object = "a.file";
$this->ossClient->putObject($this->bucket, $object, "hi oss");
$timeout = 3600;
$options = array(
"response-content-disposition" => "inline"
);
try {
$signedUrl = $this->ossClient->signUrl($this->bucket, $object, $timeout, OssClient::OSS_HTTP_GET, $options);
} catch (OssException $e) {
$this->assertFalse(true);
}
$this->assertStringContainsString("response-content-disposition=inline", $signedUrl);
$options = array(
"response-content-disposition" => "attachment",
);
try {
$signedUrl = $this->ossClient->signUrl($this->bucket, $object, $timeout, OssClient::OSS_HTTP_GET, $options);
} catch (OssException $e) {
$this->assertFalse(true);
}
$this->assertStringContainsString("response-content-disposition=attachment", $signedUrl);
$httpCore = new RequestCore($signedUrl);
$httpCore->set_body("");
$httpCore->set_method("GET");
$httpCore->connect_timeout = 10;
$httpCore->timeout = 10;
$httpCore->add_header("Content-Type", "");
$httpCore->send_request();
$this->assertEquals(200, $httpCore->response_code);
}
protected function tearDown(): void
{
$this->ossClient->deleteObject($this->bucket, "a.file");
parent::tearDown();
}
protected function setUp(): void
{
}
}

View File

@@ -305,6 +305,49 @@ class OssClientPresignV4Test extends TestOssClientBase
}
}
public function testObjectWithSignV4AndResponseQuery()
{
$config = array(
'signatureVersion' => OssClient::OSS_SIGNATURE_VERSION_V4
);
$this->bucket = Common::getBucketName() . '-' . time();
$this->ossClient = Common::getOssClient($config);
$this->ossClient->createBucket($this->bucket);
Common::waitMetaSync();
$object = "a.file";
$this->ossClient->putObject($this->bucket, $object, "hi oss");
$timeout = 3600;
$options = array(
"response-content-disposition" => "inline"
);
try {
$signedUrl = $this->ossClient->signUrl($this->bucket, $object, $timeout, OssClient::OSS_HTTP_GET, $options);
} catch (OssException $e) {
$this->assertFalse(true);
}
$this->assertStringContainsString("response-content-disposition=inline", $signedUrl);
$options = array(
"response-content-disposition" => "attachment"
);
try {
$signedUrl = $this->ossClient->signUrl($this->bucket, $object, $timeout, OssClient::OSS_HTTP_GET, $options);
} catch (OssException $e) {
$this->assertFalse(true);
}
$this->assertStringContainsString("response-content-disposition=attachment", $signedUrl);
$httpCore = new RequestCore($signedUrl);
$httpCore->set_body("");
$httpCore->set_method("GET");
$httpCore->connect_timeout = 10;
$httpCore->timeout = 10;
$httpCore->add_header("Content-Type", "");
$httpCore->send_request();
$this->assertEquals(200, $httpCore->response_code);
}
protected function tearDown(): void
{
$this->ossClient->deleteObject($this->bucket, "a.file");

View File

@@ -2,6 +2,24 @@
// autoload.php @generated by Composer
if (PHP_VERSION_ID < 50600) {
if (!headers_sent()) {
header('HTTP/1.1 500 Internal Server Error');
}
$err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL;
if (!ini_get('display_errors')) {
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
fwrite(STDERR, $err);
} elseif (!headers_sent()) {
echo $err;
}
}
trigger_error(
$err,
E_USER_ERROR
);
}
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit075712db3cd06cc0fc14f5ab72673de0::getLoader();

Some files were not shown because too many files have changed in this diff Show More