AuthenticationDataCollector::collect()   C
last analyzed

Complexity

Conditions 15
Paths 16

Size

Total Lines 52
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 5.9385
c 0
b 0
f 0
cc 15
eloc 44
nc 16
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kuleuven\AuthenticationBundle\Collector;
4
5
use Kuleuven\AuthenticationBundle\Model\KuleuvenUser;
6
use Kuleuven\AuthenticationBundle\Security\ShibbolethAuthenticationListener;
7
use Kuleuven\AuthenticationBundle\Service\FirewallHelper;
8
use Kuleuven\AuthenticationBundle\Service\ShibbolethServiceProvider;
9
use Kuleuven\AuthenticationBundle\Security\KuleuvenUserToken;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
13
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
14
use Symfony\Component\HttpKernel\KernelInterface;
15
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
16
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
17
use Symfony\Component\Security\Core\Role\SwitchUserRole;
18
use Symfony\Component\Security\Core\User\UserInterface;
19
20
class AuthenticationDataCollector extends DataCollector implements DataCollectorInterface
21
{
22
    /**
23
     * @var ShibbolethServiceProvider
24
     */
25
    protected $shibbolethServiceProvider;
26
27
    /**
28
     * @var FirewallHelper
29
     */
30
    protected $firewallHelper;
31
32
    /**
33
     * @var null|TokenStorageInterface
34
     */
35
    protected $tokenStorage;
36
37
    /**
38
     * @var KernelInterface
39
     */
40
    protected $kernel;
41
42
    /**
43
     * @param ShibbolethServiceProvider  $shibbolethServiceProvider
44
     * @param FirewallHelper             $firewallHelper
45
     * @param TokenStorageInterface|null $tokenStorage
46
     * @param KernelInterface            $kernel
47
     */
48
    public function __construct(ShibbolethServiceProvider $shibbolethServiceProvider, FirewallHelper $firewallHelper, KernelInterface $kernel, TokenStorageInterface $tokenStorage = null)
49
    {
50
        $this->shibbolethServiceProvider = $shibbolethServiceProvider;
51
        $this->firewallHelper = $firewallHelper;
52
        $this->tokenStorage = $tokenStorage;
53
        $this->kernel = $kernel;
54
        $this->data = [];
55
    }
56
57
    /**
58
     * @return bool
59
     */
60
    protected function isTokenStorageEnabled()
61
    {
62
        return null !== $this->tokenStorage;
63
    }
64
65
    /**
66
     * @return TokenInterface
67
     */
68
    protected function findToken()
69
    {
70
        if (!$this->isTokenStorageEnabled()) {
71
            return null;
72
        }
73
        return $this->tokenStorage->getToken();
74
    }
75
76
    /**
77
     * @return null|KuleuvenUserToken
78
     */
79
    protected function findSourceToken()
80
    {
81
        $token = $this->findToken();
82
        if (null === $token) {
83
            return null;
84
        }
85
        $roles = $token->getRoles();
86
        foreach ($roles as $role) {
87
            if ($role instanceof SwitchUserRole) {
88
                return $role->getSource();
89
            }
90
        }
91
        return null;
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97
    public function collect(Request $request, Response $response, \Exception $exception = null)
98
    {
99
        $image = base64_encode(file_get_contents($this->kernel->locateResource('@KuleuvenAuthenticationBundle/Resources/images/shibboleth.png')));
100
        $this->data = array_replace($this->data, [
101
            'image'      => $image,
102
            'shibboleth' => [
103
                'username'                   => $this->shibbolethServiceProvider->getUsername(),
104
                'authenticated'              => $this->shibbolethServiceProvider->isAuthenticated(),
105
                'handlerPath'                => $this->shibbolethServiceProvider->getHandlerPath(),
106
                'statusPath'                 => $this->shibbolethServiceProvider->getStatusPath(),
107
                'sessionLoginPath'           => $this->shibbolethServiceProvider->getSessionLoginPath(),
108
                'sessionLogoutPath'          => $this->shibbolethServiceProvider->getSessionLogoutPath(),
109
                'sessionOverviewPath'        => $this->shibbolethServiceProvider->getSessionOverviewPath(),
110
                'handlerUrl'                 => $this->shibbolethServiceProvider->getHandlerUrl(),
111
                'statusUrl'                  => $this->shibbolethServiceProvider->getStatusUrl(),
112
                'loginUrl'                   => $this->shibbolethServiceProvider->getLoginUrl(),
113
                'logoutUrl'                  => $this->shibbolethServiceProvider->getLogoutUrl(),
114
                'overviewUrl'                => $this->shibbolethServiceProvider->getOverviewUrl(),
115
                'reachable'                  => $this->shibbolethServiceProvider->isReachable(),
116
                'securedHandler'             => $this->shibbolethServiceProvider->isSecuredHandler(),
117
                'usernameAttribute'          => $this->shibbolethServiceProvider->getUsernameAttribute(),
118
                'authenticatedAttribute'     => $this->shibbolethServiceProvider->getAuthenticatedAttribute(),
119
                'logoutUrlAttribute'         => $this->shibbolethServiceProvider->getLogoutUrlAttribute(),
120
                'authenticationRequirements' => $this->shibbolethServiceProvider->getAuthenticationRequirements(),
121
                'defaultCharset'             => $this->shibbolethServiceProvider->getDefaultCharset(),
122
                'attributes'                 => $this->shibbolethServiceProvider->getAttributes(),
123
            ],
124
        ]);
125
126
        $token = $this->findToken();
127
        $user = (!empty($token) ? $token->getUser() : null);
128
        $sourceToken = $this->findSourceToken();
129
        $source = (!empty($sourceToken) ? $sourceToken->getUser() : null);
130
131
        $shibbolethUser = (empty($source) ? $user : $source);
132
        $impersonatedUser = (empty($source) ? null : $user);
133
134
        $this->data = array_replace($this->data, [
135
            'enabled'                   => $this->firewallHelper->isProtectedBy(ShibbolethAuthenticationListener::class),
136
            'authenticated'             => $this->shibbolethServiceProvider->isAuthenticated(),
137
            'user'                      => ($shibbolethUser instanceof UserInterface ? $shibbolethUser->getUsername() : $shibbolethUser),
138
            'display_name'              => ($shibbolethUser instanceof KuleuvenUser ? $shibbolethUser->getDisplayName() : ''),
139
            'affiliation'               => ($shibbolethUser instanceof KuleuvenUser ? $shibbolethUser->getAffiliation() : ''),
140
            'attributes'                => ($shibbolethUser instanceof KuleuvenUser ? $shibbolethUser->getAttributes() : []),
141
            'token_class'               => (!empty($token) ? get_class($token) : ''),
142
            'impersonated_user'         => ($impersonatedUser instanceof UserInterface ? $impersonatedUser->getUsername() : $impersonatedUser),
143
            'impersonated_display_name' => ($impersonatedUser instanceof KuleuvenUser ? $impersonatedUser->getDisplayName() : ''),
144
            'impersonated_affiliation'  => ($impersonatedUser instanceof KuleuvenUser ? $impersonatedUser->getAffiliation() : ''),
145
            'impersonated_attributes'   => ($impersonatedUser instanceof KuleuvenUser ? $impersonatedUser->getAttributes() : []),
146
            'impersonated_token_class'  => (!empty($sourceToken) ? get_class($sourceToken) : ''),
147
        ]);
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    public function getImage()
154
    {
155
        return $this->data['image'];
156
    }
157
158
    /**
159
     * @return array
160
     */
161
    public function getShibboleth()
162
    {
163
        return $this->data['shibboleth'];
164
    }
165
166
    /**
167
     * Checks if security is enabled.
168
     *
169
     * @return bool true if security is enabled, false otherwise
170
     */
171
    public function isEnabled()
172
    {
173
        return $this->data['enabled'];
174
    }
175
176
    /**
177
     * Checks if the user is authenticated or not.
178
     *
179
     * @return bool true if the user is authenticated, false otherwise
180
     */
181
    public function isAuthenticated()
182
    {
183
        return $this->data['authenticated'];
184
    }
185
186
    /**
187
     * Gets the user.
188
     *
189
     * @return string The user
190
     */
191
    public function getUser()
192
    {
193
        return $this->data['user'];
194
    }
195
196
    /**
197
     * Gets the display name.
198
     *
199
     * @return string The display name
200
     */
201
    public function getDisplayName()
202
    {
203
        return $this->data['display_name'];
204
    }
205
206
    /**
207
     * Gets the unscoped affiliation.
208
     *
209
     * @return string The unscoped affiliation
210
     */
211
    public function getAffiliation()
212
    {
213
        return $this->data['affiliation'];
214
    }
215
216
    /**
217
     * Gets all the attributes.
218
     *
219
     * @return string The attributes
220
     */
221
    public function getAttributes()
222
    {
223
        return $this->data['attributes'];
224
    }
225
226
    /**
227
     * Get the class name of the security token.
228
     *
229
     * @return string The token
230
     */
231
    public function getTokenClass()
232
    {
233
        return $this->data['token_class'];
234
    }
235
236
    /**
237
     * Gets the source user.
238
     *
239
     * @return string The source user
240
     */
241
    public function getImpersonatedUser()
242
    {
243
        return $this->data['impersonated_user'];
244
    }
245
246
    /**
247
     * Gets the display name.
248
     *
249
     * @return string The display name
250
     */
251
    public function getImpersonatedDisplayName()
252
    {
253
        return $this->data['impersonated_display_name'];
254
    }
255
256
    /**
257
     * Gets the unscoped affiliation.
258
     *
259
     * @return string The unscoped affiliation
260
     */
261
    public function getImpersonatedAffiliation()
262
    {
263
        return $this->data['impersonated_affiliation'];
264
    }
265
266
    /**
267
     * Gets all the attributes.
268
     *
269
     * @return string The attributes
270
     */
271
    public function getImpersonatedAttributes()
272
    {
273
        return $this->data['impersonated_attributes'];
274
    }
275
276
    /**
277
     * Get the class name of the source security token.
278
     *
279
     * @return string The source token
280
     */
281
    public function getImpersonatedTokenClass()
282
    {
283
        return $this->data['impersonated_token_class'];
284
    }
285
286
    /**
287
     * @inheritdoc
288
     */
289
    public function getName()
290
    {
291
        return 'kuleuven_authentication';
292
    }
293
}
294