1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Eole\Sandstone\OAuth2\Security\Authentication\Provider; |
4
|
|
|
|
5
|
|
|
use League\OAuth2\Server\Exception\AccessDeniedException; |
6
|
|
|
use League\OAuth2\Server\ResourceServer; |
7
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
8
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
9
|
|
|
use Symfony\Component\Security\Core\User\UserCheckerInterface; |
10
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
11
|
|
|
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface; |
12
|
|
|
use Eole\Sandstone\OAuth2\Security\Exception\OAuth2AuthenticationException; |
13
|
|
|
use Eole\Sandstone\OAuth2\Security\Authentication\Token\OAuth2Token; |
14
|
|
|
|
15
|
|
|
class OAuth2Provider implements AuthenticationProviderInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var UserProviderInterface |
19
|
|
|
*/ |
20
|
|
|
private $userProvider; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var UserCheckerInterface |
24
|
|
|
*/ |
25
|
|
|
private $userChecker; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ResourceServer |
29
|
|
|
*/ |
30
|
|
|
private $resourceServer; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param UserProviderInterface $userProvider |
34
|
|
|
* @param UserCheckerInterface $userChecker |
35
|
|
|
* @param ResourceServer $resourceServer |
36
|
|
|
*/ |
37
|
|
|
public function __construct( |
38
|
|
|
UserProviderInterface $userProvider, |
39
|
|
|
UserCheckerInterface $userChecker, |
40
|
|
|
ResourceServer $resourceServer |
41
|
|
|
) { |
42
|
|
|
$this->userProvider = $userProvider; |
43
|
|
|
$this->userChecker = $userChecker; |
44
|
|
|
$this->resourceServer = $resourceServer; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@InheritDoc} |
49
|
|
|
*/ |
50
|
|
|
public function authenticate(TokenInterface $token) |
51
|
|
|
{ |
52
|
|
|
try { |
53
|
|
|
$this->resourceServer->isValidRequest(true, $token->getTokenData()); |
|
|
|
|
54
|
|
|
} catch (AccessDeniedException $e) { |
|
|
|
|
55
|
|
|
throw new OAuth2AuthenticationException('OAuth2 token expired or invalid.'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$username = $this->resourceServer->getAccessToken()->getSession()->getId(); |
|
|
|
|
59
|
|
|
$user = $this->userProvider->loadUserByUsername($username); |
60
|
|
|
$isUser = $user instanceof UserInterface; |
61
|
|
|
|
62
|
|
|
if (!$isUser) { |
63
|
|
|
throw new OAuth2AuthenticationException('User not found.'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->userChecker->checkPreAuth($user); |
67
|
|
|
$this->userChecker->checkPostAuth($user); |
68
|
|
|
|
69
|
|
|
$authenticatedToken = new OAuth2Token($token->getTokenData()); |
|
|
|
|
70
|
|
|
$authenticatedToken->setUser($user); |
71
|
|
|
|
72
|
|
|
return $authenticatedToken; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@InheritDoc} |
77
|
|
|
*/ |
78
|
|
|
public function supports(TokenInterface $token) |
79
|
|
|
{ |
80
|
|
|
return $token instanceof OAuth2Token; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: