|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright 2018 Sourcefabric z.ú. and contributors. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please see the |
|
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
12
|
|
|
* |
|
13
|
|
|
* @copyright 2018 Sourcefabric z.ú |
|
14
|
|
|
* @license http://www.superdesk.org/license |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace SWP\Bundle\CoreBundle\WebSocket\Topic; |
|
18
|
|
|
|
|
19
|
|
|
use Gos\Bundle\WebSocketBundle\Router\WampRequest; |
|
20
|
|
|
use Gos\Bundle\WebSocketBundle\Server\Exception\FirewallRejectionException; |
|
21
|
|
|
use Gos\Bundle\WebSocketBundle\Topic\SecuredTopicInterface; |
|
22
|
|
|
use Psr\Log\LoggerInterface; |
|
23
|
|
|
use Ratchet\ConnectionInterface; |
|
24
|
|
|
use Ratchet\Wamp\Topic; |
|
25
|
|
|
use SWP\Bundle\CoreBundle\Repository\ApiKeyRepositoryInterface; |
|
26
|
|
|
|
|
27
|
|
|
abstract class AbstractSecuredTopic implements SecuredTopicInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var ApiKeyRepositoryInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $apiKeyRepository; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var LoggerInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $logger; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* AbstractSecuredTopic constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param ApiKeyRepositoryInterface $apiKeyRepository |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(ApiKeyRepositoryInterface $apiKeyRepository, LoggerInterface $logger) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->apiKeyRepository = $apiKeyRepository; |
|
47
|
|
|
$this->logger = $logger; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function secure(ConnectionInterface $conn = null, Topic $topic, WampRequest $request, $payload = null, $exclude = null, $eligible = null, $provider = null) |
|
54
|
|
|
{ |
|
55
|
|
|
$httpRequest = $conn->httpRequest; |
|
|
|
|
|
|
56
|
|
|
$token = null; |
|
57
|
|
|
parse_str($httpRequest->getUri()->getQuery(), $params); |
|
58
|
|
|
|
|
59
|
|
|
if (isset($params['token'])) { |
|
60
|
|
|
$token = (string) $params['token']; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (null !== $token) { |
|
64
|
|
|
$this->logger->info( |
|
65
|
|
|
'Token was found in the request', |
|
66
|
|
|
['remoteAddress' => $conn->remoteAddress, 'connectionId' => $conn->resourceId] |
|
|
|
|
|
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
$apiKey = $this->apiKeyRepository |
|
70
|
|
|
->getValidToken($token) |
|
71
|
|
|
->getQuery() |
|
72
|
|
|
->getOneOrNullResult(); |
|
73
|
|
|
|
|
74
|
|
|
if (null === $apiKey) { |
|
75
|
|
|
throw new FirewallRejectionException(); |
|
76
|
|
|
} |
|
77
|
|
|
} else { |
|
78
|
|
|
$this->logger->warning( |
|
79
|
|
|
'Token was not found in the request', |
|
80
|
|
|
['remoteAddress' => $conn->remoteAddress, 'connectionId' => $conn->resourceId] |
|
|
|
|
|
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: