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
|
|
|
return true; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @see GuardAuthenticatorInterface::onAuthenticationSuccess() |
98
|
|
|
*/ |
99
|
|
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) |
100
|
|
|
{ |
101
|
|
|
$request->query->remove('access_token'); |
102
|
|
|
$request->headers->remove('Authorization'); |
103
|
|
|
$request->attributes->set('access_token', $this->currentAccessToken); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @see GuardAuthenticatorInterface::onAuthenticationFailure() |
108
|
|
|
*/ |
109
|
|
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception) |
110
|
|
|
{ |
111
|
|
|
return new JsonResponse( |
112
|
|
|
array('message' => 'OAuth authentication required.'), |
113
|
|
|
403 |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @see GuardAuthenticatorInterface::onAuthenticationFailure() |
119
|
|
|
*/ |
120
|
|
|
public function start(Request $request, AuthenticationException $authException = null) |
121
|
|
|
{ |
122
|
|
|
return !$authException ? null : new JsonResponse( |
123
|
|
|
array('message' => 'Invalid OAuth credentials.'), |
124
|
|
|
401 |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @see GuardAuthenticatorInterface::supportsRememberMe() |
130
|
|
|
*/ |
131
|
|
|
public function supportsRememberMe() |
132
|
|
|
{ |
133
|
|
|
return false; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
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.