PageRenderTime 51ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/Tests/Unit/Security/ContextTest.php

https://github.com/christianjul/FLOW3-Composer
PHP | 844 lines | 541 code | 200 blank | 103 comment | 5 complexity | 1adb1108e91566d7c7b9a9c9286e15ae MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. namespace TYPO3\FLOW3\Tests\Unit\Security;
  3. /* *
  4. * This script belongs to the FLOW3 framework. *
  5. * *
  6. * It is free software; you can redistribute it and/or modify it under *
  7. * the terms of the GNU Lesser General Public License, either version 3 *
  8. * of the License, or (at your option) any later version. *
  9. * *
  10. * The TYPO3 project - inspiring people to share! *
  11. * */
  12. use TYPO3\FLOW3\Http\Request;
  13. use TYPO3\FLOW3\Http\Uri;
  14. use TYPO3\FLOW3\Security\Policy\Role;
  15. /**
  16. * Testcase for the security context
  17. */
  18. class ContextTest extends \TYPO3\FLOW3\Tests\UnitTestCase {
  19. /**
  20. * @test
  21. */
  22. public function currentRequestIsSetInTheSecurityContext() {
  23. $request = Request::create(new Uri('http://robertlemke.com/admin'))->createActionRequest();
  24. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  25. $mockAuthenticationManager->expects($this->any())->method('getTokens')->will($this->returnValue(array()));
  26. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('separateActiveAndInactiveTokens'));
  27. $securityContext->injectAuthenticationManager($mockAuthenticationManager);
  28. $securityContext->setRequest($request);
  29. $securityContext->_call('initialize');
  30. $this->assertSame($request, $securityContext->_get('request'));
  31. }
  32. /**
  33. * @test
  34. */
  35. public function securityContextIsSetToInitialized() {
  36. $request = Request::create(new Uri('http://robertlemke.com/admin'))->createActionRequest();
  37. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  38. $mockAuthenticationManager->expects($this->any())->method('getTokens')->will($this->returnValue(array()));
  39. $mockRequestHandler = $this->getMock('TYPO3\FLOW3\Mvc\ActionRequestHandler', array(), array(), '', FALSE);
  40. $mockRequestHandler->expects($this->any())->method('getRequest')->will($this->returnValue($request));
  41. $bootstrap = $this->getMock('TYPO3\FLOW3\Core\Bootstrap', array(), array(), '', FALSE);
  42. $bootstrap->expects($this->any())->method('getActiveRequestHandler')->will($this->returnValue($mockRequestHandler));
  43. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('separateActiveAndInactiveTokens'));
  44. $securityContext->injectAuthenticationManager($mockAuthenticationManager);
  45. $securityContext->_set('bootstrap', $bootstrap);
  46. $this->assertFalse($securityContext->isInitialized());
  47. $securityContext->_call('initialize');
  48. $this->assertTrue($securityContext->isInitialized());
  49. }
  50. /**
  51. * @test
  52. */
  53. public function initializeSeparatesActiveAndInactiveTokens() {
  54. $request = Request::create(new Uri('http://robertlemke.com/admin'))->createActionRequest();
  55. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  56. $mockAuthenticationManager->expects($this->any())->method('getTokens')->will($this->returnValue(array()));
  57. $mockRequestHandler = $this->getMock('TYPO3\FLOW3\Mvc\ActionRequestHandler', array(), array(), '', FALSE);
  58. $mockRequestHandler->expects($this->any())->method('getRequest')->will($this->returnValue($request));
  59. $bootstrap = $this->getMock('TYPO3\FLOW3\Core\Bootstrap', array(), array(), '', FALSE);
  60. $bootstrap->expects($this->any())->method('getActiveRequestHandler')->will($this->returnValue($mockRequestHandler));
  61. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('separateActiveAndInactiveTokens'));
  62. $securityContext->expects($this->once())->method('separateActiveAndInactiveTokens');
  63. $securityContext->injectAuthenticationManager($mockAuthenticationManager);
  64. $securityContext->_set('bootstrap', $bootstrap);
  65. $securityContext->_call('initialize');
  66. }
  67. /**
  68. * @test
  69. */
  70. public function initializeUpdatesAndSeparatesActiveAndInactiveTokensCorrectly() {
  71. $settings = array();
  72. $settings['security']['authentication']['authenticationStrategy'] = 'allTokens';
  73. $request = Request::create(new Uri('http://robertlemke.com/admin'))->createActionRequest();
  74. $matchingRequestPattern = $this->getMock('TYPO3\FLOW3\Security\RequestPatternInterface', array(), array(), '', FALSE);
  75. $matchingRequestPattern->expects($this->any())->method('matchRequest')->will($this->returnValue(TRUE));
  76. $notMatchingRequestPattern = $this->getMock('TYPO3\FLOW3\Security\RequestPatternInterface', array(), array(), '', FALSE);
  77. $notMatchingRequestPattern->expects($this->any())->method('matchRequest')->will($this->returnValue(FALSE));
  78. $token1 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), '', FALSE);
  79. $token1->expects($this->once())->method('hasRequestPatterns')->will($this->returnValue(TRUE));
  80. $token1->expects($this->once())->method('getRequestPatterns')->will($this->returnValue(array($matchingRequestPattern)));
  81. $token1->expects($this->any())->method('getAuthenticationProviderName')->will($this->returnValue('token1Provider'));
  82. $token2 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), '', FALSE);
  83. $token2->expects($this->once())->method('hasRequestPatterns')->will($this->returnValue(FALSE));
  84. $token2->expects($this->never())->method('getRequestPatterns');
  85. $token2->expects($this->any())->method('getAuthenticationProviderName')->will($this->returnValue('token2Provider'));
  86. $token3 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), '', FALSE);
  87. $token3->expects($this->once())->method('hasRequestPatterns')->will($this->returnValue(TRUE));
  88. $token3->expects($this->once())->method('getRequestPatterns')->will($this->returnValue(array($notMatchingRequestPattern)));
  89. $token3->expects($this->any())->method('getAuthenticationProviderName')->will($this->returnValue('token3Provider'));
  90. $token4 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), '', FALSE);
  91. $token4->expects($this->once())->method('hasRequestPatterns')->will($this->returnValue(TRUE));
  92. $token4->expects($this->once())->method('getRequestPatterns')->will($this->returnValue(array()));
  93. $token4->expects($this->any())->method('getAuthenticationProviderName')->will($this->returnValue('token4Provider'));
  94. $token5 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), '', FALSE);
  95. $token5->expects($this->once())->method('hasRequestPatterns')->will($this->returnValue(TRUE));
  96. $token5->expects($this->once())->method('getRequestPatterns')->will($this->returnValue(array($notMatchingRequestPattern, $matchingRequestPattern)));
  97. $token5->expects($this->any())->method('getAuthenticationProviderName')->will($this->returnValue('token5Provider'));
  98. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  99. $mockAuthenticationManager->expects($this->once())->method('getTokens')->will($this->returnValue(array($token1, $token2, $token3, $token4, $token5)));
  100. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('dummy'), array(), '', FALSE);
  101. $securityContext->injectSettings($settings);
  102. $securityContext->setRequest($request);
  103. $securityContext->injectAuthenticationManager($mockAuthenticationManager);
  104. $securityContext->_set('tokens', array($token1, $token3, $token4));
  105. $securityContext->_call('initialize');
  106. $this->assertEquals(array($token1, $token2, $token4), array_values($securityContext->_get('activeTokens')));
  107. $this->assertEquals(array($token3, $token5), array_values($securityContext->_get('inactiveTokens')));
  108. }
  109. /**
  110. * @test
  111. */
  112. public function securityContextCallsTheAuthenticationManagerToSetItsTokens() {
  113. $request = Request::create(new Uri('http://robertlemke.com/admin'))->createActionRequest();
  114. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  115. $mockAuthenticationManager->expects($this->once())->method('getTokens')->will($this->returnValue(array()));
  116. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('dummy'), array(), '', FALSE);
  117. $securityContext->setRequest($request);
  118. $securityContext->injectAuthenticationManager($mockAuthenticationManager);
  119. $securityContext->_call('initialize');
  120. }
  121. /**
  122. * @test
  123. */
  124. public function tokenFromAnAuthenticationManagerIsReplacedIfThereIsOneOfTheSameTypeInTheSession() {
  125. $request = Request::create(new Uri('http://robertlemke.com/admin'))->createActionRequest();
  126. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  127. $token1 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), '', FALSE);
  128. $token1->expects($this->any())->method('getAuthenticationProviderName')->will($this->returnValue('token1Provider'));
  129. $token1Clone = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), '', FALSE);
  130. $token1Clone->expects($this->any())->method('getAuthenticationProviderName')->will($this->returnValue('token1Provider'));
  131. $token2 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), '', FALSE);
  132. $token2->expects($this->any())->method('getAuthenticationProviderName')->will($this->returnValue('token2Provider'));
  133. $token2Clone = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), '', FALSE);
  134. $token2Clone->expects($this->any())->method('getAuthenticationProviderName')->will($this->returnValue('token2Provider'));
  135. $token3 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), '', FALSE);
  136. $token3->expects($this->any())->method('getAuthenticationProviderName')->will($this->returnValue('token3Provider'));
  137. $tokensFromTheManager = array($token1, $token2, $token3);
  138. $tokensFromTheSession = array($token1Clone, $token2Clone);
  139. $mockAuthenticationManager->expects($this->once())->method('getTokens')->will($this->returnValue($tokensFromTheManager));
  140. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('dummy'), array(), '', FALSE);
  141. $securityContext->injectAuthenticationManager($mockAuthenticationManager);
  142. $securityContext->setRequest($request);
  143. $securityContext->_set('tokens', $tokensFromTheSession);
  144. $securityContext->_call('initialize');
  145. $expectedMergedTokens = array($token1Clone, $token2Clone, $token3);
  146. $this->assertEquals(array_values($securityContext->_get('tokens')), $expectedMergedTokens);
  147. }
  148. /**
  149. * @test
  150. */
  151. public function initializeCallsUpdateCredentialsOnAllActiveTokens() {
  152. $request = Request::create(new Uri('http://robertlemke.com/admin'))->createActionRequest();
  153. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  154. $notMatchingRequestPattern = $this->getMock('TYPO3\FLOW3\Security\RequestPatternInterface', array(), array(), '', FALSE);
  155. $notMatchingRequestPattern->expects($this->any())->method('matchRequest')->will($this->returnValue(FALSE));
  156. $mockToken1 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), '', FALSE);
  157. $mockToken1->expects($this->any())->method('getAuthenticationProviderName')->will($this->returnValue('token1Provider'));
  158. $mockToken2 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), '', FALSE);
  159. $mockToken2->expects($this->any())->method('getAuthenticationProviderName')->will($this->returnValue('token2Provider'));
  160. $mockToken2->expects($this->atLeastOnce())->method('hasRequestPatterns')->will($this->returnValue(TRUE));
  161. $mockToken2->expects($this->atLeastOnce())->method('getRequestPatterns')->will($this->returnValue(array($notMatchingRequestPattern)));
  162. $mockToken3 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), '', FALSE);
  163. $mockToken3->expects($this->any())->method('getAuthenticationProviderName')->will($this->returnValue('token3Provider'));
  164. $mockToken1->expects($this->once())->method('updateCredentials');
  165. $mockToken2->expects($this->never())->method('updateCredentials');
  166. $mockToken3->expects($this->once())->method('updateCredentials');
  167. $mockAuthenticationManager->expects($this->once())->method('getTokens')->will($this->returnValue(array($mockToken1, $mockToken2, $mockToken3)));
  168. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('dummy'));
  169. $securityContext->setRequest($request);
  170. $securityContext->_set('authenticationManager', $mockAuthenticationManager);
  171. $securityContext->_call('initialize');
  172. }
  173. /**
  174. * @test
  175. */
  176. public function injectAuthenticationManagerSetsAReferenceToTheSecurityContextInTheAuthenticationManager() {
  177. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('dummy'));
  178. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  179. $mockAuthenticationManager->expects($this->once())->method('setSecurityContext')->with($securityContext);
  180. $securityContext->injectAuthenticationManager($mockAuthenticationManager);
  181. }
  182. /**
  183. * Data provider for authentication strategy settings
  184. *
  185. * @return array
  186. */
  187. public function authenticationStrategies() {
  188. $data = array();
  189. $settings = array();
  190. $settings['security']['authentication']['authenticationStrategy'] = 'allTokens';
  191. $data[] = array($settings, \TYPO3\FLOW3\Security\Context::AUTHENTICATE_ALL_TOKENS);
  192. $settings['security']['authentication']['authenticationStrategy'] = 'oneToken';
  193. $data[] = array($settings, \TYPO3\FLOW3\Security\Context::AUTHENTICATE_ONE_TOKEN);
  194. $settings['security']['authentication']['authenticationStrategy'] = 'atLeastOneToken';
  195. $data[] = array($settings, \TYPO3\FLOW3\Security\Context::AUTHENTICATE_AT_LEAST_ONE_TOKEN);
  196. $settings['security']['authentication']['authenticationStrategy'] = 'anyToken';
  197. $data[] = array($settings, \TYPO3\FLOW3\Security\Context::AUTHENTICATE_ANY_TOKEN);
  198. return $data;
  199. }
  200. /**
  201. * @dataProvider authenticationStrategies()
  202. * @test
  203. */
  204. public function authenticationStrategyIsSetCorrectlyFromConfiguration($settings, $expectedAuthenticationStrategy) {
  205. $request = Request::create(new Uri('http://robertlemke.com/admin'))->createActionRequest();
  206. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  207. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('dummy'), array(), '', FALSE);
  208. $securityContext->injectSettings($settings);
  209. $securityContext->setRequest($request);
  210. $securityContext->_set('authenticationManager', $mockAuthenticationManager);
  211. $this->assertEquals($expectedAuthenticationStrategy, $securityContext->getAuthenticationStrategy());
  212. }
  213. /**
  214. * @test
  215. */
  216. public function getRolesReturnsTheCorrectRoles() {
  217. $settings = array();
  218. $request = Request::create(new Uri('http://robertlemke.com/admin'))->createActionRequest();
  219. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  220. $mockAuthenticationManager->expects($this->once())->method('isAuthenticated')->will($this->returnValue(TRUE));
  221. $role1 = new Role('role1');
  222. $role11 = new Role('role11');
  223. $token1 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), '', FALSE);
  224. $token1->expects($this->any())->method('isAuthenticated')->will($this->returnValue(TRUE));
  225. $token1->expects($this->any())->method('getRoles')->will($this->returnValue(array($role1, $role11)));
  226. $token1->expects($this->any())->method('getAuthenticationProviderName')->will($this->returnValue('token1Provider'));
  227. $role2 = new Role('role2');
  228. $token2 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), '', FALSE);
  229. $token2->expects($this->any())->method('isAuthenticated')->will($this->returnValue(TRUE));
  230. $token2->expects($this->any())->method('getRoles')->will($this->returnValue(array($role2)));
  231. $token2->expects($this->any())->method('getAuthenticationProviderName')->will($this->returnValue('token2Provider'));
  232. $role3 = new Role('role3');
  233. $role33 = new Role('role33');
  234. $token3 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), '', FALSE);
  235. $token3->expects($this->any())->method('isAuthenticated')->will($this->returnValue(FALSE));
  236. $token3->expects($this->any())->method('getRoles')->will($this->returnValue(array($role3, $role33)));
  237. $token3->expects($this->any())->method('getAuthenticationProviderName')->will($this->returnValue('token3Provider'));
  238. $role4 = new Role('role4');
  239. $role44 = new Role('role44');
  240. $token4 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), '', FALSE);
  241. $token4->expects($this->any())->method('isAuthenticated')->will($this->returnValue(FALSE));
  242. $token4->expects($this->any())->method('getRoles')->will($this->returnValue(array($role4, $role44)));
  243. $token4->expects($this->any())->method('getAuthenticationProviderName')->will($this->returnValue('token4Provider'));
  244. $role5 = new Role('role5');
  245. $token5 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), '', FALSE);
  246. $token5->expects($this->any())->method('isAuthenticated')->will($this->returnValue(TRUE));
  247. $token5->expects($this->any())->method('getRoles')->will($this->returnValue(array($role5)));
  248. $token5->expects($this->any())->method('getAuthenticationProviderName')->will($this->returnValue('token6Provider'));
  249. $everybodyRole = new Role('Everybody');
  250. $mockPolicyService = $this->getMock('TYPO3\FLOW3\Security\Policy\PolicyService', array(), array(), '', FALSE);
  251. $mockPolicyService->expects($this->any())->method('getAllParentRoles')->will($this->returnValue(array()));
  252. $mockPolicyService->expects($this->any())->method('getRoles')->will($this->returnValue(array(
  253. $everybodyRole, $role1, $role11, $role2, $role5
  254. )));
  255. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('dummy'), array(), '', FALSE);
  256. $securityContext->setRequest($request);
  257. $securityContext->_set('settings', $settings);
  258. $securityContext->_set('policyService', $mockPolicyService);
  259. $securityContext->_set('activeTokens', array($token1, $token2, $token3, $token4, $token5));
  260. $securityContext->_set('authenticationManager', $mockAuthenticationManager);
  261. $expectedResult = array($everybodyRole, $role1, $role11, $role2, $role5);
  262. $this->assertEquals($expectedResult, $securityContext->getRoles());
  263. }
  264. /**
  265. * @test
  266. */
  267. public function getRolesTakesInheritanceOfRolesIntoAccount() {
  268. $request = Request::create(new Uri('http://robertlemke.com/admin'))->createActionRequest();
  269. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  270. $mockAuthenticationManager->expects($this->once())->method('isAuthenticated')->will($this->returnValue(TRUE));
  271. $role1 = new Role('role1');
  272. $role2 = new Role('role2');
  273. $role3 = new Role('role3');
  274. $role4 = new Role('role4');
  275. $role5 = new Role('role5');
  276. $role6 = new Role('role6');
  277. $role7 = new Role('role7');
  278. $role8 = new Role('role8');
  279. $role9 = new Role('role9');
  280. $everybodyRole = new Role('Everybody');
  281. $token1 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), '', FALSE);
  282. $token1->expects($this->any())->method('isAuthenticated')->will($this->returnValue(TRUE));
  283. $token1->expects($this->once())->method('getRoles')->will($this->returnValue(array($role1, $role2, $role3)));
  284. $token2 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), '', FALSE);
  285. $token2->expects($this->any())->method('isAuthenticated')->will($this->returnValue(TRUE));
  286. $token2->expects($this->once())->method('getRoles')->will($this->returnValue(array($role2, $role4, $role5)));
  287. $policyServiceCallback = function() use (&$role1, &$role2, &$role5, &$role6, &$role7, &$role8, &$role9) {
  288. $args = func_get_args();
  289. if ((string)$args[0] === 'role1') return array($role6);
  290. if ((string)$args[0] === 'role2') return array($role6, $role7);
  291. if ((string)$args[0] === 'role5') return array($role8, $role9);
  292. return array();
  293. };
  294. $mockPolicyService = $this->getMock('TYPO3\FLOW3\Security\Policy\PolicyService', array(), array(), '', FALSE);
  295. $mockPolicyService->expects($this->any())->method('getAllParentRoles')->will($this->returnCallback($policyServiceCallback));
  296. $mockPolicyService->expects($this->any())->method('getRoles')->will($this->returnValue(array(
  297. $everybodyRole, $role1, $role2, $role3, $role4, $role5, $role6, $role7, $role8, $role9
  298. )));
  299. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('getAuthenticationTokens'), array(), '', FALSE);
  300. $securityContext->expects($this->once())->method('getAuthenticationTokens')->will($this->returnValue(array($token1, $token2)));
  301. $securityContext->setRequest($request);
  302. $securityContext->_set('policyService', $mockPolicyService);
  303. $securityContext->_set('authenticationManager', $mockAuthenticationManager);
  304. $expectedResult = array($everybodyRole, $role1, $role2, $role3, $role4, $role5, $role6, $role7, $role8, $role9);
  305. $result = $securityContext->getRoles();
  306. sort($expectedResult);
  307. sort($result);
  308. $this->assertEquals($expectedResult, $result);
  309. }
  310. /**
  311. * @test
  312. */
  313. public function getRolesReturnsTheEverybodyRoleEvenIfNoTokenIsAuthenticated() {
  314. $request = Request::create(new Uri('http://robertlemke.com/admin'))->createActionRequest();
  315. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  316. $mockAuthenticationManager->expects($this->once())->method('isAuthenticated')->will($this->returnValue(FALSE));
  317. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('getAuthenticationTokens'), array(), '', FALSE);
  318. $securityContext->setRequest($request);
  319. $securityContext->_set('authenticationManager', $mockAuthenticationManager);
  320. $result = $securityContext->getRoles();
  321. $this->assertInstanceOf('TYPO3\FLOW3\Security\Policy\Role', $result[0]);
  322. $this->assertEquals('Everybody', (string)($result[0]));
  323. }
  324. /**
  325. * @test
  326. */
  327. public function getRolesReturnsTheAnonymousRoleIfNoTokenIsAuthenticated() {
  328. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  329. $mockAuthenticationManager->expects($this->once())->method('isAuthenticated')->will($this->returnValue(FALSE));
  330. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('getAuthenticationTokens'), array(), '', FALSE);
  331. $securityContext->_set('authenticationManager', $mockAuthenticationManager);
  332. $result = $securityContext->getRoles();
  333. $this->assertInstanceOf('TYPO3\FLOW3\Security\Policy\Role', $result[1]);
  334. $this->assertEquals('Anonymous', (string)($result[1]));
  335. }
  336. /**
  337. * @test
  338. * @category unit
  339. */
  340. public function getRolesAddsTheEverybodyRoleToTheRolesFromTheAuthenticatedTokens() {
  341. $request = Request::create(new Uri('http://robertlemke.com/admin'))->createActionRequest();
  342. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  343. $mockAuthenticationManager->expects($this->once())->method('isAuthenticated')->will($this->returnValue(TRUE));
  344. $role1 = new Role('Role1');
  345. $role2 = new Role('Role2');
  346. $role3 = new Role('Role3');
  347. $token1 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), '', FALSE);
  348. $token1->expects($this->any())->method('isAuthenticated')->will($this->returnValue(TRUE));
  349. $token1->expects($this->once())->method('getRoles')->will($this->returnValue(array($role1)));
  350. $token2 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), '', FALSE);
  351. $token2->expects($this->any())->method('isAuthenticated')->will($this->returnValue(TRUE));
  352. $token2->expects($this->once())->method('getRoles')->will($this->returnValue(array($role2, $role3)));
  353. $mockPolicyService = $this->getMock('TYPO3\FLOW3\Security\Policy\PolicyService', array(), array(), '', FALSE);
  354. $mockPolicyService->expects($this->any())->method('getAllParentRoles')->will($this->returnValue(array()));
  355. $mockPolicyService->expects($this->any())->method('getRoles')->will($this->returnValue(array(new Role('Everybody'))));
  356. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('getAuthenticationTokens'), array(), '', FALSE);
  357. $securityContext->setRequest($request);
  358. $securityContext->_set('authenticationManager', $mockAuthenticationManager);
  359. $securityContext->expects($this->once())->method('getAuthenticationTokens')->will($this->returnValue(array($token1, $token2)));
  360. $securityContext->_set('policyService', $mockPolicyService);
  361. $result = $securityContext->getRoles();
  362. $everybodyRoleFound = FALSE;
  363. foreach ($result as $resultRole) {
  364. $this->assertInstanceOf('TYPO3\FLOW3\Security\Policy\Role', $resultRole);
  365. if ('Everybody' === (string)($resultRole)) $everybodyRoleFound = TRUE;
  366. }
  367. $this->assertTrue($everybodyRoleFound, 'The Everybody role could not be found as expected.');
  368. }
  369. /**
  370. * @test
  371. */
  372. public function hasRoleWorks() {
  373. $request = Request::create(new Uri('http://robertlemke.com/admin'))->createActionRequest();
  374. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  375. $mockAuthenticationManager->expects($this->any())->method('isAuthenticated')->will($this->returnValue(TRUE));
  376. $roleAdministrator = new Role('Administrator');
  377. $roleLicenseToKill = new Role('LicenseToKill');
  378. $roleCustomer = new Role('Customer');
  379. $token1 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), 'token1' . md5(uniqid(mt_rand(), TRUE)));
  380. $token1->expects($this->any())->method('getRoles')->will($this->returnValue(array($roleAdministrator, $roleLicenseToKill)));
  381. $token1->expects($this->any())->method('isAuthenticated')->will($this->returnValue(TRUE));
  382. $token2 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), 'token2' . md5(uniqid(mt_rand(), TRUE)));
  383. $token2->expects($this->any())->method('getRoles')->will($this->returnValue(array($roleCustomer)));
  384. $token2->expects($this->any())->method('isAuthenticated')->will($this->returnValue(FALSE));
  385. $mockPolicyService = $this->getMock('TYPO3\FLOW3\Security\Policy\PolicyService', array(), array(), '', FALSE);
  386. $mockPolicyService->expects($this->any())->method('getAllParentRoles')->will($this->returnValue(array()));
  387. $mockPolicyService->expects($this->any())->method('getRoles')->will($this->returnValue(array($roleAdministrator, $roleLicenseToKill, $roleCustomer)));
  388. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('getAuthenticationTokens'), array(), '', FALSE);
  389. $securityContext->setRequest($request);
  390. $securityContext->_set('authenticationManager', $mockAuthenticationManager);
  391. $securityContext->_set('policyService', $mockPolicyService);
  392. $securityContext->expects($this->any())->method('isInitialized')->will($this->returnValue(TRUE));
  393. $securityContext->expects($this->any())->method('getAuthenticationTokens')->will($this->returnValue(array($token1, $token2)));
  394. $this->assertTrue($securityContext->hasRole('LicenseToKill'));
  395. $this->assertFalse($securityContext->hasRole('Customer'));
  396. }
  397. /**
  398. * @test
  399. */
  400. public function hasRoleWorksWithRecursiveRoles() {
  401. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  402. $mockAuthenticationManager->expects($this->once())->method('isAuthenticated')->will($this->returnValue(TRUE));
  403. $roleAdministrator = new Role('Administrator');
  404. $roleLicenseToKill = new Role('LicenseToKill');
  405. $roleCustomer = new Role('Customer');
  406. $token1 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), 'token1' . md5(uniqid(mt_rand(), TRUE)));
  407. $token1->expects($this->any())->method('getRoles')->will($this->returnValue(array($roleAdministrator, $roleLicenseToKill)));
  408. $token1->expects($this->any())->method('isAuthenticated')->will($this->returnValue(TRUE));
  409. $token2 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), 'token2' . md5(uniqid(mt_rand(), TRUE)));
  410. $token2->expects($this->any())->method('getRoles')->will($this->returnValue(array($roleCustomer)));
  411. $token2->expects($this->any())->method('isAuthenticated')->will($this->returnValue(FALSE));
  412. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('getAuthenticationTokens'), array(), '', FALSE);
  413. $securityContext->_set('authenticationManager', $mockAuthenticationManager);
  414. $securityContext->expects($this->any())->method('getAuthenticationTokens')->will($this->returnValue(array($token1, $token2)));
  415. $policyServiceCallback = function() {
  416. $args = func_get_args();
  417. if ((string)$args[0] === 'Administrator') return array(new Role('Customer'));
  418. return array();
  419. };
  420. $mockPolicyService = $this->getMock('TYPO3\FLOW3\Security\Policy\PolicyService', array(), array(), '', FALSE);
  421. $mockPolicyService->expects($this->any())->method('getAllParentRoles')->will($this->returnCallback($policyServiceCallback));
  422. $mockPolicyService->expects($this->any())->method('getRoles')->will($this->returnValue(array($roleAdministrator, $roleLicenseToKill, $roleCustomer)));
  423. $securityContext->_set('policyService', $mockPolicyService);
  424. $this->assertTrue($securityContext->hasRole('Customer'));
  425. }
  426. /**
  427. * @test
  428. */
  429. public function getRolesDoesNotReturnRolesThePolicyServiceDoesNotKnowAbout() {
  430. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  431. $mockAuthenticationManager->expects($this->atLeastOnce())->method('isAuthenticated')->will($this->returnValue(TRUE));
  432. $roleAdministrator = new Role('Administrator');
  433. $roleLicenseToKill = new Role('LicenseToKill');
  434. $roleCustomer = new Role('Customer');
  435. $token1 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), 'token1' . md5(uniqid(mt_rand(), TRUE)));
  436. $token1->expects($this->any())->method('getRoles')->will($this->returnValue(array($roleAdministrator, $roleLicenseToKill)));
  437. $token1->expects($this->any())->method('isAuthenticated')->will($this->returnValue(TRUE));
  438. $token2 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), 'token2' . md5(uniqid(mt_rand(), TRUE)));
  439. $token2->expects($this->any())->method('getRoles')->will($this->returnValue(array($roleCustomer)));
  440. $token2->expects($this->any())->method('isAuthenticated')->will($this->returnValue(TRUE));
  441. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('getAuthenticationTokens'), array(), '', FALSE);
  442. $securityContext->_set('authenticationManager', $mockAuthenticationManager);
  443. $securityContext->expects($this->any())->method('getAuthenticationTokens')->will($this->returnValue(array($token1, $token2)));
  444. $mockPolicyService = $this->getMock('TYPO3\FLOW3\Security\Policy\PolicyService', array(), array(), '', FALSE);
  445. $mockPolicyService->expects($this->any())->method('getAllParentRoles')->will($this->returnValue(array()));
  446. $mockPolicyService->expects($this->any())->method('getRoles')->will($this->returnValue(array($roleAdministrator, $roleCustomer)));
  447. $securityContext->_set('policyService', $mockPolicyService);
  448. $this->assertTrue($securityContext->hasRole('Administrator'));
  449. $this->assertTrue($securityContext->hasRole('Customer'));
  450. $this->assertFalse($securityContext->hasRole('LicenseToKill'));
  451. }
  452. /**
  453. * @test
  454. */
  455. public function hasRoleReturnsTrueForTheEverybodyRoleEvenIfNoOtherRoleIsAuthenticated() {
  456. $request = Request::create(new Uri('http://robertlemke.com/admin'))->createActionRequest();
  457. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  458. $mockAuthenticationManager->expects($this->any())->method('isAuthenticated')->will($this->returnValue(TRUE));
  459. $token1 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), 'token1' . md5(uniqid(mt_rand(), TRUE)));
  460. $token1->expects($this->any())->method('isAuthenticated')->will($this->returnValue(FALSE));
  461. $token2 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), 'token2' . md5(uniqid(mt_rand(), TRUE)));
  462. $token2->expects($this->any())->method('isAuthenticated')->will($this->returnValue(FALSE));
  463. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('getAuthenticationTokens'), array(), '', FALSE);
  464. $securityContext->setRequest($request);
  465. $securityContext->_set('authenticationManager', $mockAuthenticationManager);
  466. $securityContext->expects($this->any())->method('getAuthenticationTokens')->will($this->returnValue(array($token1, $token2)));
  467. $mockPolicyService = $this->getMock('TYPO3\FLOW3\Security\Policy\PolicyService', array(), array(), '', FALSE);
  468. $mockPolicyService->expects($this->any())->method('getAllParentRoles')->will($this->returnValue(array()));
  469. $mockPolicyService->expects($this->any())->method('getRoles')->will($this->returnValue(array()));
  470. $securityContext->_set('policyService', $mockPolicyService);
  471. $this->assertTrue($securityContext->hasRole('Everybody'));
  472. }
  473. /**
  474. * @test
  475. */
  476. public function hasRoleReturnsTrueForTheEverybodyRoleInAnyCase() {
  477. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('dummy'), array(), '', FALSE);
  478. $this->assertTrue($securityContext->hasRole('Everybody'));
  479. }
  480. /**
  481. * @test
  482. */
  483. public function getPartyAsksTheCorrectAuthenticationTokenAndReturnsItsParty() {
  484. $request = Request::create(new Uri('http://robertlemke.com/admin'))->createActionRequest();
  485. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  486. $mockParty = $this->getMockForAbstractClass('TYPO3\Party\Domain\Model\AbstractParty');
  487. $mockAccount = $this->getMock('TYPO3\FLOW3\Security\Account');
  488. $mockAccount->expects($this->once())->method('getParty')->will($this->returnValue($mockParty));
  489. $token1 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), 'token1' . md5(uniqid(mt_rand(), TRUE)));
  490. $token1->expects($this->any())->method('isAuthenticated')->will($this->returnValue(FALSE));
  491. $token1->expects($this->never())->method('getAccount');
  492. $token2 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), 'token2' . md5(uniqid(mt_rand(), TRUE)));
  493. $token2->expects($this->any())->method('isAuthenticated')->will($this->returnValue(TRUE));
  494. $token2->expects($this->atLeastOnce())->method('getAccount')->will($this->returnValue($mockAccount));
  495. $token3 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), 'token3' . md5(uniqid(mt_rand(), TRUE)));
  496. $token3->expects($this->any())->method('isAuthenticated')->will($this->returnValue(TRUE));
  497. $token3->expects($this->never())->method('getAccount');
  498. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('getAuthenticationTokens'), array(), '', FALSE);
  499. $securityContext->setRequest($request);
  500. $securityContext->_set('authenticationManager', $mockAuthenticationManager);
  501. $securityContext->expects($this->once())->method('getAuthenticationTokens')->will($this->returnValue(array($token1, $token2, $token3)));
  502. $this->assertEquals($mockParty, $securityContext->getParty());
  503. }
  504. /**
  505. * @test
  506. */
  507. public function getAccountReturnsTheAccountAttachedToTheFirstAuthenticatedToken() {
  508. $request = Request::create(new Uri('http://robertlemke.com/admin'))->createActionRequest();
  509. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  510. $mockAccount = $this->getMock('TYPO3\FLOW3\Security\Account');
  511. $token1 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), 'token1' . md5(uniqid(mt_rand(), TRUE)));
  512. $token1->expects($this->any())->method('isAuthenticated')->will($this->returnValue(FALSE));
  513. $token1->expects($this->never())->method('getAccount');
  514. $token2 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), 'token2' . md5(uniqid(mt_rand(), TRUE)));
  515. $token2->expects($this->any())->method('isAuthenticated')->will($this->returnValue(TRUE));
  516. $token2->expects($this->once())->method('getAccount')->will($this->returnValue($mockAccount));
  517. $token3 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), 'token3' . md5(uniqid(mt_rand(), TRUE)));
  518. $token3->expects($this->any())->method('isAuthenticated')->will($this->returnValue(TRUE));
  519. $token3->expects($this->never())->method('getAccount');
  520. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('getAuthenticationTokens'), array(), '', FALSE);
  521. $securityContext->setRequest($request);
  522. $securityContext->_set('authenticationManager', $mockAuthenticationManager);
  523. $securityContext->expects($this->once())->method('getAuthenticationTokens')->will($this->returnValue(array($token1, $token2, $token3)));
  524. $this->assertEquals($mockAccount, $securityContext->getAccount());
  525. }
  526. /**
  527. * @test
  528. */
  529. public function getPartyByTypeReturnsTheFirstAuthenticatedPartyWithGivenType() {
  530. $request = Request::create(new Uri('http://robertlemke.com/admin'))->createActionRequest();
  531. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  532. $matchingMockParty = $this->getMockForAbstractClass('TYPO3\Party\Domain\Model\AbstractParty', array(), 'MatchingParty');
  533. $notMatchingMockParty = $this->getMockForAbstractClass('TYPO3\Party\Domain\Model\AbstractParty', array(), 'NotMatchingParty');
  534. $mockAccount1 = $this->getMock('TYPO3\FLOW3\Security\Account');
  535. $mockAccount1->expects($this->any())->method('getParty')->will($this->returnValue($notMatchingMockParty));
  536. $mockAccount2 = $this->getMock('TYPO3\FLOW3\Security\Account');
  537. $mockAccount2->expects($this->any())->method('getParty')->will($this->returnValue($matchingMockParty));
  538. $token1 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), 'token1' . md5(uniqid(mt_rand(), TRUE)));
  539. $token1->expects($this->any())->method('isAuthenticated')->will($this->returnValue(FALSE));
  540. $token1->expects($this->never())->method('getAccount');
  541. $token2 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), 'token2' . md5(uniqid(mt_rand(), TRUE)));
  542. $token2->expects($this->any())->method('isAuthenticated')->will($this->returnValue(TRUE));
  543. $token2->expects($this->any())->method('getAccount')->will($this->returnValue($mockAccount1));
  544. $token3 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), 'token3' . md5(uniqid(mt_rand(), TRUE)));
  545. $token3->expects($this->any())->method('isAuthenticated')->will($this->returnValue(TRUE));
  546. $token3->expects($this->any())->method('getAccount')->will($this->returnValue($mockAccount2));
  547. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('getAuthenticationTokens'), array(), '', FALSE);
  548. $securityContext->setRequest($request);
  549. $securityContext->_set('authenticationManager', $mockAuthenticationManager);
  550. $securityContext->expects($this->once())->method('getAuthenticationTokens')->will($this->returnValue(array($token1, $token2, $token3)));
  551. $this->assertSame($matchingMockParty, $securityContext->getPartyByType('MatchingParty'));
  552. }
  553. /**
  554. * @test
  555. */
  556. public function getAccountByAuthenticationProviderNameReturnsTheAuthenticatedAccountWithGivenProviderName() {
  557. $request = Request::create(new Uri('http://robertlemke.com/admin'))->createActionRequest();
  558. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  559. $mockAccount1 = $this->getMock('TYPO3\FLOW3\Security\Account');
  560. $mockAccount2 = $this->getMock('TYPO3\FLOW3\Security\Account');
  561. $token1 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), 'token1' . md5(uniqid(mt_rand(), TRUE)));
  562. $token1->expects($this->any())->method('isAuthenticated')->will($this->returnValue(FALSE));
  563. $token1->expects($this->never())->method('getAccount');
  564. $token2 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), 'token2' . md5(uniqid(mt_rand(), TRUE)));
  565. $token2->expects($this->any())->method('isAuthenticated')->will($this->returnValue(TRUE));
  566. $token2->expects($this->any())->method('getAccount')->will($this->returnValue($mockAccount1));
  567. $token3 = $this->getMock('TYPO3\FLOW3\Security\Authentication\TokenInterface', array(), array(), 'token3' . md5(uniqid(mt_rand(), TRUE)));
  568. $token3->expects($this->any())->method('isAuthenticated')->will($this->returnValue(TRUE));
  569. $token3->expects($this->any())->method('getAccount')->will($this->returnValue($mockAccount2));
  570. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('getAuthenticationTokens'), array(), '', FALSE);
  571. $securityContext->setRequest($request);
  572. $securityContext->_set('authenticationManager', $mockAuthenticationManager);
  573. $securityContext->_set('activeTokens', array('SomeOhterProvider' => $token1, 'SecondProvider' => $token2, 'MatchingProvider' => $token3));
  574. $this->assertSame($mockAccount2, $securityContext->getAccountByAuthenticationProviderName('MatchingProvider'));
  575. }
  576. /**
  577. * @test
  578. */
  579. public function getAccountByAuthenticationProviderNameReturnsNullIfNoAccountFound() {
  580. $request = Request::create(new Uri('http://robertlemke.com/admin'))->createActionRequest();
  581. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  582. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('getAuthenticationTokens'), array(), '', FALSE);
  583. $securityContext->setRequest($request);
  584. $securityContext->_set('authenticationManager', $mockAuthenticationManager);
  585. $securityContext->_set('activeTokens', array());
  586. $this->assertSame(NULL, $securityContext->getAccountByAuthenticationProviderName('UnknownProvider'));
  587. }
  588. /**
  589. * @test
  590. */
  591. public function getCsrfProtectionTokenReturnsANewTokenIfNoneIsPresentInTheContext() {
  592. $request = Request::create(new Uri('http://robertlemke.com/admin'))->createActionRequest();
  593. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  594. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('getAuthenticationTokens'), array(), '', FALSE);
  595. $securityContext->setRequest($request);
  596. $securityContext->_set('authenticationManager', $mockAuthenticationManager);
  597. $securityContext->_set('csrfTokens', array());
  598. $this->assertNotEmpty($securityContext->getCsrfProtectionToken());
  599. }
  600. /**
  601. * @test
  602. */
  603. public function getCsrfProtectionTokenReturnsANewTokenIfTheCsrfStrategyIsOnePerUri() {
  604. $request = Request::create(new Uri('http://robertlemke.com/admin'))->createActionRequest();
  605. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  606. $existingTokens = array('token1' => TRUE, 'token2' => TRUE);
  607. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('getAuthenticationTokens'), array(), '', FALSE);
  608. $securityContext->setRequest($request);
  609. $securityContext->_set('authenticationManager', $mockAuthenticationManager);
  610. $securityContext->_set('csrfTokens', $existingTokens);
  611. $securityContext->_set('csrfStrategy', \TYPO3\FLOW3\Security\Context::CSRF_ONE_PER_URI);
  612. $this->assertFalse(array_key_exists($securityContext->getCsrfProtectionToken(), $existingTokens));
  613. }
  614. /**
  615. * @test
  616. */
  617. public function isCsrfProtectionTokenValidChecksIfTheGivenTokenIsExistingInTheContext() {
  618. $request = Request::create(new Uri('http://robertlemke.com/admin'))->createActionRequest();
  619. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  620. $existingTokens = array('csrfToken12345' => TRUE);
  621. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('initialize'), array(), '', FALSE);
  622. $securityContext->setRequest($request);
  623. $securityContext->_set('authenticationManager', $mockAuthenticationManager);
  624. $securityContext->_set('csrfTokens', $existingTokens);
  625. $this->assertTrue($securityContext->isCsrfProtectionTokenValid('csrfToken12345'));
  626. $this->assertFalse($securityContext->isCsrfProtectionTokenValid('csrfToken'));
  627. }
  628. /**
  629. * @test
  630. */
  631. public function isCsrfProtectionTokenValidChecksIfTheGivenTokenIsExistingInTheContextAndUnsetsItIfTheCsrfStrategyIsOnePerUri() {
  632. $request = Request::create(new Uri('http://robertlemke.com/admin'))->createActionRequest();
  633. $mockAuthenticationManager = $this->getMock('TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface');
  634. $existingTokens = array('csrfToken12345' => TRUE);
  635. $securityContext = $this->getAccessibleMock('TYPO3\FLOW3\Security\Context', array('initialize'), array(), '', FALSE);
  636. $securityContext->setRequest($request);
  637. $securityContext->_set('authenticationManager', $mockAuthenticationManager);
  638. $securityContext->_set('csrfTokens', $existingTokens);
  639. $securityContext->_set('csrfStrategy', \TYPO3\FLOW3\Security\Context::CSRF_ONE_PER_URI);
  640. $this->assertTrue($securityContext->isCsrfProtectionTokenValid('csrfToken12345'));
  641. $this->assertFalse($securityContext->isCsrfProtectionTokenValid('csrfToken12345'));
  642. }
  643. }
  644. ?>