Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 9 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| SignatureValidator | |
0.00% |
0 / 9 |
|
0.00% |
0 / 3 |
56 | |
0.00% |
0 / 1 |
| checkSignatures | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| checkSignature | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| getSignatureGenerator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2018 (update and modification) Open Assessment Technologies SA |
| 19 | */ |
| 20 | |
| 21 | namespace oat\tao\model\security; |
| 22 | |
| 23 | use oat\oatbox\service\ServiceManager; |
| 24 | use oat\tao\model\metadata\exception\InconsistencyConfigException; |
| 25 | |
| 26 | class SignatureValidator |
| 27 | { |
| 28 | /** |
| 29 | * @param array $list |
| 30 | * @param string $signatureFieldName |
| 31 | * @param string $idFieldName |
| 32 | * |
| 33 | * @throws SecurityException |
| 34 | * @throws InconsistencyConfigException |
| 35 | */ |
| 36 | public function checkSignatures(array $list, $signatureFieldName = 'signature', $idFieldName = 'id') |
| 37 | { |
| 38 | foreach ($list as $item) { |
| 39 | $this->checkSignature($item[$signatureFieldName], $item[$idFieldName]); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @param string $signature |
| 45 | * @param mixed $dataToSign data to be signed |
| 46 | * |
| 47 | * @throws SecurityException |
| 48 | * @throws InconsistencyConfigException |
| 49 | */ |
| 50 | public function checkSignature($signature, ...$dataToSign) |
| 51 | { |
| 52 | if (empty($signature)) { |
| 53 | throw new SecurityException('Empty signature'); |
| 54 | } |
| 55 | |
| 56 | if (!is_string($signature)) { |
| 57 | throw new SecurityException('Signature should be a string'); |
| 58 | } |
| 59 | |
| 60 | if ($signature !== $this->getSignatureGenerator()->generate(...$dataToSign)) { |
| 61 | throw new SecurityException('Invalid signature'); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @return SignatureGenerator |
| 67 | */ |
| 68 | private function getSignatureGenerator() |
| 69 | { |
| 70 | return ServiceManager::getServiceManager()->get(SignatureGenerator::class); |
| 71 | } |
| 72 | } |