public function testIndex()
 {
     $token1 = new DefaultToken();
     $token1->setId(100);
     $token2 = new DefaultToken();
     $token2->setId(200);
     $tokens = [$token1, $token2];
     $sessionToken = new DefaultToken();
     $sessionToken->setId(100);
     $this->userManager->expects($this->once())->method('get')->with($this->uid)->will($this->returnValue($this->user));
     $this->tokenProvider->expects($this->once())->method('getTokenByUser')->with($this->user)->will($this->returnValue($tokens));
     $this->session->expects($this->once())->method('getId')->will($this->returnValue('session123'));
     $this->tokenProvider->expects($this->once())->method('getToken')->with('session123')->will($this->returnValue($sessionToken));
     $this->assertEquals([['id' => 100, 'name' => null, 'lastActivity' => null, 'type' => null, 'canDelete' => false], ['id' => 200, 'name' => null, 'lastActivity' => null, 'type' => null, 'canDelete' => true]], $this->controller->index());
 }
Exemplo n.º 2
0
 /**
  * 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 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);
     $dbToken = $this->mapper->getToken($token->getToken());
     $token->setId($dbToken->getId());
     // We don't know the ID
     $token->resetUpdatedFields();
     $this->assertEquals($token, $dbToken);
 }
 /**
  * @expectedException \OC\Authentication\Exceptions\InvalidTokenException
  */
 public function testGetPasswordDeletesInvalidToken()
 {
     $token = 'token1234';
     $tk = new DefaultToken();
     $tk->setPassword('someencryptedvalue');
     /* @var $tokenProvider DefaultTokenProvider */
     $tokenProvider = $this->getMockBuilder('\\OC\\Authentication\\Token\\DefaultTokenProvider')->setMethods(['invalidateToken'])->setConstructorArgs([$this->mapper, $this->crypto, $this->config, $this->logger, $this->timeFactory])->getMock();
     $this->config->expects($this->once())->method('getSystemValue')->with('secret')->will($this->returnValue('1f4h9s'));
     $this->crypto->expects($this->once())->method('decrypt')->with('someencryptedvalue', $token . '1f4h9s')->will($this->throwException(new \Exception('some crypto error occurred')));
     $tokenProvider->expects($this->once())->method('invalidateToken')->with($token);
     $tokenProvider->getPassword($tk, $token);
 }
 /**
  * Get all token of a user
  *
  * The provider may limit the number of result rows in case of an abuse
  * where a high number of (session) tokens is generated
  *
  * @param IUser $user
  * @return DefaultToken[]
  */
 public function getTokenByUser(IUser $user)
 {
     /* @var $qb IQueryBuilder */
     $qb = $this->db->getQueryBuilder();
     $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'token', 'last_activity', 'last_check')->from('authtoken')->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))->setMaxResults(1000);
     $result = $qb->execute();
     $data = $result->fetchAll();
     $result->closeCursor();
     $entities = array_map(function ($row) {
         return DefaultToken::fromRow($row);
     }, $data);
     return $entities;
 }