1<?php
2/**
3 * @author Christoph Wurst <christoph@owncloud.com>
4 *
5 * @copyright Copyright (c) 2018, ownCloud GmbH
6 * @license AGPL-3.0
7 *
8 * This code is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License, version 3,
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License, version 3,
18 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19 *
20 */
21
22namespace OC\Authentication\Token;
23
24use OCP\AppFramework\Db\Entity;
25
26/**
27 * @method void setId(int $id)
28 * @method void setUid(string $uid);
29 * @method void setPassword(string $password)
30 * @method string getName()
31 * @method void setToken(string $token)
32 * @method string getToken()
33 * @method void setType(string $type)
34 * @method int getType()
35 * @method void setLastActivity(int $lastActivity)
36 * @method int getLastActivity()
37 */
38class DefaultToken extends Entity implements IToken {
39
40	/**
41	 * @var string user UID
42	 */
43	protected $uid;
44
45	/**
46	 * @var string login name used for generating the token
47	 */
48	protected $loginName;
49
50	/**
51	 * @var string encrypted user password
52	 */
53	protected $password;
54
55	/**
56	 * @var string token name (e.g. browser/OS)
57	 */
58	protected $name;
59
60	/**
61	 * @var string
62	 */
63	protected $token;
64
65	/**
66	 * @var int
67	 */
68	protected $type;
69
70	/**
71	 * @var int
72	 */
73	protected $lastActivity;
74
75	/**
76	 * @var int
77	 */
78	protected $lastCheck;
79
80	public function getId() {
81		return $this->id;
82	}
83
84	public function getUID() {
85		return $this->uid;
86	}
87
88	/**
89	 * Get the login name used when generating the token
90	 *
91	 * @return string
92	 */
93	public function getLoginName() {
94		return $this->loginName;
95	}
96
97	/**
98	 * Get the (encrypted) login password
99	 *
100	 * @return string
101	 */
102	public function getPassword() {
103		return $this->password;
104	}
105
106	public function jsonSerialize() {
107		return [
108			'id' => $this->id,
109			'name' => $this->name,
110			'lastActivity' => $this->lastActivity,
111			'type' => $this->type,
112			'canDelete' => true,
113		];
114	}
115
116	/**
117	 * Get the timestamp of the last password check
118	 *
119	 * @return int
120	 */
121	public function getLastCheck() {
122		return $this->lastCheck;
123	}
124
125	/**
126	 * Get the timestamp of the last password check
127	 *
128	 * @param int $time
129	 */
130	public function setLastCheck($time) {
131		$this->setter('lastCheck', [$time]);
132	}
133
134	/**
135	 * @param string $name
136	 */
137	public function setName($name) {
138		if (\strlen($name) < 1) {
139			throw new \InvalidArgumentException();
140		}
141		$this->setter('name', [$name]);
142	}
143
144	/**
145	 * @param string $loginName
146	 */
147	public function setLoginName($loginName) {
148		if (\strlen($loginName) < 1) {
149			throw new \InvalidArgumentException();
150		}
151		$this->setter('loginName', [$loginName]);
152	}
153}
154