Completed
Push — master ( c6ce32...3be0da )
by Morris
10s
created

WopiMapper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 4
crap 2
1
<?php
2
/**
3
 * @copyright 2018, Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OCA\Richdocuments\Db;
24
25
use OCP\AppFramework\Db\DoesNotExistException;
26
use OCP\AppFramework\Db\Mapper;
27
use OCP\AppFramework\Utility\ITimeFactory;
28
use OCP\IDBConnection;
29
use OCP\ILogger;
30
use OCP\Security\ISecureRandom;
31
32
class WopiMapper extends Mapper {
33
	// Tokens expire after this many seconds (not defined by WOPI specs).
34
	const TOKEN_LIFETIME_SECONDS = 1800;
35
36
	/** @var ISecureRandom */
37
	private $random;
38
39
	/** @var ILogger */
40
	private $logger;
41
42
	/** @var ITimeFactory */
43
	private $timeFactory;
44
45
	public function __construct(IDBConnection $db,
46
								ISecureRandom $random,
47
								ILogger $logger,
48
								ITimeFactory $timeFactory) {
49
		parent::__construct($db, 'richdocuments_wopi', Wopi::class);
50
51
		$this->random = $random;
52
		$this->logger = $logger;
53
		$this->timeFactory = $timeFactory;
54
	}
55
56
	/**
57
	 * @param int $fileId
58
	 * @param string $owner
59
	 * @param string$editor
60
	 * @param int $version
61
	 * @param bool $updatable
62
	 * @param string $serverHost
63
	 * @return Wopi
64
	 */
65
	public function generateFileToken($fileId, $owner, $editor, $version, $updatable, $serverHost) {
66
		$token = $this->random->generate(32, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);
67
68
		$wopi = Wopi::fromParams([
69
			'fileid' => $fileId,
70
			'ownerUid' => $owner,
71
			'editorUid' => $editor,
72
			'version' => $version,
73
			'canwrite' => $updatable,
74
			'serverHost' => $serverHost,
75
			'token' => $token,
76
			'expiry' => $this->timeFactory->getTime() + self::TOKEN_LIFETIME_SECONDS,
77
		]);
78
79
		/** @var Wopi $wopi */
80
		$wopi = $this->insert($wopi);
81
82
		return $wopi;
83
	}
84
85
	/**
86
	 * Given a token, validates it and
87
	 * constructs and validates the path.
88
	 * Returns the path, if valid, else false.
89
	 *
90
	 * @param string $token
91
	 * @throws DoesNotExistException
92
	 * @return Wopi
93
	 */
94
	public function getPathForToken($token) {
95
96
		$qb = $this->db->getQueryBuilder();
97
		$qb->select('*')
98
			->from('richdocuments_wopi')
99
			->where(
100
				$qb->expr()->eq('token', $qb->createNamedParameter($token))
101
			);
102
		$result = $qb->execute();
103
		$row = $result->fetch();
104
		$result->closeCursor();
105
106
		$this->logger->debug('Loaded WOPI Token record: {row}.', [
107
			'row' => $row,
108
			'app' => 'richdocuments'
109
		]);
110
		if ($row === false) {
111
			throw new DoesNotExistException('Could not find token.');
112
		}
113
114
		/** @var Wopi $wopi */
115
		$wopi = Wopi::fromRow($row);
116
117
		//TODO: validate.
118
		if ($wopi->getExpiry() > $this->timeFactory->getTime()){
119
			// Expired token!
120
			//http_response_code(404);
121
			//$wopi->deleteBy('id', $row['id']);
122
			//return false;
123
		}
124
125
		return $wopi;
126
	}
127
}
128