GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 57b2f6...3e832a )
by Luis Ramón
04:08
created

FormAuthenticator::onAuthenticationFailure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
/*
3
  ÁTICA - Aplicación web para la gestión documental de centros educativos
4
5
  Copyright (C) 2015-2017: Luis Ramón López López
6
7
  This program is free software: you can redistribute it and/or modify
8
  it under the terms of the GNU Affero General Public License as published by
9
  the Free Software Foundation, either version 3 of the License, or
10
  (at your option) any later version.
11
12
  This program is distributed in the hope that it will be useful,
13
  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
  GNU Affero General Public License for more details.
16
17
  You should have received a copy of the GNU Affero General Public License
18
  along with this program.  If not, see [http://www.gnu.org/licenses/].
19
*/
20
21
namespace AppBundle\Security;
22
23
use AppBundle\Entity\User;
24
use AppBundle\Service\SenecaAuthenticatorService;
25
use Doctrine\Common\Persistence\ManagerRegistry;
26
use Symfony\Component\HttpFoundation\RedirectResponse;
27
use Symfony\Component\HttpFoundation\Request;
28
use Symfony\Component\Routing\RouterInterface;
29
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
30
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
31
use Symfony\Component\Security\Core\Exception\AuthenticationException;
32
use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
33
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
34
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
35
use Symfony\Component\Security\Core\Security;
36
use Symfony\Component\Security\Core\User\UserInterface;
37
use Symfony\Component\Security\Core\User\UserProviderInterface;
38
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
39
40
class FormAuthenticator extends AbstractGuardAuthenticator
41
{
42
    /**
43
     * @var UserPasswordEncoderInterface
44
     */
45
    private $encoder;
46
47
    /**
48
     * @var RouterInterface
49
     */
50
    private $router;
51
52
    /**
53
     * @var SenecaAuthenticatorService
54
     */
55
    private $senecaAuthenticator;
56
57
    /**
58
     * @var ManagerRegistry
59
     */
60
    private $managerRegistry;
61
62
    /**
63
     * Constructor
64
     */
65
    public function __construct(RouterInterface $router, UserPasswordEncoderInterface $encoder, SenecaAuthenticatorService $senecaAuthenticator, ManagerRegistry $managerRegistry) {
66
        $this->router = $router;
67
        $this->encoder = $encoder;
68
        $this->senecaAuthenticator = $senecaAuthenticator;
69
        $this->managerRegistry = $managerRegistry;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getCredentials(Request $request)
76
    {
77
        $username = $request->request->get('_username');
78
        $session = $request->getSession();
79
80
        if ($request->attributes->get('_route') !== 'login_check' || !$request->isMethod('POST') || !$session) {
81
            return null;
82
        }
83
84
        $session->set(Security::LAST_USERNAME, $username);
85
86
        return array(
87
            'username' => $username,
88
            'password' => $request->request->get('_password'),
89
        );
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function getUser($credentials, UserProviderInterface $userProvider)
96
    {
97
        try {
98
            return $userProvider->loadUserByUsername($credentials['username']);
99
        } catch (UsernameNotFoundException $e) {
100
            throw new BadCredentialsException();
101
        }
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function checkCredentials($credentials, UserInterface $user)
108
    {
109
        if (!$user instanceof User) {
110
            throw new AuthenticationServiceException();
111
        }
112
113
        $plainPassword = $credentials['password'];
114
115
        // ¿Comprobación de contraseña desde Séneca?
116
        if ($user->hasExternalLogin() && $user->getAllowExternalLogin() && $user->getLoginUsername()) {
117
            $result = $this->senecaAuthenticator->checkUserCredentials($user->getLoginUsername(), $plainPassword);
118
119
            if (true === $result) {
120
                // contraseña correcta, actualizar en local por si perdemos la conectividad
121
                if (false === $this->encoder->isPasswordValid($user, $plainPassword)) {
122
                    $user->setPassword($this->encoder->encodePassword($user, $plainPassword));
123
                    $em = $this->managerRegistry->getManagerForClass(User::class);
124
                    if ($em) {
125
                        $em->flush();
126
                    }
127
                }
128
                return true;
129
            }
130
131
            if (false === $result) {
132
                return false;
133
            }
134
135
            // si no es ni "true" ni "false" es que no se ha podido contactar con Séneca, intentar en local
136
        }
137
138
        // comprobación local
139
        if (false === $this->encoder->isPasswordValid($user, $plainPassword)) {
140
            throw new BadCredentialsException();
141
        }
142
143
        return true;
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
150
    {
151
        $targetPath = $request->getSession()->get('_security.'.$providerKey.'.target_path');
152
        if (!$targetPath) {
153
            $targetPath = $this->router->generate('frontpage');
154
        }
155
        return new RedirectResponse($targetPath);
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
162
    {
163
        $request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);
164
        $url = $this->router->generate('login');
165
        return new RedirectResponse($url);
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    public function start(Request $request, AuthenticationException $authException = null)
172
    {
173
        $url = $this->router->generate('login');
174
        return new RedirectResponse($url);
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function supportsRememberMe()
181
    {
182
        return false;
183
    }
184
}
185