项目作者: jasny

项目描述 :
PSR-7 middleware for HTTP Digest header (RFC 3230)
高级语言: PHP
项目地址: git://github.com/jasny/http-digest.git
创建时间: 2019-02-28T02:30:36Z
项目社区:https://github.com/jasny/http-digest

开源协议:MIT License

下载


Jasny HTTP Digest

Build Status
Scrutinizer Code Quality
Code Coverage
Packagist Stable Version
Packagist License

PSR-7 client and server middleware for HTTP Digest header creation and validation as described in
RFC 3230. Supports MD5, SHA, SHA-256 and SHA-512
(RFC 5843).

The Digest header contains a hash of the body.

  1. Digest: SHA=thvDyvhfIqlvFe+A9MYgxAfm1q5=

The Want-Digest message header field indicates the sender’s desire to receive an instance digest on messages associated
with the Request-URI.

  1. Want-Digest: MD5;q=0.3, SHA;q=1

Installation

  1. composer require jasny/http-digest

Usage

Create the HttpDigest service to create and verify digests. Give the server priorities for supported algorithms. This
value should be similar to those in the Want-Digest header.

  1. use Jasny\HttpDigest\HttpDigest;
  2. $service = new HttpDigest(["MD5;q=0.3", "SHA;q=1"]);

The priorities may also be specified as string.

  1. $service = new HttpDigest("MD5;q=0.3, SHA;q=1");

The service for content negotiating may be created and passes in the constructor for proper DI.

  1. use Jasny\HttpDigest\HttpDigest;
  2. $negotiator = new DigestNegotiator();
  3. $service = new HttpDigest(["MD5;q=0.3", "SHA;q=1"], $negotiator);

Creating a digest

You can use the service to create a digest for content.

  1. $digest = $service->create($body);

Verifying a digest

You can use the service to verify the digest.

  1. $service->verify($body, $digest);

If the digest doesn’t match or if the algorithm is unsupported, a HttpDigestException is thrown.

Priorities and the Want-Digest header

You can change the priorities using withPriorities(). This will create a new copy of the service.

  1. $newService = $service->withPriorities(["MD5;q=0.3", "SHA;q=0.5", "SHA-256;q=1"]);

To get the configured priorities use getPriorities(). The getWantDigest() function returns the priorities in as a
string in the format expected for Wanted-Digest.

  1. $priorities = $service->getPriorities();
  2. $header = $service->getWantDigest();

Server middleware

Server middleware can be used to verify the digest of PSR-7 requests.

When the middleware is used, requests with a body (like POST or GET requests) must contain a Digest header.
If the Digest header is missing, invalid or doesn’t meet the requirements, the middleware will return a
400 Bad Request response with a With-Digest header and the handler will not be called.

Single pass middleware (PSR-15)

The middleware implements the PSR-15 MiddlewareInterface. As PSR standard many new libraries support this type of
middleware, for example Zend Stratigility.

You’re required to supply a PSR-17 response factory,
to create a 400 Bad Request response for requests with invalid signatures.

  1. use Jasny\HttpDigest\HttpDigest;
  2. use Jasny\HttpDigest\ServerMiddleware;
  3. use Zend\Stratigility\MiddlewarePipe;
  4. use Zend\Diactoros\ResponseFactory;
  5. $service = new HttpDigest(["MD5;q=0.3", "SHA;q=1"]);
  6. $responseFactory = new ResponseFactory();
  7. $middleware = new ServerMiddleware($service, $responseFactory);
  8. $app = new MiddlewarePipe();
  9. $app->pipe($middleware);

Double pass middleware

Many PHP libraries support double pass middleware. These are callables with the following signature;

  1. fn(ServerRequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface

To get a callback to be used by libraries as Jasny Router and
Relay, use the asDoublePass() method.

  1. use Jasny\HttpDigest\HttpDigest;
  2. use Jasny\HttpDigest\ServerMiddleware;
  3. use Relay\RelayBuilder;
  4. $service = new HttpDigest(["MD5;q=0.3", "SHA;q=1"]);
  5. $middleware = new ServerMiddleware($service);
  6. $relayBuilder = new RelayBuilder($resolver);
  7. $relay = $relayBuilder->newInstance([
  8. $middleware->asDoublePass(),
  9. ]);
  10. $response = $relay($request, $baseResponse);

Client middleware

Client middleware can be used to sign requests send by PSR-7 compatible HTTP clients like
Guzzle and HTTPlug.

  1. use Jasny\HttpDigest\HttpDigest;
  2. use Jasny\HttpDigest\ClientMiddleware;
  3. $service = new HttpDigest(["MD5;q=0.3", "SHA;q=1"]);
  4. $middleware = new ClientMiddleware($service);

Double pass middleware

The client middleware can be used by any client that does support double pass middleware. Such middleware are callables
with the following signature;

  1. fn(RequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface

Most HTTP clients do not support double pass middleware, but a type of single pass instead. However more general
purpose PSR-7 middleware libraries, like Relay, do support double pass.

  1. use Relay\RelayBuilder;
  2. $relayBuilder = new RelayBuilder($resolver);
  3. $relay = $relayBuilder->newInstance([
  4. $middleware->asDoublePass(),
  5. ]);
  6. $response = $relay($request, $baseResponse);

The client middleware does not conform to PSR-15 (single pass) as that is intended for server requests only.

Guzzle

Guzzle is the most popular HTTP Client for PHP. The middleware has a forGuzzle() method
that creates a callback which can be used as Guzzle middleware.

  1. use GuzzleHttp\HandlerStack;
  2. use GuzzleHttp\Client;
  3. use Jasny\HttpDigest\HttpDigest;
  4. use Jasny\HttpDigest\ClientMiddleware;
  5. $service = new HttpDigest(["MD5;q=0.3", "SHA;q=1"]);
  6. $middleware = new ClientMiddleware($service);
  7. $stack = new HandlerStack();
  8. $stack->push($middleware->forGuzzle());
  9. $client = new Client(['handler' => $stack]);

HTTPlug

HTTPlug is the HTTP client of PHP-HTTP. It allows you
to write reusable libraries and applications that need an HTTP client without binding to a specific implementation.

The forHttplug() method for the middleware creates an object that can be used as HTTPlug plugin.

  1. use Http\Discovery\HttpClientDiscovery;
  2. use Http\Client\Common\PluginClient;
  3. use Jasny\HttpDigest\HttpDigest;
  4. use Jasny\HttpDigest\ClientMiddleware;
  5. $service = new HttpDigest(["MD5;q=0.3", "SHA;q=1"]);
  6. $middleware = new ClientMiddleware($service);
  7. $pluginClient = new PluginClient(
  8. HttpClientDiscovery::find(),
  9. [
  10. $middleware->forHttplug(),
  11. ]
  12. );