1<?php
2
3declare(strict_types=1);
4
5
6/**
7 * Some tools for myself.
8 *
9 * This file is licensed under the Affero General Public License version 3 or
10 * later. See the COPYING file.
11 *
12 * @author Maxence Lange <maxence@artificial-owl.com>
13 * @copyright 2020, Maxence Lange <maxence@artificial-owl.com>
14 * @license GNU AGPL version 3 or any later version
15 *
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU Affero General Public License as
18 * published by the Free Software Foundation, either version 3 of the
19 * License, or (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 * GNU Affero General Public License for more details.
25 *
26 * You should have received a copy of the GNU Affero General Public License
27 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28 *
29 */
30
31
32namespace ArtificialOwl\MySmallPhpTools\Model\Nextcloud\nc22;
33
34
35use ArtificialOwl\MySmallPhpTools\Traits\TArrayTools;
36use JsonSerializable;
37
38
39/**
40 * Class NC22Signatory
41 *
42 * @package ArtificialOwl\MySmallPhpTools\Model\Nextcloud\nc22
43 */
44class NC22Signatory implements JsonSerializable {
45
46
47	use TArrayTools;
48
49
50	const SHA256 = 'sha256';
51	const SHA512 = 'sha512';
52
53
54	/** @var string */
55	private $instance = '';
56
57	/** @var string */
58	private $id = '';
59
60	/** @var string */
61	private $keyOwner = '';
62
63	/** @var string */
64	private $keyId = '';
65
66	/** @var string */
67	private $publicKey = '';
68
69	/** @var string */
70	private $privateKey = '';
71
72	/** @var array */
73	private $origData = [];
74
75	/** @var string */
76	private $algorithm = self::SHA256;
77
78
79	/**
80	 * NC22Signatory constructor.
81	 *
82	 * @param string $id
83	 */
84	public function __construct(string $id = '') {
85		$this->id = self::removeFragment($id);
86	}
87
88
89	/**
90	 * @param string $instance
91	 *
92	 * @return self
93	 */
94	public function setInstance(string $instance): self {
95		$this->instance = $instance;
96
97		return $this;
98	}
99
100	/**
101	 * @return string
102	 */
103	public function getInstance(): string {
104		return $this->instance;
105	}
106
107
108	/**
109	 * @return array
110	 */
111	public function getOrigData(): array {
112		return $this->origData;
113	}
114
115	/**
116	 * /**
117	 * @param array $data
118	 *
119	 * @return $this
120	 */
121	public function setOrigData(array $data): self {
122		$this->origData = $data;
123
124		return $this;
125	}
126
127
128	/**
129	 * @return string
130	 */
131	public function getId(): string {
132		return $this->id;
133	}
134
135	/**
136	 * @param string $id
137	 *
138	 * @return self
139	 */
140	public function setId(string $id): self {
141		$this->id = $id;
142
143		return $this;
144	}
145
146
147	/**
148	 * @param string $keyId
149	 *
150	 * @return self
151	 */
152	public function setKeyId(string $keyId): self {
153		$this->keyId = $keyId;
154
155		return $this;
156	}
157
158	/**
159	 * @return string
160	 */
161	public function getKeyId(): string {
162		return $this->keyId;
163	}
164
165
166	/**
167	 * @param string $keyOwner
168	 *
169	 * @return self
170	 */
171	public function setKeyOwner(string $keyOwner): self {
172		$this->keyOwner = $keyOwner;
173
174		return $this;
175	}
176
177	/**
178	 * @return string
179	 */
180	public function getKeyOwner(): string {
181		return $this->keyOwner;
182	}
183
184
185	/**
186	 * @param string $publicKey
187	 *
188	 * @return self
189	 */
190	public function setPublicKey(string $publicKey): self {
191		$this->publicKey = $publicKey;
192
193		return $this;
194	}
195
196	/**
197	 * @param string $privateKey
198	 *
199	 * @return self
200	 */
201	public function setPrivateKey(string $privateKey): self {
202		$this->privateKey = $privateKey;
203
204		return $this;
205	}
206
207	/**
208	 * @return string
209	 */
210	public function getPublicKey(): string {
211		return $this->publicKey;
212	}
213
214	/**
215	 * @return string
216	 */
217	public function getPrivateKey(): string {
218		return $this->privateKey;
219	}
220
221	/**
222	 * @return bool
223	 */
224	public function hasPublicKey(): bool {
225		return ($this->publicKey !== '');
226	}
227
228	/**
229	 * @return bool
230	 */
231	public function hasPrivateKey(): bool {
232		return ($this->privateKey !== '');
233	}
234
235
236	/**
237	 * @param string $algorithm
238	 *
239	 * @return self
240	 */
241	public function setAlgorithm(string $algorithm): self {
242		$this->algorithm = $algorithm;
243
244		return $this;
245	}
246
247	/**
248	 * @return string
249	 */
250	public function getAlgorithm(): string {
251		return $this->algorithm;
252	}
253
254
255	/**
256	 * @param array $data
257	 *
258	 * @return $this
259	 */
260	public function import(array $data): self {
261		if ($this->getId() === '') {
262			$this->setId($this->get('id', $data));
263		}
264
265		$this->setKeyId($this->get('publicKey.id', $data));
266		$this->setKeyOwner($this->get('publicKey.owner', $data));
267		$this->setPublicKey($this->get('publicKey.publicKeyPem', $data));
268
269		return $this;
270	}
271
272
273	/**
274	 * @return array
275	 */
276	public function jsonSerialize(): array {
277		return [
278			'id'        => $this->getId(),
279			'publicKey' =>
280				[
281					'id'           => $this->getKeyId(),
282					'owner'        => $this->getKeyOwner(),
283					'publicKeyPem' => $this->getPublicKey()
284				]
285		];
286	}
287
288
289	/**
290	 * @param string $id
291	 *
292	 * @return string
293	 */
294	public static function removeFragment(string $id): string {
295		$temp = strtok($id, '#');
296		if (is_string($temp)) {
297			$id = $temp;
298		}
299
300		return $id;
301	}
302
303}
304
305