jmashore/signature-hmac

HMAC-SHA认证

v0.0 2018-01-11 21:44 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:08:10 UTC


README

Philip Brown的 Signature-php 的移植 Signature ruby gem 移植到 PHP 5.4+

Build Status Code Coverage Scrutinizer Code Quality

安装

jmashore/signature-hmac 添加到 composer.json 中的依赖项

$ composer require jmashore/signature-hmac

HMAC-SHA认证是什么?

HMAC-SHA认证允许您使用哈希签名实现非常简单的密钥/秘密认证,以用于您的API。

发送请求

use jmashore\Signature\Token;
use jmashore\Signature\Request;

$data    = ['name' => 'Philip Brown'];
$token   = new Token('abc123', 'qwerty');
$request = new Request('POST', 'users', $data);

$auth = $request->sign($token);

$http->post('users', array_merge($auth, $data));

验证响应

use jmashore\Signature\Auth;
use jmashore\Signature\Token;
use jmashore\Signature\Guards\CheckKey;
use jmashore\Signature\Guards\CheckVersion;
use jmashore\Signature\Guards\CheckTimestamp;
use jmashore\Signature\Guards\CheckSignature;
use jmashore\Signature\Exceptions\SignatureException;

$auth  = new Auth('POST', 'users', $_POST, [
	new CheckKey,
	new CheckVersion,
	new CheckTimestamp,
	new CheckSignature
]);

$token = new Token('abc123', 'qwerty');

try {
    $auth->attempt($token);
}

catch (SignatureException $e) {
    // return 4xx
}

更改默认HTTP请求前缀

默认情况下,此包在请求中使用 auth_*。您可以在签名和验证请求时更改此行为

// default, the HTTP request uses auth_version, auth_key, auth_timestamp and auth_signature
$request->sign($token);
// the HTTP request now uses x-version, x-key, x-timestamp and x-signature
$request->sign($token, 'x-');

如果您更改了默认设置,您需要相应地验证请求

$auth->attempt($token, 'x-');