Fix token cache recursion

removed recursion of the method, bc redis not always removes
things from cache inmediatly.

Change-Id: I00f8b64713289f5fc28f35b4e40ecfe0b1c13b0e
This commit is contained in:
Sebastian Marcet 2016-09-28 12:12:58 -03:00
parent bf35c0c927
commit 9f43479978
3 changed files with 73 additions and 15 deletions

View File

@ -22,7 +22,7 @@ use libs\oauth2\OAuth2Protocol;
use libs\utils\ConfigurationException;
use libs\utils\ICacheService;
use models\oauth2\AccessToken;
use Log;
use Illuminate\Support\Facades\Log;
/**
* Class AccessTokenService
@ -60,8 +60,7 @@ final class AccessTokenService implements IAccessTokenService
throw new InvalidGrantTypeException(OAuth2Protocol::OAuth2Protocol_Error_InvalidToken);
}
$token_info = $this->cache_service->getHash(md5($token_value), array
(
$token_info = $this->cache_service->getHash(md5($token_value),[
'access_token',
'scope',
'client_id',
@ -72,13 +71,11 @@ final class AccessTokenService implements IAccessTokenService
'application_type',
'allowed_return_uris',
'allowed_origins'
));
]);
if (count($token_info) === 0)
{
Log::debug("getting token from remote call ...");
$token_info = $this->makeRemoteCall($token_value);
$this->cache_service->storeHash(md5($token_value), $token_info, $cache_lifetime );
$token_info = $this->doIntrospection($token_value);
}
else
{
@ -97,6 +94,23 @@ final class AccessTokenService implements IAccessTokenService
);
}
$token = $this->unSerializeToken($token_info);
if($token->getLifetime() <= 0)
{
Log::debug("token lifetime is <= 0 ... retrieving from IDP");
$this->cache_service->delete(md5($token_value));
$token_info = $this->doIntrospection($token_value);
$token = $this->unSerializeToken($token_info);
}
return $token;
}
/**
* @param array $token_info
* @return AccessToken
*/
private function unSerializeToken(array $token_info){
$token = AccessToken::createFromParams
(
$token_info['access_token'],
@ -115,15 +129,22 @@ final class AccessTokenService implements IAccessTokenService
foreach($token_info as $k => $v){
$str_token_info .= sprintf("-%s=%s-", $k, $v);
}
Log::debug("token info : ". $str_token_info);
if($token->getLifetime() <= 0)
{
Log::debug("token lifetime is < 0 ... retrieving from IDP");
$this->cache_service->delete(md5($token_value));
$token = $this->get($token_value);
}
return $token;
}
/**
* @param string $token_value
* @return array
*/
private function doIntrospection($token_value){
Log::debug("getting token from remote call ...");
$cache_lifetime = intval(Config::get('server.access_token_cache_lifetime', 300));
$token_info = $this->makeRemoteCall($token_value);
$this->cache_service->storeHash(md5($token_value), $token_info, $cache_lifetime );
return $token_info;
}
/**
* @param $token_value
@ -145,7 +166,7 @@ final class AccessTokenService implements IAccessTokenService
]
]);
$client_id = Config::get('app.openstackid_client_id', '');
$client_id = Config::get('app.openstackid_client_id', '');
$client_secret = Config::get('app.openstackid_client_secret', '');
$auth_server_url = Config::get('app.openstackid_base_url', '');

View File

@ -32,7 +32,6 @@ class RedisCacheService implements ICacheService
$this->redis = Redis::connection();
}
public function boot()
{
if (is_null($this->redis))

38
tests/ServicesTest.php Normal file
View File

@ -0,0 +1,38 @@
<?php
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
class ServicesTest extends TestCase
{
public function testAccessTokenService(){
$cache = App::make('libs\utils\ICacheService');
$token_value = 'saotdCVPDuLuI6m';
$cache_lifetime = 300;
$token_info = [
'access_token' => $token_value,
'scope' => '',
'client_id' => '',
'audience' => '',
'user_id' => '',
'user_external_id' => '',
'expires_in' => 10,
'application_type' => '',
'allowed_return_uris' => '',
'allowed_origins'=> ''
];
$cache->storeHash(md5($token_value), $token_info, $cache_lifetime );
sleep(10);
$service = App::make('models\resource_server\IAccessTokenService');
$service->get($token_value);
}
}