/**
  * Create and persist a new token
  *
  * @param string $token
  * @param string $uid
  * @param string $loginName
  * @param string|null $password
  * @param string $name
  * @param int $type token type
  * @return IToken
  */
 public function generateToken($token, $uid, $loginName, $password, $name, $type = IToken::TEMPORARY_TOKEN)
 {
     $dbToken = new DefaultToken();
     $dbToken->setUid($uid);
     $dbToken->setLoginName($loginName);
     if (!is_null($password)) {
         $dbToken->setPassword($this->encryptPassword($password, $token));
     }
     $dbToken->setName($name);
     $dbToken->setToken($this->hashToken($token));
     $dbToken->setType($type);
     $dbToken->setLastActivity($this->time->getTime());
     $this->mapper->insert($dbToken);
     return $dbToken;
 }
 public function testGenerateToken()
 {
     $token = 'token';
     $uid = 'user';
     $user = '******';
     $password = '******';
     $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12' . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12' . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12' . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
     $type = IToken::PERMANENT_TOKEN;
     $toInsert = new DefaultToken();
     $toInsert->setUid($uid);
     $toInsert->setLoginName($user);
     $toInsert->setPassword('encryptedpassword');
     $toInsert->setName($name);
     $toInsert->setToken(hash('sha512', $token . '1f4h9s'));
     $toInsert->setType($type);
     $toInsert->setLastActivity($this->time);
     $this->config->expects($this->any())->method('getSystemValue')->with('secret')->will($this->returnValue('1f4h9s'));
     $this->crypto->expects($this->once())->method('encrypt')->with($password, $token . '1f4h9s')->will($this->returnValue('encryptedpassword'));
     $this->mapper->expects($this->once())->method('insert')->with($this->equalTo($toInsert));
     $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type);
     $this->assertEquals($toInsert, $actual);
 }
 public function testGetToken()
 {
     $token = '1504445f1524fc801035448a95681a9378ba2e83930c814546c56e5d6ebde221198792fd900c88ed5ead0555780dad1ebce3370d7e154941cd5de87eb419899b';
     $token = new DefaultToken();
     $token->setUid('user2');
     $token->setLoginName('User2');
     $token->setPassword('971a337057853344700bbeccf836519f|UwOQwyb34sJHtqPV|036d4890f8c21d17bbc7b88072d8ef049a5c832a38e97f3e3d5f9186e896c2593aee16883f617322fa242728d0236ff32d163caeb4bd45e14ca002c57a88665f');
     $token->setName('Firefox on Android');
     $token->setToken('1504445f1524fc801035448a95681a9378ba2e83930c814546c56e5d6ebde221198792fd900c88ed5ead0555780dad1ebce3370d7e154941cd5de87eb419899b');
     $token->setType(IToken::TEMPORARY_TOKEN);
     $token->setLastActivity($this->time - 60 * 60 * 24 * 3);
     $token->setLastCheck($this->time - 10);
     $dbToken = $this->mapper->getToken($token->getToken());
     $token->setId($dbToken->getId());
     // We don't know the ID
     $token->resetUpdatedFields();
     $this->assertEquals($token, $dbToken);
 }