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) { |
|
|
|
|
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); |
|
|
|
|
94
|
|
|
|
95
|
|
|
$token = new SSOToken(); |
96
|
|
|
$token->setAccessToken($auth0Token->getAccessToken()) |
97
|
|
|
->setExpiresAt($auth0Token->getExpiresAt()); |
98
|
|
|
|
99
|
|
|
return $this->authenticationManager->authenticate($token); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
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.