1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Jasny\HttpSignature; |
4
|
|
|
|
5
|
|
|
use Http\Client\Common\Plugin as HttpPlugin; |
6
|
|
|
use Http\Promise\Promise as HttpPromise; |
7
|
|
|
use Psr\Http\Message\RequestInterface; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Middleware to sign PSR-7 HTTP requests. |
12
|
|
|
*/ |
13
|
|
|
class ClientMiddleware |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var HttpSignature |
17
|
|
|
*/ |
18
|
|
|
protected $service; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string|null |
22
|
|
|
*/ |
23
|
|
|
protected $keyId; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class constructor. |
27
|
|
|
* |
28
|
|
|
* @param HttpSignature $service |
29
|
|
|
* @param string $keyId Default keyId |
30
|
|
|
*/ |
31
|
17 |
|
public function __construct(HttpSignature $service, ?string $keyId = null) |
32
|
|
|
{ |
33
|
17 |
|
$this->service = $service; |
34
|
17 |
|
$this->keyId = $keyId; |
35
|
17 |
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Return a callback that can be used as double pass middleware. |
40
|
|
|
* |
41
|
|
|
* @return callable |
42
|
|
|
*/ |
43
|
2 |
|
public function asDoublePass(): callable |
44
|
|
|
{ |
45
|
2 |
|
if ($this->keyId === null) { |
46
|
1 |
|
throw new \BadMethodCallException('Unable to use as double pass middleware, no keyId specified'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return function (RequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface { |
50
|
1 |
|
$signedRequest = $this->service->sign($request, $this->keyId); |
|
|
|
|
51
|
1 |
|
return $next($signedRequest, $response); |
52
|
1 |
|
}; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Return a callback that can be used as Guzzle middleware. |
57
|
|
|
* @see http://docs.guzzlephp.org/en/stable/handlers-and-middleware.html |
58
|
|
|
* |
59
|
|
|
* @return callable |
60
|
|
|
*/ |
61
|
13 |
|
public function forGuzzle(): callable |
62
|
|
|
{ |
63
|
|
|
return function (callable $handler) { |
64
|
|
|
return function (RequestInterface $request, array $options) use ($handler) { |
65
|
13 |
|
if ($request->hasHeader('Authorization')) { |
66
|
1 |
|
return $handler($request, $options); |
67
|
|
|
} |
68
|
|
|
|
69
|
12 |
|
$keyId = array_key_exists('signature_key_id', $options) ? $options['signature_key_id'] : $this->keyId; |
70
|
12 |
|
$nextRequest = $keyId !== null ? $this->service->sign($request, $keyId) : $request; |
71
|
|
|
|
72
|
12 |
|
return $handler($nextRequest, $options); |
73
|
13 |
|
}; |
74
|
13 |
|
}; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Create a version of this middleware that can be used in HTTPlug. |
79
|
|
|
* @see http://docs.php-http.org/en/latest/plugins/introduction.html |
80
|
|
|
* |
81
|
|
|
* @return self&HttpPlugin |
|
|
|
|
82
|
|
|
*/ |
83
|
2 |
|
public function forHttplug(): HttpPlugin |
84
|
|
|
{ |
85
|
2 |
|
if ($this->keyId === null) { |
86
|
1 |
|
throw new \BadMethodCallException('Unable to use as httplug plugin, no keyId specified'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return new class ($this->service, $this->keyId) extends ClientMiddleware implements HttpPlugin { |
90
|
1 |
|
public function handleRequest(RequestInterface $request, callable $next, callable $first): HttpPromise |
91
|
|
|
{ |
92
|
1 |
|
$signedRequest = $this->service->sign($request, $this->keyId); |
|
|
|
|
93
|
1 |
|
return $next($signedRequest); |
94
|
|
|
} |
95
|
|
|
}; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|