@@ -58,9 +58,12 @@ class Module extends \yii\base\Module
58
58
/** @var int $cacheTtl */
59
59
public $ cacheTtl = 60 ;
60
60
61
+ /** @var string $prefixCacheAccessToken */
62
+ public $ prefixCacheAccessToken = 'access_token_ ' ;
61
63
62
64
/**
63
65
* @param ClientInterface $httpClient
66
+ *
64
67
* @return $this
65
68
*/
66
69
public function setHttpClient (ClientInterface $ httpClient )
@@ -79,18 +82,12 @@ public function init()
79
82
throw new InvalidConfigException ('Auth server url not configured ' );
80
83
}
81
84
$ this ->setHttpClient (new $ this ->httpClientClass );
82
-
83
- if ($ this ->cache !== false && $ this ->cache !== null ) {
84
- try {
85
- $ this ->cache = Instance::ensure ($ this ->cache , 'yii\caching\CacheInterface ' );
86
- } catch (InvalidConfigException $ e ) {
87
- Yii::warning ('Unable to use cache for URL manager: ' . $ e ->getMessage ());
88
- }
89
- }
85
+ $ this ->initCacheInstance ();
90
86
}
91
87
92
88
/**
93
89
* @param Request $request
90
+ *
94
91
* @return string
95
92
* @throws HttpException
96
93
*/
@@ -117,6 +114,7 @@ public function determineAccessToken(Request $request)
117
114
118
115
/**
119
116
* @param Response $response
117
+ *
120
118
* @return array
121
119
* @throws HttpException
122
120
*/
@@ -130,7 +128,11 @@ public function validateAuthServerResponce(Response $response)
130
128
$ error = !empty ($ tokenInfo ['error ' ])
131
129
? $ tokenInfo ['error ' ]
132
130
: 'Invalid access token ' ;
133
- throw new HttpException ($ response ->statusCode , $ error , $ response ->statusCode );
131
+ throw new HttpException (
132
+ $ response ->statusCode ,
133
+ $ error ,
134
+ $ response ->statusCode
135
+ );
134
136
}
135
137
136
138
return $ tokenInfo ;
@@ -139,6 +141,7 @@ public function validateAuthServerResponce(Response $response)
139
141
140
142
/**
141
143
* @param Request $request
144
+ *
142
145
* @return array
143
146
* @throws HttpException
144
147
*/
@@ -156,7 +159,10 @@ public function validateRequest(Request $request)
156
159
}
157
160
}
158
161
try {
159
- $ url = rtrim ($ this ->authServerUrl , '/ ' ) . '/ ' . ltrim ($ this ->tokenInfoEndpoint , '/ ' );
162
+ $ url = rtrim ($ this ->authServerUrl , '/ ' ) . '/ ' . ltrim (
163
+ $ this ->tokenInfoEndpoint ,
164
+ '/ '
165
+ );
160
166
$ response = $ this ->httpClient ->sendRequest (
161
167
'GET ' ,
162
168
$ url ,
@@ -167,28 +173,56 @@ public function validateRequest(Request $request)
167
173
]
168
174
);
169
175
} catch (\Exception $ e ) {
170
- throw new HttpException (503 , 'Authentication server not available ' , 503 );
176
+ throw new HttpException (
177
+ 503 ,
178
+ 'Authentication server not available ' ,
179
+ 503
180
+ );
171
181
}
172
182
173
183
$ afterValidate = $ this ->validateAuthServerResponce ($ response );
174
184
175
185
if ($ this ->cache instanceof CacheInterface) {
176
- $ this ->cache ->set ($ accessToken , json_encode ($ afterValidate ), $ this ->cacheTtl );
186
+ $ this ->cache ->set (
187
+ $ accessToken ,
188
+ json_encode ($ afterValidate ),
189
+ $ this ->cacheTtl
190
+ );
177
191
}
178
192
return $ afterValidate ;
179
193
}
180
194
195
+ private function initCacheInstance ()
196
+ {
197
+ if ($ this ->cache !== false && $ this ->cache !== null && !($ this ->cache instanceof CacheInterface)) {
198
+ try {
199
+ $ this ->cache = Instance::ensure ($ this ->cache , 'yii\caching\CacheInterface ' );
200
+ } catch (InvalidConfigException $ e ) {
201
+ Yii::warning ('Unable to use cache for URL manager: ' . $ e ->getMessage ());
202
+ }
203
+ }
204
+ }
205
+
206
+
181
207
/**
182
208
* @param string $username
183
209
* @param string $password
184
210
* @param string $scope
185
211
* @param bool $rawResponse
212
+ *
186
213
* @return array|string
187
214
* @throws HttpException
188
215
* @throws InvalidConfigException
189
216
*/
190
- public function requestAccessToken ($ username , $ password , $ scope = '' , $ rawResponse = false , $ grantType = 'password ' )
191
- {
217
+ public function requestAccessToken (
218
+ $ username ,
219
+ $ password ,
220
+ $ scope = '' ,
221
+ $ rawResponse = false ,
222
+ $ grantType = 'password ' ,
223
+ $ cacheTtl = 0
224
+ ) {
225
+ $ this ->initCacheInstance ();
192
226
if ($ this ->testMode ) {
193
227
return TestHelper::getTokenInfo ($ rawResponse );
194
228
}
@@ -198,39 +232,67 @@ public function requestAccessToken($username, $password, $scope = '', $rawRespon
198
232
if (empty ($ this ->clientSecret )) {
199
233
throw new InvalidConfigException ('Client secret not configured ' );
200
234
}
235
+
236
+ $ requestParams = [
237
+ 'grant_type ' => $ grantType ,
238
+ 'client_id ' => $ this ->clientId ,
239
+ 'client_secret ' => $ this ->clientSecret ,
240
+ 'scope ' => $ scope ,
241
+ 'username ' => $ username ,
242
+ 'password ' => $ password
243
+ ];
244
+
245
+ $ cacheKey = $ this ->prefixCacheAccessToken . sha1 (json_encode ($ requestParams ));
246
+ if ($ this ->cache instanceof CacheInterface && $ cacheTtl > 0 ) {
247
+ $ cacheValue = $ this ->cache ->get ($ cacheKey );
248
+ if (!empty ($ cacheValue )) {
249
+ return $ rawResponse
250
+ ? $ cacheValue
251
+ : json_decode (
252
+ $ cacheValue ,
253
+ true
254
+ );
255
+ }
256
+ }
257
+
201
258
try {
202
- $ url = rtrim ($ this ->authServerUrl , '/ ' ) . '/ ' . ltrim ($ this ->tokenIssueEndpoint , '/ ' );
259
+ $ url = rtrim ($ this ->authServerUrl , '/ ' ) . '/ ' . ltrim (
260
+ $ this ->tokenIssueEndpoint ,
261
+ '/ '
262
+ );
203
263
$ response = $ this ->httpClient ->sendRequest (
204
264
'POST ' ,
205
265
$ url ,
206
- [
207
- 'grant_type ' => $ grantType ,
208
- 'client_id ' => $ this ->clientId ,
209
- 'client_secret ' => $ this ->clientSecret ,
210
- 'scope ' => $ scope ,
211
- 'username ' => $ username ,
212
- 'password ' => $ password
213
- ],
266
+ $ requestParams ,
214
267
[
215
268
'Accept ' => 'application/json '
216
269
]
217
270
);
218
271
} catch (\Exception $ e ) {
219
272
throw new HttpException (503 , 'Authentication server not available ' );
220
273
}
274
+
275
+ if ($ this ->cache instanceof CacheInterface && $ cacheTtl > 0 ) {
276
+ $ this ->cache ->set ($ cacheKey , $ response ->content , $ cacheTtl );
277
+ }
278
+
221
279
return $ rawResponse ? $ response : json_decode ($ response ->content , true );
222
280
}
223
281
224
282
/**
225
283
* @param string $refresh_token
226
284
* @param string $scope
227
285
* @param bool $rawResponse
286
+ *
228
287
* @return array|string
229
288
* @throws HttpException
230
289
* @throws InvalidConfigException
231
290
*/
232
- public function requestAccessByRefreshToken ($ refresh_token , $ scope = '' , $ rawResponse = false )
233
- {
291
+ public function requestAccessByRefreshToken (
292
+ $ refresh_token ,
293
+ $ scope = '' ,
294
+ $ rawResponse = false
295
+ ) {
234
296
if ($ this ->testMode ) {
235
297
return TestHelper::getTokenInfo ($ rawResponse );
236
298
}
@@ -241,7 +303,10 @@ public function requestAccessByRefreshToken($refresh_token, $scope = '', $rawRes
241
303
throw new InvalidConfigException ('Client secret not configured ' );
242
304
}
243
305
try {
244
- $ url = rtrim ($ this ->authServerUrl , '/ ' ) . '/ ' . ltrim ($ this ->tokenIssueEndpoint , '/ ' );
306
+ $ url = rtrim ($ this ->authServerUrl , '/ ' ) . '/ ' . ltrim (
307
+ $ this ->tokenIssueEndpoint ,
308
+ '/ '
309
+ );
245
310
$ response = $ this ->httpClient ->sendRequest (
246
311
'POST ' ,
247
312
$ url ,
0 commit comments