Passed
Pull Request — master (#50)
by
unknown
04:25 queued 32s
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\User\UserInterface;
12
13
/**
14
 * Class JWTUserProvider
15
 */
16
class JWTUserProvider implements JWTUserProviderInterface
17
{
18
    /**
19
     * @var TenantStorageInterface
20
     */
21
    private $tenantStorage;
22
23
    /**
24
     * JWTUserProvider constructor.
25
     *
26
     * @param TenantStorageInterface $tenantStorage
27
     */
28
    public function __construct(TenantStorageInterface $tenantStorage)
29
    {
30
        $this->tenantStorage = $tenantStorage;
31
    }
32
33
    /**
34
     * @param string $jwt
35
     *
36
     * @return object|mixed
37
     */
38
    public function getDecodedToken(string $jwt)
39
    {
40
        try {
41
            /** @noinspection PhpUnusedLocalVariableInspection */
42
            $bodyb64 = \explode('.', $jwt)[1];
43
            $decodedToken = \json_decode(JWT::urlsafeB64Decode($bodyb64));
44
45
            /** @noinspection NullPointerExceptionInspection */
46
            JWT::decode($jwt, $this->tenantStorage->findByClientKey($decodedToken->iss)->getSharedSecret(), ['HS256']);
47
48
            return $decodedToken;
49
        } catch (\Throwable $e) {
50
            throw new AuthenticationException($e->getMessage());
51
        }
52
    }
53
54
    /**
55
     * @param mixed $clientKey
56
     *
57
     * @return TenantInterface|UserInterface
58
     */
59
    public function loadUserByUsername($clientKey): TenantInterface
60
    {
61
        $tenant = $this->tenantStorage->findByClientKey($clientKey);
62
63
        if (!$tenant) {
64
            throw new UsernameNotFoundException('Can\'t find tenant with such username');
65
        }
66
67
        return $tenant;
68
    }
69
70
    /**
71
     * @param UserInterface $user
72
     */
73
    public function refreshUser(UserInterface $user): void
74
    {
75
        throw new UnsupportedUserException('Refresh prohibited');
76
    }
77
78
    /**
79
     * @param string|mixed $class
80
     *
81
     * @return bool
82
     */
83
    public function supportsClass($class): bool
84
    {
85
        return \is_subclass_of($class, TenantInterface::class);
86
    }
87
}
88