Passed
Push — master ( 8d3590...a319dd )
by Angel Fernando Quiroz
17:46 queued 08:38
created

AbstractScimController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 62
rs 10
c 1
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A authenticateRequest() 0 27 5
A getAndValidateJson() 0 15 3
A __construct() 0 7 1
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Controller\Scim;
8
9
use Chamilo\CoreBundle\Exception\ScimException;
10
use Chamilo\CoreBundle\Helpers\AccessUrlHelper;
11
use Chamilo\CoreBundle\Helpers\AuthenticationConfigHelper;
12
use Chamilo\CoreBundle\Helpers\ScimHelper;
13
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
17
use Symfony\Contracts\Translation\TranslatorInterface;
18
19
use const JSON_ERROR_NONE;
20
21
abstract class AbstractScimController extends AbstractController
22
{
23
    public const SCIM_CONTENT_TYPE = 'application/scim+json';
24
25
    protected array $scimConfig;
26
27
    public function __construct(
28
        protected readonly TranslatorInterface $translator,
29
        protected readonly ScimHelper $scimHelper,
30
        protected readonly AccessUrlHelper $accessUrlHelper,
31
        AuthenticationConfigHelper $authenticationConfigHelper,
32
    ) {
33
        $this->scimConfig = $authenticationConfigHelper->getScimConfig();
34
    }
35
36
    protected function getAndValidateJson(Request $request): array
37
    {
38
        $content = $request->getContent();
39
40
        if (empty($content)) {
41
            throw new ScimException('No content');
42
        }
43
44
        $data = json_decode($content, true);
45
46
        if (JSON_ERROR_NONE !== json_last_error()) {
47
            throw new ScimException('Invalid JSON: '.json_last_error_msg());
48
        }
49
50
        return $data;
51
    }
52
53
    /**
54
     * @throws ScimException
55
     */
56
    protected function authenticateRequest(Request $request): void
57
    {
58
        if (!$this->scimConfig['enabled']) {
59
            throw new AccessDeniedHttpException(
60
                $this->translator->trans('SCIM is not enabled')
61
            );
62
        }
63
64
        $authHeader = $request->headers->get('Authorization');
65
66
        $invalidTokenException = new ScimException(
67
            $this->translator->trans('Invalid Token'),
68
            Response::HTTP_UNAUTHORIZED
69
        );
70
71
        if (!$authHeader) {
72
            throw $invalidTokenException;
73
        }
74
75
        if (!preg_match('/^Bearer\s+(\S+)/i', $authHeader, $matches)) {
76
            throw $invalidTokenException;
77
        }
78
79
        $providedToken = $matches[1];
80
81
        if (!hash_equals($this->getParameter('scim_token'), $providedToken)) {
82
            throw $invalidTokenException;
83
        }
84
    }
85
}
86