Completed
Push — master ( e45f3d...06da8e )
by Tobias
08:34
created

SSOToken::getRoleNames()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 1
cp 0
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 12
1
<?php
2
3
namespace Happyr\Auth0Bundle\Security\Authentication\Token;
4
5
use Happyr\Auth0Bundle\Model\Authentication\UserProfile\UserInfo;
6
use Happyr\Auth0Bundle\Model\Authorization\Token\Token;
7
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
8
use Symfony\Component\Security\Core\Role\Role;
9
10
class SSOToken extends AbstractToken
11
{
12
    /**
13
     * @var Token|null
14
     */
15
    private $auth0Data;
16
17
    /**
18
     * @var array
19
     */
20
    private $storedRoles = [];
21
22
    /**
23
     * The user model for the API.
24
     *
25
     * @var mixed
26
     */
27
    private $userModel;
28
29
    /**
30
     * @param Token $data
31
     *
32
     * @return SSOToken
33
     */
34
    public function setAuth0Data(Token $data)
35
    {
36
        $this->auth0Data = $data;
37
38
        return $this;
39
    }
40
41
    /**
42
     * @return Token|null
43
     */
44
    public function getAuth0Data()
45
    {
46
        return $this->auth0Data;
47
    }
48
49
    /**
50
     * @return mixed
51
     */
52
    public function getAccessToken()
53
    {
54
        if (null === $this->auth0Data) {
55
            return null;
56
        }
57
58
        return $this->auth0Data->getAccessToken();
59
    }
60
61
    /**
62
     * @return mixed
63
     */
64
    public function getExpiresAt()
65
    {
66
        if (null === $this->auth0Data) {
67
            return null;
68
        }
69
70
        return $this->auth0Data->getExpiresAt();
71
    }
72
73
    /**
74
     * @return mixed
75
     */
76
    public function getUserModel()
77
    {
78
        return $this->userModel;
79
    }
80
81
    /**
82
     * @param UserInfo $userModel
83
     *
84
     * @return SSOToken
85
     */
86
    public function setUserModel(UserInfo $userModel)
87
    {
88
        $this->userModel = $userModel;
89
90
        return $this;
91
    }
92
93
    public function getCredentials()
94
    {
95
        return '';
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function serialize()
102
    {
103
        $user = $this->getUser();
104
105
        return serialize(
106
            [
107
                is_object($user) ? clone $user : $user,
108
                is_object($this->userModel) ? clone $this->userModel : $this->userModel,
109
                $this->isAuthenticated(),
110
                $this->getRoles(),
111
                $this->getAttributes(),
112
                $this->auth0Data,
113
            ]
114
        );
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function unserialize($serialized)
121
    {
122
        list($user, $this->userModel, $isAuthenticated, $this->storedRoles, $attributes, $auth0Data) = unserialize($serialized);
123
        if ($user) {
124
            $this->setUser($user);
125
        }
126
127
        if ($auth0Data instanceof Token) {
128
            $this->setAuth0Data($auth0Data);
129
        }
130
131
        $this->setAuthenticated($isAuthenticated);
132
        $this->setAttributes($attributes);
133
    }
134
135
    public function getRoleNames(): array
136
    {
137
        $allRoles = array_merge(parent::getRoleNames(), $this->storedRoles);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Securi...ion\Token\AbstractToken as the method getRoleNames() does only exist in the following sub-classes of Symfony\Component\Securi...ion\Token\AbstractToken: Happyr\Auth0Bundle\Secur...tication\Token\SSOToken. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
138
        $uniqueRoles = [];
139
140
        /** @var Role $role */
141
        foreach ($allRoles as $role) {
142
            $name = is_string($role) ? $role : $role->getRole();
143
            $uniqueRoles[$name] = true;
144
        }
145
146
        return array_keys($uniqueRoles);
147
    }
148
149
150
    /**
151
     * This function is deprecated by Symfony 4.3.
152
     */
153
    public function getRoles()
154
    {
155
        $allRoles = array_merge(parent::getRoles(), $this->storedRoles);
156
        $uniqueRoles = [];
157
158
        /** @var Role $role */
159
        foreach ($allRoles as $role) {
160
            $uniqueRoles[$role->getRole()] = $role;
161
        }
162
163
        return array_values($uniqueRoles);
164
    }
165
}
166