Passed
Push — master ( ef89ee...96f6f3 )
by Tobias
02:56
created

Auth0EntryPoint::start()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 2
dl 0
loc 15
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Happyr\Auth0Bundle\Security;
4
5
use Symfony\Component\HttpFoundation\RedirectResponse;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\Security\Core\Exception\AuthenticationException;
8
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
9
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
10
use Symfony\Component\Security\Http\HttpUtils;
11
12
/**
13
 * @author Tobias Nyholm <[email protected]>
14
 */
15
class Auth0EntryPoint implements AuthenticationEntryPointInterface
16
{
17
    private $csrfTokenManager;
18
    private $httpUtils;
19
    private $auth0ClientId;
20
    private $auth0Domain;
21
    private $scope;
22
    private $callbackRoute;
23
24
    public function __construct(
25
        CsrfTokenManagerInterface $csrfTokenManager,
26
        HttpUtils $httpUtils,
27
        string $auth0ClientId,
28
        string $auth0Domain,
29
        string $scope,
30
        string $callbackRoute
31
    ) {
32
        $this->csrfTokenManager = $csrfTokenManager;
33
        $this->httpUtils = $httpUtils;
34
        $this->auth0ClientId = $auth0ClientId;
35
        $this->auth0Domain = $auth0Domain;
36
        $this->scope = $scope;
37
        $this->callbackRoute = $callbackRoute;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function start(Request $request, AuthenticationException $authException = null)
44
    {
45
        $csrfToken = $this->csrfTokenManager->getToken('auth0-sso');
46
47
        $query = [
48
            'response_type' => 'code',
49
            'client_id' => $this->auth0ClientId,
50
            'redirect_uri' => $this->httpUtils->generateUri($request, $this->callbackRoute),
51
            'state' => $csrfToken->getValue(),
52
            'scope' => $this->scope,
53
            // https://auth0.com/docs/universal-login/i18n
54
            'ui_locales' => $request->getLocale(),
55
        ];
56
57
        return new RedirectResponse(sprintf('https://%s/authorize?%s', $this->auth0Domain, http_build_query($query)));
58
    }
59
}
60