diff --git a/app/Cache/Redis/Admin/MenuCache.php b/app/Cache/Redis/Admin/MenuCache.php index 130cb72..3f2a035 100644 --- a/app/Cache/Redis/Admin/MenuCache.php +++ b/app/Cache/Redis/Admin/MenuCache.php @@ -3,9 +3,8 @@ namespace App\Cache\Redis\Admin; use App\Cache\Redis\RedisCache; -use App\Constants\Admin\AuthCode; use App\Model\AdminMenu; -use App\Service\ServiceTrait\AdminRoleMenuTrait; +use App\Service\ServiceTrait\Admin\AdminRoleMenuTrait; use Hyperf\Di\Annotation\Inject; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; diff --git a/app/Cache/Redis/Admin/RoleCache.php b/app/Cache/Redis/Admin/RoleCache.php index 5386997..42a67db 100644 --- a/app/Cache/Redis/Admin/RoleCache.php +++ b/app/Cache/Redis/Admin/RoleCache.php @@ -6,7 +6,7 @@ use App\Cache\Redis\RedisCache; use App\Constants\Admin\AuthCode; use App\Model\AdminMenu; use App\Model\AdminRoleMenu; -use App\Service\ServiceTrait\AdminRoleMenuTrait; +use App\Service\ServiceTrait\Admin\AdminRoleMenuTrait; use Hyperf\Di\Annotation\Inject; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; diff --git a/app/Controller/Admin/ThirdController.php b/app/Controller/Admin/ThirdController.php new file mode 100644 index 0000000..0377a18 --- /dev/null +++ b/app/Controller/Admin/ThirdController.php @@ -0,0 +1,23 @@ +handle(); + } +} diff --git a/app/Lib/Log.php b/app/Lib/Log.php index 8fe77e0..6363e2c 100644 --- a/app/Lib/Log.php +++ b/app/Lib/Log.php @@ -18,7 +18,7 @@ class Log * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ - private function getLogger(string $name = 'app',string $group = 'app') + private function getLogger(string $name = 'app',string $group = 'app'): LoggerInterface { return ApplicationContext::getContainer()->get(LoggerFactory::class)->get($name, $group); } diff --git a/app/Service/Admin/Third/AliStsService.php b/app/Service/Admin/Third/AliStsService.php new file mode 100644 index 0000000..c62c587 --- /dev/null +++ b/app/Service/Admin/Third/AliStsService.php @@ -0,0 +1,99 @@ +bucket = config('ali.bucket'); + $this->roleArn = config('ali.role_arn'); //acs:ram::1987853712163999:role/video-access + } + + /** + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function handle(): array + { + $payload = [ + 'durationSeconds' => $this->seconds, + 'roleArn' => $this->roleArn, + 'roleSessionName' => 'adminUpload', + 'policy' => [ + 'Version' => '1', + 'Statement' => [ + [ + 'Effect' => 'Allow', + 'Action' => [ + 'oss:*' + ], + 'Resource' => [ + sprintf('acs:oss:*:*:%s', $this->bucket), + sprintf('acs:oss:*:*:%s/*', $this->bucket), + ] + ], + [ + 'Effect' => 'Deny', + 'Action' => [ + 'oss:DeleteBucket' + ], + 'Resource' => [ + sprintf('acs:oss:*:*:%s', $this->bucket), + ] + ], + [ + 'Effect' => 'Allow', + 'Action' => [ + 'oss:DeleteObject' + ], + 'Resource' => [ + sprintf('acs:oss:*:*:%s/*', $this->bucket), + ] + ], + + ], + ] + ]; + + $res = $this->getAliStsControls($payload); + $this->log->info(__CLASS__.__FUNCTION__.':'.json_encode($res)); + return $this->return->success(); + } +} \ No newline at end of file diff --git a/app/Service/ServiceTrait/AdminRoleMenuTrait.php b/app/Service/ServiceTrait/Admin/AdminRoleMenuTrait.php similarity index 96% rename from app/Service/ServiceTrait/AdminRoleMenuTrait.php rename to app/Service/ServiceTrait/Admin/AdminRoleMenuTrait.php index 8b9bbc6..c390afe 100644 --- a/app/Service/ServiceTrait/AdminRoleMenuTrait.php +++ b/app/Service/ServiceTrait/Admin/AdminRoleMenuTrait.php @@ -8,7 +8,7 @@ declare(strict_types=1); -namespace App\Service\ServiceTrait; +namespace App\Service\ServiceTrait\Admin; use App\Constants\Admin\AuthCode; diff --git a/app/Service/ServiceTrait/Common/AliStsTrait.php b/app/Service/ServiceTrait/Common/AliStsTrait.php new file mode 100644 index 0000000..412ef62 --- /dev/null +++ b/app/Service/ServiceTrait/Common/AliStsTrait.php @@ -0,0 +1,66 @@ + config('ali.accessKeyId'), + "accessKeySecret" => config('ali.accessKeySecret') + ]); + $config->endpoint = config('ali.sts_endpoint'); + + return new Sts($config); + } + + /** + * 获取 ali sts 控制器 + * @param $payload + * @return Sts|void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function getAliStsControls($payload){ + $client = self::createClient(); + $assumeRoleRequest = new AssumeRoleRequest($payload); + $runtime = new RuntimeOptions([]); + try { + $client->assumeRoleWithOptions($assumeRoleRequest, $runtime); + + return $client; + } catch (Exception $error) { + $this->log->error(__CLASS__.__FUNCTION__.'-'.__LINE__, [$error->getMessage()]); + } + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index 10d6083..80eedda 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "license": "Apache-2.0", "require": { "php": ">=8.3", - "alibabacloud/sts-20150401": "^1.1", + "alibabacloud/sts-20150401": "1.1.4", "aliyuncs/oss-sdk-php": "^2.7", "firebase/php-jwt": "^6.10", "hyperf/amqp": "~3.1.0", diff --git a/config/autoload/ali.php b/config/autoload/ali.php index 2d55ae5..11d2e0c 100644 --- a/config/autoload/ali.php +++ b/config/autoload/ali.php @@ -14,17 +14,21 @@ use function Hyperf\Support\env; return [ // 阿里云 accessKeyId - 'access_key_id' => env('ALI_ACCESS_KEY_ID', ''), + 'access_key_id' => env('ALI_ACCESS_KEY_ID', 'LTAI5tFsBsKskcrRmkjpMXay'), // 阿里云 accessKeySecret - 'access_key_secret' => env('ALI_ACCESS_KEY_SECRET', ''), + 'access_key_secret' => env('ALI_ACCESS_KEY_SECRET', 'YSIMtOGLu7W8tpQLKdgioTUYmgXB8M'), // 阿里云 oss bucket - 'bucket' => env('ALI_BUCKET', ''), + 'bucket' => env('ALI_BUCKET', 'hhl-catering'), // 阿里云 oss region 'region' => env('ALI_REGION', 'cn-shenzhen'), // 阿里云 oss 临时上传目录 - 'upload_dir' => env('ALI_UPLOAD_DIR', ''), + 'upload_dir' => env('ALI_UPLOAD_DIR', '/tmp/upload'), // 阿里云 oss 回调地址 - 'callback_url' => env('ALI_CALLBACK_URL', ''), + 'callback_url' => env('ALI_CALLBACK_URL', 'https://xxx.xxx.com/common/third/oss/callback'), // 阿里云 oss 外网访问地址 - 'oss_url' => env('ALI_OSS_URL', ''), + 'oss_url' => env('ALI_OSS_URL', 'https://playlet-video-asset.oss-cn-shenzhen.aliyuncs.com/'), + // 阿里云 sts 访问端点 + 'sts_endpoint' => env('ALI_STS_ENDPOINT', 'sts.cn-shenzhen.aliyuncs.com'), + // 阿里云 sts 角色 + 'role_arn' => env('ALI_ROLE_ARN', 'acs:ram::1644087445786901:role/oss'), ]; \ No newline at end of file diff --git a/sync/http/admin/auth.http b/sync/http/admin/auth.http index f2510cb..7416483 100644 --- a/sync/http/admin/auth.http +++ b/sync/http/admin/auth.http @@ -109,4 +109,9 @@ Authorization: Bearer {{admin_token}} ### 账号重置密码 GET {{host}}/admin/employee/reset_password?id=2 Content-Type: application/x-www-form-urlencoded +Authorization: Bearer {{admin_token}} + +### ali sts 临时授权 +GET {{host}}/admin/third/sts/accredit +Content-Type: application/x-www-form-urlencoded Authorization: Bearer {{admin_token}} \ No newline at end of file