|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Majora\Bundle\OAuthServerBundle\Security; |
|
4
|
|
|
|
|
5
|
|
|
use Majora\Component\OAuth\Exception\InvalidAccessTokenException; |
|
6
|
|
|
use Majora\Component\OAuth\Server\Server as OAuthServer; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
10
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
|
11
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
12
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
|
13
|
|
|
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* AccessToken authenticator class using Guard Symfony component and OAuth server. |
|
17
|
|
|
* |
|
18
|
|
|
* @link https://knpuniversity.com/screencast/guard/api-token |
|
19
|
|
|
*/ |
|
20
|
|
|
class AccessTokenAuthenticator extends AbstractGuardAuthenticator |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var OAuthServer |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $oauthServer; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var AccessTokenInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $currentAccessToken; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Construct. |
|
34
|
|
|
* |
|
35
|
|
|
* @param OAuthServer $oauthServer |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(OAuthServer $oauthServer) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->oauthServer = $oauthServer; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @see GuardAuthenticatorInterface::getCredentials() |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getCredentials(Request $request) |
|
46
|
|
|
{ |
|
47
|
|
|
switch (true) { |
|
48
|
|
|
|
|
49
|
|
|
// token through query params |
|
50
|
|
|
case $request->query->has('access_token') : |
|
|
|
|
|
|
51
|
|
|
$hash = $request->query->get('access_token'); |
|
52
|
|
|
break; |
|
53
|
|
|
|
|
54
|
|
|
// token through headers |
|
55
|
|
|
case $request->headers->has('Authorization') : |
|
|
|
|
|
|
56
|
|
|
if (!preg_match('#^Bearer ([\w]+)$#', $request->headers->get('Authorization'), $matches)) { |
|
57
|
|
|
break; // bad Authorization format |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$hash = $matches[1]; |
|
61
|
|
|
break; |
|
62
|
|
|
|
|
63
|
|
|
// otherwise auth failed |
|
64
|
|
|
default: |
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
try { |
|
69
|
|
|
return $this->currentAccessToken = $this->oauthServer->check($hash); |
|
|
|
|
|
|
70
|
|
|
} catch (InvalidAccessTokenException $e) { |
|
71
|
|
|
// log there |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @see GuardAuthenticatorInterface::getUser() |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getUser($accessToken, UserProviderInterface $userProvider) |
|
79
|
|
|
{ |
|
80
|
|
|
return $accessToken->getAccount(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @see GuardAuthenticatorInterface::checkCredentials() |
|
85
|
|
|
*/ |
|
86
|
|
|
public function checkCredentials($accessToken, UserInterface $user) |
|
87
|
|
|
{ |
|
88
|
|
|
// test here if access token is valid (expiration date etc...) |
|
89
|
|
|
// or api limit isnt crossed |
|
90
|
|
|
// or anonymous allowed |
|
91
|
|
|
// etc... |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @see GuardAuthenticatorInterface::onAuthenticationSuccess() |
|
96
|
|
|
*/ |
|
97
|
|
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) |
|
98
|
|
|
{ |
|
99
|
|
|
$request->query->remove('access_token'); |
|
100
|
|
|
$request->headers->remove('Authorization'); |
|
101
|
|
|
$request->attributes->set('access_token', $this->currentAccessToken); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @see GuardAuthenticatorInterface::onAuthenticationFailure() |
|
106
|
|
|
*/ |
|
107
|
|
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception) |
|
108
|
|
|
{ |
|
109
|
|
|
return new JsonResponse( |
|
110
|
|
|
array('message' => 'OAuth authentication required.'), |
|
111
|
|
|
403 |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @see GuardAuthenticatorInterface::onAuthenticationFailure() |
|
117
|
|
|
*/ |
|
118
|
|
|
public function start(Request $request, AuthenticationException $authException = null) |
|
119
|
|
|
{ |
|
120
|
|
|
// if anonymous granted |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @see GuardAuthenticatorInterface::supportsRememberMe() |
|
125
|
|
|
*/ |
|
126
|
|
|
public function supportsRememberMe() |
|
127
|
|
|
{ |
|
128
|
|
|
return false; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.