innobyte/token-bundle
Composer 安装命令:
composer require innobyte/token-bundle
包简介
A Symfony2 Bundle implementing an access-based token system, with generation, validation and consumption mechanisms.
关键字:
README 文档
README
The goal of TokenBundle is to provide a means to validate actions taken by users.
The validation is achieved by using a hash, which could, for example, be embedded in a URL, be it in an email or a link generated on-the-spot inside a page.
Installation
composer.json - install bundle
"require": {
"innobyte/token-bundle": "~1.1",
},
AppKernel.php - register bundle
Add the following line into $bundles array:
new Innobyte\TokenBundle\InnobyteTokenBundle(),
config.yml - Add bundle mapping
Add the mapping for the bundle under an entity manager (here "local"). Note: "default" is the default name for the Entity Manager, if not specified otherwise.
doctrine:
...
orm:
...
entity_managers:
local:
...
mappings:
...
InnobyteTokenBundle: ~
paramaters.yml - Add the entity manager name
Add the entity manager name (here "local") - the one you put the mapping under in the above step. If none is provided, "default" will be used. Note: this must be specified only if the entity manager name is different than "default". Otherwise, this step can be omitted entirely.
innobyte_token:
entity_manager: local
Update schema
First, run app/console doctrine:schema:update --em=default --dump-sql to see the generated SQL.
Then, run app/console doctrine:schema:update --em=default --force to run the query on the Database.
Usage
Generate token
$token = $this->get('innobyte_token')->generate(
'scope',
'owner_type',
123, // owner_id
1, // number of uses - optional
new \DateTime('+1 day'), // expiry time - optional
array('author' => 'admin') // additional data to check against - optional
);
// use hash (embed in a link/email etc.)
$hash = $token->getHash();
Validate token and consume it
try {
$this->get('innobyte_token')->consume(
'19debf971fb937853d77fca8fd3bb775',
'scope',
'owner_type',
123 // owner_id
);
} catch (\Innobyte\TokenBundle\Exception\TokenNotFoundException $e) {
echo 'cannot find Token';
} catch (\Innobyte\TokenBundle\Exception\TokenInactiveException $e) {
echo 'handle explicit disabled token here';
} catch (\Innobyte\TokenBundle\Exception\TokenConsumedException $e) {
echo 'handle over-used token here';
} catch (\Innobyte\TokenBundle\Exception\TokenExpiredException $e) {
echo 'handle expired token here';
} catch (\Innobyte\TokenBundle\Exception\TokenException $e) {
// or, alternately, you can catch all token-related exceptions
echo 'handle all token-related exceptions';
}
echo 'Token is valid. Token is valid. Perform logic here.';
Get the token and manually validate and consume it
$token = $this->get('innobyte_token')->get(
'5c15e262c692dbaac75451dcb28282ab',
'scope',
'owner_type',
123 // owner_id
);
// if wrong link or disabled/overused token
if (!$this->get('innobyte_token')->isValid($token)) {
echo 'Handle invalid token here';
}
// or even more explicit
if (!$token instanceof \Innobyte\TokenBundle\Entity\Token) {
echo 'handle invalid token here';
} else {
// manually mark the usage
try {
$this->get('innobyte_token')->consumeToken($token);
} catch (\LogicException $e) {
echo 'cannot consumed Token because it is not managed';
}
echo 'Token is valid. Perform logic here.';
}
Invalidate
Similar methods to "consume" are available for invalidation: "invalidate" (disable the token)
try {
$this->get('innobyte_token')->invalidate(
'5c15e262c692dbaac75451dcb28282ab',
'scope',
'owner_type',
123 // owner_id
);
} catch (\Innobyte\TokenBundle\Exception\TokenNotFoundException $e) {
echo 'Handle Token not found here';
}
if (!$token instanceof \Innobyte\TokenBundle\Entity\Token) {
echo 'Handle invalid token here';
} else {
try {
$this->get('innobyte_token')->invalidateToken($token);
} catch (\LogicException $e) {
echo 'Cannot consume Token because it is not managed';
}
}
Advanced validation
Additional data can be used in validating additional conditions after performing the standard Token validation.
// first, generate the Token
$token = $this->get('innobyte_token')->generate(
'scope',
'owner_type',
123, // owner_id
1, // number of uses - optional
new \DateTime('+1 hour'), // expiry time - optional
array('ip' => $request->getClientIp()) // additional data to check against - optional
);
// use hash (embed in a link/email etc.)
$hash = $token->getHash();
// then, validate it
$token = $this->get('innobyte_token')->get(
$hash,
'scope',
'owner_type',
123 // owner_id
);
// if wrong link or disabled/overused token
if (!$this->get('innobyte_token')->isValid($token)) {
echo 'Handle invalid token here';
}
// additional validation by IP
$additionalData = $token->getData();
if ($additionalData['ip'] != $request->getClientIp()) {
echo 'Handle invalid token here';
}
Unit Testing
Do not forget to override database credentials in config_test.yml with the test database ones. Example:
parameters:
database_host: 127.0.0.1
database_port: null
database_name: test_db
database_user: root
database_password: 123
To run the tests, execute:
phpunit -c app/ vendor/innobyte/token-bundle/Innobyte/TokenBundle/Tests/
innobyte/token-bundle 适用场景与选型建议
innobyte/token-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.72k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 04 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「generator」 「hash」 「token」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 innobyte/token-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 innobyte/token-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 innobyte/token-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
A package for validating Google Cloud's JWT provided by webhooks
JSON Web Token Authentication for Laravel and Lumen
Article CSS-ID Frontend Output Optimization
♺ Laravel Nova Hashids card. Convert your ids.
JSON Web Token Authentication for Laravel and Lumen
统计信息
- 总下载量: 10.72k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-04-02