1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Svycka\SocialUser\OAuth2\GrantType; |
4
|
|
|
|
5
|
|
|
use OAuth2\GrantType\GrantTypeInterface; |
6
|
|
|
use OAuth2\RequestInterface; |
7
|
|
|
use OAuth2\ResponseInterface; |
8
|
|
|
use OAuth2\ResponseType\AccessTokenInterface; |
9
|
|
|
use Svycka\SocialUser\UserProfileInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Vytautas Stankus <[email protected]> |
13
|
|
|
* @license MIT |
14
|
|
|
*/ |
15
|
|
|
abstract class AbstractSocialGrantType implements GrantTypeInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $userInfo; |
21
|
|
|
|
22
|
13 |
|
public function validateRequest(RequestInterface $request, ResponseInterface $response) |
23
|
|
|
{ |
24
|
13 |
|
$token = $request->request("token"); |
25
|
|
|
|
26
|
13 |
|
if (!$token) { |
27
|
2 |
|
$response->setError(400, 'invalid_request', 'Missing parameter: "token" is required'); |
28
|
2 |
|
return null; |
29
|
|
|
} |
30
|
|
|
|
31
|
11 |
|
$socialUser = $this->getTokenInfo($token); |
32
|
|
|
|
33
|
11 |
|
if (!$socialUser) { |
34
|
6 |
|
$response->setError(401, 'invalid_grant', 'Invalid or expired token'); |
35
|
6 |
|
return null; |
36
|
|
|
} |
37
|
|
|
|
38
|
5 |
|
$user_id = $this->getLocalUser($socialUser); |
39
|
|
|
|
40
|
5 |
|
if (!$user_id) { |
|
|
|
|
41
|
2 |
|
$response->setError(401, 'invalid_grant', 'Unable to identify or create user'); |
42
|
2 |
|
return null; |
43
|
|
|
} |
44
|
|
|
|
45
|
3 |
|
$this->userInfo = [ |
46
|
3 |
|
'user_id' => $user_id |
47
|
|
|
]; |
48
|
|
|
|
49
|
3 |
|
return true; |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
public function getClientId() |
53
|
|
|
{ |
54
|
3 |
|
return null; |
55
|
|
|
} |
56
|
|
|
|
57
|
3 |
|
public function getUserId() |
58
|
|
|
{ |
59
|
3 |
|
return $this->userInfo['user_id']; |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
public function getScope() |
63
|
|
|
{ |
64
|
3 |
|
return isset($this->userInfo['scope']) ? $this->userInfo['scope'] : null; |
65
|
|
|
} |
66
|
|
|
|
67
|
3 |
|
public function createAccessToken(AccessTokenInterface $accessToken, $client_id, $user_id, $scope) |
68
|
|
|
{ |
69
|
3 |
|
return $accessToken->createAccessToken($client_id, $user_id, $scope); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $token |
74
|
|
|
* |
75
|
|
|
* @return UserProfileInterface|null |
76
|
|
|
*/ |
77
|
|
|
abstract protected function getTokenInfo($token); |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param UserProfileInterface $socialUser |
81
|
|
|
* |
82
|
|
|
* @return int|null |
83
|
|
|
*/ |
84
|
|
|
abstract protected function getLocalUser(UserProfileInterface $socialUser); |
85
|
|
|
} |
86
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: