Passed
Push — master ( f75ef5...782b7a )
by Matthieu
05:01
created

JWTUserProvider::getNotFoundException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AtlassianConnectBundle\Security;
6
7
use AtlassianConnectBundle\Entity\TenantInterface;
8
use Doctrine\ORM\EntityManagerInterface;
9
use Firebase\JWT\JWT;
10
use Symfony\Component\Security\Core\Exception\AuthenticationException;
11
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
12
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
13
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
14
use Symfony\Component\Security\Core\User\UserInterface;
15
16
class JWTUserProvider implements JWTUserProviderInterface
17
{
18
    protected EntityManagerInterface $em;
19
20
    protected string $tenantClass;
21
22
    public function __construct(EntityManagerInterface $entityManager, string $tenantClass)
23
    {
24
        $this->em = $entityManager;
25
        $this->tenantClass = $tenantClass;
26
    }
27
28
    /**
29
     * @return object|mixed
30
     */
31
    public function getDecodedToken(string $jwt)
32
    {
33
        try {
34
            $bodyb64 = explode('.', $jwt)[1];
35
            $decodedToken = json_decode(JWT::urlsafeB64Decode($bodyb64));
36
37
            JWT::decode($jwt, $this->findTenant($decodedToken->iss)->getSharedSecret(), ['HS256']);
38
39
            return $decodedToken;
40
        } catch (\Throwable $e) {
41
            throw new AuthenticationException('Failed to parse token');
42
        }
43
    }
44
45
    public function loadUserByUsername($username): UserInterface
46
    {
47
        $tenant = $this->findTenant($username);
48
49
        if (!$tenant) {
50
            throw $this->getNotFoundException();
51
        }
52
53
        return $tenant;
54
    }
55
56
    public function refreshUser(UserInterface $user): void
57
    {
58
        throw new UnsupportedUserException('Refresh prohibited');
59
    }
60
61
    /**
62
     * @param string|mixed $class
63
     */
64
    public function supportsClass($class): bool
65
    {
66
        return is_subclass_of($class, TenantInterface::class);
67
    }
68
69
    public function loadUserByIdentifier(string $identifier): UserInterface
70
    {
71
        $tenant = $this->findTenant($identifier);
72
73
        if (!$tenant) {
74
            throw $this->getNotFoundException();
75
        }
76
77
        return $tenant;
78
    }
79
80
    private function getNotFoundException(): AuthenticationException
81
    {
82
        $message = 'Can\'t find tenant with such username';
83
84
        if (class_exists(UserNotFoundException::class)) {
85
            return new UserNotFoundException($message);
86
        }
87
88
        return new UsernameNotFoundException($message);
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Securi...ernameNotFoundException has been deprecated: since Symfony 5.3 to be removed in 6.0, use UserNotFoundException instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

88
        return /** @scrutinizer ignore-deprecated */ new UsernameNotFoundException($message);
Loading history...
89
    }
90
91
    private function findTenant(string $clientKey): ?TenantInterface
92
    {
93
        return $this->em
94
            ->getRepository($this->tenantClass)
95
            ->findOneBy(['clientKey' => $clientKey]);
96
    }
97
}
98