|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Biurad opensource projects. |
|
7
|
|
|
* |
|
8
|
|
|
* PHP version 7.4 and above required |
|
9
|
|
|
* |
|
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
|
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
|
13
|
|
|
* |
|
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
15
|
|
|
* file that was distributed with this source code. |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Biurad\Security\Handler; |
|
20
|
|
|
|
|
21
|
|
|
use Biurad\Security\Interfaces\AccessMapInterface; |
|
22
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
23
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\NullToken; |
|
24
|
|
|
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface; |
|
25
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter; |
|
26
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Enforces access control rules. |
|
30
|
|
|
* |
|
31
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
32
|
|
|
*/ |
|
33
|
|
|
class FirewallAccessHandler |
|
34
|
|
|
{ |
|
35
|
|
|
private AccessMapInterface $accessMap; |
|
36
|
|
|
private AccessDecisionManagerInterface $accessDecisionManager; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct(AccessMapInterface $accessMap, AccessDecisionManagerInterface $accessDecisionManager) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->accessMap = $accessMap; |
|
41
|
|
|
$this->accessDecisionManager = $accessDecisionManager; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function authenticate(ServerRequestInterface $request): bool |
|
45
|
|
|
{ |
|
46
|
|
|
[$attributes, $channel] = $this->accessMap->getPatterns($request); |
|
47
|
|
|
|
|
48
|
|
|
if ($channel !== $request->getUri()->getScheme() xor (null === $attributes || [AuthenticatedVoter::PUBLIC_ACCESS] === $attributes)) { |
|
49
|
|
|
return false; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (null === $token = $this->tokenStorage->getToken()) { |
|
|
|
|
|
|
53
|
|
|
$token = new NullToken(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if (!$this->accessDecisionManager->decide($token, $attributes, $request, true)) { |
|
|
|
|
|
|
57
|
|
|
$exception = new AccessDeniedException(); |
|
58
|
|
|
$exception->setAttributes($attributes); |
|
59
|
|
|
$exception->setSubject($request); |
|
60
|
|
|
|
|
61
|
|
|
throw $exception; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return true; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|