Issues (7)

Classes/Authentication/SamlAuth.php (3 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace DMK\MKSamlAuth\Authentication;
6
7
use DMK\MKSamlAuth\AttributeExtractor;
8
use DMK\MKSamlAuth\Container\BuildContainer;
9
use DMK\MKSamlAuth\Exception\MissingConfigurationException;
10
use DMK\MKSamlAuth\Exception\RuntimeException;
11
use DMK\MKSamlAuth\Model\FrontendUser;
12
use DMK\MKSamlAuth\Repository\IdentityProviderRepository;
13
use DMK\MKSamlAuth\Service\UserCreator;
14
use LightSaml\Builder\Profile\WebBrowserSso\Sp\SsoSpReceiveResponseProfileBuilder;
15
use LightSaml\Builder\Profile\WebBrowserSso\Sp\SsoSpSendAuthnRequestProfileBuilderFactory;
16
use LightSaml\Context\Profile\Helper\MessageContextHelper;
17
use TYPO3\CMS\Core\Authentication\AuthenticationService;
0 ignored issues
show
The type TYPO3\CMS\Core\Authentic...n\AuthenticationService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use TYPO3\CMS\Core\Log\LogManager;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Extbase\Object\ObjectManager;
21
22
class SamlAuth extends AuthenticationService
23
{
24
    /**
25
     * @var ObjectManager
26
     */
27
    private $om;
28
29
    /**
30
     * @var array|false
31
     */
32
    private $configuration;
33
34
    public function __construct()
35
    {
36
        $this->om = GeneralUtility::makeInstance(ObjectManager::class);
37
38
        $this->configuration = $this->om->get(IdentityProviderRepository::class)
39
            ->findByHostname(GeneralUtility::getIndpEnv('HTTP_HOST'));
40
    }
41
42
    public function getUser()
43
    {
44
        if (!\is_array($this->configuration)) {
45
            return false;
46
        }
47
48
        if ('getUserFE' === $this->mode && 'logout' !== $this->login['status']) {
49
            if (null !== GeneralUtility::_POST('SAMLResponse')) {
50
                try {
51
                    return $this->pObj->getRawUserByUid($this->receive()->getUid());
52
                } catch (\Exception $e) {
53
                    GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__)
54
                        ->emergency($e->getMessage(), ['e' => $e->getMessage()]);
55
56
                    return false;
57
                }
58
            } else {
59
                return $this->send();
60
            }
61
        }
62
63
        return false;
64
    }
65
66
    public function authUser(array $user): int
67
    {
68
        return \is_array($this->configuration) ? 200 : 0;
69
    }
70
71
    private function receive(): FrontendUser
72
    {
73
        if (!\is_array($this->configuration)) {
74
            throw new MissingConfigurationException(sprintf('There is no configuration for %s', GeneralUtility::getIndpEnv('HTTP_HOST')));
75
        }
76
77
        $buildContainer = $this->om->get(BuildContainer::class);
78
        $pb = new SsoSpReceiveResponseProfileBuilder($buildContainer);
79
80
        $context = $pb->buildContext();
81
        $action = $pb->buildAction();
82
        $action->execute($context);
83
84
        $response = MessageContextHelper::asResponse($context->getInboundContext());
85
        $attributes = AttributeExtractor::extractAttributes($response);
86
        $attributes = iterator_to_array($attributes);
87
88
        return $this->om->get(UserCreator::class)->updateOrCreate($attributes, $this->configuration);
89
    }
90
91
    private function send(): bool
92
    {
93
        if (!\is_array($this->configuration)) {
94
            return false;
95
        }
96
97
        $buildContainer = $this->om->get(BuildContainer::class);
98
        $factory = new SsoSpSendAuthnRequestProfileBuilderFactory($buildContainer);
99
        $pb = $factory->get($this->configuration['idp_entity_id']);
100
101
        $action = $pb->buildAction();
102
        $context = $pb->buildContext();
103
        $action->execute($context);
104
105
        $response = $context->getHttpResponseContext()->getResponse();
106
107
        if (null === $response) {
108
            throw new RuntimeException('Expected to have a response none given.');
109
        }
110
111
        $response->send();
112
        exit;
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return boolean. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
113
    }
114
}
115