此包已被弃用且不再维护。未建议替代包。

Hawk身份验证

1.1 2012-12-02 20:40 UTC

This package is not auto-updated.

Last update: 2019-02-20 17:19:01 UTC


README

这是Hawk HTTP身份验证方案的实现。

安装

Composer

在您的composer.json中包含alexbilbie/hawk

{
	"require": {
		"alexbilbie/hawk": "*"
	}
}

然后运行composer update

Git

运行git clone git://github.com/alexbilbie/PHP-Hawk.git /path/to/php-hawk

客户端使用

假设您正在调用以下端点

https://api.example.com/user/123?foo=bar

API服务器已提供以下凭据

  • 密钥 - ghU3QVGgXM
  • 密钥 - 5jNP12yT17Hx5Md3DCZ5pGI5sui82efX

要生成头部,请运行以下操作

$key = 'ghU3QVGgXM';
$secret = '5jNP12yT17Hx5Md3DCZ5pGI5sui82efX';
$hawk = Hawk::generateHeader($key, $secret, 'GET', 'https://api.example.com/user/123?foo=bar');

您还可以通过在数组中使用ext键传递额外的应用程序特定数据。

一旦您有了Hawk字符串,请将其包含在HTTP请求的Authorization头部中。

服务器使用

在您的API端点,如果传入的请求缺少身份验证头部,则返回以下两个头部

HTTP/1.1 401 Unauthorized WWW-Authenticate: Hawk

如果请求包含Hawk身份验证头部,则按如下方式处理

$hawk = ''; // the authorisation header

// First parse the header to get the parts from the string
$hawk_parts = Hawk::parseHeader($hawk);

// Then with your own function, get the secret for the key from the database
$secret = getSecret($hawk_parts['id']);

// Now validate the request
$valid = Hawk::verifyHeader($hawk, array(
		'host'	=>	'api.example.com',
		'port'	=>	443,
		'path'	=>	'/user/123',
		'method'	=>	'GET'
	), $secret); // return true if the request is valid, otherwise false