QCloud COS FederationToken generator for Laravel.

Overview

Laravel 腾讯云联合身份临时访问凭证生成器

Laravel Octane Ready Status GitHub release (latest SemVer) GitHub License Packagist Downloads

Laravel 腾讯云联合身份临时访问凭证 生成器,主要用于下发腾讯云联合身份临时访问凭证,比如前端直传等场景。

Sponsor me

开始之前,请您仔细阅读并理解一下官方文档:

安装

$ composer require overtrue/laravel-qcloud-federation-token -vvv

配置

你可以通过以下命令将配置文件写入 config/federation-token.php:

$ php artisan vendor:publish --provider="Overtrue\\LaravelQCloudFederationToken\\QCloudFederationTokenServiceProvider"

config/federation-token.php

return [
    // 默认配置,strategies 下的每一个策略将合并此基础配置
    'default' => [
        'secret_id' => env('QCLOUD_COS_SECRET_ID', ''),
        'secret_key' => env('QCLOUD_COS_SECRET_KEY', ''),
        'region' => env('QCLOUD_COS_REGION', 'ap-guangzhou'),
        "effect" => "allow",
        
        // 全局变量,会被替换到所有策略中
        'variables' => [
            'uid' => env('QCLOUD_COS_APP_ID'),
            'region' => env('QCLOUD_COS_REGION', 'ap-guangzhou'),
            //...
        ],
    ],
    // strategies
    'strategies' => [
        'cos' => [
            // 策略名称,可选
            'name' => 'cos-put',
            
            // 临时凭证过期时间
            'expires_in' => 1800,
            
            // 将与默认配置合并
            'variables' => [
                'appid' => env('QCLOUD_COS_APP_ID'),
                'bucket' => env('QCLOUD_COS_BUCKET', ''),
                //...
            ],
            
            // Statement 请参考:https://cloud.tencent.com/document/product/598/10603
            "statements" => [
                [
                    "action" => [
                        "cos:PutObject",
                        "cos:GetObject",
                    ],
                    "resource" => [
                        "qcs::cos:ap-beijing:uid/<appid>:<bucket>-<appid>/<date>/<uuid>/*",
                    ],
                ]
            ],
        ],
    ],
];

配置语法请参考:https://cloud.tencent.com/document/product/598/10603

你可以根据使用场景配置多个策略,然后按策略分发访问凭证。

Warning

注意:condition 中使用 cos:content-type 记得 urlencode, 不然遇到带+号的 MIME 将会失效,比如 image/svg+xml

安全提醒

临时 token 规则配置不合理,可能会造成安全问题,请谨慎配置,请严格检查如下:

变量替换

在配置中难免会用到各种上下文变量或者一些动态 resouce 路径等,你可以在配置中指定 variables 变量来实现变量替换,例如:

仅 principal 和 resource 中的变量可以替换,其他变量不支持替换。

// config/federation-token.php
<?php

return [
    // 默认配置,strategies 下的每一个策略将合并此基础配置
    'default' => [
        'secret_id' => env('QCLOUD_COS_SECRET_ID'),
        'secret_key' => env('QCLOUD_COS_SECRET_KEY'),
        'region' => env('QCLOUD_COS_REGION', 'ap-guangzhou'),
        "effect" => "allow",
        
        // 全局变量,会被替换到所有策略中
        'variables' => [
            'region' => env('QCLOUD_COS_REGION', 'ap-guangzhou'),
            //...
        ],
    ],
    // strategies
    'strategies' => [
        // 请参考:https://cloud.tencent.com/document/product/598/10603
        'cos' => [
            // 将与默认配置合并
            'variables' => [
                'appid' => env('QCLOUD_COS_APP_ID'),
                'bucket' => env('QCLOUD_COS_BUCKET'),
                //...
            ],
            "statements" => [
                [
                    "action" => [
                        "cos:PutObject",
                        "cos:GetObject",
                    ],
                    "resource" => [
                        "qcs::cos:ap-beijing:uid/<appid>:<bucket>-<appid>/<date>/<uuid>/*",
                    ],
                ]
            ],
        ],
    ],
];

以上配置将会生成如下结果:

{
    "effect": "allow",
    "action": [
        "cos:PutObject",
        "cos:GetObject",
    ],
    "resource": [
        "qcs::cos:ap-beijing:uid/12278900:example-12278900/20220202/bbeae9bb-d650-46f9-aab3-f4171a1bfdea/*"
    ]
}

内置变量如下

  • <uuid> - UUID 例如:ca007813-4a49-4d5a-afab-abae18a969a5
  • <timestamp> - 当前时间戳,例如:1654485526
  • <random> - 随机字符串,16 位,例如:Bbq6gkXXIPyCDsEL
  • <random:32> - 随机字符串,32 位,例如:FykbMqi6GT6JHiyv6E2xqUeo3CZLPjo7
  • <date> - 日期,例如:20220606
  • <Ymd> - 日期,例如:20220606
  • <YmdHis> - 日期时间(年月日时分秒),例如:20220606031846
  • <Y> - 年,例如:2022
  • <m> - 月,例如:06
  • <d> - 日,例如:06
  • <H> - 时,例如:03
  • <i> - 分,例如:18
  • <s> - 秒,例如:46

使用

use Overtrue\LaravelQCloudFederationToken\FederationToken;

// 使用默认策略(配置项 strategies 中第一个)
$token = FederationToken::createToken();

// 或者指定策略
$token = FederationToken::strategy('cos')->createToken();

$token->toArray();

// 'credentials' => [
//     'token' => 'kTRtHpOSOCUzTVWmzlPKweHffXjT9Izo7b61a142d6b56d31c0a7ace4d22bcff3zpbsXKTIrCo43dRRh7bDIKE1ZOE1KRYHEm0KNLjWG_aSF63YoQWchg',
//     'tmp_secret_id' => 'AKIDw7dwZbmFSup9CnAOraJ7skiPMybaV3WPP5B4oVMCIL5kLyphV_3IyAHFJ5QMCjE6',
//     'tmp_secret_key' => '/lvEo280/AlGt4orjDl9tWLIOMl5nkexS5Pg+xys7ps=',
// ],
// 'expired_at' => 1547696355,

格式请参考: https://cloud.tencent.com/document/product/1312/48195

事件

Event Description
Overtrue\LaravelQcloudFederationToken\Events\TokenCreated token 生成时触发

❤️ 赞助我

Sponsor me

如果你喜欢我的项目并想支持它,点击这里 ❤️

贡献代码

你可以通过以下方式参与贡献:

  1. 通过 issue tracker 提交 Bug;
  2. 通过 issue tracker 回答问题或修复 Bug;
  3. 通过 Pull Request 增加新特性或优化文档。

代码贡献过程不需要很正式。你只需要确保你遵循 PSR-0、PSR-1 和 PSR-2 的编码准则。任何新的代码贡献都必须附带对应的单元测试。

Project supported by JetBrains

Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.

PHP 扩展包开发

想知道如何从零开始构建 PHP 扩展包?

请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》

License

MIT

You might also like...
PHP package built for Laravel 5.* to easily handle a user email verification and validate the email

jrean/laravel-user-verification is a PHP package built for Laravel 5.* & 6.* & 7.* & 8.* to easily handle a user verification and validate the e-mail.

LDAP Authentication & Management for Laravel

👋 Hey there! Looking for something even easier to use for LDAP integration in your Laravel applications? 🎉 Introducing LdapRecord 🎉 LdapRecord is t

Limit access to your Laravel applications by using invite codes

Doorman Doorman provides a way to limit access to your Laravel applications by using invite codes. Invite Codes: Can be tied to a specific email addre

Declarative style of authorization and validation in laravel.
Declarative style of authorization and validation in laravel.

Laravel Hey Man Readability Counts. In fact, Readability is the primary value of your code !!! 🎀 Heyman continues where the other role-permission pac

Set up Laravel Auth guards using Eloquent in seconds

Nightguard Set up Auth guards using Eloquent in seconds. Introduction Laravel guards provide a super convenient way of authorizing different areas of

Paquete laravel para leer indicadores previsionales desde Previred y tramos de impuesto de 2da categoría desde SII
Paquete laravel para leer indicadores previsionales desde Previred y tramos de impuesto de 2da categoría desde SII

Laravel PreviScraper Indicadores previsionales Chile 🇨🇱 Indicadores previsionales desde Previred y tramos de impuesto desde SII Los datos se obtiene

Laravel starter kit with Livewire & Bootstrap 5 auth scaffolding.

Laravel Livewire Auth Laravel starter kit with Livewire & Bootstrap 5 auth scaffolding. Requirements NPM Installation Create a new Laravel app: larave

Minimalistic token-based authorization for Laravel API endpoints.

Bearer Minimalistic token-based authorization for Laravel API endpoints. Installation You can install the package via Composer: composer require ryang

Library to manage HTTP authentication with PHP. Includes ServiceProviders for easy Laravel integration.

Intervention HttpAuth Library to manage HTTP authentication with PHP. Includes ServiceProviders for easy Laravel integration. Installation You can ins

Releases(2.1.5)
Owner
安正超
Keep calm and coding.
安正超
A Laravel 5 package for OAuth Social Login/Register implementation using Laravel socialite and (optionally) AdminLTE Laravel package

laravel-social A Laravel 5 package for OAuth Social Login/Register implementation using Laravel socialite and (optionally) AdminLTE Laravel package. I

Sergi Tur Badenas 42 Nov 29, 2022
:octocat: Socialite is an OAuth2 Authentication tool. It is inspired by laravel/socialite, you can easily use it without Laravel.

Socialite Socialite is an OAuth2 Authentication tool. It is inspired by laravel/socialite, You can easily use it in any PHP project. 中文文档 This tool no

安正超 1.2k Dec 22, 2022
An OAuth 2.0 bridge for Laravel and Lumen [DEPRECATED FOR LARAVEL 5.3+]

OAuth 2.0 Server for Laravel (deprecated for Laravel 5.3+) Note: This package is no longer maintaned for Laravel 5.3+ since Laravel now features the P

Luca Degasperi 2.4k Jan 6, 2023
A Simple method to create laravel authentication for an existing laravel project.

Laravel Simple Auth A Simple method to create laravel authentication for an existing laravel project. Indroduction Why I created this kind of package?

Dasun Tharanga 10 Dec 14, 2021
Laravel Auth is a Complete Build of Laravel 8 with Email Registration Verification, Social Authentication, User Roles and Permissions, User Profiles, and Admin restricted user management system.

Laravel Auth is a Complete Build of Laravel 8 with Email Registration Verification, Social Authentication, User Roles and Permissions, User Profiles, and Admin restricted user management system. Built on Bootstrap 4.

Jeremy Kenedy 2.8k Dec 31, 2022
🔐 JSON Web Token Authentication for Laravel & Lumen

Documentation Documentation for 1.* here For version 0.5.* See the WIKI for documentation. Supported by Auth0 If you want to easily add secure authent

Sean Tymon 10.7k Dec 31, 2022
Handle roles and permissions in your Laravel application

Laratrust (Laravel Package) Version Compatibility Laravel Laratrust 8.x 6.x 7.x 6.x 6.x 6.x 5.6.x - 5.8.x 5.2 5.3.x - 5.5.x 5.1 5.0.x - 5.2.x 4.0. Ins

Santiago García 2k Dec 30, 2022
Role-based Permissions for Laravel 5

ENTRUST (Laravel 5 Package) Entrust is a succinct and flexible way to add Role-based Permissions to Laravel 5. If you are looking for the Laravel 4 ve

Zizaco 6.1k Jan 5, 2023
Roles & Permissions for Laravel 8 / 7 / 6 / 5

Defender Defender is an Access Control List (ACL) Solution for Laravel 5 / 6 / 7 (single auth). (Not compatible with multi-auth) With security and usa

Artesãos 437 Dec 22, 2022
Laravel wrapper around OAuth 1 & OAuth 2 libraries.

Introduction Laravel Socialite provides an expressive, fluent interface to OAuth authentication with Facebook, Twitter, Google, LinkedIn, GitHub, GitL

The Laravel Framework 5.2k Dec 27, 2022