1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Javis\OAuth2; |
4
|
|
|
|
5
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
6
|
|
|
use League\OAuth2\Client\Provider\GenericProvider; |
7
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* [PasswordGrantClient description]. |
11
|
|
|
* |
12
|
|
|
* [ |
13
|
|
|
* 'clientId' => 'demoapp', // The client ID assigned to you by the provider |
14
|
|
|
* 'clientSecret' => 'demopass', // The client password assigned to you by the provider |
15
|
|
|
* 'redirectUri' => 'http://example.com/your-redirect-url/', |
16
|
|
|
* 'urlAuthorize' => 'http://brentertainment.com/oauth2/lockdin/authorize', |
17
|
|
|
* 'urlAccessToken' => 'http://brentertainment.com/oauth2/lockdin/token', |
18
|
|
|
* 'urlResourceOwnerDetails' => 'http://brentertainment.com/oauth2/lockdin/resource' |
19
|
|
|
* ] |
20
|
|
|
*/ |
21
|
|
|
class PasswordGrantClient |
22
|
|
|
{ |
23
|
|
|
protected $provider; |
24
|
|
|
|
25
|
|
|
public function __construct(GenericProvider $provider) |
26
|
|
|
{ |
27
|
|
|
$this->provider = $provider; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* retrieves from endpoint, session or refreshes the Token |
32
|
|
|
* for a given user. |
33
|
|
|
* |
34
|
|
|
* @param [type] $username [description] |
|
|
|
|
35
|
|
|
* @param [type] $password [description] |
36
|
|
|
* |
37
|
|
|
* @return AccessToken |
38
|
|
|
*/ |
39
|
|
|
public function getAccessToken($username, $password) |
40
|
|
|
{ |
41
|
|
|
// get token from session |
42
|
|
|
if ($token = $this->retrievePersistedAccessToken()) { |
43
|
|
|
try { |
44
|
|
|
$token = $this->refreshTokenIfNecessary($token); |
45
|
|
|
} catch (IdentityProviderException $e) { |
46
|
|
|
$this->removePersistedToken(); |
47
|
|
|
$token = false; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (!$token) { |
52
|
|
|
$token = $this->requestAccessToken($username, $password); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $token; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* forgets stored token. |
60
|
|
|
*/ |
61
|
|
|
public function forgetToken() |
62
|
|
|
{ |
63
|
|
|
$this->removePersistedToken(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* request access token for a specific user from endpoint. |
68
|
|
|
* |
69
|
|
|
* @param [type] $username [description] |
|
|
|
|
70
|
|
|
* @param [type] $password [description] |
71
|
|
|
* |
72
|
|
|
* @return [type] [description] |
|
|
|
|
73
|
|
|
*/ |
74
|
|
|
public function requestAccessToken($username, $password) |
75
|
|
|
{ |
76
|
|
|
// Try to get an access token using the resource owner password credentials grant. |
77
|
|
|
$token = $this->provider->getAccessToken('password', [ |
78
|
|
|
'username' => $username, |
79
|
|
|
'password' => $password, |
80
|
|
|
]); |
81
|
|
|
|
82
|
|
|
// save to session |
83
|
|
|
$this->persistAccessToken($token); |
84
|
|
|
|
85
|
|
|
return $token; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* attempt to refresh a given token. |
90
|
|
|
* |
91
|
|
|
* @param AccessToken $token [description] |
92
|
|
|
* |
93
|
|
|
* @return AccessToken [description] |
94
|
|
|
*/ |
95
|
|
|
public function refreshAccessToken(AccessToken $token) |
96
|
|
|
{ |
97
|
|
|
$token = $this->provider->getAccessToken('refresh_token', [ |
98
|
|
|
'refresh_token' => $token->getRefreshToken(), |
99
|
|
|
]); |
100
|
|
|
|
101
|
|
|
// save to session |
102
|
|
|
$this->persistAccessToken($token); |
103
|
|
|
|
104
|
|
|
// return |
105
|
|
|
return $token; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function getPersistingKey() |
109
|
|
|
{ |
110
|
|
|
return 'token_'.md5($this->provider->getBaseAccessTokenUrl([])); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* saves token in session. |
115
|
|
|
* |
116
|
|
|
* @param AccessToken $token |
117
|
|
|
*/ |
118
|
|
|
protected function persistAccessToken(AccessToken $token) |
119
|
|
|
{ |
120
|
|
|
// basic session storage |
121
|
|
|
$_SESSION[$this->getPersistingKey()] = json_encode($token); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* [retrievePersistedAccessToken description]. |
126
|
|
|
* |
127
|
|
|
* @return AccessToken |
128
|
|
|
*/ |
129
|
|
|
protected function retrievePersistedAccessToken() |
130
|
|
|
{ |
131
|
|
|
$key = $this->getPersistingKey(); |
132
|
|
|
if (!empty($_SESSION[$key])) { |
133
|
|
|
return new AccessToken(json_decode($_SESSION[$key], true)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return false; |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
protected function removePersistedToken() |
140
|
|
|
{ |
141
|
|
|
unset($_SESSION[$this->getPersistingKey()]); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param AccessToken $token |
146
|
|
|
* |
147
|
|
|
* @throws IdentityProviderException |
148
|
|
|
* |
149
|
|
|
* @return AccessToken |
150
|
|
|
*/ |
151
|
|
|
protected function refreshTokenIfNecessary(AccessToken $token) |
152
|
|
|
{ |
153
|
|
|
if ($token->hasExpired() && $token->getRefreshToken()) { |
154
|
|
|
$token = $this->refreshAccessToken($token); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $token; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|