2016-04-25 15:10:55 +03:00
|
|
|
<?php
|
2018-05-15 11:24:46 +03:00
|
|
|
declare(strict_types=1);
|
2016-04-25 15:10:55 +03:00
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-04-25 15:10:55 +03:00
|
|
|
* @author Christoph Wurst <christoph@owncloud.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-04-25 15:10:55 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Authentication\Token;
|
|
|
|
|
|
|
|
use OCP\AppFramework\Db\Entity;
|
|
|
|
|
2016-05-02 20:58:19 +03:00
|
|
|
/**
|
|
|
|
* @method void setId(int $id)
|
|
|
|
* @method void setUid(string $uid);
|
2018-05-15 11:24:46 +03:00
|
|
|
* @method void setLoginName(string $loginname)
|
|
|
|
* @method void setName(string $name)
|
2016-05-02 20:58:19 +03:00
|
|
|
* @method string getToken()
|
2018-05-15 11:24:46 +03:00
|
|
|
* @method void setType(int $type)
|
2016-05-02 20:58:19 +03:00
|
|
|
* @method int getType()
|
2016-09-06 22:41:15 +03:00
|
|
|
* @method void setRemember(int $remember)
|
2018-05-15 11:24:46 +03:00
|
|
|
* @method void setLastActivity(int $lastactivity)
|
2016-05-02 20:58:19 +03:00
|
|
|
* @method int getLastActivity()
|
2018-05-18 20:48:08 +03:00
|
|
|
* @method void setVersion(int $version)
|
2016-05-02 20:58:19 +03:00
|
|
|
*/
|
2016-05-19 12:20:22 +03:00
|
|
|
class DefaultToken extends Entity implements IToken {
|
2016-04-25 15:10:55 +03:00
|
|
|
|
2018-05-15 11:24:46 +03:00
|
|
|
/** @var string user UID */
|
2016-04-25 15:10:55 +03:00
|
|
|
protected $uid;
|
|
|
|
|
2018-05-15 11:24:46 +03:00
|
|
|
/** @var string login name used for generating the token */
|
2016-05-24 11:50:18 +03:00
|
|
|
protected $loginName;
|
|
|
|
|
2018-05-15 11:24:46 +03:00
|
|
|
/** @var string encrypted user password */
|
2016-04-25 15:10:55 +03:00
|
|
|
protected $password;
|
|
|
|
|
2018-05-15 11:24:46 +03:00
|
|
|
/** @var string token name (e.g. browser/OS) */
|
2016-04-25 15:10:55 +03:00
|
|
|
protected $name;
|
|
|
|
|
2018-05-15 11:24:46 +03:00
|
|
|
/** @var string */
|
2016-04-25 15:10:55 +03:00
|
|
|
protected $token;
|
|
|
|
|
2018-05-15 11:24:46 +03:00
|
|
|
/** @var int */
|
2016-04-26 13:48:19 +03:00
|
|
|
protected $type;
|
|
|
|
|
2018-05-15 11:24:46 +03:00
|
|
|
/** @var int */
|
2016-09-06 22:41:15 +03:00
|
|
|
protected $remember;
|
|
|
|
|
2018-05-15 11:24:46 +03:00
|
|
|
/** @var int */
|
2016-04-25 17:40:41 +03:00
|
|
|
protected $lastActivity;
|
|
|
|
|
2018-05-15 11:24:46 +03:00
|
|
|
/** @var int */
|
2016-06-17 14:59:15 +03:00
|
|
|
protected $lastCheck;
|
|
|
|
|
2018-05-15 11:24:46 +03:00
|
|
|
/** @var string */
|
2016-08-01 20:06:54 +03:00
|
|
|
protected $scope;
|
|
|
|
|
2018-05-16 13:39:00 +03:00
|
|
|
/** @var int */
|
|
|
|
protected $expires;
|
|
|
|
|
2018-05-18 20:48:08 +03:00
|
|
|
/** @var int */
|
|
|
|
protected $version;
|
|
|
|
|
2016-10-31 16:38:41 +03:00
|
|
|
public function __construct() {
|
2018-05-15 11:24:46 +03:00
|
|
|
$this->addType('uid', 'string');
|
|
|
|
$this->addType('loginName', 'string');
|
|
|
|
$this->addType('password', 'string');
|
|
|
|
$this->addType('name', 'string');
|
|
|
|
$this->addType('token', 'string');
|
2016-10-31 16:38:41 +03:00
|
|
|
$this->addType('type', 'int');
|
2018-05-15 11:24:46 +03:00
|
|
|
$this->addType('remember', 'int');
|
2016-10-31 16:38:41 +03:00
|
|
|
$this->addType('lastActivity', 'int');
|
|
|
|
$this->addType('lastCheck', 'int');
|
2018-05-15 11:24:46 +03:00
|
|
|
$this->addType('scope', 'string');
|
2018-05-16 13:39:00 +03:00
|
|
|
$this->addType('expires', 'int');
|
2018-05-18 20:48:08 +03:00
|
|
|
$this->addType('version', 'int');
|
|
|
|
|
|
|
|
$this->setVersion(1);
|
2016-10-31 16:38:41 +03:00
|
|
|
}
|
|
|
|
|
2018-05-15 11:24:46 +03:00
|
|
|
public function getId(): int {
|
2016-04-26 12:32:35 +03:00
|
|
|
return $this->id;
|
2016-04-25 15:10:55 +03:00
|
|
|
}
|
|
|
|
|
2018-05-15 11:24:46 +03:00
|
|
|
public function getUID(): string {
|
2016-04-27 10:38:30 +03:00
|
|
|
return $this->uid;
|
|
|
|
}
|
2016-05-24 11:50:18 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the login name used when generating the token
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-05-15 11:24:46 +03:00
|
|
|
public function getLoginName(): string {
|
2016-05-24 11:50:18 +03:00
|
|
|
return parent::getLoginName();
|
|
|
|
}
|
|
|
|
|
2016-05-17 18:20:54 +03:00
|
|
|
/**
|
|
|
|
* Get the (encrypted) login password
|
|
|
|
*
|
2018-05-15 11:56:40 +03:00
|
|
|
* @return string|null
|
2016-05-17 18:20:54 +03:00
|
|
|
*/
|
2018-05-15 11:56:40 +03:00
|
|
|
public function getPassword() {
|
2016-05-17 18:20:54 +03:00
|
|
|
return parent::getPassword();
|
|
|
|
}
|
2016-04-27 10:38:30 +03:00
|
|
|
|
2016-05-18 13:03:22 +03:00
|
|
|
public function jsonSerialize() {
|
|
|
|
return [
|
|
|
|
'id' => $this->id,
|
|
|
|
'name' => $this->name,
|
2016-10-31 16:38:41 +03:00
|
|
|
'lastActivity' => $this->lastActivity,
|
|
|
|
'type' => $this->type,
|
2016-08-03 16:57:06 +03:00
|
|
|
'scope' => $this->getScopeAsArray()
|
2016-05-18 13:03:22 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-06-17 14:59:15 +03:00
|
|
|
/**
|
|
|
|
* Get the timestamp of the last password check
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2018-05-15 11:24:46 +03:00
|
|
|
public function getLastCheck(): int {
|
2016-06-17 14:59:15 +03:00
|
|
|
return parent::getLastCheck();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the timestamp of the last password check
|
|
|
|
*
|
|
|
|
* @param int $time
|
|
|
|
*/
|
2018-05-15 11:56:40 +03:00
|
|
|
public function setLastCheck(int $time) {
|
|
|
|
parent::setLastCheck($time);
|
2016-06-17 14:59:15 +03:00
|
|
|
}
|
|
|
|
|
2018-05-15 11:24:46 +03:00
|
|
|
public function getScope(): string {
|
2018-05-15 11:56:40 +03:00
|
|
|
$scope = parent::getScope();
|
|
|
|
if ($scope === null) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $scope;
|
2016-08-03 16:57:06 +03:00
|
|
|
}
|
|
|
|
|
2018-05-15 11:24:46 +03:00
|
|
|
public function getScopeAsArray(): array {
|
2016-08-03 16:57:06 +03:00
|
|
|
$scope = json_decode($this->getScope(), true);
|
|
|
|
if (!$scope) {
|
|
|
|
return [
|
2016-10-19 16:11:58 +03:00
|
|
|
'filesystem'=> true
|
2016-08-03 16:57:06 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
return $scope;
|
2016-08-01 20:06:54 +03:00
|
|
|
}
|
|
|
|
|
2018-05-15 12:41:27 +03:00
|
|
|
public function setScope($scope) {
|
|
|
|
if (\is_array($scope)) {
|
2018-05-15 11:56:40 +03:00
|
|
|
parent::setScope(json_encode($scope));
|
2018-05-15 12:41:27 +03:00
|
|
|
} else {
|
|
|
|
parent::setScope((string)$scope);
|
2018-05-15 11:56:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName(): string {
|
|
|
|
return parent::getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRemember(): int {
|
|
|
|
return parent::getRemember();
|
2016-08-01 20:06:54 +03:00
|
|
|
}
|
2018-05-15 22:10:43 +03:00
|
|
|
|
|
|
|
public function setToken(string $token) {
|
|
|
|
parent::setToken($token);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setPassword(string $password = null) {
|
|
|
|
parent::setPassword($password);
|
|
|
|
}
|
2018-05-16 13:39:00 +03:00
|
|
|
|
|
|
|
public function setExpires($expires) {
|
|
|
|
parent::setExpires($expires);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int|null
|
|
|
|
*/
|
|
|
|
public function getExpires() {
|
|
|
|
return parent::getExpires();
|
|
|
|
}
|
2016-04-25 15:10:55 +03:00
|
|
|
}
|