Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
9.09% |
1 / 11 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| LtiAuthAdapter | |
9.09% |
1 / 11 |
|
33.33% |
1 / 3 |
23.78 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| authenticate | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| getLaunchData | |
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) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\taoLti\models\classes; |
| 24 | |
| 25 | use common_http_InvalidSignatureException; |
| 26 | use common_http_Request; |
| 27 | use oat\tao\model\oauth\lockout\LockOutException; |
| 28 | use oat\taoLti\models\classes\LtiMessages\LtiErrorMessage; |
| 29 | use oat\taoLti\models\classes\user\LtiUserService; |
| 30 | use Zend\ServiceManager\ServiceLocatorAwareInterface; |
| 31 | use Zend\ServiceManager\ServiceLocatorAwareTrait; |
| 32 | use oat\tao\model\oauth\OauthService; |
| 33 | |
| 34 | /** |
| 35 | * Authentication adapter interface to be implemented by authentication methods |
| 36 | * |
| 37 | * @access public |
| 38 | * @author Joel Bout, <joel@taotesting.com> |
| 39 | * @package taoLti |
| 40 | */ |
| 41 | class LtiAuthAdapter implements \common_user_auth_Adapter, ServiceLocatorAwareInterface |
| 42 | { |
| 43 | use ServiceLocatorAwareTrait; |
| 44 | |
| 45 | /** |
| 46 | * |
| 47 | * @var common_http_Request |
| 48 | */ |
| 49 | protected $request; |
| 50 | |
| 51 | /** |
| 52 | * Creates an Authentication adapter from an OAuth Request |
| 53 | * |
| 54 | * @param common_http_Request $request |
| 55 | */ |
| 56 | public function __construct(common_http_Request $request) |
| 57 | { |
| 58 | $this->request = $request; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * (non-PHPdoc) |
| 63 | * @see \common_user_auth_Adapter::authenticate() |
| 64 | * |
| 65 | * @return user\LtiUser |
| 66 | * @throws LtiException |
| 67 | * @throws LtiVariableMissingException |
| 68 | * @throws \ResolverException |
| 69 | * @throws \common_Exception |
| 70 | * @throws \common_exception_Error |
| 71 | * @throws \core_kernel_users_CacheException |
| 72 | * @throws \core_kernel_users_Exception |
| 73 | */ |
| 74 | public function authenticate() |
| 75 | { |
| 76 | try { |
| 77 | /** @var OauthService $oauthService */ |
| 78 | $oauthService = $this->getServiceLocator()->get(OauthService::SERVICE_ID); |
| 79 | $oauthService->validate($this->request); |
| 80 | $ltiLaunchData = $this->getLaunchData(); |
| 81 | /** @var LtiUserService $userService */ |
| 82 | $userService = $this->getServiceLocator()->get(LtiUserService::SERVICE_ID); |
| 83 | return $userService->findOrSpawnUser($ltiLaunchData); |
| 84 | } catch (common_http_InvalidSignatureException $e) { |
| 85 | throw new LtiException('Invalid LTI signature', LtiErrorMessage::ERROR_UNAUTHORIZED); |
| 86 | } catch (LockOutException $e) { |
| 87 | throw new LtiException('Too many incorrect attempts', LtiErrorMessage::ERROR_UNAUTHORIZED); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @return LtiLaunchData |
| 93 | * @throws \ResolverException |
| 94 | */ |
| 95 | protected function getLaunchData() |
| 96 | { |
| 97 | return LtiLaunchData::fromRequest($this->request); |
| 98 | } |
| 99 | } |