{"id":16619050,"url":"https://github.com/jasny/http-digest","last_synced_at":"2025-03-11T05:45:02.328Z","repository":{"id":56996771,"uuid":"173026468","full_name":"jasny/http-digest","owner":"jasny","description":"PSR-7 middleware for HTTP Digest header (RFC 3230)","archived":false,"fork":false,"pushed_at":"2020-05-15T23:05:23.000Z","size":28,"stargazers_count":0,"open_issues_count":2,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-06T02:54:42.100Z","etag":null,"topics":["digest","middleware","php7","psr-15","psr-7","rfc-3230"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jasny.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-02-28T02:30:36.000Z","updated_at":"2020-05-15T23:05:25.000Z","dependencies_parsed_at":"2022-08-21T14:10:59.479Z","dependency_job_id":null,"html_url":"https://github.com/jasny/http-digest","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasny%2Fhttp-digest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasny%2Fhttp-digest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasny%2Fhttp-digest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasny%2Fhttp-digest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jasny","download_url":"https://codeload.github.com/jasny/http-digest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242980782,"owners_count":20216285,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["digest","middleware","php7","psr-15","psr-7","rfc-3230"],"created_at":"2024-10-12T02:22:36.933Z","updated_at":"2025-03-11T05:45:02.301Z","avatar_url":"https://github.com/jasny.png","language":"PHP","readme":"Jasny HTTP Digest\n===\n\n[![Build Status](https://travis-ci.org/jasny/http-digest.svg?branch=master)](https://travis-ci.org/jasny/http-digest)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/jasny/http-digest/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/jasny/http-digest/?branch=master)\n[![Code Coverage](https://scrutinizer-ci.com/g/jasny/http-digest/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/jasny/http-digest/?branch=master)\n[![Packagist Stable Version](https://img.shields.io/packagist/v/jasny/http-digest.svg)](https://packagist.org/packages/jasny/http-digest)\n[![Packagist License](https://img.shields.io/packagist/l/jasny/http-digest.svg)](https://packagist.org/packages/jasny/http-digest)\n\nPSR-7 client and server middleware for HTTP Digest header creation and validation as described in\n[RFC 3230](https://tools.ietf.org/html/rfc3230). Supports MD5, SHA, SHA-256 and SHA-512\n([RFC 5843](https://tools.ietf.org/html/rfc5843)).\n\nThe `Digest` header contains a hash of the body.\n\n    Digest: SHA=thvDyvhfIqlvFe+A9MYgxAfm1q5=\n\nThe Want-Digest message header field indicates the sender's desire to receive an instance digest on messages associated\nwith the Request-URI.\n\n    Want-Digest: MD5;q=0.3, SHA;q=1\n\nInstallation\n---\n\n    composer require jasny/http-digest\n\nUsage\n---\n\nCreate the `HttpDigest` service to create and verify digests. Give the server priorities for supported algorithms. This\nvalue should be similar to those in the `Want-Digest` header.\n\n```php\nuse Jasny\\HttpDigest\\HttpDigest;\n\n$service = new HttpDigest([\"MD5;q=0.3\", \"SHA;q=1\"]);\n```\n\nThe priorities may also be specified as string.\n\n```php\n$service = new HttpDigest(\"MD5;q=0.3, SHA;q=1\");\n```\n\nThe service for content negotiating may be created and passes in the constructor for proper DI.\n\n```php\nuse Jasny\\HttpDigest\\HttpDigest;\n\n$negotiator = new DigestNegotiator();\n$service = new HttpDigest([\"MD5;q=0.3\", \"SHA;q=1\"], $negotiator);\n```\n\n### Creating a digest\n\nYou can use the service to create a digest for content.\n\n```php\n$digest = $service-\u003ecreate($body);\n```\n\n### Verifying a digest\n\nYou can use the service to verify the digest.\n\n```php\n$service-\u003everify($body, $digest);\n```\n\nIf the digest doesn't match or if the algorithm is unsupported, a `HttpDigestException` is thrown.\n\n### Priorities and the `Want-Digest` header\n\nYou can change the priorities using `withPriorities()`. This will create a new copy of the service.\n\n```php\n$newService = $service-\u003ewithPriorities([\"MD5;q=0.3\", \"SHA;q=0.5\", \"SHA-256;q=1\"]);\n```\n\nTo get the configured priorities use `getPriorities()`. The `getWantDigest()` function returns the priorities in as a\nstring in the format expected for `Wanted-Digest`.\n\n```php\n$priorities = $service-\u003egetPriorities();\n$header = $service-\u003egetWantDigest();\n```\n\n### Server middleware\n\nServer middleware can be used to verify the digest of PSR-7 requests.\n\nWhen the middleware is used, requests with a body (like `POST` or `GET` requests) must contain a `Digest` header.\nIf the `Digest` header is missing, invalid or doesn't meet the requirements, the middleware will return a\n`400 Bad Request` response with a `With-Digest` header and the handler will not be called.\n\n#### Single pass middleware (PSR-15)\n\nThe middleware implements the PSR-15 `MiddlewareInterface`. As PSR standard many new libraries support this type of\nmiddleware, for example [Zend Stratigility](https://docs.zendframework.com/zend-stratigility/). \n\nYou're required to supply a [PSR-17 response factory](https://www.php-fig.org/psr/psr-17/#22-responsefactoryinterface),\nto create a `400 Bad Request` response for requests with invalid signatures.\n\n```php\nuse Jasny\\HttpDigest\\HttpDigest;\nuse Jasny\\HttpDigest\\ServerMiddleware;\nuse Zend\\Stratigility\\MiddlewarePipe;\nuse Zend\\Diactoros\\ResponseFactory;\n\n$service = new HttpDigest([\"MD5;q=0.3\", \"SHA;q=1\"]);\n$responseFactory = new ResponseFactory();\n$middleware = new ServerMiddleware($service, $responseFactory);\n\n$app = new MiddlewarePipe();\n$app-\u003epipe($middleware);\n```\n\n#### Double pass middleware\n\nMany PHP libraries support double pass middleware. These are callables with the following signature;\n\n```php\nfn(ServerRequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface\n```\n\nTo get a callback to be used by libraries as [Jasny Router](https://github.com/jasny/router) and\n[Relay](http://relayphp.com/), use the `asDoublePass()` method.\n\n```php\nuse Jasny\\HttpDigest\\HttpDigest;\nuse Jasny\\HttpDigest\\ServerMiddleware;\nuse Relay\\RelayBuilder;\n\n$service = new HttpDigest([\"MD5;q=0.3\", \"SHA;q=1\"]);\n$middleware = new ServerMiddleware($service);\n\n$relayBuilder = new RelayBuilder($resolver);\n$relay = $relayBuilder-\u003enewInstance([\n    $middleware-\u003easDoublePass(),\n]);\n\n$response = $relay($request, $baseResponse);\n```\n\n### Client middleware\n\nClient middleware can be used to sign requests send by PSR-7 compatible HTTP clients like\n[Guzzle](http://docs.guzzlephp.org) and [HTTPlug](http://docs.php-http.org).\n\n```php\nuse Jasny\\HttpDigest\\HttpDigest;\nuse Jasny\\HttpDigest\\ClientMiddleware;\n\n$service = new HttpDigest([\"MD5;q=0.3\", \"SHA;q=1\"]);\n$middleware = new ClientMiddleware($service);\n```\n\n#### Double pass middleware\n\nThe client middleware can be used by any client that does support double pass middleware. Such middleware are callables\nwith the following signature;\n\n```php\nfn(RequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface\n```\n\nMost HTTP clients do not support double pass middleware, but a type of single pass instead. However more general\npurpose PSR-7 middleware libraries, like [Relay](http://relayphp.com/), do support double pass.\n\n```php\nuse Relay\\RelayBuilder;\n\n$relayBuilder = new RelayBuilder($resolver);\n$relay = $relayBuilder-\u003enewInstance([\n    $middleware-\u003easDoublePass(),\n]);\n\n$response = $relay($request, $baseResponse);\n```\n\n_The client middleware does not conform to PSR-15 (single pass) as that is intended for server requests only._\n\n#### Guzzle\n\n[Guzzle](http://docs.guzzlephp.org) is the most popular HTTP Client for PHP. The middleware has a `forGuzzle()` method\nthat creates a callback which can be used as Guzzle middleware.\n\n```php\nuse GuzzleHttp\\HandlerStack;\nuse GuzzleHttp\\Client;\nuse Jasny\\HttpDigest\\HttpDigest;\nuse Jasny\\HttpDigest\\ClientMiddleware;\n\n$service = new HttpDigest([\"MD5;q=0.3\", \"SHA;q=1\"]);\n$middleware = new ClientMiddleware($service);\n\n$stack = new HandlerStack();\n$stack-\u003epush($middleware-\u003eforGuzzle());\n\n$client = new Client(['handler' =\u003e $stack]);\n```\n\n#### HTTPlug\n\n[HTTPlug](http://docs.php-http.org/en/latest/httplug/introduction.html) is the HTTP client of PHP-HTTP. It allows you\nto write reusable libraries and applications that need an HTTP client without binding to a specific implementation.\n\nThe `forHttplug()` method for the middleware creates an object that can be used as HTTPlug plugin.\n\n```php\nuse Http\\Discovery\\HttpClientDiscovery;\nuse Http\\Client\\Common\\PluginClient;\nuse Jasny\\HttpDigest\\HttpDigest;\nuse Jasny\\HttpDigest\\ClientMiddleware;\n\n$service = new HttpDigest([\"MD5;q=0.3\", \"SHA;q=1\"]);\n$middleware = new ClientMiddleware($service);\n\n$pluginClient = new PluginClient(\n    HttpClientDiscovery::find(),\n    [\n        $middleware-\u003eforHttplug(),\n    ]\n);\n```\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjasny%2Fhttp-digest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjasny%2Fhttp-digest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjasny%2Fhttp-digest/lists"}