Completed
Push — master ( de7f23...e1bc7d )
by Tobias
05:32
created

SSOListener::attemptAuthentication()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 40
ccs 0
cts 31
cp 0
rs 6.7272
cc 7
eloc 23
nc 8
nop 1
crap 56
1
<?php
2
3
namespace Happyr\Auth0Bundle\Security\Firewall;
4
5
use Auth0\SDK\API\Authentication;
6
use Auth0\SDK\Exception;
7
use Auth0\SDK\Exception\CoreException;
8
use Happyr\Auth0Bundle\Model\Authorization\Token\Token;
9
use Happyr\Auth0Bundle\Security\Authentication\Token\SSOToken;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\Security\Core\Exception\AuthenticationException;
12
use Symfony\Component\Security\Csrf\CsrfToken;
13
use Symfony\Component\Security\Csrf\CsrfTokenManager;
14
use Symfony\Component\Security\Http\Firewall\AbstractAuthenticationListener;
15
16
/**
17
 * @author Tobias Nyholm <[email protected]>
18
 */
19
class SSOListener extends AbstractAuthenticationListener
20
{
21
    /**
22
     * @var Authentication
23
     */
24
    private $authenticationApi;
25
26
    /**
27
     * @var string
28
     */
29
    private $callbackPath;
30
31
    /**
32
     * @var CsrfTokenManager
33
     */
34
    private $csrfTokenManager;
35
36
    public function setCsrfTokenManager(CsrfTokenManager $csrfTokenManager)
37
    {
38
        $this->csrfTokenManager = $csrfTokenManager;
39
    }
40
41
    /**
42
     * @param Authentication $authenticationApi
43
     */
44
    public function setAuthenticationApi($authenticationApi)
45
    {
46
        $this->authenticationApi = $authenticationApi;
47
    }
48
49
    /**
50
     * @param string $callbackPath
51
     *
52
     * @return SSOListener
53
     */
54
    public function setCallbackPath($callbackPath)
55
    {
56
        $this->callbackPath = $callbackPath;
57
58
        return $this;
59
    }
60
61
    protected function attemptAuthentication(Request $request)
62
    {
63
        if (null === $code = $request->query->get('code')) {
64
            throw new AuthenticationException('No oauth code in the request.');
65
        }
66
67
        if (null === $state = $request->query->get('state')) {
68
            throw new AuthenticationException('No state in the request.');
69
        }
70
71
        if (!$this->csrfTokenManager->isTokenValid(new CsrfToken('auth0-sso', $state))) {
72
            throw new AuthenticationException('Invalid CSRF token');
73
        }
74
75
76
        try {
77
            $redirectUri = $this->httpUtils->generateUri($request, $this->callbackPath);
78
            $tokenStruct = $this->authenticationApi->codeExchange($code, $redirectUri);
79
        } catch (Exception\ForbiddenException $e) {
0 ignored issues
show
Bug introduced by
The class Auth0\SDK\Exception\ForbiddenException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
80
            throw new AuthenticationException($e->getMessage(), $e->getCode(), $e);
81
        }
82
83
        // TODO, remove this legacy code
84
        if (isset($tokenStruct['error'])) {
85
            switch ($tokenStruct['error']) {
86
                case 'invalid_grant':
87
                    throw new AuthenticationException($tokenStruct['error_description']);
88
                default:
89
                    throw new CoreException($tokenStruct['error_description']);
90
            }
91
        }
92
93
        $auth0Token = Token::create($tokenStruct);
0 ignored issues
show
Bug introduced by
It seems like $tokenStruct defined by $this->authenticationApi...ge($code, $redirectUri) on line 78 can also be of type string; however, Happyr\Auth0Bundle\Model...n\Token\Token::create() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
94
95
        $token = new SSOToken();
96
        $token->setAccessToken($auth0Token->getAccessToken())
97
            ->setExpiresAt($auth0Token->getExpiresAt());
98
99
        return $this->authenticationManager->authenticate($token);
100
    }
101
}
102