Integration Testing
refactored db transactions to allow IOC that would make easier unit testing Change-Id: Ifbaa14e8b8025c3a68b4eddd794c92f389087a94 Implements: blueprint openid-oauth2-integration-testing
This commit is contained in:
parent
0876a3aab2
commit
ab777ecb8d
@ -7,11 +7,10 @@ use auth\exceptions\AuthenticationLockedUserLoginAttempt;
|
||||
use Exception;
|
||||
use Illuminate\Auth\UserInterface;
|
||||
use Illuminate\Auth\UserProviderInterface;
|
||||
use Log;
|
||||
use Member;
|
||||
use DB;
|
||||
use openid\services\IUserService;
|
||||
use utils\services\ICheckPointService;
|
||||
use utils\db\ITransactionService;
|
||||
use utils\services\ILogService;
|
||||
|
||||
/**
|
||||
* Class CustomAuthProvider
|
||||
@ -26,18 +25,24 @@ class CustomAuthProvider implements UserProviderInterface
|
||||
private $checkpoint_service;
|
||||
private $user_repository;
|
||||
private $member_repository;
|
||||
private $tx_service;
|
||||
private $log_service;
|
||||
|
||||
public function __construct(IUserRepository $user_repository,
|
||||
IMemberRepository $member_repository,
|
||||
IAuthenticationExtensionService $auth_extension_service,
|
||||
IUserService $user_service,
|
||||
ICheckPointService $checkpoint_service){
|
||||
ICheckPointService $checkpoint_service,
|
||||
ITransactionService $tx_service,
|
||||
ILogService $log_service){
|
||||
|
||||
$this->auth_extension_service = $auth_extension_service;
|
||||
$this->user_service = $user_service;
|
||||
$this->checkpoint_service = $checkpoint_service;
|
||||
$this->user_repository = $user_repository;
|
||||
$this->member_repository = $member_repository;
|
||||
$this->tx_service = $tx_service;
|
||||
$this->log_service = $log_service;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -50,15 +55,15 @@ class CustomAuthProvider implements UserProviderInterface
|
||||
{
|
||||
try {
|
||||
//here we do the manuel join between 2 DB, (openid and SS db)
|
||||
$user = User::where('external_id', '=', $identifier)->first();
|
||||
$member = Member::where('Email', '=', $identifier)->first();
|
||||
$user = $this->user_repository->getByExternalId($identifier);
|
||||
$member = $this->member_repository->getByEmail($identifier);
|
||||
if (!is_null($member) && !is_null($user)) {
|
||||
$user->setMember($member);
|
||||
return $user;
|
||||
}
|
||||
return null;
|
||||
} catch (Exception $ex) {
|
||||
Log::error($ex);
|
||||
$this->log_service->error($ex);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -80,7 +85,7 @@ class CustomAuthProvider implements UserProviderInterface
|
||||
try {
|
||||
|
||||
|
||||
DB::transaction(function () use ($credentials, &$user,&$user_repository,&$member_repository, &$user_service,&$auth_extension_service) {
|
||||
$this->tx_service->transaction(function () use ($credentials, &$user,&$user_repository,&$member_repository, &$user_service,&$auth_extension_service) {
|
||||
|
||||
if (!isset($credentials['username']) || !isset($credentials['password']))
|
||||
throw new AuthenticationException("invalid crendentials");
|
||||
@ -139,7 +144,7 @@ class CustomAuthProvider implements UserProviderInterface
|
||||
});
|
||||
} catch (Exception $ex) {
|
||||
$this->checkpoint_service->trackException($ex);
|
||||
Log::error($ex);
|
||||
$this->log_service->error($ex);
|
||||
$user = null;
|
||||
}
|
||||
return $user;
|
||||
@ -161,16 +166,13 @@ class CustomAuthProvider implements UserProviderInterface
|
||||
try {
|
||||
$identifier = $credentials['username'];
|
||||
$password = $credentials['password'];
|
||||
$user = User::where('external_id', '=', $identifier)->first();
|
||||
|
||||
$user = $this->user_repository->getByExternalId($identifier);
|
||||
if (is_null($user) || $user->lock || !$user->active)
|
||||
return false;
|
||||
|
||||
$member = Member::where('Email', '=', $identifier)->first();
|
||||
|
||||
$member = $this->member_repository->getByEmail($identifier);
|
||||
return !is_null($member) ? $member->checkPassword($password) : false;
|
||||
} catch (Exception $ex) {
|
||||
Log::error($ex);
|
||||
$this->log_service->error($ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
18
app/libs/utils/db/ITransactionService.php
Normal file
18
app/libs/utils/db/ITransactionService.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace utils\db;
|
||||
|
||||
use Closure;
|
||||
|
||||
|
||||
interface ITransactionService {
|
||||
/**
|
||||
* Execute a Closure within a transaction.
|
||||
*
|
||||
* @param Closure $callback
|
||||
* @return mixed
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function transaction(Closure $callback);
|
||||
}
|
@ -11,4 +11,5 @@ class UtilsServiceCatalog {
|
||||
const ServerConfigurationService = 'utils\\services\\IServerConfigurationService';
|
||||
const CacheService = 'utils\\services\\ICacheService';
|
||||
const BannedIpService = 'utils\\services\\IBannedIPService';
|
||||
const TransactionService = 'utils\\db\\ITransactionService';
|
||||
}
|
@ -16,8 +16,6 @@ class ServicesProvider extends ServiceProvider
|
||||
protected $defer = false;
|
||||
|
||||
public function boot(){
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function register(){
|
||||
|
@ -9,13 +9,22 @@ use ApiScope;
|
||||
use DB;
|
||||
use oauth2\exceptions\InvalidApiEndpoint;
|
||||
use oauth2\exceptions\InvalidApiScope;
|
||||
|
||||
use utils\db\ITransactionService;
|
||||
/**
|
||||
* Class ApiEndpointService
|
||||
* @package services\oauth2
|
||||
*/
|
||||
class ApiEndpointService implements IApiEndpointService {
|
||||
|
||||
private $tx_service;
|
||||
|
||||
/**
|
||||
* @param ITransactionService $tx_service
|
||||
*/
|
||||
public function __construct(ITransactionService $tx_service){
|
||||
$this->tx_service = $tx_service;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param $http_method
|
||||
@ -71,7 +80,7 @@ class ApiEndpointService implements IApiEndpointService {
|
||||
{
|
||||
$instance = null;
|
||||
|
||||
DB::transaction(function () use ($name, $description, $active,$allow_cors, $route, $http_method, $api_id, &$instance) {
|
||||
$this->tx_service->transaction(function () use ($name, $description, $active,$allow_cors, $route, $http_method, $api_id, &$instance) {
|
||||
|
||||
//check that does not exists an endpoint with same http method and same route
|
||||
if(ApiEndpoint::where('http_method','=',$http_method)->where('route','=',$route)->count()>0)
|
||||
@ -104,7 +113,7 @@ class ApiEndpointService implements IApiEndpointService {
|
||||
$res = false;
|
||||
$this_var = $this;
|
||||
|
||||
DB::transaction(function () use ($id,$params, &$res,&$this_var){
|
||||
$this->tx_service->transaction(function () use ($id,$params, &$res,&$this_var){
|
||||
$endpoint = ApiEndpoint::find($id);
|
||||
if(is_null($endpoint))
|
||||
throw new InvalidApiEndpoint(sprintf('api endpoint id %s does not exists!',$id));
|
||||
@ -136,7 +145,7 @@ class ApiEndpointService implements IApiEndpointService {
|
||||
{
|
||||
$res = false;
|
||||
|
||||
DB::transaction(function () use($api_endpoint_id, $scope_id,&$res){
|
||||
$this->tx_service->transaction(function () use($api_endpoint_id, $scope_id,&$res){
|
||||
|
||||
$api_endpoint = ApiEndpoint::find($api_endpoint_id);
|
||||
|
||||
@ -178,7 +187,7 @@ class ApiEndpointService implements IApiEndpointService {
|
||||
|
||||
$res = false;
|
||||
|
||||
DB::transaction(function () use($api_endpoint_id, $scope_id,&$res){
|
||||
$this->tx_service->transaction(function () use($api_endpoint_id, $scope_id,&$res){
|
||||
|
||||
$api_endpoint = ApiEndpoint::find($api_endpoint_id);
|
||||
|
||||
@ -213,7 +222,7 @@ class ApiEndpointService implements IApiEndpointService {
|
||||
public function delete($id)
|
||||
{
|
||||
$res = false;
|
||||
DB::transaction(function () use ($id,&$res) {
|
||||
$this->tx_service->transaction(function () use ($id,&$res) {
|
||||
$endpoint = ApiEndpoint::find($id);
|
||||
if(!is_null($endpoint)){
|
||||
$res = $endpoint->delete();
|
||||
|
@ -9,13 +9,22 @@ use oauth2\services\IApiScopeService;
|
||||
use ApiScope;
|
||||
use Api;
|
||||
use DB;
|
||||
|
||||
use utils\db\ITransactionService;
|
||||
/**
|
||||
* Class ApiScopeService
|
||||
* @package services\oauth2
|
||||
*/
|
||||
class ApiScopeService implements IApiScopeService {
|
||||
|
||||
private $tx_service;
|
||||
|
||||
/**
|
||||
* @param ITransactionService $tx_service
|
||||
*/
|
||||
public function __construct(ITransactionService $tx_service){
|
||||
$this->tx_service = $tx_service;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $scopes_names
|
||||
* @return mixed
|
||||
@ -134,7 +143,7 @@ class ApiScopeService implements IApiScopeService {
|
||||
$res = false;
|
||||
$this_var = $this;
|
||||
|
||||
DB::transaction(function () use ($id,$params,&$res,&$this_var) {
|
||||
$this->tx_service->transaction(function () use ($id,$params,&$res,&$this_var) {
|
||||
|
||||
//check that scope exists...
|
||||
$scope = ApiScope::find($id);
|
||||
@ -184,7 +193,7 @@ class ApiScopeService implements IApiScopeService {
|
||||
public function delete($id)
|
||||
{
|
||||
$res = false;
|
||||
DB::transaction(function () use ($id,&$res) {
|
||||
$this->tx_service->transaction(function () use ($id,&$res) {
|
||||
|
||||
$scope = ApiScope::find($id);
|
||||
if(is_null($scope))
|
||||
@ -210,7 +219,7 @@ class ApiScopeService implements IApiScopeService {
|
||||
public function add($name, $short_description, $description, $active, $default, $system, $api_id)
|
||||
{
|
||||
$instance = null;
|
||||
DB::transaction(function () use ($name, $short_description, $description, $active, $default, $system, $api_id, &$instance) {
|
||||
$this->tx_service->transaction(function () use ($name, $short_description, $description, $active, $default, $system, $api_id, &$instance) {
|
||||
|
||||
// check if api exists...
|
||||
if(is_null(Api::find($api_id)))
|
||||
|
@ -6,9 +6,24 @@ use oauth2\services\IApiService;
|
||||
use Api;
|
||||
use DB;
|
||||
use oauth2\exceptions\InvalidApi;
|
||||
use utils\db\ITransactionService;
|
||||
|
||||
/**
|
||||
* Class ApiService
|
||||
* @package services\oauth2
|
||||
*/
|
||||
class ApiService implements IApiService {
|
||||
/**
|
||||
|
||||
private $tx_service;
|
||||
|
||||
/**
|
||||
* @param ITransactionService $tx_service
|
||||
*/
|
||||
public function __construct(ITransactionService $tx_service){
|
||||
$this->tx_service = $tx_service;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $api_id
|
||||
* @return IApi
|
||||
*/
|
||||
@ -33,7 +48,7 @@ class ApiService implements IApiService {
|
||||
public function delete($id)
|
||||
{
|
||||
$res = false;
|
||||
DB::transaction(function () use ($id,&$res) {
|
||||
$this->tx_service->transaction(function () use ($id,&$res) {
|
||||
$api = Api::find($id);
|
||||
if(!is_null($api)){
|
||||
$res = $api->delete();
|
||||
@ -56,7 +71,7 @@ class ApiService implements IApiService {
|
||||
$active = strtoupper($active) == 'TRUE'?true:false;
|
||||
}
|
||||
|
||||
DB::transaction(function () use ($name, $description, $active, $resource_server_id, &$instance) {
|
||||
$this->tx_service->transaction(function () use ($name, $description, $active, $resource_server_id, &$instance) {
|
||||
|
||||
$count = Api::where('name','=',$name)->count();
|
||||
if($count>0)
|
||||
@ -86,7 +101,7 @@ class ApiService implements IApiService {
|
||||
$res = false;
|
||||
$this_var = $this;
|
||||
|
||||
DB::transaction(function () use ($id,$params, &$res, &$this_var) {
|
||||
$this->tx_service->transaction(function () use ($id,$params, &$res, &$this_var) {
|
||||
|
||||
$api = Api::find($id);
|
||||
if(is_null($api))
|
||||
|
@ -1,5 +1,4 @@
|
||||
<?php
|
||||
|
||||
namespace services\oauth2;
|
||||
|
||||
use Exception;
|
||||
@ -9,12 +8,19 @@ use utils\services\ICacheService;
|
||||
use utils\services\IServerConfigurationService;
|
||||
use services\AbstractBlacklistSecurityPolicy;
|
||||
use utils\services\ILockManagerService;
|
||||
use utils\db\ITransactionService;
|
||||
|
||||
class AuthorizationCodeRedeemPolicy extends AbstractBlacklistSecurityPolicy {
|
||||
|
||||
public function __construct(IServerConfigurationService $server_configuration_service, ILockManagerService $lock_manager_service, ICacheService $cache_service)
|
||||
/**
|
||||
* @param IServerConfigurationService $server_configuration_service
|
||||
* @param ILockManagerService $lock_manager_service
|
||||
* @param ICacheService $cache_service
|
||||
* @param ITransactionService $tx_service
|
||||
*/
|
||||
public function __construct(IServerConfigurationService $server_configuration_service, ILockManagerService $lock_manager_service, ICacheService $cache_service,ITransactionService $tx_service)
|
||||
{
|
||||
parent::__construct($server_configuration_service,$lock_manager_service,$cache_service);
|
||||
parent::__construct($server_configuration_service,$lock_manager_service,$cache_service,$tx_service);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -23,6 +23,7 @@ use Request;
|
||||
use utils\services\IAuthService;
|
||||
use Zend\Math\Rand;
|
||||
use Event;
|
||||
use utils\db\ITransactionService;
|
||||
|
||||
/**
|
||||
* Class ClientService
|
||||
@ -33,10 +34,16 @@ class ClientService implements IClientService
|
||||
private $auth_service;
|
||||
private $scope_service;
|
||||
|
||||
public function __construct(IAuthService $auth_service, IApiScopeService $scope_service)
|
||||
/**
|
||||
* @param IAuthService $auth_service
|
||||
* @param IApiScopeService $scope_service
|
||||
* @param ITransactionService $tx_service
|
||||
*/
|
||||
public function __construct(IAuthService $auth_service, IApiScopeService $scope_service,ITransactionService $tx_service)
|
||||
{
|
||||
$this->auth_service = $auth_service;
|
||||
$this->scope_service = $scope_service;
|
||||
$this->tx_service = $tx_service;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -84,7 +91,7 @@ class ClientService implements IClientService
|
||||
$this_var = $this;
|
||||
$scope_service = $this_var->scope_service;
|
||||
|
||||
DB::transaction(function () use ($application_type, $user_id, $app_name,$app_url, $app_description, $app_logo, &$instance, &$this_var,&$scope_service) {
|
||||
$this->tx_service->transaction(function () use ($application_type, $user_id, $app_name,$app_url, $app_description, $app_logo, &$instance, &$this_var,&$scope_service) {
|
||||
|
||||
//check $application_type vs client_type
|
||||
$client_type = $application_type == IClient::ApplicationType_JS_Client? IClient::ClientType_Public : IClient::ClientType_Confidential;
|
||||
@ -122,7 +129,7 @@ class ClientService implements IClientService
|
||||
public function addClientAllowedUri($id, $uri)
|
||||
{
|
||||
$res = false;
|
||||
DB::transaction(function () use ($id,$uri,&$res){
|
||||
$this->tx_service->transaction(function () use ($id,$uri,&$res){
|
||||
$client = Client::find($id);
|
||||
|
||||
if (is_null($client))
|
||||
@ -170,7 +177,7 @@ class ClientService implements IClientService
|
||||
public function deleteClientByIdentifier($id)
|
||||
{
|
||||
$res = false;
|
||||
DB::transaction(function () use ($id,&$res){
|
||||
$this->tx_service->transaction(function () use ($id,&$res){
|
||||
$client = Client::find($id);
|
||||
if (!is_null($client)) {
|
||||
$client->authorized_uris()->delete();
|
||||
@ -190,7 +197,7 @@ class ClientService implements IClientService
|
||||
public function regenerateClientSecret($id)
|
||||
{
|
||||
$new_secret = '';
|
||||
DB::transaction(function () use ($id, &$new_secret) {
|
||||
$this->tx_service->transaction(function () use ($id, &$new_secret) {
|
||||
|
||||
$client = Client::find($id);
|
||||
|
||||
@ -220,7 +227,7 @@ class ClientService implements IClientService
|
||||
$res = false;
|
||||
$this_var = $this;
|
||||
|
||||
DB::transaction(function () use ($client_id, &$res, &$this_var) {
|
||||
$this->tx_service->transaction(function () use ($client_id, &$res, &$this_var) {
|
||||
|
||||
$client = $this_var->getClientByIdentifier($client_id);
|
||||
if (is_null($client))
|
||||
@ -241,7 +248,7 @@ class ClientService implements IClientService
|
||||
$res = false;
|
||||
$this_var = $this;
|
||||
|
||||
DB::transaction(function () use ($client_id, &$res, &$this_var) {
|
||||
$this->tx_service->transaction(function () use ($client_id, &$res, &$this_var) {
|
||||
|
||||
$client = $this_var->getClientByIdentifier($client_id);
|
||||
if (is_null($client))
|
||||
@ -348,7 +355,7 @@ class ClientService implements IClientService
|
||||
$res = false;
|
||||
$this_var = $this;
|
||||
|
||||
DB::transaction(function () use ($id,$params, &$res, &$this_var) {
|
||||
$this->tx_service->transaction(function () use ($id,$params, &$res, &$this_var) {
|
||||
|
||||
$client = Client::find($id);
|
||||
if(is_null($client))
|
||||
@ -377,7 +384,8 @@ class ClientService implements IClientService
|
||||
public function addClientAllowedOrigin($id, $origin)
|
||||
{
|
||||
$res = false;
|
||||
DB::transaction(function () use ($id, $origin, &$res) {
|
||||
|
||||
$this->tx_service->transaction(function () use ($id, $origin, &$res) {
|
||||
|
||||
$client = Client::find($id);
|
||||
|
||||
|
@ -11,13 +11,23 @@ use oauth2\services\IClientService;
|
||||
use ResourceServer;
|
||||
use DB;
|
||||
use \oauth2\exceptions\InvalidResourceServer;
|
||||
use utils\db\ITransactionService;
|
||||
|
||||
/**
|
||||
* Class ResourceServerService
|
||||
* @package services\oauth2
|
||||
*/
|
||||
class ResourceServerService implements IResourceServerService {
|
||||
|
||||
private $client_service;
|
||||
|
||||
public function __construct(IClientService $client_service){
|
||||
/**
|
||||
* @param IClientService $client_service
|
||||
* @param ITransactionService $tx_service
|
||||
*/
|
||||
public function __construct(IClientService $client_service,ITransactionService $tx_service){
|
||||
$this->client_service = $client_service;
|
||||
$this->tx_service = $tx_service;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -44,7 +54,7 @@ class ResourceServerService implements IResourceServerService {
|
||||
$res = false;
|
||||
$this_var = $this;
|
||||
|
||||
DB::transaction(function () use ($id,$params,&$res, &$this_var) {
|
||||
$this->tx_service->transaction(function () use ($id,$params,&$res, &$this_var) {
|
||||
|
||||
$resource_server = ResourceServer::find($id);
|
||||
|
||||
@ -106,7 +116,7 @@ class ResourceServerService implements IResourceServerService {
|
||||
$res = false;
|
||||
$client_service = $this->client_service;
|
||||
|
||||
DB::transaction(function () use ($id,&$res,&$client_service) {
|
||||
$this->tx_service->transaction(function () use ($id,&$res,&$client_service) {
|
||||
|
||||
$resource_server = ResourceServer::find($id);
|
||||
|
||||
@ -148,7 +158,7 @@ class ResourceServerService implements IResourceServerService {
|
||||
$active = strtoupper($active) =='TRUE' ?true:false;
|
||||
}
|
||||
|
||||
DB::transaction(function () use ($host, $ip, $friendly_name, $active, &$instance, &$client_service) {
|
||||
$this->tx_service->transaction(function () use ($host, $ip, $friendly_name, $active, &$instance, &$client_service) {
|
||||
|
||||
if(ResourceServer::where('host','=',$host)->count()>0)
|
||||
throw new InvalidResourceServer(sprintf('there is already another resource server with that hostname (%s).',$host));
|
||||
@ -183,7 +193,7 @@ class ResourceServerService implements IResourceServerService {
|
||||
$res = null;
|
||||
$client_service = $this->client_service;
|
||||
|
||||
DB::transaction(function () use ($id,&$res,&$client_service) {
|
||||
$this->tx_service->transaction(function () use ($id,&$res,&$client_service) {
|
||||
|
||||
$resource_server = ResourceServer::find($id);
|
||||
|
||||
|
@ -34,6 +34,7 @@ use utils\services\ICacheService;
|
||||
use utils\services\IAuthService;
|
||||
|
||||
use Event;
|
||||
use utils\db\ITransactionService;
|
||||
/**
|
||||
* Class TokenService
|
||||
* Provides all Tokens related operations (create, get and revoke)
|
||||
@ -61,8 +62,9 @@ class TokenService implements ITokenService
|
||||
private $cache_service;
|
||||
private $auth_service;
|
||||
private $user_consent_service;
|
||||
private $tx_service;
|
||||
|
||||
public function __construct(IClientService $client_service, ILockManagerService $lock_manager_service, IServerConfigurationService $configuration_service, ICacheService $cache_service, IAuthService $auth_service, IUserConsentService $user_consent_service)
|
||||
public function __construct(IClientService $client_service, ILockManagerService $lock_manager_service, IServerConfigurationService $configuration_service, ICacheService $cache_service, IAuthService $auth_service, IUserConsentService $user_consent_service,ITransactionService $tx_service)
|
||||
{
|
||||
$this->client_service = $client_service;
|
||||
$this->lock_manager_service = $lock_manager_service;
|
||||
@ -70,7 +72,7 @@ class TokenService implements ITokenService
|
||||
$this->cache_service = $cache_service;
|
||||
$this->auth_service = $auth_service;
|
||||
$this->user_consent_service = $user_consent_service;
|
||||
|
||||
$this->tx_service = $tx_service;
|
||||
$this_var = $this;
|
||||
|
||||
Event::listen('oauth2.client.delete', function($client_id) use (&$this_var)
|
||||
@ -179,7 +181,7 @@ class TokenService implements ITokenService
|
||||
$auth_service = $this->auth_service;
|
||||
$this_var = $this;
|
||||
|
||||
DB::transaction(function () use ($auth_code, $redirect_uri, &$access_token,&$cache_service,&$client_service,&$auth_service,&$this_var) {
|
||||
$this->tx_service->transaction(function () use ($auth_code, $redirect_uri, &$access_token,&$cache_service,&$client_service,&$auth_service,&$this_var) {
|
||||
|
||||
$value = $access_token->getValue();
|
||||
$hashed_value = Hash::compute('sha256', $value);
|
||||
@ -239,7 +241,7 @@ class TokenService implements ITokenService
|
||||
$auth_service = $this->auth_service;
|
||||
$this_var = $this;
|
||||
|
||||
DB::transaction(function () use ($client_id,$scope, $audience,$user_id, &$access_token,&$this_var,&$cache_service,&$client_service,&$auth_service) {
|
||||
$this->tx_service->transaction(function () use ($client_id,$scope, $audience,$user_id, &$access_token,&$this_var,&$cache_service,&$client_service,&$auth_service) {
|
||||
|
||||
|
||||
$value = $access_token->getValue();
|
||||
@ -297,7 +299,7 @@ class TokenService implements ITokenService
|
||||
|
||||
|
||||
//preserve entire operation on db transaction...
|
||||
DB::transaction(function () use ($refresh_token, $scope, &$access_token, &$this_var,&$cache_service,&$client_service,&$auth_service,&$configuration_service) {
|
||||
$this->tx_service->transaction(function () use ($refresh_token, $scope, &$access_token, &$this_var,&$cache_service,&$client_service,&$auth_service,&$configuration_service) {
|
||||
|
||||
$refresh_token_value = $refresh_token->getValue();
|
||||
$refresh_token_hashed_value = Hash::compute('sha256', $refresh_token_value);
|
||||
@ -538,7 +540,7 @@ class TokenService implements ITokenService
|
||||
$cache_service = $this->cache_service;
|
||||
$this_var = $this;
|
||||
|
||||
DB::transaction(function () use (&$refresh_token, &$access_token, &$this_var,&$client_service,&$auth_service,&$cache_service) {
|
||||
$this->tx_service->transaction(function () use (&$refresh_token, &$access_token, &$this_var,&$client_service,&$auth_service,&$cache_service) {
|
||||
$value = $refresh_token->getValue();
|
||||
//hash the given value, bc tokens values are stored hashed on DB
|
||||
$hashed_value = Hash::compute('sha256', $value);
|
||||
@ -625,7 +627,7 @@ class TokenService implements ITokenService
|
||||
$auth_code_hashed_value = Hash::compute('sha256', $auth_code);
|
||||
$cache_service = $this->cache_service;
|
||||
|
||||
DB::transaction(function () use ($auth_code_hashed_value,&$cache_service) {
|
||||
$this->tx_service->transaction(function () use ($auth_code_hashed_value,&$cache_service) {
|
||||
//get related access tokens
|
||||
$db_access_tokens = DBAccessToken::where('associated_authorization_code', '=', $auth_code_hashed_value)->get();
|
||||
|
||||
@ -660,7 +662,7 @@ class TokenService implements ITokenService
|
||||
$res = 0;
|
||||
$cache_service = $this->cache_service;
|
||||
|
||||
DB::transaction(function () use ($value, $is_hashed, &$res,&$cache_service) {
|
||||
$this->tx_service->transaction(function () use ($value, $is_hashed, &$res,&$cache_service) {
|
||||
//hash the given value, bc tokens values are stored hashed on DB
|
||||
$hashed_value = !$is_hashed?Hash::compute('sha256', $value):$value;
|
||||
|
||||
@ -690,7 +692,7 @@ class TokenService implements ITokenService
|
||||
$cache_service = $this->cache_service;
|
||||
|
||||
|
||||
DB::transaction(function () use ($client_id, $auth_codes, $access_tokens,&$cache_service,&$client_service) {
|
||||
$this->tx_service->transaction(function () use ($client_id, $auth_codes, $access_tokens,&$cache_service,&$client_service) {
|
||||
$client = $client_service->getClientById($client_id);
|
||||
if(is_null($client)) return;
|
||||
//revoke on cache
|
||||
@ -730,7 +732,7 @@ class TokenService implements ITokenService
|
||||
$res = false;
|
||||
$this_var = $this;
|
||||
|
||||
DB::transaction(function () use ($value,$is_hashed, &$res,&$this_var) {
|
||||
$this->tx_service->transaction(function () use ($value,$is_hashed, &$res,&$this_var) {
|
||||
$res = $this_var->invalidateRefreshToken($value,$is_hashed);
|
||||
$res = $res && $this_var->clearAccessTokensForRefreshToken($value,$is_hashed);
|
||||
});
|
||||
@ -749,7 +751,7 @@ class TokenService implements ITokenService
|
||||
$res = false;
|
||||
$cache_service = $this->cache_service;
|
||||
|
||||
DB::transaction(function () use ($hashed_value, &$res,&$cache_service) {
|
||||
$this->tx_service->transaction(function () use ($hashed_value, &$res,&$cache_service) {
|
||||
|
||||
$refresh_token_db = DBRefreshToken::where('value','=',$hashed_value)->first();
|
||||
if(!is_null($refresh_token_db)){
|
||||
|
@ -2,13 +2,11 @@
|
||||
namespace services\openid;
|
||||
|
||||
use auth\IUserRepository;
|
||||
use auth\User;
|
||||
use openid\model\IOpenIdUser;
|
||||
use DB;
|
||||
use Exception;
|
||||
use openid\services\IUserService;
|
||||
use utils\services\ILogService;
|
||||
|
||||
use utils\db\ITransactionService;
|
||||
/**
|
||||
* Class UserService
|
||||
* @package services\openid
|
||||
@ -18,14 +16,17 @@ class UserService implements IUserService
|
||||
|
||||
private $repository;
|
||||
private $log_service;
|
||||
private $tx_service;
|
||||
|
||||
/**
|
||||
* @param IUserRepository $repository
|
||||
* @param ILogService $log_service
|
||||
* @param IUserRepository $repository
|
||||
* @param ITransactionService $tx_service
|
||||
* @param ILogService $log_service
|
||||
*/
|
||||
public function __construct(IUserRepository $repository, ILogService $log_service){
|
||||
public function __construct(IUserRepository $repository, ITransactionService $tx_service, ILogService $log_service){
|
||||
$this->repository = $repository;
|
||||
$this->log_service = $log_service;
|
||||
$this->tx_service = $tx_service;
|
||||
}
|
||||
|
||||
|
||||
@ -40,7 +41,7 @@ class UserService implements IUserService
|
||||
try {
|
||||
$repository = $this->repository;
|
||||
if (!is_null($user) && $user->identifier === $user->external_id) {
|
||||
DB::transaction(function () use ($proposed_username,&$user,&$repository) {
|
||||
$this->tx_service->transaction(function () use ($proposed_username,&$user,&$repository) {
|
||||
|
||||
$done = false;
|
||||
$fragment_nbr = 1;
|
||||
|
@ -12,7 +12,12 @@ use utils\services\ISecurityPolicy;
|
||||
use utils\services\ISecurityPolicyCounterMeasure;
|
||||
use utils\services\IServerConfigurationService;
|
||||
use utils\IPHelper;
|
||||
use utils\db\ITransactionService;
|
||||
|
||||
/**
|
||||
* Class AbstractBlacklistSecurityPolicy
|
||||
* @package services
|
||||
*/
|
||||
abstract class AbstractBlacklistSecurityPolicy implements ISecurityPolicy
|
||||
{
|
||||
|
||||
@ -20,13 +25,19 @@ abstract class AbstractBlacklistSecurityPolicy implements ISecurityPolicy
|
||||
protected $counter_measure;
|
||||
protected $lock_manager_service;
|
||||
protected $cache_service;
|
||||
protected $tx_service;
|
||||
|
||||
public function __construct(IServerConfigurationService $server_configuration_service, ILockManagerService $lock_manager_service, ICacheService $cache_service)
|
||||
{
|
||||
|
||||
/**
|
||||
* @param IServerConfigurationService $server_configuration_service
|
||||
* @param ILockManagerService $lock_manager_service
|
||||
* @param ICacheService $cache_service
|
||||
* @param ITransactionService $tx_service
|
||||
*/
|
||||
public function __construct(IServerConfigurationService $server_configuration_service, ILockManagerService $lock_manager_service, ICacheService $cache_service,ITransactionService $tx_service) {
|
||||
$this->server_configuration_service = $server_configuration_service;
|
||||
$this->lock_manager_service = $lock_manager_service;
|
||||
$this->cache_service = $cache_service;
|
||||
$this->lock_manager_service = $lock_manager_service;
|
||||
$this->cache_service = $cache_service;
|
||||
$this->tx_service = $tx_service;
|
||||
}
|
||||
|
||||
public function setCounterMeasure(ISecurityPolicyCounterMeasure $counter_measure)
|
||||
@ -49,7 +60,7 @@ abstract class AbstractBlacklistSecurityPolicy implements ISecurityPolicy
|
||||
Log::warning(sprintf("AbstractBlacklistSecurityPolicy: Banning ip %s by Exception %s", $remote_address, $exception_type));
|
||||
//try to create on db
|
||||
|
||||
DB::transaction(function () use ($remote_address, $exception_type, $initial_hits) {
|
||||
$this->tx_service->transaction(function () use ($remote_address, $exception_type, $initial_hits) {
|
||||
|
||||
$banned_ip = BannedIP::where("ip", "=", $remote_address)->first();
|
||||
|
||||
|
@ -13,6 +13,7 @@ use utils\services\ICacheService;
|
||||
use utils\services\ILockManagerService;
|
||||
use utils\services\IServerConfigurationService;
|
||||
use utils\IPHelper;
|
||||
use utils\db\ITransactionService;
|
||||
/**
|
||||
* Class BlacklistSecurityPolicy
|
||||
* implements check point security pattern
|
||||
@ -23,9 +24,15 @@ class BlacklistSecurityPolicy extends AbstractBlacklistSecurityPolicy
|
||||
|
||||
private $exception_dictionary = array();
|
||||
|
||||
public function __construct(IServerConfigurationService $server_configuration_service, ILockManagerService $lock_manager_service, ICacheService $cache_service)
|
||||
/**
|
||||
* @param IServerConfigurationService $server_configuration_service
|
||||
* @param ILockManagerService $lock_manager_service
|
||||
* @param ICacheService $cache_service
|
||||
* @param ITransactionService $tx_service
|
||||
*/
|
||||
public function __construct(IServerConfigurationService $server_configuration_service, ILockManagerService $lock_manager_service, ICacheService $cache_service,ITransactionService $tx_service)
|
||||
{
|
||||
parent::__construct($server_configuration_service, $lock_manager_service, $cache_service);
|
||||
parent::__construct($server_configuration_service, $lock_manager_service, $cache_service,$tx_service);
|
||||
// here we configure on which exceptions are we interested and the max occurrence attempts and initial delay on tar pit for
|
||||
// offending IP address
|
||||
$this->exception_dictionary = array(
|
||||
|
@ -11,7 +11,7 @@ use utils\services\IServerConfigurationService;
|
||||
use utils\services\IBannedIPService;
|
||||
use utils\services\IAuthService;
|
||||
use utils\services\ILogService;
|
||||
|
||||
use utils\db\ITransactionService;
|
||||
/**
|
||||
* Class BannedIPService
|
||||
* @package utils\services
|
||||
@ -22,22 +22,25 @@ class BannedIPService implements IBannedIPService {
|
||||
private $server_configuration_service;
|
||||
private $log_service;
|
||||
private $auth_service;
|
||||
|
||||
/**
|
||||
* @param ICacheService $cache_service
|
||||
* @param IServerConfigurationService $server_configuration_service
|
||||
* @param IAuthService $auth_service
|
||||
* @param ILogService $log_service
|
||||
*/
|
||||
public function __construct(ICacheService $cache_service,
|
||||
private $tx_service;
|
||||
/**
|
||||
* @param ICacheService $cache_service
|
||||
* @param IServerConfigurationService $server_configuration_service
|
||||
* @param IAuthService $auth_service
|
||||
* @param ILogService $log_service
|
||||
* @param ITransactionService $tx_service
|
||||
*/
|
||||
public function __construct(ICacheService $cache_service,
|
||||
IServerConfigurationService $server_configuration_service,
|
||||
IAuthService $auth_service,
|
||||
ILogService $log_service){
|
||||
ILogService $log_service,
|
||||
ITransactionService $tx_service){
|
||||
|
||||
$this->cache_service = $cache_service;
|
||||
$this->server_configuration_service = $server_configuration_service;
|
||||
$this->log_service = $log_service;
|
||||
$this->auth_service = $auth_service;
|
||||
$this->tx_service = $tx_service;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -53,7 +56,7 @@ class BannedIPService implements IBannedIPService {
|
||||
//try to create on cache
|
||||
$this->cache_service->addSingleValue($remote_address, $initial_hits, intval($this->server_configuration_service->getConfigValue("BlacklistSecurityPolicy.BannedIpLifeTimeSeconds")));
|
||||
|
||||
DB::transaction(function () use ($remote_address, $exception_type, $initial_hits,&$res) {
|
||||
$this->tx_service->transaction(function () use ($remote_address, $exception_type, $initial_hits,&$res) {
|
||||
|
||||
$banned_ip = BannedIP::where("ip", "=", $remote_address)->first();
|
||||
if (!$banned_ip) {
|
||||
@ -82,7 +85,7 @@ class BannedIPService implements IBannedIPService {
|
||||
$res = false;
|
||||
$cache_service = $this->cache_service;
|
||||
$this_var = $this;
|
||||
DB::transaction(function () use ($ip,&$res,&$cache_service,&$this_var) {
|
||||
$this->tx_service->transaction(function () use ($ip,&$res,&$cache_service,&$this_var) {
|
||||
|
||||
if($banned_ip = $this_var->getByIP($ip)){
|
||||
$res = $banned_ip->delete();
|
||||
|
27
app/services/utils/EloquentTransactionService.php
Normal file
27
app/services/utils/EloquentTransactionService.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
namespace services\utils;
|
||||
|
||||
|
||||
use Closure;
|
||||
use utils\db\ITransactionService;
|
||||
use DB;
|
||||
|
||||
/**
|
||||
* Class EloquentTransactionService
|
||||
* @package services\utils
|
||||
*/
|
||||
class EloquentTransactionService implements ITransactionService {
|
||||
|
||||
/**
|
||||
* Execute a Closure within a transaction.
|
||||
*
|
||||
* @param Closure $callback
|
||||
* @return mixed
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function transaction(Closure $callback)
|
||||
{
|
||||
DB::transaction($callback);
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@ use openid\services\IServerConfigurationService as IOpenIdServerConfigurationSer
|
||||
use ServerConfiguration;
|
||||
use utils\services\ICacheService;
|
||||
use utils\services\IServerConfigurationService;
|
||||
|
||||
use utils\db\ITransactionService;
|
||||
/**
|
||||
* Class ServerConfigurationService
|
||||
* @package services
|
||||
@ -25,14 +25,17 @@ class ServerConfigurationService implements IOpenIdServerConfigurationService, I
|
||||
const DefaultNonceLifetime = 360;
|
||||
|
||||
private $default_config_params;
|
||||
private $tx_service;
|
||||
|
||||
/**
|
||||
* @param ICacheService $cache_service
|
||||
*/
|
||||
public function __construct(ICacheService $cache_service)
|
||||
/***
|
||||
* @param ICacheService $cache_service
|
||||
* @param ITransactionService $tx_service
|
||||
*/
|
||||
public function __construct(ICacheService $cache_service, ITransactionService $tx_service)
|
||||
{
|
||||
|
||||
$this->cache_service = $cache_service;
|
||||
$this->tx_service = $tx_service;
|
||||
$this->default_config_params = array();
|
||||
//default config values
|
||||
|
||||
@ -114,7 +117,7 @@ class ServerConfigurationService implements IOpenIdServerConfigurationService, I
|
||||
$cache_service = $this->cache_service;
|
||||
$default_config_params = $this->default_config_params;
|
||||
|
||||
DB::transaction(function () use ($key, &$res,&$cache_service,&$default_config_params) {
|
||||
$this->tx_service->transaction(function () use ($key, &$res,&$cache_service,&$default_config_params) {
|
||||
try {
|
||||
|
||||
if (!$cache_service->exists($key)) {
|
||||
@ -152,7 +155,7 @@ class ServerConfigurationService implements IOpenIdServerConfigurationService, I
|
||||
$res = false;
|
||||
$cache_service = $this->cache_service;
|
||||
|
||||
DB::transaction(function () use ($key, $value, &$res,&$cache_service) {
|
||||
$this->tx_service->transaction(function () use ($key, $value, &$res,&$cache_service) {
|
||||
|
||||
$conf = ServerConfiguration::where('key', '=', $key)->first();
|
||||
|
||||
|
@ -37,6 +37,7 @@ class UtilsProvider extends ServiceProvider {
|
||||
App::singleton(UtilsServiceCatalog::LockManagerService, 'services\\utils\\LockManagerService');
|
||||
App::singleton(UtilsServiceCatalog::ServerConfigurationService, 'services\\utils\\ServerConfigurationService');
|
||||
App::singleton(UtilsServiceCatalog::BannedIpService, 'services\\utils\\BannedIPService');
|
||||
App::singleton(UtilsServiceCatalog::TransactionService, 'services\\utils\\EloquentTransactionService');
|
||||
}
|
||||
|
||||
public function provides()
|
||||
|
@ -65,7 +65,7 @@ $framework = $app['path.base'].'/vendor/laravel/framework/src';
|
||||
require $framework.'/Illuminate/Foundation/start.php';
|
||||
|
||||
|
||||
//custom authenticationbootstrap/start.php
|
||||
//custom authentication
|
||||
use Illuminate\Auth\Guard;
|
||||
use auth\CustomAuthProvider;
|
||||
|
||||
@ -76,7 +76,9 @@ Auth::extend('custom', function($app) {
|
||||
App::make('auth\\IMemberRepository'),
|
||||
App::make('auth\\IAuthenticationExtensionService'),
|
||||
App::make(OpenIdServiceCatalog::UserService),
|
||||
App::make(UtilsServiceCatalog::CheckPointService)
|
||||
App::make(UtilsServiceCatalog::CheckPointService),
|
||||
App::make(UtilsServiceCatalog::TransactionService),
|
||||
App::make(UtilsServiceCatalog::LogService)
|
||||
),
|
||||
App::make('session.store')
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user