Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
84.21% |
16 / 19 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Lti1p3UserAuthenticator | |
84.21% |
16 / 19 |
|
50.00% |
2 / 4 |
8.25 | |
0.00% |
0 / 1 |
| authenticate | |
33.33% |
1 / 3 |
|
0.00% |
0 / 1 |
3.19 | |||
| getUserIdentity | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
4.01 | |||
| getPropertyValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getUserService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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) 2020 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoLti\models\classes\Platform\Service\Oidc; |
| 24 | |
| 25 | use ErrorException; |
| 26 | use oat\generis\model\user\UserRdf; |
| 27 | use OAT\Library\Lti1p3Core\Registration\RegistrationInterface; |
| 28 | use OAT\Library\Lti1p3Core\Security\User\Result\UserAuthenticationResult; |
| 29 | use OAT\Library\Lti1p3Core\Security\User\Result\UserAuthenticationResultInterface; |
| 30 | use OAT\Library\Lti1p3Core\Security\User\UserAuthenticatorInterface; |
| 31 | use OAT\Library\Lti1p3Core\User\UserIdentity; |
| 32 | use oat\oatbox\service\ConfigurableService; |
| 33 | use oat\oatbox\user\User; |
| 34 | use oat\oatbox\user\UserService; |
| 35 | use Throwable; |
| 36 | |
| 37 | class Lti1p3UserAuthenticator extends ConfigurableService implements UserAuthenticatorInterface |
| 38 | { |
| 39 | public function authenticate( |
| 40 | RegistrationInterface $registration, |
| 41 | string $loginHint |
| 42 | ): UserAuthenticationResultInterface { |
| 43 | try { |
| 44 | return new UserAuthenticationResult(true, $this->getUserIdentity($loginHint)); |
| 45 | } catch (Throwable $exception) { |
| 46 | return new UserAuthenticationResult(false); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @throws ErrorException |
| 52 | */ |
| 53 | private function getUserIdentity(string $userId): ?UserIdentity |
| 54 | { |
| 55 | // anonymous user without login data |
| 56 | if ($userId === '') { |
| 57 | return null; |
| 58 | } |
| 59 | |
| 60 | $user = $this->getUserService() |
| 61 | ->getUser($userId); |
| 62 | |
| 63 | if (!$user instanceof User) { |
| 64 | throw new ErrorException(sprintf('User [%s] not found', $userId)); |
| 65 | } |
| 66 | |
| 67 | $login = $this->getPropertyValue($user, UserRdf::PROPERTY_LOGIN); |
| 68 | $login = empty($login) ? $userId : $login; |
| 69 | |
| 70 | $firstName = $this->getPropertyValue($user, UserRdf::PROPERTY_FIRSTNAME); |
| 71 | $lastName = $this->getPropertyValue($user, UserRdf::PROPERTY_LASTNAME); |
| 72 | |
| 73 | $fullName = "$firstName $lastName"; |
| 74 | |
| 75 | $email = $this->getPropertyValue($user, UserRdf::PROPERTY_MAIL); |
| 76 | |
| 77 | $locale = $this->getPropertyValue($user, UserRdf::PROPERTY_DEFLG); |
| 78 | |
| 79 | return new UserIdentity($login, trim($fullName), $email, $firstName, $lastName, null, $locale); |
| 80 | } |
| 81 | |
| 82 | private function getPropertyValue(User $user, string $propertyName): ?string |
| 83 | { |
| 84 | return $user->getPropertyValues($propertyName)[0] ?? null; |
| 85 | } |
| 86 | |
| 87 | private function getUserService(): UserService |
| 88 | { |
| 89 | return $this->getServiceLocator()->get(UserService::SERVICE_ID); |
| 90 | } |
| 91 | } |