|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Zemit Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Zemit Team <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Zemit\Modules\Oauth2\Controllers; |
|
13
|
|
|
|
|
14
|
|
|
use League\OAuth2\Client\Grant\RefreshToken; |
|
15
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
|
16
|
|
|
use League\OAuth2\Client\Provider\GenericProvider; |
|
17
|
|
|
use League\OAuth2\Client\Provider\ResourceOwnerInterface; |
|
18
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
|
19
|
|
|
use League\OAuth2\Client\Token\AccessTokenInterface; |
|
20
|
|
|
use Phalcon\Http\ResponseInterface; |
|
21
|
|
|
use Zemit\Modules\Oauth2\Controller; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @property GenericProvider $oauth2Provider |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract class AbstractController extends Controller |
|
27
|
|
|
{ |
|
28
|
|
|
public const PROVIDER_CLIENT = 'client'; |
|
29
|
|
|
public const PROVIDER_FACEBOOK = 'facebook'; |
|
30
|
|
|
public const PROVIDER_GITHUB = 'github'; |
|
31
|
|
|
public const PROVIDER_GOOGLE = 'google'; |
|
32
|
|
|
public const PROVIDER_INSTAGRAM = 'instagram'; |
|
33
|
|
|
public const PROVIDER_LINKEDIN = 'linkedin'; |
|
34
|
|
|
|
|
35
|
|
|
public string $defaultScope = 'email'; |
|
36
|
|
|
|
|
37
|
|
|
public string $providerName = self::PROVIDER_CLIENT; |
|
38
|
|
|
|
|
39
|
|
|
public string $sessionKey = 'oauth2-generic-state'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Redirect to Authorization Url |
|
43
|
|
|
*/ |
|
44
|
|
|
public function authorizationUrlAction(?string $scope = null): ResponseInterface |
|
45
|
|
|
{ |
|
46
|
|
|
$redirectUrl = $this->oauth2Provider->getAuthorizationUrl([ |
|
47
|
|
|
'scope' => explode(',', $scope ?: $this->request->get('scope', 'string', $this->defaultScope)), |
|
48
|
|
|
]); |
|
49
|
|
|
$this->session->set($this->sessionKey, $this->oauth2Provider->getState()); |
|
50
|
|
|
return $this->response->redirect($redirectUrl); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Validate State |
|
55
|
|
|
*/ |
|
56
|
|
|
public function validateState(?string $state = null): bool |
|
57
|
|
|
{ |
|
58
|
|
|
$state ??= $this->request->get('state', 'string'); |
|
59
|
|
|
if (empty($state) || !$this->session->has($this->sessionKey)) { |
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $state === $this->session->get($this->sessionKey); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get Access Token |
|
68
|
|
|
* @throws IdentityProviderException |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getAccessToken(?string $code = null): AccessTokenInterface |
|
71
|
|
|
{ |
|
72
|
|
|
$code ??= $this->request->get('code', 'string'); |
|
73
|
|
|
return $this->oauth2Provider->getAccessToken('authorization_code', ['code' => $code]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Refresh Token |
|
78
|
|
|
* @throws IdentityProviderException |
|
79
|
|
|
*/ |
|
80
|
|
|
public function refreshToken(?string $refreshToken = null): AccessTokenInterface |
|
81
|
|
|
{ |
|
82
|
|
|
$refreshToken ??= $this->request->get('refreshToken', 'string'); |
|
83
|
|
|
return $this->oauth2Provider->getAccessToken(new RefreshToken(), ['code' => $refreshToken]); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Use this to interact with an API on the users behalf |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getToken(AccessTokenInterface $token): string |
|
90
|
|
|
{ |
|
91
|
|
|
return $token->getToken(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Use this to get a new access token if the old one expires |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getRefreshToken(AccessTokenInterface $token): ?string |
|
98
|
|
|
{ |
|
99
|
|
|
return $token->getRefreshToken(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Unix timestamp at which the access token expires |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getExpires(AccessTokenInterface $token): ?int |
|
106
|
|
|
{ |
|
107
|
|
|
return $token->getExpires(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Requests and returns the resource owner of given access token. |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getResourceOwner(AccessToken $token): ResourceOwnerInterface |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->oauth2Provider->getResourceOwner($token); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|