|
1 |
| -<?php |
2 |
| -/** |
3 |
| - * Copyright (C) 2018 Daniel Pfeil <[email protected] |
4 |
| - * |
5 |
| - * This program is free software: you can redistribute it and/or modify |
6 |
| - * it under the terms of the GNU General Public License as published by |
7 |
| - * the Free Software Foundation, either version 3 of the License, or |
8 |
| - * (at your option) any later version. |
9 |
| - * |
10 |
| - * This program is distributed in the hope that it will be useful, |
11 |
| - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
| - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
| - * GNU General Public License for more details. |
14 |
| - * |
15 |
| - * You should have received a copy of the GNU General Public License |
16 |
| - * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 |
| - * |
18 |
| - */ |
19 |
| - |
20 |
| -namespace DanielPfeil\Samlauthentication\Service; |
21 |
| - |
22 |
| -use DanielPfeil\Samlauthentication\Enum\AuthenticationStatus; |
23 |
| -use DanielPfeil\Samlauthentication\Utility\FactoryUtility; |
24 |
| -use DanielPfeil\Samlauthentication\Utility\ServiceProviderUtility; |
25 |
| -use TYPO3\CMS\Extbase\Utility\DebuggerUtility; |
26 |
| - |
27 |
| -final class AuthenticationService extends \TYPO3\CMS\Core\Authentication\AbstractAuthenticationService |
28 |
| -{ |
29 |
| - final public function authUser(array $user): int |
30 |
| - { |
31 |
| - $serviceProviderUtility = ServiceProviderUtility::getInstance(); |
32 |
| - |
33 |
| - $activeServiceProviders = $serviceProviderUtility->getActive(FactoryUtility::getServiceProviderModels()); |
34 |
| - |
35 |
| - foreach ($activeServiceProviders as $activeServiceProvider) { |
36 |
| - $samlComponent = FactoryUtility::getSAMLUtility($activeServiceProvider); |
37 |
| - $samlUserData = $samlComponent->getUserData($activeServiceProvider); |
38 |
| - |
39 |
| - if (TYPO3_MODE === "FE") { |
40 |
| - $samlUsername = $samlUserData["fe_users"]["username"]->getValue(); |
41 |
| - } else { |
42 |
| - $samlUsername = $samlUserData["be_users"]["username"]->getValue(); |
43 |
| - } |
44 |
| - |
45 |
| - if ($samlUsername == $user["username"]) { |
46 |
| - return AuthenticationStatus::SUCCESS_BREAK; |
47 |
| - } |
48 |
| - } |
49 |
| - return AuthenticationStatus::FAIL_CONTINUE; |
50 |
| - } |
51 |
| - |
52 |
| - final public function getUser() |
53 |
| - { |
54 |
| - //todo check if parent is needed before or after? |
55 |
| - $user = parent::fetchUserRecord($this->login["uname"]); |
56 |
| - if (!$user) { |
57 |
| - try { |
58 |
| - $serviceProviderUtility = ServiceProviderUtility::getInstance(); |
59 |
| - $activeServiceProviders = $serviceProviderUtility->getActive(FactoryUtility::getServiceProviderModels()); |
60 |
| - |
61 |
| - foreach ($activeServiceProviders as $activeServiceProvider) { |
62 |
| - $samlComponent = FactoryUtility::getSAMLUtility($activeServiceProvider); |
63 |
| - $storedSuccessfull = $samlComponent->saveUserData($activeServiceProvider); |
64 |
| - if ($storedSuccessfull) { |
65 |
| - break; |
66 |
| - } |
67 |
| - } |
68 |
| - |
69 |
| - $user = parent::fetchUserRecord($this->login["uname"]); |
70 |
| - } catch (\Exception $exception) { |
71 |
| - //TODO implement |
72 |
| - DebuggerUtility::var_dump($exception); |
73 |
| - } |
74 |
| - } |
75 |
| - return $user; |
76 |
| - } |
77 |
| -} |
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright (C) 2018 Daniel Pfeil <[email protected] |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +namespace DanielPfeil\Samlauthentication\Service; |
| 21 | + |
| 22 | +use DanielPfeil\Samlauthentication\Enum\AuthenticationStatus; |
| 23 | +use DanielPfeil\Samlauthentication\Utility\FactoryUtility; |
| 24 | +use DanielPfeil\Samlauthentication\Utility\ServiceProviderUtility; |
| 25 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 26 | +use TYPO3\CMS\Extbase\Utility\DebuggerUtility; |
| 27 | + |
| 28 | +final class AuthenticationService extends \TYPO3\CMS\Core\Authentication\AbstractAuthenticationService |
| 29 | +{ |
| 30 | + final public function authUser(array $user): int |
| 31 | + { |
| 32 | + if (!$this->isResponsible()) { |
| 33 | + return AuthenticationStatus::FAIL_CONTINUE; |
| 34 | + } |
| 35 | + |
| 36 | + $serviceProviderUtility = ServiceProviderUtility::getInstance(); |
| 37 | + |
| 38 | + $activeServiceProviders = $serviceProviderUtility->getActive(FactoryUtility::getServiceProviderModels()); |
| 39 | + |
| 40 | + foreach ($activeServiceProviders as $activeServiceProvider) { |
| 41 | + $samlComponent = FactoryUtility::getSAMLUtility($activeServiceProvider); |
| 42 | + $samlUserData = $samlComponent->getUserData($activeServiceProvider); |
| 43 | + |
| 44 | + if (TYPO3_MODE === "FE") { |
| 45 | + $samlUsername = $samlUserData["fe_users"]["username"]->getValue(); |
| 46 | + } else { |
| 47 | + $samlUsername = $samlUserData["be_users"]["username"]->getValue(); |
| 48 | + } |
| 49 | + |
| 50 | + if ($samlUsername == $user["username"]) { |
| 51 | + return AuthenticationStatus::SUCCESS_BREAK; |
| 52 | + } |
| 53 | + } |
| 54 | + return AuthenticationStatus::FAIL_CONTINUE; |
| 55 | + } |
| 56 | + |
| 57 | + final public function getUser() |
| 58 | + { |
| 59 | + if (!$this->isResponsible()) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + //todo check if parent is needed before or after? |
| 64 | + $user = parent::fetchUserRecord($this->login["uname"]); |
| 65 | + if (!$user) { |
| 66 | + try { |
| 67 | + $serviceProviderUtility = ServiceProviderUtility::getInstance(); |
| 68 | + $activeServiceProviders = $serviceProviderUtility->getActive( |
| 69 | + FactoryUtility::getServiceProviderModels() |
| 70 | + ); |
| 71 | + |
| 72 | + foreach ($activeServiceProviders as $activeServiceProvider) { |
| 73 | + $samlComponent = FactoryUtility::getSAMLUtility($activeServiceProvider); |
| 74 | + $storedSuccessfull = $samlComponent->saveUserData($activeServiceProvider); |
| 75 | + if ($storedSuccessfull) { |
| 76 | + break; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + $user = parent::fetchUserRecord($this->login["uname"]); |
| 81 | + } catch (\Exception $exception) { |
| 82 | + //TODO implement |
| 83 | + DebuggerUtility::var_dump($exception); |
| 84 | + } |
| 85 | + } |
| 86 | + return $user; |
| 87 | + } |
| 88 | + |
| 89 | + private function isResponsible(): bool |
| 90 | + { |
| 91 | + if (GeneralUtility::_POST("login-provider") === "samlauthentication" && |
| 92 | + isset($this->login["uname"]) && |
| 93 | + isset($this->login["status"]) && |
| 94 | + $this->login["status"] === "login" |
| 95 | + ) { |
| 96 | + return true; |
| 97 | + } |
| 98 | + return false; |
| 99 | + } |
| 100 | +} |
0 commit comments