|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Caspeco. |
|
5
|
|
|
* |
|
6
|
|
|
(c) HOY Multimedia AB <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Hoy\Caspeco\Http; |
|
13
|
|
|
|
|
14
|
|
|
use DateTime; |
|
15
|
|
|
use DateTimeZone; |
|
16
|
|
|
use Psr\Http\Message\RequestInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* This is the signature middleware class. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Vincent Klaiber <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
final class Signature |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* The client id. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $id; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The client secret. |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $secret; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The client url. |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $url; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Create a new signature instance. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $id |
|
50
|
|
|
* @param string $secret |
|
51
|
|
|
* @param string $url |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct($id, $secret, $url) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->id = $id; |
|
56
|
|
|
$this->secret = $secret; |
|
57
|
|
|
$this->url = $url; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Sign given request. |
|
62
|
|
|
* |
|
63
|
|
|
* @param \Psr\Http\Message\RequestInterface $request |
|
64
|
|
|
* |
|
65
|
|
|
* @return \Psr\Http\Message\RequestInterface |
|
66
|
|
|
*/ |
|
67
|
|
|
public function sign(RequestInterface $request) |
|
68
|
|
|
{ |
|
69
|
|
|
$request = $request->withHeader('Accept', 'application/json; charset=utf-8'); |
|
70
|
|
|
|
|
71
|
|
|
$host = $this->getHostFromUrl($this->url); |
|
72
|
|
|
$request = $request->withHeader('Host', $host); |
|
73
|
|
|
|
|
74
|
|
|
$date = $this->formatDate(new DateTime('now', new DateTimeZone('GMT'))); |
|
75
|
|
|
$request = $request->withHeader('Date', $date); |
|
76
|
|
|
|
|
77
|
|
|
$body = strtolower($request->getMethod()) === 'get' ? '' : $request->getBody()->getContents(); |
|
78
|
|
|
$digest = $this->hashDigest($body); |
|
79
|
|
|
$request = $request->withHeader('Digest', $digest); |
|
80
|
|
|
|
|
81
|
|
|
$target = strtolower(sprintf('%s %s', $request->getMethod(), $request->getRequestTarget())); |
|
82
|
|
|
$signature = "(request-target): {$target}\nhost: {$host}\ndate: {$date}\ndigest: {$digest}"; |
|
83
|
|
|
|
|
84
|
|
|
$request = $request->withHeader('Authorization', $this->authorizeSignature($signature)); |
|
85
|
|
|
|
|
86
|
|
|
return $request; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get the authorization header. |
|
91
|
|
|
* |
|
92
|
|
|
* @param $signature |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function authorizeSignature($signature) |
|
97
|
|
|
{ |
|
98
|
|
|
$hash = base64_encode(hash_hmac('sha256', $signature, base64_decode($this->secret), true)); |
|
99
|
|
|
|
|
100
|
|
|
return sprintf('Signature keyId="%s",algorithm="hmac-sha256",headers="(request-target) host date digest",signature="%s"', $this->id, $hash); |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get the date header. |
|
105
|
|
|
* |
|
106
|
|
|
* @param \DateTime $time |
|
107
|
|
|
* |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function formatDate(DateTime $time) |
|
111
|
|
|
{ |
|
112
|
|
|
return $time->format('D, d M Y H:i:s').' GMT'; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get the hash header. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $body |
|
119
|
|
|
* |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function hashDigest($body) |
|
123
|
|
|
{ |
|
124
|
|
|
return 'SHA-256='.base64_encode(hash('sha256', $body, true)); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Get the host header. |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $url |
|
131
|
|
|
* |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function getHostFromUrl($url) |
|
135
|
|
|
{ |
|
136
|
|
|
preg_match('/([a-z0-9\-]+\.){1,2}[a-z]{2,4}/', $url, $matches); |
|
137
|
|
|
|
|
138
|
|
|
return $matches[0]; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.