Completed
Push — master ( 04edcb...d3404c )
by
unknown
07:52
created

WopiMapper::getPathForToken()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 33
ccs 0
cts 22
cp 0
crap 12
rs 9.392
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
	 * @param string $guestDisplayname
64
	 * @return Wopi
65
	 */
66
	public function generateFileToken($fileId, $owner, $editor, $version, $updatable, $serverHost, $guestDisplayname) {
67
		$token = $this->random->generate(32, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);
68
69
		$wopi = Wopi::fromParams([
70
			'fileid' => $fileId,
71
			'ownerUid' => $owner,
72
			'editorUid' => $editor,
73
			'version' => $version,
74
			'canwrite' => $updatable,
75
			'serverHost' => $serverHost,
76
			'token' => $token,
77
			'expiry' => $this->timeFactory->getTime() + self::TOKEN_LIFETIME_SECONDS,
78
			'guestDisplayname' => $guestDisplayname,
79
		]);
80
81
		/** @var Wopi $wopi */
82
		$wopi = $this->insert($wopi);
83
84
		return $wopi;
85
	}
86
87
	/**
88
	 * Given a token, validates it and
89
	 * constructs and validates the path.
90
	 * Returns the path, if valid, else false.
91
	 *
92
	 * @param string $token
93
	 * @throws DoesNotExistException
94
	 * @return Wopi
95
	 */
96
	public function getPathForToken($token) {
97
98
		$qb = $this->db->getQueryBuilder();
99
		$qb->select('*')
100
			->from('richdocuments_wopi')
101
			->where(
102
				$qb->expr()->eq('token', $qb->createNamedParameter($token))
103
			);
104
		$result = $qb->execute();
105
		$row = $result->fetch();
106
		$result->closeCursor();
107
108
		$this->logger->debug('Loaded WOPI Token record: {row}.', [
109
			'row' => $row,
110
			'app' => 'richdocuments'
111
		]);
112
		if ($row === false) {
113
			throw new DoesNotExistException('Could not find token.');
114
		}
115
116
		/** @var Wopi $wopi */
117
		$wopi = Wopi::fromRow($row);
118
119
		//TODO: validate.
120
		if ($wopi->getExpiry() > $this->timeFactory->getTime()){
121
			// Expired token!
122
			//http_response_code(404);
123
			//$wopi->deleteBy('id', $row['id']);
124
			//return false;
125
		}
126
127
		return $wopi;
128
	}
129
}
130