1<?php
2
3declare(strict_types=1);
4
5/**
6 * @copyright Copyright (c) 2016 Roeland Jago Douma <roeland@famdouma.nl>
7 *
8 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
9 * @author Daniel Kesselberg <mail@danielkesselberg.de>
10 * @author Roeland Jago Douma <roeland@famdouma.nl>
11 *
12 * @license GNU AGPL version 3 or any later version
13 *
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Affero General Public License as
16 * published by the Free Software Foundation, either version 3 of the
17 * License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Affero General Public License for more details.
23 *
24 * You should have received a copy of the GNU Affero General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 *
27 */
28namespace OC\Authentication\Token;
29
30use OCP\AppFramework\Db\Entity;
31
32/**
33 * @method void setId(int $id)
34 * @method void setUid(string $uid);
35 * @method void setLoginName(string $loginname)
36 * @method string getToken()
37 * @method void setType(int $type)
38 * @method int getType()
39 * @method void setRemember(int $remember)
40 * @method void setLastActivity(int $lastactivity)
41 * @method int getLastActivity()
42 * @method string getPrivateKey()
43 * @method void setPrivateKey(string $key)
44 * @method string getPublicKey()
45 * @method void setPublicKey(string $key)
46 * @method void setVersion(int $version)
47 * @method bool getPasswordInvalid()
48 */
49class PublicKeyToken extends Entity implements INamedToken, IWipeableToken {
50	public const VERSION = 2;
51
52	/** @var string user UID */
53	protected $uid;
54
55	/** @var string login name used for generating the token */
56	protected $loginName;
57
58	/** @var string encrypted user password */
59	protected $password;
60
61	/** @var string token name (e.g. browser/OS) */
62	protected $name;
63
64	/** @var string */
65	protected $token;
66
67	/** @var int */
68	protected $type;
69
70	/** @var int */
71	protected $remember;
72
73	/** @var int */
74	protected $lastActivity;
75
76	/** @var int */
77	protected $lastCheck;
78
79	/** @var string */
80	protected $scope;
81
82	/** @var int */
83	protected $expires;
84
85	/** @var string */
86	protected $privateKey;
87
88	/** @var string */
89	protected $publicKey;
90
91	/** @var int */
92	protected $version;
93
94	/** @var bool */
95	protected $passwordInvalid;
96
97	public function __construct() {
98		$this->addType('uid', 'string');
99		$this->addType('loginName', 'string');
100		$this->addType('password', 'string');
101		$this->addType('name', 'string');
102		$this->addType('token', 'string');
103		$this->addType('type', 'int');
104		$this->addType('remember', 'int');
105		$this->addType('lastActivity', 'int');
106		$this->addType('lastCheck', 'int');
107		$this->addType('scope', 'string');
108		$this->addType('expires', 'int');
109		$this->addType('publicKey', 'string');
110		$this->addType('privateKey', 'string');
111		$this->addType('version', 'int');
112		$this->addType('passwordInvalid', 'bool');
113	}
114
115	public function getId(): int {
116		return $this->id;
117	}
118
119	public function getUID(): string {
120		return $this->uid;
121	}
122
123	/**
124	 * Get the login name used when generating the token
125	 *
126	 * @return string
127	 */
128	public function getLoginName(): string {
129		return parent::getLoginName();
130	}
131
132	/**
133	 * Get the (encrypted) login password
134	 *
135	 * @return string|null
136	 */
137	public function getPassword() {
138		return parent::getPassword();
139	}
140
141	public function jsonSerialize() {
142		return [
143			'id' => $this->id,
144			'name' => $this->name,
145			'lastActivity' => $this->lastActivity,
146			'type' => $this->type,
147			'scope' => $this->getScopeAsArray()
148		];
149	}
150
151	/**
152	 * Get the timestamp of the last password check
153	 *
154	 * @return int
155	 */
156	public function getLastCheck(): int {
157		return parent::getLastCheck();
158	}
159
160	/**
161	 * Get the timestamp of the last password check
162	 *
163	 * @param int $time
164	 */
165	public function setLastCheck(int $time) {
166		parent::setLastCheck($time);
167	}
168
169	public function getScope(): string {
170		$scope = parent::getScope();
171		if ($scope === null) {
172			return '';
173		}
174
175		return $scope;
176	}
177
178	public function getScopeAsArray(): array {
179		$scope = json_decode($this->getScope(), true);
180		if (!$scope) {
181			return [
182				'filesystem' => true
183			];
184		}
185		return $scope;
186	}
187
188	public function setScope($scope) {
189		if (is_array($scope)) {
190			parent::setScope(json_encode($scope));
191		} else {
192			parent::setScope((string)$scope);
193		}
194	}
195
196	public function getName(): string {
197		return parent::getName();
198	}
199
200	public function setName(string $name): void {
201		parent::setName($name);
202	}
203
204	public function getRemember(): int {
205		return parent::getRemember();
206	}
207
208	public function setToken(string $token) {
209		parent::setToken($token);
210	}
211
212	public function setPassword(string $password = null) {
213		parent::setPassword($password);
214	}
215
216	public function setExpires($expires) {
217		parent::setExpires($expires);
218	}
219
220	/**
221	 * @return int|null
222	 */
223	public function getExpires() {
224		return parent::getExpires();
225	}
226
227	public function setPasswordInvalid(bool $invalid) {
228		parent::setPasswordInvalid($invalid);
229	}
230
231	public function wipe(): void {
232		parent::setType(IToken::WIPE_TOKEN);
233	}
234}
235