[Libreoffice-commits] online.git: loleaflet/dist loleaflet/main.js loleaflet/src wsd/FileServer.cpp

Pranav Kant pranavk at collabora.co.uk
Fri Dec 2 12:39:09 UTC 2016


 loleaflet/dist/errormessages.js |    2 ++
 loleaflet/dist/loleaflet.html   |    1 +
 loleaflet/main.js               |    2 +-
 loleaflet/src/core/Socket.js    |   23 +++++++++++++++++++++++
 wsd/FileServer.cpp              |   12 +++++++++++-
 5 files changed, 38 insertions(+), 2 deletions(-)

New commits:
commit dde653f9208342f42ea207cb840a27bb089d7347
Author: Pranav Kant <pranavk at collabora.co.uk>
Date:   Tue Nov 29 21:17:18 2016 +0530

    tdf#103825: Prompt the user when session is about to expire
    
    Set a timer in loleaflet 15 minutes before access token expiry
    date (access_token_ttl value) to prompt the user to save and
    refresh the session.
    
    Change-Id: I98c3e47c9b7031e29e002f653d488747b9c17df8
    Reviewed-on: https://gerrit.libreoffice.org/31381
    Reviewed-by: Jan Holesovsky <kendy at collabora.com>
    Tested-by: Jan Holesovsky <kendy at collabora.com>

diff --git a/loleaflet/dist/errormessages.js b/loleaflet/dist/errormessages.js
index 036f965..83b6674 100644
--- a/loleaflet/dist/errormessages.js
+++ b/loleaflet/dist/errormessages.js
@@ -4,6 +4,8 @@ exports.limitreached = _('This development build is limited to %0 documents, and
 exports.serviceunavailable = _('Service is unavailable. Please try again later and report to your administrator if the issue persists.');
 exports.unauthorized = _('Unauthorized WOPI host. Please try again later and report to your administrator if the issue persists.');
 exports.wrongwopisrc = _('Wrong WOPISrc, usage: WOPISrc=valid encoded URI, or file_path, usage: file_path=/path/to/doc/');
+exports.sessionexpiry = _('Your session will expire in %time. Please save your work and refresh the session (or webpage) to continue.');
+exports.sessionexpired = _('Your session has been expired. Further changes to document might not be saved. Please refresh the session (or webpage) to continue.');
 
 exports.storage = {
 	savediskfull: _('Save failed due to no disk space left on storage server. Document will now be read-only. Please contact the server administrator to continue editing.'),
diff --git a/loleaflet/dist/loleaflet.html b/loleaflet/dist/loleaflet.html
index 084b710..b154b08 100644
--- a/loleaflet/dist/loleaflet.html
+++ b/loleaflet/dist/loleaflet.html
@@ -69,6 +69,7 @@
     <script>
       window.host = '%HOST%';
       window.access_token = '%ACCESS_TOKEN%';
+      window.access_token_ttl = '%ACCESS_TOKEN_TTL%';
     </script>
     <script src="/loleaflet/%VERSION%/branding.js"></script> <!-- logo onclick handler -->
     <script src="/loleaflet/%VERSION%/bundle.js"></script>
diff --git a/loleaflet/main.js b/loleaflet/main.js
index 93e8511..bd9dd1d 100644
--- a/loleaflet/main.js
+++ b/loleaflet/main.js
@@ -48,7 +48,7 @@ require('./dist/plugins/draw-0.2.4/dist/leaflet.draw.js');
 var wopiSrc = getParameterByName('WOPISrc');
 
 if (wopiSrc !== '' && access_token !== '') {
-    var wopiParams = { 'access_token': access_token };
+	var wopiParams = { 'access_token': access_token, 'access_token_ttl': access_token_ttl };
 }
 
 var filePath = getParameterByName('file_path');
diff --git a/loleaflet/src/core/Socket.js b/loleaflet/src/core/Socket.js
index 40c4cf2..5bb9f6c 100644
--- a/loleaflet/src/core/Socket.js
+++ b/loleaflet/src/core/Socket.js
@@ -22,14 +22,37 @@ L.Socket = L.Class.extend({
 			this._map.fire('error', {msg: _('Oops, there is a problem connecting to LibreOffice Online : ' + e), cmd: 'socket', kind: 'failed', id: 3});
 			return null;
 		}
+
+		if (map.options.docParams.access_token && parseInt(map.options.docParams.access_token_ttl)) {
+			var tokenExpiryWarning = 900 * 1000; // Warn when 15 minutes remain
+			clearTimeout(this._accessTokenExpireTimeout);
+			this._accessTokenExpireTimeout = setTimeout(L.bind(this._sessionExpiredWarning, this),
+			                                            parseInt(map.options.docParams.access_token_ttl) - Date.now() - tokenExpiryWarning);
+		}
 		this._msgQueue = [];
 	},
 
+	_sessionExpiredWarning: function() {
+		clearTimeout(this._accessTokenExpireTimeout);
+		var expirymsg = errorMessages.sessionexpiry;
+		if (parseInt(this._map.options.docParams.access_token_ttl) - Date.now() <= 0) {
+			expirymsg = errorMessages.sessionexpired;
+		}
+		var timerepr = $.timeago(parseInt(this._map.options.docParams.access_token_ttl)).replace(' ago', '');
+		this._map.fire('warn', {msg: expirymsg.replace('%time', timerepr)});
+
+		// If user still doesn't refresh the session, warn again periodically
+		this._accessTokenExpireTimeout = setTimeout(L.bind(this._sessionExpiredWarning, this),
+		                                            120 * 1000);
+	},
+
 	close: function () {
 		this.socket.onerror = function () {};
 		this.socket.onclose = function () {};
 		this.socket.onmessage = function () {};
 		this.socket.close();
+
+		clearTimeout(this._accessTokenExpireTimeout);
 	},
 
 	connected: function() {
diff --git a/wsd/FileServer.cpp b/wsd/FileServer.cpp
index 5c71764..1c9ba47 100644
--- a/wsd/FileServer.cpp
+++ b/wsd/FileServer.cpp
@@ -236,8 +236,18 @@ void FileServerRequestHandler::preprocessFile(HTTPServerRequest& request, HTTPSe
     std::string escapedAccessToken;
     Poco::URI::encode(accessToken, "'", escapedAccessToken);
 
+    unsigned long tokenTtl = 0;
+    try
+    {
+        tokenTtl = std::stoul(accessTokenTtl);
+    }
+    catch(const std::exception& exc)
+    {
+        LOG_ERR("access_token_ttl must be a unix timestamp of when token will expire");
+    }
+
     Poco::replaceInPlace(preprocess, std::string("%ACCESS_TOKEN%"), escapedAccessToken);
-    Poco::replaceInPlace(preprocess, std::string("%ACCESS_TOKEN_TTL%"), accessTokenTtl);
+    Poco::replaceInPlace(preprocess, std::string("%ACCESS_TOKEN_TTL%"), std::to_string(tokenTtl));
     Poco::replaceInPlace(preprocess, std::string("%HOST%"), host);
     Poco::replaceInPlace(preprocess, std::string("%VERSION%"), std::string(LOOLWSD_VERSION_HASH));
 


More information about the Libreoffice-commits mailing list