定制 masterei/laravel-signer 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

masterei/laravel-signer

Composer 安装命令:

composer require masterei/laravel-signer

包简介

Laravel signed URL wrapper with additional feature such as consumable and user-based signed URLs.

README 文档

README

Latest Version on Packagist Total Downloads

Laravel signed URL wrapper with additional feature such as consumable and user-based signed URLs.

Installation

You can install the package via composer:

composer require masterei/laravel-signer

You need to publish the migration to create the package table:

php artisan vendor:publish --tag="signer-migration"

After that, you need to run migration command.

php artisan migrate

Optionally, you can publish the config file with:

php artisan vendor:publish --tag="signer-config"

Compatibility

  • Laravel version 9.x.x or later

Usage

Basic Usage

use App\Models\User;
use Masterei\Signer\Signer;

// creating signed url that can only be accessed by limited number of times
Signer::consumableRoute('subscribe', 1, ['user' => 1]);

// expires after a specified amount of time
Signer::temporaryConsumableRoute('subscribe', now()->addMinute(), 1, ['user' => 1]);


// creating signed url that can only be access by certain specified user/s
$user = User::first();
Signer::authenticatedRoute('subscribe', $user, ['user' => 1]); 

// note: user parameter can accept; user id as int or array, model, collection

// expires after a specified amount of time
Signer::temporaryAuthenticatedRoute('subscribe', now()->addMinute(), $user, ['user' => 1]);

Additional Arguments

As usual, you may exclude the domain from the signed URL hash by providing the absolute argument to the class method.

return Signer::consumableRoute('subscribe', 1, ['user' => 1], absolute: false);

You may also want to force the domain prefix, even if you excluded the domain from the signed URL hash by providing the prefixDomain argument to the class method.

return Signer::consumableRoute('subscribe', 1, ['user' => 1], prefixDomain: true);

Advance Usage

return Signer::route('subscribe')   // route name
    ->parameters(['user' => 1])     // additional parameters
    ->authenticated([1, 2])         // user id as int or array, model, collection
    ->consumable(2)                 // number of times url can be accessed
    ->relative()                    // exclude domain from signature hashing
    ->prefixDomain()                // force domain prefix on relative url
    ->expiration(now()->addDays(2)) // url expiration period; accepts: Carbon/Carbon instance
    ->make();                       // finally create the url

Native Signed URL

If you don't want to use the additional feature. Native signedRoute and temporarySignedRoute is a goto option.

Note: This does not store in database and will only be validated using the framework native validation method.

Signer::signedRoute('subscribe', ['user' => 1]);

// You may exclude the domain from the signed URL hash
// by providing the `absolute` argument to the signedRoute method:
Signer::signedRoute('subscribe', ['user' => 1], absolute: false);

// If you would like to generate a temporary signed URL
// that expires after a specified amount of time.
Signer::temporarySignedRoute('subscribe', now()->addDay(), ['user' => 1]);

Validating Signed Route Requests

Package Signed Route

// To ensure that the incoming request has a valid signature,
// you have to include the middleware into the route for it to work.
Route::post('subscribe/{user}', function (Request $request) {
    // ...
})->name('subscribe')->middleware('signer');

// Sometimes, you want to forcefully disable the framework native validation,
// you should provide the `strict` argument to the middleware parameter.
Route::post('subscribe/{user}', function (Request $request) {
    // ...
})->name('subscribe')->middleware('signer:strict');

Native Signed Route

If your route uses the native signed url method namely signedRoute and temporarySignedRoute; and excluded the domain from the signed URL hash you should provide the relative argument to the middleware for it to work.

Route::post('subscribe/{user}', function (Request $request) {
    // ...
})->name('subscribe')->middleware('signer:relative');

Classes

Way to reconstruct signed route model back into signed URL:

use Masterei\Signer\Models\Signed;

return Signed::first()->url();

To parse URL and get its underlying data:

use Masterei\Signer\URLParser;

$parsedUrl = URLParser::fromString(request()->getUri());
return $parsedUrl->getSignature();

Finding signed URL model using its signature:

use Masterei\Signer\Models\Signed;
use Masterei\Signer\URLParser;

$parsedUrl = URLParser::fromString(request()->getUri());
return Signed::findValidSignature($parsedUrl->getSignature());
// or specify with path
return Signed::findValidSignature($parsedUrl->getSignature(), $parsedUrl->getPath());

Changelog

Please see CHANGELOG for more information on what has changed recently.

License

The MIT License (MIT). Please see License File for more information.

masterei/laravel-signer 适用场景与选型建议

masterei/laravel-signer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 04 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「masterei」 「laravel-signer」 「consumable signed url」 「user-based signed url」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 masterei/laravel-signer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 masterei/laravel-signer 我们能提供哪些服务?
定制开发 / 二次开发

基于 masterei/laravel-signer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 14
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-04-24