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 connect() : bool |
43
|
|
|
{ |
44
|
|
|
// TODO |
45
|
|
|
$response = $this->client('/auth', 'GET', [ |
46
|
|
|
'auth' => [ |
47
|
|
|
$this->config()->get('connection', 'username'), |
48
|
|
|
$this->config()->get('connection', 'password'), |
49
|
|
|
'digest' |
50
|
|
|
] |
51
|
|
|
]); |
52
|
|
|
|
53
|
|
|
return $response->getStatusCode() === 200; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function writeHash(string $hash) : string |
61
|
|
|
{ |
62
|
|
|
|
63
|
|
|
} |
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function getProof(string $hash) : string |
70
|
|
|
{ |
71
|
|
|
if (!$this->connect()) { |
72
|
|
|
return []; |
|
|
|
|
73
|
|
|
} |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function verifyProof(string $proof) : bool |
81
|
|
|
{ |
82
|
|
|
|
83
|
|
|
} |
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Return a client to use for all RPC traffic to this backend. |
87
|
|
|
* |
88
|
|
|
* @param string $url |
89
|
|
|
* @param string $verb |
90
|
|
|
* @param array $payload |
91
|
|
|
* @return GuzzleHTTPResponse |
|
|
|
|
92
|
|
|
* @throws VerifiableBackendException |
93
|
|
|
*/ |
94
|
|
|
private function client(string $url, string $verb, array $payload = []) |
95
|
|
|
{ |
96
|
|
|
$verb = strtoupper($verb); |
97
|
|
|
// See Client()->setSslVerification() if required |
98
|
|
|
$client = new Client([ |
99
|
|
|
'base_uri' => $this->config()->get('trillian', 'params')['base_uri'], |
100
|
|
|
'timeout' => $this->config()->get('trillian', 'params')['timeout'], |
101
|
|
|
]); |
102
|
|
|
$request = new Request($verb, $url, $payload); |
103
|
|
|
|
104
|
|
|
try { |
105
|
|
|
$client->send($request); |
106
|
|
|
|
107
|
|
|
if (!preg_match("#^2#", $code = $request->getStatusCode())) { |
|
|
|
|
108
|
|
|
throw new VerifiableBackendException(sprintf('Request gave HTTP status: %d', $code)); |
109
|
|
|
} |
110
|
|
|
} catch (RequestException $e) { |
111
|
|
|
throw new VerifiableBackendException($e->getMessage()); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $data The data to be verified |
117
|
|
|
* @throws VerifiableValidationException In the event invalid data is detected |
118
|
|
|
* Sure-fire way to prevent a malformed |
119
|
|
|
* write to the backend. |
120
|
|
|
* @return void |
121
|
|
|
* @todo Implement a dedicated hash-specific handler |
122
|
|
|
*/ |
123
|
|
|
public function validate(string $data) |
124
|
|
|
{ |
125
|
|
|
$func = Verifiable::config()->get('hash_func'); |
126
|
|
|
|
127
|
|
|
if ($func == 'sha1') { |
128
|
|
|
if (strlen($data) !== 40) { |
129
|
|
|
throw new VerifiableValidationException(sprintf('Invalid %s hash: Length', $func)); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
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