/**
  * Creates a new account and sets the given password and roles
  *
  * @param string $identifier Identifier of the account, must be unique
  * @param string $password The clear text password
  * @param array $roleIdentifiers Optionally an array of role identifiers to assign to the new account
  * @param string $authenticationProviderName Optional name of the authentication provider the account is affiliated with
  * @param string $passwordHashingStrategy Optional password hashing strategy to use for the password
  * @return \TYPO3\Flow\Security\Account A new account, not yet added to the account repository
  */
 public function createAccountWithPassword($identifier, $password, $roleIdentifiers = array(), $authenticationProviderName = 'DefaultProvider', $passwordHashingStrategy = 'default')
 {
     $account = new \TYPO3\Flow\Security\Account();
     $account->setAccountIdentifier($identifier);
     $account->setCredentialsSource($this->hashService->hashPassword($password, $passwordHashingStrategy));
     $account->setAuthenticationProviderName($authenticationProviderName);
     $roles = array();
     foreach ($roleIdentifiers as $roleIdentifier) {
         $roles[] = $this->policyService->getRole($roleIdentifier);
     }
     $account->setRoles($roles);
     return $account;
 }
 /**
  * @param \Flowpack\SingleSignOn\Client\Domain\Model\SsoClient $ssoClient
  * @param array $globalAccountData
  * @return \TYPO3\Flow\Security\Account
  */
 public function getAccount(SsoClient $ssoClient, array $globalAccountData)
 {
     $account = new \TYPO3\Flow\Security\Account();
     // TODO Check validity of globalAccountData
     $account->setAccountIdentifier($globalAccountData['accountIdentifier']);
     $account->setAuthenticationProviderName('SingleSignOn');
     $account->setRoles(array_map(function ($roleIdentifier) {
         return new \TYPO3\Flow\Security\Policy\Role($roleIdentifier);
     }, $globalAccountData['roles']));
     if (isset($globalAccountData['party'])) {
         $party = $this->mapParty($globalAccountData['party']);
         if ($party !== NULL) {
             $account->setParty($party);
         }
     }
     return $account;
 }
 /**
  * Sets isAuthenticated to TRUE for all tokens.
  *
  * @param \TYPO3\Flow\Security\Authentication\TokenInterface $authenticationToken The token to be authenticated
  * @return void
  * @throws \TYPO3\Flow\Security\Exception\UnsupportedAuthenticationTokenException
  */
 public function authenticate(\TYPO3\Flow\Security\Authentication\TokenInterface $authenticationToken)
 {
     if (!$authenticationToken instanceof \TYPO3\Flow\Security\Authentication\Token\PasswordToken) {
         throw new \TYPO3\Flow\Security\Exception\UnsupportedAuthenticationTokenException('This provider cannot authenticate the given token.', 1217339840);
     }
     $credentials = $authenticationToken->getCredentials();
     if (is_array($credentials) && isset($credentials['password'])) {
         if ($this->hashService->validatePassword($credentials['password'], $this->fileBasedSimpleKeyService->getKey($this->options['keyName']))) {
             $authenticationToken->setAuthenticationStatus(\TYPO3\Flow\Security\Authentication\TokenInterface::AUTHENTICATION_SUCCESSFUL);
             $account = new \TYPO3\Flow\Security\Account();
             $roles = array();
             foreach ($this->options['authenticateRoles'] as $roleIdentifier) {
                 $roles[] = new Role($roleIdentifier, Role::SOURCE_SYSTEM);
             }
             $account->setRoles($roles);
             $authenticationToken->setAccount($account);
         } else {
             $authenticationToken->setAuthenticationStatus(\TYPO3\Flow\Security\Authentication\TokenInterface::WRONG_CREDENTIALS);
         }
     } elseif ($authenticationToken->getAuthenticationStatus() !== \TYPO3\Flow\Security\Authentication\TokenInterface::AUTHENTICATION_SUCCESSFUL) {
         $authenticationToken->setAuthenticationStatus(\TYPO3\Flow\Security\Authentication\TokenInterface::NO_CREDENTIALS_GIVEN);
     }
 }
예제 #4
0
 /**
  * @test
  */
 public function hasRoleWorksWithRecursiveRoles()
 {
     $everybodyRole = new Role('Everybody', Role::SOURCE_SYSTEM);
     $testRole1 = new Role('Acme.Demo:TestRole1');
     $testRole2 = new Role('Acme.Demo:TestRole2');
     // Set parents
     $testRole1->setParentRoles(array($testRole2));
     $account = new \TYPO3\Flow\Security\Account();
     $account->setRoles(array($testRole1));
     $mockAuthenticationManager = $this->getMock('TYPO3\\Flow\\Security\\Authentication\\AuthenticationManagerInterface');
     $mockAuthenticationManager->expects($this->atLeastOnce())->method('isAuthenticated')->will($this->returnValue(TRUE));
     $mockToken = $this->getMock('TYPO3\\Flow\\Security\\Authentication\\TokenInterface');
     $mockToken->expects($this->atLeastOnce())->method('isAuthenticated')->will($this->returnValue(TRUE));
     $mockToken->expects($this->atLeastOnce())->method('getAccount')->will($this->returnValue($account));
     $mockPolicyService = $this->getAccessibleMock('TYPO3\\Flow\\Security\\Policy\\PolicyService', array('getRole', 'initializeRolesFromPolicy'));
     $mockPolicyService->expects($this->atLeastOnce())->method('getRole')->will($this->returnCallback(function ($roleIdentifier) use($everybodyRole) {
         switch ($roleIdentifier) {
             case 'Everybody':
                 return $everybodyRole;
         }
     }));
     $securityContext = $this->getAccessibleMock('TYPO3\\Flow\\Security\\Context', array('initialize', 'getAccount'));
     $securityContext->expects($this->any())->method('getAccount')->will($this->returnValue($account));
     $securityContext->_set('activeTokens', array($mockToken));
     $securityContext->_set('policyService', $mockPolicyService);
     $securityContext->_set('authenticationManager', $mockAuthenticationManager);
     $this->assertTrue($securityContext->hasRole('Acme.Demo:TestRole2'));
 }
 /**
  * Creates a new account, assigns it the given roles and authenticates it.
  * The created account is returned for further modification, for example for attaching a Party object to it.
  *
  * @param array $roleNames A list of roles the new account should have
  * @return \TYPO3\Flow\Security\Account The created account
  * @api
  */
 protected function authenticateRoles(array $roleNames)
 {
     $account = new \TYPO3\Flow\Security\Account();
     $roles = array();
     foreach ($roleNames as $roleName) {
         $roles[] = $this->policyService->getRole($roleName);
     }
     $account->setRoles($roles);
     $this->authenticateAccount($account);
     return $account;
 }