Completed
Pull Request — master (#5)
by Daniel
02:14
created

SessionAuthenticator::getUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CodeCloud\Bundle\ShopifyBundle\Security;
4
5
use CodeCloud\Bundle\ShopifyBundle\EventListener\SessionAuthenticationListener;
6
use Symfony\Component\HttpFoundation\RedirectResponse;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
10
use Symfony\Component\Security\Core\User\UserInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Security\Core\User\UserInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...tractGuardAuthenticator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...on\Token\TokenInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Symfony\Component\Security\Core\Exception\AuthenticationException;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...AuthenticationException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Symfony\Component\Security\Core\User\UserProviderInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...r\UserProviderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
/**
17
 * Authenticates users via session parameter.
18
 */
19
class SessionAuthenticator extends AbstractGuardAuthenticator
20
{
21
    /**
22
     * @var UrlGeneratorInterface
23
     */
24
    private $urlGenerator;
25
26
    /**
27
     * @param UrlGeneratorInterface $urlGenerator
28
     */
29
    public function __construct(UrlGeneratorInterface $urlGenerator)
30
    {
31
        $this->urlGenerator = $urlGenerator;
32
    }
33
34
    public function supports(Request $request)
35
    {
36
        if (!$session = $request->getSession()) {
37
            return false;
38
        }
39
40
        if (!$session->has(SessionAuthenticationListener::SESSION_PARAMETER)) {
41
            return false;
42
        }
43
44
        return true;
45
    }
46
47
    public function getCredentials(Request $request)
48
    {
49
        return [
50
            'shop' => $request->getSession()->get(SessionAuthenticationListener::SESSION_PARAMETER),
51
        ];
52
    }
53
54
    public function getUser($credentials, UserProviderInterface $userProvider)
55
    {
56
        return $userProvider->loadUserByUsername($credentials['shop']);
57
    }
58
59
    public function checkCredentials($credentials, UserInterface $user)
60
    {
61
        return true;
62
    }
63
64
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
65
    {
66
    }
67
68
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
69
    {
70
    }
71
72
    public function supportsRememberMe()
73
    {
74
        return false;
75
    }
76
77
    public function start(Request $request, AuthenticationException $authException = null)
78
    {
79
        if (!$request->get('shop')) {
80
            return new Response('Your session has expired. Please access the app via Shopify Admin again.');
81
        }
82
83
        return new RedirectResponse(
84
            $this->urlGenerator->generate('codecloud_shopify_auth', [
85
                'shop' => $request->get('shop'),
86
            ])
87
        );
88
    }
89
}
90