emartech/escher
Composer 安装命令:
composer require emartech/escher
包简介
Library for HTTP request signing (PHP implementation)
README 文档
README
Escher helps you creating secure HTTP requests (for APIs) by signing HTTP(s) requests. It's both a server side and client side implementation. The status is work in progress.
The algorithm is based on Amazon's AWS Signature Version 4, but we have generalized and extended it.
More details will be available at our documentation site.
Signing a request
Escher works by calculating a cryptographic signature of your request, and adding it (and other authentication information) to said request. Usually you will want to add the authentication information to the request by appending extra headers to it. Let's say you want to send a signed POST request to http://example.com/ using the Guzzle\Http library:
<?php use Escher\Escher; $method = 'POST'; $url = 'http://example.com'; $requestBody = '{ "this_is": "a_request_body" }'; $yourHeaders = array('Content-Type' => 'application/json'); $headersWithAuthInfo = Escher::create('example/credential/scope') ->signRequest('YOUR_ACCESS_KEY_ID', 'YOUR SECRET', $method, $url, $requestBody, $yourHeaders); $client = new \GuzzleHttp\Client(); $response = $client->post($url, array( 'body' => $requestBody, 'headers' => $headersWithAuthInfo ));
Presigning an URL
In some cases you may want to send authenticated requests from a context where you cannot modify the request headers, e.g. when embedding an API generated iframe. You can however generate a presigned URL, where the authentication information is added to the query string.
<?php use Escher\Escher; $presignedUrl = Escher::create('example/credential/scope') ->presignUrl('YOUR_ACCESS_KEY_ID', 'YOUR SECRET', 'http://example.com');
Validating a request
You can validate a request signed by the methods described above. For that you will need a database of the access keys and secrets of your clients. Escher accepts any kind of object as a key database that implements the ArrayAccess interface. (It also accepts plain arrays, however it is not recommended to use a php array for a database of API secrets - it's just there to ease testing)
<?php use Escher\Escher; use Escher\Exception; try { $keyDB = new \ArrayObject(array( 'ACCESS_KEY_OF_CLIENT_1' => 'SECRET OF CLIENT 1', 'ACCESS_KEY_OF_CLIENT_42' => 'SECRET OF CLIENT 42', )); Escher::create('example/credential/scope')->authenticate($keyDB); } catch (Exception $ex) { echo 'The validation failed! ' . $ex->getMessage(); }
Exceptions
| Code pattern | Exception type |
|---|---|
| 1xxx | Missing exceptions |
| 2xxx | Invalid format exceptions |
| 3xxx | Argument invalid exceptions |
| 4xxx | Not signed exceptions |
| 5xxx | Expired exception |
| 6xxx | Signature exceptions |
| Code | Message |
|---|---|
| 1001 | The authorization header is missing |
| 1100 | The {PARAM} header is missing |
| 1101 | Query key: {PARAM} is missing |
| 1102 | The host header is missing |
| 2001 | Date header is invalid, the expected format is Wed, 04 Nov 2015 09:20:22 GMT |
| 2002 | Could not parse auth header |
| 2003 | Invalid {PARAM} query key format |
| 2004 | Date header is invalid, the expected format is 20151104T092022Z |
| 3001 | Invalid Escher key |
| 3002 | Only SHA256 and SHA512 hash algorithms are allowed |
| 3003 | The credential scope is invalid |
| 3004 | The credential date does not match with the request date |
| 4001 | The host header is not signed |
| 4002 | The {PARAM} header is not signed |
| 5001 | The request date is not within the accepted time range |
| 6001 | The signatures do not match |
Configuration
TBA
Running tests
- Install packages with Composer:
composer install - Run tests with
make tests
emartech/escher 适用场景与选型建议
emartech/escher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 306.08k 次下载、GitHub Stars 达 18, 最近一次更新时间为 2014 年 09 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「rest」 「api」 「Authentication」 「aws」 「request」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 emartech/escher 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 emartech/escher 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 emartech/escher 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
http客户端
Easily add Excepct-CT header to your project
Build mini web servers in PHP for testing
统计信息
- 总下载量: 306.08k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 20
- 点击次数: 30
- 依赖项目数: 8
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-09-05