|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bgy\OAuth2ServerBundle\Security; |
|
4
|
|
|
|
|
5
|
|
|
use Bgy\OAuth2\AccessToken; |
|
6
|
|
|
use Bgy\OAuth2\Storage\AccessTokenNotFound; |
|
7
|
|
|
use Bgy\OAuth2\Storage\AccessTokenStorage; |
|
8
|
|
|
use Bgy\OAuth2ServerBundle\Security\Utils\BearerFetcher; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
11
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
12
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
|
13
|
|
|
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
|
14
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
15
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
|
16
|
|
|
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Boris Guéry <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class OAuth2Authenticator extends AbstractGuardAuthenticator |
|
22
|
|
|
{ |
|
23
|
|
|
private $accessTokenStorage; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(AccessTokenStorage $accessTokenStorage) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->accessTokenStorage = $accessTokenStorage; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function start(Request $request, AuthenticationException $authException = null) |
|
31
|
|
|
{ |
|
32
|
|
|
// TODO: Implement start() method. |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function getCredentials(Request $request) |
|
36
|
|
|
{ |
|
37
|
|
|
if (null !== $rawAccessToken = BearerFetcher::fromRequest($request)) { |
|
38
|
|
|
|
|
39
|
|
|
try { |
|
40
|
|
|
|
|
41
|
|
|
$accessToken = $this->accessTokenStorage->findByToken($rawAccessToken); |
|
42
|
|
|
|
|
43
|
|
|
return $accessToken; |
|
44
|
|
|
|
|
45
|
|
|
} catch (AccessTokenNotFound $e) { |
|
46
|
|
|
// nothing to do |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return null; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getUser($credentials, UserProviderInterface $userProvider) |
|
54
|
|
|
{ |
|
55
|
|
|
/** @var AccessToken $accessToken */ |
|
56
|
|
|
$accessToken = $credentials; |
|
57
|
|
|
|
|
58
|
|
|
try { |
|
59
|
|
|
if ($userProvider->supportsClass($accessToken->getResourceOwner()->getResourceOwnerType())) { |
|
60
|
|
|
|
|
61
|
|
|
$userAccount = $userProvider->loadUserByUsername( |
|
62
|
|
|
$accessToken->getResourceOwner()->getResourceOwnerId() |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
return $userAccount; |
|
66
|
|
|
} |
|
67
|
|
|
} catch (UsernameNotFoundException $e) { |
|
68
|
|
|
// nothing to do |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return null; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function checkCredentials($credentials, UserInterface $user) |
|
75
|
|
|
{ |
|
76
|
|
|
/** @var AccessToken $accessToken */ |
|
77
|
|
|
$accessToken = $credentials; |
|
78
|
|
|
|
|
79
|
|
|
return !$accessToken->isExpired(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception) |
|
83
|
|
|
{ |
|
84
|
|
|
// TODO: Implement onAuthenticationFailure() method. |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) |
|
88
|
|
|
{ |
|
89
|
|
|
// TODO: Implement onAuthenticationSuccess() method. |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function supportsRememberMe() |
|
93
|
|
|
{ |
|
94
|
|
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|