/** * Performs an authentication attempt * * @throws Zend_Auth_Adapter_Exception If authentication cannot be performed * @return Zend_Auth_Result */ public function authenticate() { if (empty($this->_request) || empty($this->_response)) { throw new Zend_Auth_Adapter_Exception('Request and Response objects must be set before calling authenticate method'); } try { $token = $this->_getAuthToken(); $serviceSession = ServiceSessionService::getInstance()->checkToken($token); $fakeUserId = $serviceSession->originServiceId; $orgId = ''; if ($serviceSession->contractId) { $contract = ContractService::getInstance()->load($serviceSession->contractId); if (empty($contract)) { throw new InvalidArgumentException("Invalid contract id '{$serviceSession->contractId}'"); } $orgId = $contract->organizationId; } \Application\Model\Mapper\ProtoAbstractMapper::$accountingTransactionPrefix = $this->_prefix; \Application\Model\Mapper\ProtoAbstractMapper::$accountingUserId = '3rdParty_' . $fakeUserId; \Application\Model\Mapper\AbstractMapper::$organizationId = $orgId; $orgSrv = \Application\Service\OrgService::getInstance(); $organization = $orgSrv->load($orgId); if (empty($organization)) { throw new NotFoundException("Org '{$orgId}' not found"); } \Application\Model\Mapper\ProtoAbstractMapper::$language = $organization->defaultLanguage; $result = array('id' => $fakeUserId, 'username' => '3rdParty_' . $serviceSession->originServiceId, 'token' => $token, 'orgId' => $orgId, 'role' => 'admin', 'authType' => App_Controller_Plugin_Auth::AUTH_TYPE_THIRD_PARTY, 'serviceId' => $serviceSession->originServiceId, 'contractId' => $serviceSession->contractId, 'monetaryDataAccess' => false); return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $result); } catch (Exception $e) { \App::log()->err($e->getMessage()); } if (!empty($token)) { \App::log()->warn("3rd party token authentication failed: TOKEN => " . $token); } else { \App::log()->warn("3rd party token authentication failed: NO TOKEN "); } return new Zend_Auth_Result(Zend_Auth_Result::FAILURE, null); }
/** * @expectedException \Application\Exceptions\InvalidArgumentException */ public function testCheckTokenFail() { $result = $this->_serviceSessionService->checkToken(null); }