1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Zemit Framework. |
4
|
|
|
* |
5
|
|
|
* (c) Zemit Team <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Zemit\Modules\Oauth2\Controllers; |
12
|
|
|
|
13
|
|
|
use League\OAuth2\Client\Grant\RefreshToken; |
14
|
|
|
use League\OAuth2\Client\Provider\Facebook; |
15
|
|
|
use League\OAuth2\Client\Provider\ResourceOwnerInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class FacebookController |
19
|
|
|
* |
20
|
|
|
* @property Facebook $oauth2Facebook |
21
|
|
|
* @package Zemit\Modules\Oauth2\Controllers |
22
|
|
|
*/ |
23
|
|
|
class FacebookController extends AbstractController |
24
|
|
|
{ |
25
|
|
|
const DEFAULT_SCOPE = 'email'; |
26
|
|
|
|
27
|
|
|
public string $sessionKey = 'oauth2-facebook-state'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Redirect to Authorization Url |
31
|
|
|
* |
32
|
|
|
* @param null $scope |
|
|
|
|
33
|
|
|
* |
34
|
|
|
* @return \Phalcon\Http\ResponseInterface |
35
|
|
|
*/ |
36
|
|
|
public function authorizationUrlAction($scope = null) |
37
|
|
|
{ |
38
|
|
|
$redirectUrl = $this->oauth2Facebook->getAuthorizationUrl([ |
39
|
|
|
'scope' => explode(',', $scope ?: $this->request->get('scope', 'string', self::DEFAULT_SCOPE)) |
|
|
|
|
40
|
|
|
]); |
41
|
|
|
$this->session->set($this->sessionKey, $this->oauth2Facebook->getState()); |
42
|
|
|
return $this->response->redirect($redirectUrl); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* |
47
|
|
|
*/ |
48
|
|
|
public function callbackAction() { |
49
|
|
|
|
50
|
|
|
if ($this->validateState()) { |
51
|
|
|
$accessToken = $this->getAccessToken(); |
52
|
|
|
$longLivedAccessToken = $this->getLongLivedAccessToken($accessToken); |
|
|
|
|
53
|
|
|
$resourceOwner = $this->getResourceOwner($accessToken); |
|
|
|
|
54
|
|
|
|
55
|
|
|
// $resourceOwner->toArray(); |
56
|
|
|
// $session = $this->identity->getSession(); |
57
|
|
|
// $session->setUserId(); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Validate State |
63
|
|
|
* |
64
|
|
|
* @param null $state |
|
|
|
|
65
|
|
|
* |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public function validateState($state = null) |
69
|
|
|
{ |
70
|
|
|
$state ??= $this->request->get('state', 'string'); |
71
|
|
|
|
72
|
|
|
if (empty($state) || !$this->session->has($this->sessionKey)) { |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $state === $this->session->get($this->sessionKey); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get Access Token |
81
|
|
|
* |
82
|
|
|
* @param null $code |
|
|
|
|
83
|
|
|
* |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
|
|
public function getAccessToken($code = null) |
87
|
|
|
{ |
88
|
|
|
$code ??= $this->request->get('code', 'string'); |
89
|
|
|
return $this->oauth2Facebook->getAccessToken('authorization_code', ['code' => $code]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param null $shortLivedAccessToken |
|
|
|
|
94
|
|
|
* |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
public function getLongLivedAccessToken($shortLivedAccessToken = null) |
98
|
|
|
{ |
99
|
|
|
return $this->oauth2Facebook->getLongLivedAccessToken($shortLivedAccessToken); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Refresh Token |
104
|
|
|
* |
105
|
|
|
* @param null $code |
|
|
|
|
106
|
|
|
* |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
|
|
public function refreshToken($refreshToken = null) |
110
|
|
|
{ |
111
|
|
|
$refreshToken ??= $this->request->get('refreshToken', 'string'); |
112
|
|
|
return $this->oauth2Facebook->getAccessToken(new RefreshToken(), ['code' => $refreshToken]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param null $token |
|
|
|
|
117
|
|
|
* |
118
|
|
|
* @return ResourceOwnerInterface |
119
|
|
|
*/ |
120
|
|
|
public function getResourceOwner($token = null) |
121
|
|
|
{ |
122
|
|
|
$token ??= $this->getAccessToken(); |
123
|
|
|
return $this->oauth2Facebook->getResourceOwner($token); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|