Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Lti11LaunchDataValidator | |
0.00% |
0 / 20 |
|
0.00% |
0 / 6 |
156 | |
0.00% |
0 / 1 |
| validate | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
42 | |||
| isValidLinkId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isValidLtiVersion | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isCorrectLtiVersion | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isValidLtiMessageType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLaunchDataParameter | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 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 (original work) Open Assessment Technologies SA |
| 19 | */ |
| 20 | |
| 21 | namespace oat\taoLti\models\classes\LaunchData\Validator; |
| 22 | |
| 23 | use oat\oatbox\Configurable; |
| 24 | use oat\taoLti\models\classes\LtiException; |
| 25 | use oat\taoLti\models\classes\LtiInvalidLaunchDataException; |
| 26 | use oat\taoLti\models\classes\LtiVariableMissingException; |
| 27 | use oat\taoLti\models\classes\LtiLaunchData; |
| 28 | |
| 29 | /** |
| 30 | * Class LaunchDataValidator |
| 31 | * @package oat\taoLti\models\classes\LaunchData\validator |
| 32 | */ |
| 33 | class Lti11LaunchDataValidator extends Configurable implements LtiValidatorInterface |
| 34 | { |
| 35 | public const LTI_VERSION_PATTERN = '/^(LTI-)[0-9]+(p)[0-9]+$/'; |
| 36 | public const LTI_VERSION_1_PATTERN = '/^(LTI-1p)[0-9]+$/'; |
| 37 | public const LTI_MESSAGE_TYPE = 'basic-lti-launch-request'; |
| 38 | |
| 39 | /** |
| 40 | * Check if provides launch data object is valid. |
| 41 | * |
| 42 | * @param LtiLaunchData $data |
| 43 | * @return bool |
| 44 | * @throws LtiException |
| 45 | * @throws LtiInvalidLaunchDataException |
| 46 | * @throws LtiVariableMissingException |
| 47 | */ |
| 48 | public function validate(LtiLaunchData $data) |
| 49 | { |
| 50 | try { |
| 51 | if (!$this->isValidLinkId($this->getLaunchDataParameter($data, LtiLaunchData::RESOURCE_LINK_ID))) { |
| 52 | throw new LtiInvalidLaunchDataException("Required parameter resource_link_id can not be empty."); |
| 53 | } |
| 54 | |
| 55 | $ltiVersion = $this->getLaunchDataParameter($data, LtiLaunchData::LTI_VERSION); |
| 56 | if (!$this->isValidLtiVersion($ltiVersion)) { |
| 57 | throw new LtiInvalidLaunchDataException("Invalid LTI version provided."); |
| 58 | } |
| 59 | |
| 60 | if (!$this->isCorrectLtiVersion($ltiVersion)) { |
| 61 | throw new LtiInvalidLaunchDataException("Wrong LTI version provided."); |
| 62 | } |
| 63 | |
| 64 | if (!$this->isValidLtiMessageType($this->getLaunchDataParameter($data, LtiLaunchData::LTI_MESSAGE_TYPE))) { |
| 65 | throw new LtiInvalidLaunchDataException('Invalid LTI message type provided.'); |
| 66 | } |
| 67 | } catch (LtiException $e) { |
| 68 | $e->setLaunchData($data); |
| 69 | throw $e; |
| 70 | } |
| 71 | |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Verify if link id has valid value. |
| 77 | * |
| 78 | * @param $linkId |
| 79 | * @return bool |
| 80 | */ |
| 81 | private function isValidLinkId($linkId) |
| 82 | { |
| 83 | return !empty($linkId); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Verify if LTI version value has correct format. |
| 88 | * |
| 89 | * @param $ltiVersion |
| 90 | * @return bool |
| 91 | */ |
| 92 | private function isValidLtiVersion($ltiVersion) |
| 93 | { |
| 94 | return (bool) preg_match(self::LTI_VERSION_PATTERN, $ltiVersion); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Verify if LTI version has correct value. |
| 99 | * |
| 100 | * @param $ltiVersion |
| 101 | * @return bool |
| 102 | */ |
| 103 | private function isCorrectLtiVersion($ltiVersion) |
| 104 | { |
| 105 | return (bool) preg_match(self::LTI_VERSION_1_PATTERN, $ltiVersion); |
| 106 | } |
| 107 | |
| 108 | private function isValidLtiMessageType($ltiMessageType) |
| 109 | { |
| 110 | return $ltiMessageType == self::LTI_MESSAGE_TYPE; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get launch data parameter by name. |
| 115 | * |
| 116 | * @param LtiLaunchData $data |
| 117 | * @param $name |
| 118 | * @return mixed |
| 119 | * @throws LtiVariableMissingException |
| 120 | */ |
| 121 | private function getLaunchDataParameter(LtiLaunchData $data, $name) |
| 122 | { |
| 123 | if ($data->hasVariable($name)) { |
| 124 | return $data->getVariable($name); |
| 125 | } |
| 126 | |
| 127 | throw new LtiVariableMissingException($name); |
| 128 | } |
| 129 | } |