Completed
Push — master ( a6f5e7...505dfd )
by John
02:13
created

JwtUserProvider::refreshUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\JwtBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\JwtBundle\User;
10
11
use KleijnWeb\JwtBundle\Authenticator\JwtToken;
12
use Symfony\Component\Security\Core\User\UserInterface;
13
use Symfony\Component\Security\Core\User\UserProviderInterface;
14
15
/**
16
 * @author John Kleijn <[email protected]>
17
 */
18
class JwtUserProvider implements UserProviderInterface
19
{
20
    const BASE_ROLE = 'IS_AUTHENTICATED_FULLY';
21
22
    /**
23
     * @var array
24
     */
25
    private $claims = [];
26
27
    /**
28
     * @deprecated
29
     * @param array $claims
30
     * @return array
31
     */
32
    public static function getRolesFromAudienceClaims(array $claims)
33
    {
34
        return self::extractRoles($claims);
35
    }
36
37
    /**
38
     * @param JwtToken $token
39
     */
40
    public function setClaimsUsingToken(JwtToken $token)
41
    {
42
        $this->claims[$token->getSubject()] = $token->getClaims();
43
    }
44
45
    /**
46
     * @param string $username
47
     * @return JwtUser
48
     */
49
    public function loadUserByUsername($username)
50
    {
51
        $claims = $this->getClaims($username);
52
53
        $roles = array_merge([self::BASE_ROLE], self::extractRoles($claims));
54
55
        return new JwtUser($username, $roles, $claims);
56
    }
57
58
    /**
59
     * @param UserInterface $user
60
     * @return JwtUser
61
     */
62
    public function refreshUser(UserInterface $user)
63
    {
64
        return $this->loadUserByUsername($user->getUsername());
65
    }
66
67
    /**
68
     * @param string $class
69
     * @return bool
70
     */
71
    public function supportsClass($class)
72
    {
73
        return $class === JwtUser::class;
74
    }
75
76
    /**
77
     * @param string $username
78
     * @return array
79
     */
80
    private function getClaims(string $username): array
81
    {
82
        return $this->claims[$username];
83
    }
84
85
    /**
86
     * @param array $claims
87
     * @return array
88
     */
89
    private static function extractRoles(array $claims)
90
    {
91
        $roles = [];
92
93
        foreach ($claims as $claimKey => $claimValue) {
94
            if ($claimKey === 'aud') {
95
                if (is_array($claimValue)) {
96
                    foreach ($claimValue as $role) {
97
                        $roles[] = "ROLE_" . strtoupper($role);
98
                    }
99
                } elseif (is_string($claimValue)) {
100
                    $roles[] = "ROLE_" . strtoupper($claimValue);
101
                }
102
            }
103
        }
104
105
        return $roles;
106
    }
107
}
108