Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 83 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| LtiService | |
0.00% |
0 / 83 |
|
0.00% |
0 / 9 |
462 | |
0.00% |
0 / 1 |
| createLtiSession | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
| createLti1p3Session | |
0.00% |
0 / 46 |
|
0.00% |
0 / 1 |
56 | |||
| startLtiSession | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| startLti1p3Session | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getLtiSession | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getCredential | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| getLtiConsumerResource | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| singleton | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLocaleFromMessagePayload | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| 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) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | */ |
| 20 | |
| 21 | namespace oat\taoLti\models\classes; |
| 22 | |
| 23 | use common_Exception; |
| 24 | use common_exception_Error; |
| 25 | use common_http_Request; |
| 26 | use common_session_SessionManager; |
| 27 | use core_kernel_classes_Class; |
| 28 | use core_kernel_classes_Property; |
| 29 | use core_kernel_classes_Resource; |
| 30 | use oat\generis\model\GenerisRdf; |
| 31 | use OAT\Library\Lti1p3Core\Message\Payload\Claim\LaunchPresentationClaim; |
| 32 | use OAT\Library\Lti1p3Core\Message\Payload\LtiMessagePayloadInterface; |
| 33 | use OAT\Library\Lti1p3Core\Message\Payload\MessagePayloadInterface; |
| 34 | use OAT\Library\Lti1p3Core\Registration\RegistrationRepositoryInterface; |
| 35 | use oat\oatbox\log\LoggerService; |
| 36 | use oat\oatbox\service\ConfigurableService; |
| 37 | use oat\oatbox\service\ServiceManager; |
| 38 | use oat\oatbox\session\SessionService; |
| 39 | use oat\tao\model\session\Context\TenantDataSessionContext; |
| 40 | use oat\tao\model\session\Context\UserDataSessionContext; |
| 41 | use oat\tao\model\TaoOntology; |
| 42 | use oat\taoLti\models\classes\LtiMessages\LtiErrorMessage; |
| 43 | use oat\taoLti\models\classes\user\Lti1p3User; |
| 44 | use Psr\Log\LogLevel; |
| 45 | |
| 46 | class LtiService extends ConfigurableService |
| 47 | { |
| 48 | public const LIS_CONTEXT_ROLE_NAMESPACE = 'urn:lti:role:ims/lis/'; |
| 49 | |
| 50 | public const LTICONTEXT_SESSION_KEY = 'LTICONTEXT'; |
| 51 | |
| 52 | public const DEFAULT_USER_EXTENSION = 'tao/Main/index?structure=items&ext=taoItems'; |
| 53 | |
| 54 | public function createLtiSession(common_http_Request $request) |
| 55 | { |
| 56 | try { |
| 57 | /** @var FactoryLtiAuthAdapterService $factoryAuth */ |
| 58 | $factoryAuth = $this->getServiceLocator()->get(FactoryLtiAuthAdapterServiceInterface::SERVICE_ID); |
| 59 | $adapter = $factoryAuth->create($request); |
| 60 | $user = $adapter->authenticate(); |
| 61 | $session = new TaoLtiSession($user); |
| 62 | |
| 63 | $this->getServiceLocator()->propagate($session); |
| 64 | return $session; |
| 65 | } catch (LtiInvalidVariableException $e) { |
| 66 | $this->getServiceLocator()->get(LoggerService::SERVICE_ID) |
| 67 | ->log(LogLevel::INFO, $e->getMessage()); |
| 68 | throw new LtiException( |
| 69 | __('You are not authorized to use this system'), |
| 70 | LtiErrorMessage::ERROR_UNAUTHORIZED |
| 71 | ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | public function createLti1p3Session( |
| 76 | LtiMessagePayloadInterface $messagePayload, |
| 77 | core_kernel_classes_Resource $user = null |
| 78 | ) { |
| 79 | try { |
| 80 | /** @var RegistrationRepositoryInterface $registrationRepository */ |
| 81 | $registrationRepository = $this->getServiceLocator() |
| 82 | ->getContainer() |
| 83 | ->get(RegistrationRepositoryInterface::class); |
| 84 | |
| 85 | $issuer = $messagePayload->getMandatoryClaim(MessagePayloadInterface::CLAIM_ISS); |
| 86 | $clientId = $messagePayload->getMandatoryClaim(MessagePayloadInterface::CLAIM_AUD)[0]; |
| 87 | |
| 88 | $registration = $registrationRepository->findByPlatformIssuer($issuer, $clientId); |
| 89 | |
| 90 | if ($registration === null) { |
| 91 | throw new LtiException( |
| 92 | sprintf('Cannot find a registration with issuer "%s" and client ID "%s"', $issuer, $clientId), |
| 93 | LtiErrorMessage::ERROR_UNAUTHORIZED |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | $ltiUser = new Lti1p3User( |
| 98 | LtiLaunchData::fromLti1p3MessagePayload($messagePayload, $registration->getPlatform()), |
| 99 | $user ? $user->getUri() : null |
| 100 | ); |
| 101 | |
| 102 | if ($user !== null) { |
| 103 | $userLatestExtension = new core_kernel_classes_Property(TaoOntology::PROPERTY_USER_LAST_EXTENSION); |
| 104 | |
| 105 | //do not consider lti users with UserFirstTime as true because they should not see the help modal |
| 106 | $ltiUser->setUserFirstTimeUri(GenerisRdf::GENERIS_FALSE); |
| 107 | $ltiUser->setUserLatestExtension(self::DEFAULT_USER_EXTENSION); |
| 108 | |
| 109 | |
| 110 | $userLatestExtensionValue = (string)$user->getOnePropertyValue($userLatestExtension); |
| 111 | if (!empty($userLatestExtensionValue)) { |
| 112 | $ltiUser->setUserLatestExtension($userLatestExtensionValue); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | $ltiUser->setRegistrationId($registration->getIdentifier()); |
| 117 | |
| 118 | $contexts = []; |
| 119 | if ($clientId) { |
| 120 | $userId = $messagePayload->getUserIdentity(); |
| 121 | $clientIdParts = explode('-', $clientId); |
| 122 | $contexts = [ |
| 123 | new UserDataSessionContext( |
| 124 | $userId->getIdentifier(), |
| 125 | $userId->getIdentifier(), |
| 126 | $userId->getName(), |
| 127 | $userId->getEmail(), |
| 128 | $userId->getLocale() ?? $this->getLocaleFromMessagePayload($messagePayload) |
| 129 | ), |
| 130 | new TenantDataSessionContext(end($clientIdParts)) |
| 131 | ]; |
| 132 | } |
| 133 | |
| 134 | $session = TaoLtiSession::fromVersion1p3($ltiUser, $contexts); |
| 135 | |
| 136 | $this->getServiceLocator()->propagate($session); |
| 137 | |
| 138 | |
| 139 | return $session; |
| 140 | } catch (LtiInvalidVariableException $e) { |
| 141 | $this->logInfo($e->getMessage()); |
| 142 | |
| 143 | throw new LtiException( |
| 144 | $e->getMessage(), |
| 145 | LtiErrorMessage::ERROR_UNAUTHORIZED |
| 146 | ); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * start a session from the provided OAuth Request |
| 152 | * |
| 153 | * @param common_http_Request $request |
| 154 | * |
| 155 | * @throws LtiException |
| 156 | * @throws common_Exception |
| 157 | * @throws \ResolverException |
| 158 | */ |
| 159 | public function startLtiSession(common_http_Request $request) |
| 160 | { |
| 161 | $this->getServiceLocator()->get(SessionService::SERVICE_ID)->setSession($this->createLtiSession($request)); |
| 162 | } |
| 163 | |
| 164 | public function startLti1p3Session( |
| 165 | LtiMessagePayloadInterface $messagePayload, |
| 166 | core_kernel_classes_Resource $user = null |
| 167 | ) { |
| 168 | $this->getServiceLocator()->get(SessionService::SERVICE_ID)->setSession( |
| 169 | $this->createLti1p3Session($messagePayload, $user) |
| 170 | ); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Returns the current LTI session |
| 175 | * |
| 176 | * @return TaoLtiSession |
| 177 | * @throws LtiException |
| 178 | * @throws common_exception_Error |
| 179 | */ |
| 180 | public function getLtiSession() |
| 181 | { |
| 182 | $session = common_session_SessionManager::getSession(); |
| 183 | if (!$session instanceof TaoLtiSession) { |
| 184 | throw new LtiException(__FUNCTION__ . ' called on a non LTI session', LtiErrorMessage::ERROR_SYSTEM_ERROR); |
| 185 | } |
| 186 | $this->getServiceLocator()->propagate($session); |
| 187 | |
| 188 | return $session; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * @param $key |
| 193 | * @return mixed |
| 194 | * @throws LtiException |
| 195 | */ |
| 196 | public function getCredential($key) |
| 197 | { |
| 198 | $class = new core_kernel_classes_Class(ConsumerService::CLASS_URI); |
| 199 | $instances = $class->searchInstances([TaoOntology::PROPERTY_OAUTH_KEY => $key], ['like' => false]); |
| 200 | if (count($instances) == 0) { |
| 201 | throw new LtiException('No Credentials for consumer key ' . $key, LtiErrorMessage::ERROR_UNAUTHORIZED); |
| 202 | } |
| 203 | if (count($instances) > 1) { |
| 204 | throw new LtiException( |
| 205 | 'Multiple Credentials for consumer key ' . $key, |
| 206 | LtiErrorMessage::ERROR_INVALID_PARAMETER |
| 207 | ); |
| 208 | } |
| 209 | |
| 210 | return current($instances); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Returns the LTI Consumer resource associated to this lti session |
| 215 | * |
| 216 | * @access public |
| 217 | * @param LtiLaunchData $launchData |
| 218 | * @return core_kernel_classes_Resource resource of LtiConsumer |
| 219 | * @throws LtiVariableMissingException |
| 220 | * @author Joel Bout, <joel@taotesting.com> |
| 221 | * @deprecated use LtiLaunchData::getLtiConsumer instead |
| 222 | */ |
| 223 | public function getLtiConsumerResource($launchData) |
| 224 | { |
| 225 | return $launchData->getLtiConsumer(); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * @return LtiService |
| 230 | * @deprecated |
| 231 | */ |
| 232 | public static function singleton() |
| 233 | { |
| 234 | return ServiceManager::getServiceManager()->get(static::class); |
| 235 | } |
| 236 | |
| 237 | private function getLocaleFromMessagePayload(LtiMessagePayloadInterface $messagePayload): ?string |
| 238 | { |
| 239 | if ($messagePayload && $messagePayload->getLaunchPresentation() instanceof LaunchPresentationClaim) { |
| 240 | return $messagePayload->getLaunchPresentation()->getLocale(); |
| 241 | } |
| 242 | |
| 243 | return null; |
| 244 | } |
| 245 | } |