承接 hotrush/laravel-signer 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

hotrush/laravel-signer

最新稳定版本:1.1.0

Composer 安装命令:

composer require hotrush/laravel-signer

包简介

Package to create and validate signatures for laravel models.

README 文档

README

Latest Version on Packagist Total Downloads Tests and coverage Maintainability Test Coverage

Laravel Signer

Package to create and validate signatures for laravel models.

Installation

composer require hotrush/laravel-signer

Usage

  1. Add Signable contract to your model

    use Hotrush\Signer\Contracts\Signable;
    
    class Post extends Model implements Signable
    {
    
    }
  2. Implement contract methods. To simplify this process CanBeSigned trait can be used.

    use Hotrush\Signer\Contracts\Signable;
    use Hotrush\Signer\Contracts\Traits\CanBeSigned;
        
    class Post extends Model implements Signable
    {
        use CanBeSigned;
        
        /**
         * Return null if never expires.
         *
         * @return Carbon|null
         */
        public function getSignExpiration(): ?Carbon
        {
            return null;
        }
    
        /**
         * Payload used for making signature hash.
         *
         * @return array
         */
        public function getSignPayload(): array
        {
            return [
                $this->getKeyName() => $this->getKey(),
                'field' => $this->field,
            ];
        }
    
        /**
         * Payload put into encoded code. Will be publicly accessible.
         *
         * @return array
         */
        public function getPublicSignPayload(): array
        {
            return [
                $this->getKeyName() => $this->getKey(),
            ];
        }
        
        /**
         * Define where clause for getting signable model instance by signature.
         * Only values from public payload can be used.
         */
        public static function signableClauses(Signature $signature): \Closure
        {
            return function (Builder $query) use ($signature) {
                $query->where('id', '=', $signature->payload['id']);
            };
        }
    }
  3. Use facade to generate signature

    use Hotrush\Signer\Facades\Signer;
    
    $signable = Post::find(1);
    $signature = Signer::generate($signable);
    
    echo (string) $signature;

    Signature can be converted into a string and send a confirmation code for example.

  4. To verify code facade can be used as well. But first need to decode signature.

    use Hotrush\Signer\Facades\Signer;
    use Hotrush\Signer\Signature;
    
    // decode signature
    $signature = Signature::decode('signature-string-value');
    
    // get signable
    $signable = Post::findSignable($signature);
    
    // verify
    $valid = Signer::validate($signable, $signature);

Testing

composer test

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-11-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固