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

SSOListener   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 1
cbo 12
dl 0
loc 83
ccs 0
cts 44
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setCsrfTokenManager() 0 4 1
A setAuthenticationApi() 0 4 1
A setCallbackPath() 0 6 1
C attemptAuthentication() 0 40 7
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