|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author Russell Michell 2018 <[email protected]> |
|
5
|
|
|
* @package silverstripe-verifiable |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace PhpTek\Verifiable\Backend; |
|
9
|
|
|
|
|
10
|
|
|
use PhpTek\Verifiable\Backend\BackendProvider; |
|
11
|
|
|
use PhpTek\Verifiable\Verifiable; |
|
|
|
|
|
|
12
|
|
|
use PhpTek\Verifiable\Exception\VerifiableValidationException; |
|
13
|
|
|
use GuzzleHttp\Client; |
|
14
|
|
|
use Guzzle\Http\Exception\RequestException; |
|
|
|
|
|
|
15
|
|
|
use Guzzle\Http\Message\Request; |
|
|
|
|
|
|
16
|
|
|
use PhpTek\Verifiable\Exception\VerifiableBackendException; |
|
17
|
|
|
use SilverStripe\Core\Config\Configurable; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Trillian relies on something called a "Personality" to supply it with the exact |
|
21
|
|
|
* type and format of data, that the overall application is expecting it to store. |
|
22
|
|
|
* As such Trillian itself will perform no data validation or normalisation, favouring |
|
23
|
|
|
* instead to farm out this responsibility to personalities. |
|
24
|
|
|
*/ |
|
25
|
|
|
class Trillian implements BackendProvider |
|
26
|
|
|
{ |
|
27
|
|
|
use Configurable; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
public function name() : string |
|
34
|
|
|
{ |
|
35
|
|
|
return 'trillian'; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function hashFunc() : string |
|
43
|
|
|
{ |
|
44
|
|
|
return 'sha256'; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function connect() : bool |
|
52
|
|
|
{ |
|
53
|
|
|
// TODO |
|
54
|
|
|
$response = $this->client('/auth', 'GET', [ |
|
55
|
|
|
'auth' => [ |
|
56
|
|
|
$this->config()->get('connection', 'username'), |
|
57
|
|
|
$this->config()->get('connection', 'password'), |
|
58
|
|
|
'digest' |
|
59
|
|
|
] |
|
60
|
|
|
]); |
|
61
|
|
|
|
|
62
|
|
|
return $response->getStatusCode() === 200; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function writeHash(array $hash) : string |
|
70
|
|
|
{ |
|
71
|
|
|
|
|
72
|
|
|
} |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getProof(string $hash) : string |
|
79
|
|
|
{ |
|
80
|
|
|
if (!$this->connect()) { |
|
81
|
|
|
return []; |
|
|
|
|
|
|
82
|
|
|
} |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* |
|
87
|
|
|
* {@inheritdoc} |
|
88
|
|
|
*/ |
|
89
|
|
|
public function verifyProof(string $proof) : bool |
|
90
|
|
|
{ |
|
91
|
|
|
|
|
92
|
|
|
} |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Return a client to use for all RPC traffic to this backend. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $url |
|
98
|
|
|
* @param string $verb |
|
99
|
|
|
* @param array $payload |
|
100
|
|
|
* @return GuzzleHTTPResponse |
|
|
|
|
|
|
101
|
|
|
* @throws VerifiableBackendException |
|
102
|
|
|
*/ |
|
103
|
|
|
private function client(string $url, string $verb, array $payload = []) |
|
104
|
|
|
{ |
|
105
|
|
|
$verb = strtoupper($verb); |
|
106
|
|
|
// See Client()->setSslVerification() if required |
|
107
|
|
|
$client = new Client([ |
|
108
|
|
|
'base_uri' => $this->config()->get('trillian', 'params')['base_uri'], |
|
109
|
|
|
'timeout' => $this->config()->get('trillian', 'params')['timeout'], |
|
110
|
|
|
]); |
|
111
|
|
|
$request = new Request($verb, $url, $payload); |
|
112
|
|
|
|
|
113
|
|
|
try { |
|
114
|
|
|
$client->send($request); |
|
115
|
|
|
|
|
116
|
|
|
if (!preg_match("#^2#", $code = $request->getStatusCode())) { |
|
117
|
|
|
throw new VerifiableBackendException(sprintf('Request gave HTTP status: %d', $code)); |
|
118
|
|
|
} |
|
119
|
|
|
} catch (RequestException $e) { |
|
120
|
|
|
throw new VerifiableBackendException($e->getMessage()); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param string $data The data to be verified |
|
126
|
|
|
* @throws VerifiableValidationException In the event invalid data is detected |
|
127
|
|
|
* Sure-fire way to prevent a malformed |
|
128
|
|
|
* write to the backend. |
|
129
|
|
|
* @return void |
|
130
|
|
|
* @todo Implement a dedicated hash-specific handler |
|
131
|
|
|
*/ |
|
132
|
|
|
public function validate(string $data) |
|
133
|
|
|
{ |
|
134
|
|
|
$func = Verifiable::config()->get('hash_func'); |
|
135
|
|
|
|
|
136
|
|
|
if ($func == 'sha1') { |
|
137
|
|
|
if (strlen($data) !== 40) { |
|
138
|
|
|
throw new VerifiableValidationException(sprintf('Invalid %s hash: Length', $func)); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths