From 32153209d1bb7faf0d803dcab14afe98251ff324 Mon Sep 17 00:00:00 2001
From: Damyan Mitev <damyan.mitev@vereign.com>
Date: Mon, 5 Aug 2019 12:52:13 +0300
Subject: [PATCH] add getAccessToken function

---
 javascript/src/iframe/viamapi-iframe.js | 20 +++++++++++-----
 javascript/src/iframe/wopiapi-iframe.js | 32 +++++++++++++++++++++++++
 2 files changed, 46 insertions(+), 6 deletions(-)

diff --git a/javascript/src/iframe/viamapi-iframe.js b/javascript/src/iframe/viamapi-iframe.js
index 8a67381..b4a3471 100644
--- a/javascript/src/iframe/viamapi-iframe.js
+++ b/javascript/src/iframe/viamapi-iframe.js
@@ -1623,7 +1623,7 @@ const connection = Penpal.connectToParent({
       return response.data;
     },
 
-    wopiCreateDocument: async (passportUUID, path, contentType, title) => {
+    wopiCreateDocument: async (passportUUID, path, title) => {
       const authenticationPublicKey = localStorage.getItem(
         "authenticatedIdentity"
       );
@@ -1639,7 +1639,6 @@ const connection = Penpal.connectToParent({
         headers: {
           path,
           passportuuid: passportUUID,
-          contentType,
           title
         }
       };
@@ -1651,11 +1650,20 @@ const connection = Penpal.connectToParent({
       );
       if (executeResult.code !== "200") return executeResult;
       const resourceID = executeResult.data;
-      const passports = await wopiAPI.getPassports(resourceID, contentType);
-      return passports;
+
+      const accessTokenResponse = await wopiAPI.getAccessToken(passportUUID, resourceID)
+      if (accessTokenResponse.code !== "200") {
+        return accessTokenResponse;
+      }
+      const accessToken = accessTokenResponse.data;
+      const result = {
+        resourceID,
+        accessToken
+      };
+      return encodeResponse("200", result, "ok");
     },
 
-    wopiPutFile: async (path, accessToken, file) => {
+    wopiPutFile: async (resourceID, accessToken, file) => {
       const authenticationPublicKey = localStorage.getItem(
         "authenticatedIdentity"
       );
@@ -1668,7 +1676,7 @@ const connection = Penpal.connectToParent({
         return encodeResponse("400", "", "Identity not authenticated");
       }
 
-      const response = await wopiAPI.putDocument(path, accessToken, file);
+      const response = await wopiAPI.putDocument(resourceID, accessToken, file);
       return response.data;
     }
   }
diff --git a/javascript/src/iframe/wopiapi-iframe.js b/javascript/src/iframe/wopiapi-iframe.js
index 9de61de..1cde86a 100644
--- a/javascript/src/iframe/wopiapi-iframe.js
+++ b/javascript/src/iframe/wopiapi-iframe.js
@@ -47,6 +47,38 @@ WopiAPI.prototype.getPassports = function(fileID) {
   return axios(requestConfig);
 };
 
+// contentType is optional.
+// When not specified, function returns token, which can write multiple files and only read the last one.
+// When specified, function returns read-write token for the specified contentType.
+WopiAPI.prototype.getAccessToken = function(passportUUID, resourceID, contentType = "") {
+  const {
+    publicKey,
+    uuid,
+    token,
+    deviceHash
+  } = window.viamApi.getConfig().headers;
+
+  if (!contentType) {
+    contentType = "";
+  }
+
+  const requestConfig = {
+    url: `${window.WOPI_URL}getAccessToken`,
+    method: "POST",
+    headers: {
+      publicKey,
+      uuid,
+      token,
+      deviceHash,
+      passportUUID: encodeURI(passportUUID),
+      resourceID: encodeURI(resourceID),
+      contentType: encodeURI(contentType)
+    }
+  };
+
+  return axios(requestConfig);
+}
+
 WopiAPI.prototype.putDocument = function(resourceID, accessToken, file) {
   const {
     publicKey,
-- 
GitLab