HandshakeController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AtlassianConnectBundle\Controller;
6
7
use AtlassianConnectBundle\Repository\TenantRepositoryInterface;
8
use Firebase\JWT\JWT;
9
use Firebase\JWT\Key;
10
use Psr\Log\LoggerInterface;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
14
class HandshakeController
15
{
16
    public function __construct(private TenantRepositoryInterface $repository, private LoggerInterface $logger)
17
    {
18
    }
19
20
    public function registerAction(Request $request): Response
21
    {
22
        $content = $request->getContent();
23
        $content = json_decode($content, true);
24
25
        $tenant = $this->repository->findByClientKey($content['clientKey']);
26
27
        if (null !== $tenant) {
28
            try {
29
                $authorizationHeaderArray = explode(' ', $request->headers->get('authorization'));
30
31
                if (\count($authorizationHeaderArray) <= 1) {
32
                    throw new \InvalidArgumentException('Bad authorization header');
33
                }
34
35
                $jwt = $authorizationHeaderArray[1];
36
                JWT::decode($jwt, new Key($tenant->getSharedSecret(), 'HS256'));
37
            } catch (\Throwable $e) {
38
                $this->logger->error($e->getMessage(), ['exception' => $e]);
39
40
                return new Response('Unauthorized', 401);
41
            }
42
        } else {
43
            $tenant = $this->repository->initializeTenant();
44
        }
45
46
        $tenant
47
            ->setAddonKey($content['key'])
48
            ->setClientKey($content['clientKey'])
49
            ->setPublicKey($content['publicKey'])
50
            ->setSharedSecret($content['sharedSecret'])
51
            ->setServerVersion($content['serverVersion'])
52
            ->setPluginsVersion($content['pluginsVersion'])
53
            ->setBaseUrl($content['baseUrl'])
54
            ->setProductType($content['productType'])
55
            ->setDescription($content['description'])
56
            ->setEventType($content['eventType'])
57
        ;
58
59
        if (\array_key_exists('oauthClientId', $content)) {
60
            $tenant->setOauthClientId($content['oauthClientId']);
61
        }
62
63
        $this->repository->save($tenant);
64
65
        return new Response('OK', 200);
66
    }
67
}
68