Passed
Pull Request — master (#50)
by
unknown
03:21
created

JWTUserProvider::findTenant()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types = 1);
2
3
namespace AtlassianConnectBundle\Security;
4
5
use AtlassianConnectBundle\Entity\TenantInterface;
6
use AtlassianConnectBundle\Storage\TenantStorageInterface;
7
use Firebase\JWT\JWT;
8
use Symfony\Component\Security\Core\Exception\AuthenticationException;
9
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
10
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
11
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
12
use Symfony\Component\Security\Core\User\UserInterface;
13
14
/**
15
 * Class JWTUserProvider
16
 */
17
class JWTUserProvider implements JWTUserProviderInterface
18
{
19
    /**
20
     * @var TenantStorageInterface
21
     */
22
    private $tenantStorage;
23
24
    /**
25
     * JWTUserProvider constructor.
26
     *
27
     * @param TenantStorageInterface $tenantStorage
28
     */
29
    public function __construct(TenantStorageInterface $tenantStorage)
30
    {
31
        $this->tenantStorage = $tenantStorage;
32
    }
33
34
    /**
35
     * @param string $jwt
36
     *
37
     * @return object|mixed
38
     */
39
    public function getDecodedToken(string $jwt)
40
    {
41
        try {
42
            /** @noinspection PhpUnusedLocalVariableInspection */
43
            $bodyb64 = \explode('.', $jwt)[1];
44
            $decodedToken = \json_decode(JWT::urlsafeB64Decode($bodyb64));
45
46
            /** @noinspection NullPointerExceptionInspection */
47
            JWT::decode($jwt, $this->tenantStorage->findByClientKey($decodedToken->iss)->getSharedSecret(), ['HS256']);
48
49
            return $decodedToken;
50
        } catch (\Throwable $e) {
51
            throw new AuthenticationException($e->getMessage());
52
        }
53
    }
54
55
    /**
56
     * @param mixed $clientKey
57
     *
58
     * @return TenantInterface|UserInterface
59
     */
60
    public function loadUserByUsername($clientKey): TenantInterface
61
    {
62
        $tenant = $this->tenantStorage->findByClientKey($clientKey);
63
64
        if (!$tenant) {
65
            throw new UsernameNotFoundException('Can\'t find tenant with such username');
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

65
            throw /** @scrutinizer ignore-deprecated */ new UsernameNotFoundException('Can\'t find tenant with such username');
Loading history...
66
        }
67
68
        return $tenant;
69
    }
70
71
    /**
72
     * @param UserInterface $user
73
     */
74
    public function refreshUser(UserInterface $user): void
75
    {
76
        throw new UnsupportedUserException('Refresh prohibited');
77
    }
78
79
    /**
80
     * @param string|mixed $class
81
     *
82
     * @return bool
83
     */
84
    public function supportsClass($class): bool
85
    {
86
        return \is_subclass_of($class, TenantInterface::class);
87
    }
88
89
    /**
90
     * @param string $identifier
91
     *
92
     * @return UserInterface
93
     */
94
    public function loadUserByIdentifier(string $identifier): UserInterface
95
    {
96
        $tenant = $this->tenantStorage->findByClientKey($identifier);
97
98
        if (!$tenant) {
99
            throw new UserNotFoundException('Can\'t find tenant with such identifier');
100
        }
101
102
        return $tenant;
103
    }
104
105
}
106