wearesho-team/yii2-tokens 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

wearesho-team/yii2-tokens

Composer 安装命令:

composer require wearesho-team/yii2-tokens

包简介

Storing tokens (authorization, registration) in redis database.

README 文档

README

PHP Composer Latest Stable Version Total Downloads codecov

Storing tokens (authorization, registration) in redis database.

Installation

composer require wearesho-team/yii2-tokens:^1.1.1

Usage

Repository

To store tokens you should use Repository that will receive token entity and generate UUIDv4 hash. Then, you can receive token using generated hash.

<?php

use Wearesho\Yii2\Token;

$repository = new Token\Repository([
    'redis' => 'redis', // your yii2-redis connection
]);

$token = new Token\Entity(
    $type = 'registration',
    $tokenOwner = 'foo@bar.com',
    $value = 'random-token',
    (new \DateTime())->add('PT30M') // expire after 30 minutes
);

$hash = $repository->put($token);

// If you want to receive token

$token = $repository->get($hash); // entity or null

ValidationBehavior

To validate model attributes, that contains hash, owner and value you should use validation behavior, instead of validators because three attributes have to be validated one-time. {attribute} is invalid. message will be add to attribute errors and validation will fail (beforeValidate event).

<?php

use yii\base;
use Wearesho\Yii2\Token;

class Model extends base\Model {
    /** @var string */
    public $hash;
    
    /** @var string */
    public $tokenOwner; // or `phone`, `email` etc.
    
    /** @var string */
    public $token;
    
    public function behaviors(): array
    {
        return [
            'validateToken' => [
                'class' => Token\ValidationBehavior::class,
                'type' => 'registration',
            ],    
        ];
    }
    
    public function rules(): array
    {
        return [
            [['hash', 'tokenOwner', 'token',], 'safe',], // to load using $model->load    
        ];
    }
}

$repository = new Token\Repository;

$model = new Model;
$model->hash = 'some-invalid-hash';
$model->owner = 'foo@bar.com';
$model->token = 'some-random-token';

$model->validate(); // false

$hash = $repository->put(new Token\Entity(
    'registration',
    $model->tokenWwner,
    $model->token,
    (new \DateTime)->add(new \DateInterval('PT1M'))
));

$model->hash = $hash;

$model->validate(); // true

TODO

  • Fix test to rely on the order in which methods are invoked (Upgrade PHPUnit to 10)

Contributors

License

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-01-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固