tuupola/branca
Composer 安装命令:
composer require tuupola/branca
包简介
Authenticated and encrypted API tokens using modern crypto.
关键字:
README 文档
README
Authenticated and encrypted API tokens using modern crypto.
What?
Branca is a secure easy to use token format which makes it hard to shoot yourself in the foot. It uses IETF XChaCha20-Poly1305 AEAD symmetric encryption to create encrypted and tamperproof tokens. Payload itself is an arbitrary sequence of bytes. You can use for example a JSON object, plain text string or even binary data serialized by MessagePack or Protocol Buffers.
It is possible to use Branca as an alternative to JWT. There is also an authentication middleware for frameworks which support PSR-7 doublepass or PSR-15 standards.
Install
Install the library using Composer.
$ composer require tuupola/branca
This branch requires PHP 7.2 or up. The older 1.x branch supports also PHP 5.6, 7.0 and 7.1.
$ composer require "tuupola/branca:^1.0"
Usage
Token payload can be any arbitrary data such as string containing an email address. You also must provide a 32 byte secret key. The key is used for encrypting the payload.
use Branca\Branca; $key = random_bytes(32); $branca = new Branca($key); $payload = "tuupola@appelsiini.net"; $token = $branca->encode($payload); /* hGgg0dPSseaUPZqGloWlDGb2i8hb6iamFBIQaatgYDRhEuaXyByaX0nzmyQk1WYAuSBEMWpB20Z1dENLFItwf1 */ $decoded = $branca->decode($token); /* tuupola@appelsiini.net */
Sometimes you might prefer JSON.
use Branca\Branca; $key = random_bytes(32); $branca = new Branca($key); $payload = json_encode(["scope" => ["read", "write", "delete"]]); $token = $branca->encode($payload); /* 5R7p5pC1gU5kfVuBUzhl43Ndh4HLT9fxAHrhN1zNRivTuehY8zYYzrVZ8C6d6VcNLfCk3EUgBwwW6kIk0wm32O34OFIYz5LnOIezwcV2Xsfc */ $decoded = $branca->decode($token); $array = json_decode($decoded, true); /* Array ( [scope] => Array ( [0] => read [1] => write [2] => delete ) ) */
You can keep the token size small by using a space efficient serialization method such as MessagePack or Protocol Buffers.
use Branca\Branca; use MessagePack\MessagePack; use MessagePack\Packer; use MessagePack\BufferUnpacker; $key = random_bytes(32); $branca = new Branca($key); $payload = (new Packer)->pack(["scope" => ["read", "write", "delete"]]); $token = $branca->encode($payload); /* 3iJt0CjqTRh3FGuAf0DHEmhULFIbPVInjguWIkmyCm7RMps5BMJZKa1KwZMN0z58IpPeCxdjoTdkurn9pl0YNrxAQfg3deP0 */ $decoded = $branca->decode($token); $unpacked = (new BufferUnpacker($decoded))->unpack(); print_r($unpacked); /* Array ( [scope] => Array ( [0] => read [1] => write [2] => delete ) ) */
Timestamp
Branca token includes a timestamp when it was created. When decoding you can optionally pass a ttl parameter. Value is passed in seconds. Below example throws en exception if token is older than 60 minutes.
use Branca\Branca; $key = hex2bin("73757065727365637265746b6579796f7573686f756c646e6f74636f6d6d6974"); $branca = new Branca($key); $token = "1jJDJOEeG2FutA8g7NAOHK4Mh5RIE8jtbXd63uYbrFDSR06dtQl9o2gZYhBa36nZHXVfiGFz"; print $branca->timestamp($token); /* 123206400 */ try { $decoded = $branca->decode($token, 3600); } catch (RuntimeException $exception) { print $exception->getMessage(); /* Token is expired */ }
Testing
You can run tests either manually or automatically on every code change. Automatic tests require entr to work.
$ make test
$ brew install entr $ make watch
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email tuupola@appelsiini.net instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.
tuupola/branca 适用场景与选型建议
tuupola/branca 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 332.3k 次下载、GitHub Stars 达 52, 最近一次更新时间为 2017 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Authentication」 「token」 「jwt」 「fernet」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tuupola/branca 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tuupola/branca 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tuupola/branca 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Automatically logs-in users if they are already authenticated by a remote source. (e.g. environment variable REMOTE_USER)
GraphQL authentication for your headless Craft CMS applications.
A simple library to decode and parse Apple Sign In client tokens.
A package for validating Google Cloud's JWT provided by webhooks
JSON Web Token Authentication for Laravel and Lumen
Laravel middleware to restrict a site or specific routes using HTTP basic authentication
统计信息
- 总下载量: 332.3k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 52
- 点击次数: 21
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-07-19