diff --git a/appinfo/database.xml b/appinfo/database.xml
index 443a4f54..7d7c9fce 100644
--- a/appinfo/database.xml
+++ b/appinfo/database.xml
@@ -274,4 +274,53 @@
+
+ *dbprefix*richdocuments_wopi
+
+
+ id
+ integer
+ true
+ 1
+ true
+ 4
+ Unique per token
+
+
+ uid
+ text
+ 64
+ UserId - a textual user identifier (unique?)
+
+
+ fileid
+ integer
+ true
+ 4
+ The unique ID of the file authorized
+
+
+ path
+ text
+ true
+ 512
+ Relative to storage e.g. /welcome.odt
+
+
+ token
+ text
+
+ true
+ 32
+ File access token
+
+
+ expiry
+ integer
+ true
+ 4
+ Expiration time of the token
+
+
+
diff --git a/appinfo/info.xml b/appinfo/info.xml
index 5d9b74a5..4db1815e 100644
--- a/appinfo/info.xml
+++ b/appinfo/info.xml
@@ -4,7 +4,7 @@
Collabora Online Development Edition
An ownCloud app to work with office documents
AGPL
- 0.12.0
+ 0.13.0
Collabora Productivity based on work of Frank Karlitschek, Victor Dubiniuk
https://www.collaboraoffice.com/
git://gerrit.libreoffice.org/online.git
diff --git a/lib/db/wopi.php b/lib/db/wopi.php
new file mode 100644
index 00000000..306204d3
--- /dev/null
+++ b/lib/db/wopi.php
@@ -0,0 +1,101 @@
+getPath($fileId);
+
+ if (!$view->is_file($path)) {
+ throw new \Exception('Invalid fileId.');
+ }
+
+ $token = \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate(32,
+ \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_UPPER .
+ \OCP\Security\ISecureRandom::CHAR_DIGITS);
+
+ \OC::$server->getLogger()->debug('Issuing token for {user} file {fileId}: {token}',
+ [ 'user' => $user, 'fileId' => $fileId, 'token' => $token ]);
+
+ $wopi = new \OCA\Richdocuments\Db\Wopi([
+ $user,
+ $fileId,
+ $path,
+ $token,
+ time() + self::TOKEN_LIFETIME_SECONDS
+ ]);
+
+ if (!$wopi->insert()){
+ throw new \Exception('Failed to add wopi token into database');
+ }
+
+ return $token;
+ }
+
+ /*
+ * Given a token, validates it and
+ * constructs and validates the path.
+ * Returns the path, if valid, else false.
+ */
+ public function getPathForToken($fileId, $token){
+
+ $wopi = new Wopi();
+ $row = $wopi->loadBy('token', $token)->getData();
+ \OC::$server->getLogger()->debug('Loaded WOPI Token record: {row}.', [ 'row' => $row ]);
+
+ //TODO: validate.
+ if ($row['expiry'] > time() || $row['fileid'] !== $fileId){
+ // Expired token!
+ //$wopi->deleteBy('id', $row['id']);
+ //return false;
+ }
+
+ $user = $row['uid'];
+ $view = new \OC\Files\View('/' . $user . '/');
+ $path = $row['path'];
+
+ if (!$view->is_file($path)) {
+ throw new \Exception('Invalid file path.');
+ }
+
+ return array('user' => $user, 'path' => $path);
+ }
+}