From 95403b8135bc28ef4b897c53f096c4b12e5abb55 Mon Sep 17 00:00:00 2001 From: Oana-Lavinia Florean Date: Mon, 10 Jul 2023 11:54:38 +0300 Subject: [PATCH 1/2] Add option to configure the file edit token timeout #40 * first version, commited to save work, needs refactoring --- .../configuration/CollaboraConfiguration.java | 7 +++++++ .../com/xwiki/collabora/internal/FileToken.java | 7 ++++--- .../collabora/internal/FileTokenManager.java | 13 +++++++++++-- .../DefaultCollaboraConfiguration.java | 10 ++++++++++ .../resources/Collabora/Code/Configuration.xml | 17 +++++++++++++++++ .../Collabora/Code/ConfigurationClass.xml | 14 ++++++++++++++ .../resources/Collabora/Code/Translations.xml | 3 +++ 7 files changed, 66 insertions(+), 5 deletions(-) diff --git a/application-collabora-api/src/main/java/com/xwiki/collabora/configuration/CollaboraConfiguration.java b/application-collabora-api/src/main/java/com/xwiki/collabora/configuration/CollaboraConfiguration.java index eac5a4a..762593c 100644 --- a/application-collabora-api/src/main/java/com/xwiki/collabora/configuration/CollaboraConfiguration.java +++ b/application-collabora-api/src/main/java/com/xwiki/collabora/configuration/CollaboraConfiguration.java @@ -71,4 +71,11 @@ public interface CollaboraConfiguration * used */ boolean editUsingMainWiki(); + + /** + * To add. + * + * @return to add. + */ + int getTokenTimeout(); } diff --git a/application-collabora-default/src/main/java/com/xwiki/collabora/internal/FileToken.java b/application-collabora-default/src/main/java/com/xwiki/collabora/internal/FileToken.java index d2b17a5..89a714d 100644 --- a/application-collabora-default/src/main/java/com/xwiki/collabora/internal/FileToken.java +++ b/application-collabora-default/src/main/java/com/xwiki/collabora/internal/FileToken.java @@ -55,15 +55,16 @@ public class FileToken } /** - * Check if this token is expired. Tokens have a valability of 1 hour. + * Check if this token is expired. Tokens have a default valability of 4 hours, but this value can be configured. * + * @param timeout timeout for the editing token, in hours * @return {@code true} if the token has expired, {@code false} otherwise. */ - public boolean isExpired() + public boolean isExpired(long timeout) { long currentTime = new Date().getTime(); long differenceInSec = (currentTime - timestamp) / 1000; - return differenceInSec > 3600; + return differenceInSec > timeout * 1200; } /** diff --git a/application-collabora-default/src/main/java/com/xwiki/collabora/internal/FileTokenManager.java b/application-collabora-default/src/main/java/com/xwiki/collabora/internal/FileTokenManager.java index 91f6601..80696c8 100644 --- a/application-collabora-default/src/main/java/com/xwiki/collabora/internal/FileTokenManager.java +++ b/application-collabora-default/src/main/java/com/xwiki/collabora/internal/FileTokenManager.java @@ -25,6 +25,7 @@ import javax.inject.Inject; import javax.inject.Named; +import javax.inject.Provider; import javax.inject.Singleton; import org.slf4j.Logger; @@ -33,6 +34,8 @@ import org.xwiki.model.reference.DocumentReferenceResolver; import org.xwiki.model.reference.EntityReferenceSerializer; +import com.xwiki.collabora.configuration.CollaboraConfiguration; + /** * Manage existing {@link FileToken} instances. * @@ -55,6 +58,9 @@ public class FileTokenManager @Named("current") private DocumentReferenceResolver documentReferenceResolver; + @Inject + private Provider configurationProvider; + private Map tokens = new HashMap<>(); /** @@ -70,7 +76,7 @@ public FileToken getToken(DocumentReference userReference, String fileId) String user = userReference != null ? this.referenceSerializer.serialize(userReference) : XWIKI_GUEST; FileToken token = getExistingToken(user, fileId); if (token != null) { - if (token.isExpired()) { + if (token.isExpired(configurationProvider.get().getTokenTimeout())) { tokens.remove(token.toString()); } else { token.setUsage(token.getUsage() + 1); @@ -90,8 +96,11 @@ public FileToken getToken(DocumentReference userReference, String fileId) public boolean isInvalid(String token) { FileToken foundToken = tokens.get(token); + logger.debug("Is token expired [{}]. timeout: [{}]", + foundToken.isExpired(configurationProvider.get().getTokenTimeout()), + configurationProvider.get().getTokenTimeout()); - return foundToken == null || foundToken.isExpired(); + return foundToken == null || foundToken.isExpired(configurationProvider.get().getTokenTimeout()); } /** diff --git a/application-collabora-default/src/main/java/com/xwiki/collabora/internal/configuration/DefaultCollaboraConfiguration.java b/application-collabora-default/src/main/java/com/xwiki/collabora/internal/configuration/DefaultCollaboraConfiguration.java index 797e1a2..4c28e0f 100644 --- a/application-collabora-default/src/main/java/com/xwiki/collabora/internal/configuration/DefaultCollaboraConfiguration.java +++ b/application-collabora-default/src/main/java/com/xwiki/collabora/internal/configuration/DefaultCollaboraConfiguration.java @@ -48,6 +48,8 @@ public class DefaultCollaboraConfiguration implements CollaboraConfiguration private static final String SERVER = "server"; + private static final String TOKEN_TIMEOUT = "tokenTimeout"; + @Inject @Named(MainCollaboraConfigurationSource.HINT) private ConfigurationSource mainConfiguration; @@ -84,4 +86,12 @@ public boolean editUsingMainWiki() return editUsingMainWiki == null ? this.mainConfiguration.getProperty(EDIT_USING_MAIN_WIKI, false) : editUsingMainWiki; } + + @Override + public int getTokenTimeout() + { + int currentWikiTokenTimeout = this.currentConfiguration.getProperty(TOKEN_TIMEOUT); + return currentWikiTokenTimeout == 0 ? this.mainConfiguration.getProperty(TOKEN_TIMEOUT, 4) + : currentWikiTokenTimeout; + } } diff --git a/application-collabora-ui/src/main/resources/Collabora/Code/Configuration.xml b/application-collabora-ui/src/main/resources/Collabora/Code/Configuration.xml index 47c4f45..eadde4f 100644 --- a/application-collabora-ui/src/main/resources/Collabora/Code/Configuration.xml +++ b/application-collabora-ui/src/main/resources/Collabora/Code/Configuration.xml @@ -78,6 +78,20 @@ 0 com.xpn.xwiki.objects.classes.StringClass + + + 0 + + tokenTimeout + 4 + integer + tokenTimeout + 30 + 0 + + + com.xpn.xwiki.objects.classes.NumberClass + @@ -88,6 +102,9 @@ + + + Collabora.Code.Configuration diff --git a/application-collabora-ui/src/main/resources/Collabora/Code/ConfigurationClass.xml b/application-collabora-ui/src/main/resources/Collabora/Code/ConfigurationClass.xml index 5385494..437f3c2 100644 --- a/application-collabora-ui/src/main/resources/Collabora/Code/ConfigurationClass.xml +++ b/application-collabora-ui/src/main/resources/Collabora/Code/ConfigurationClass.xml @@ -73,6 +73,20 @@ 0 com.xpn.xwiki.objects.classes.StringClass + + + 0 + + tokenTimeout + 4 + integer + tokenTimeout + 30 + 0 + + + com.xpn.xwiki.objects.classes.NumberClass + Collabora.Code.ConfigurationClass diff --git a/application-collabora-ui/src/main/resources/Collabora/Code/Translations.xml b/application-collabora-ui/src/main/resources/Collabora/Code/Translations.xml index a5eaab5..88a05d5 100644 --- a/application-collabora-ui/src/main/resources/Collabora/Code/Translations.xml +++ b/application-collabora-ui/src/main/resources/Collabora/Code/Translations.xml @@ -45,6 +45,9 @@ Collabora.Code.ConfigurationClass_server=Server Collabora.Code.ConfigurationClass_server.hint=Collabora Online server address. The main wiki configuration will be used if not set at subwiki level. Collabora.Code.ConfigurationClass_editUsingMainWiki=Edit using main wiki Collabora.Code.ConfigurationClass_editUsingMainWiki.hint=Use the main wiki domain when editing files, in order to not add each subwiki domain to the docker hosts configuration of Collabora server. The main wiki configuration will be used if not set at subwiki level. +Collabora.Code.ConfigurationClass_tokenTimeout=Editing token timeout +Collabora.Code.ConfigurationClass_tokenTimeout.hint=The time after an editing session token expires. Note that after expiration, edition will stop working and a refresh is needed. The number of hours should be added (e.g. 1 for 1 hour, 2 for 2 hours). Default value is 4. + ## Attachments tab collabora.attachment.edit.title=Edit using Collabora collabora.attachment.modal.title=Create new file using Collabora From 263993af0b9015a88239b247ff41d570e6c29175 Mon Sep 17 00:00:00 2001 From: Oana-Lavinia Florean Date: Fri, 14 Jul 2023 16:36:51 +0300 Subject: [PATCH 2/2] Add option to configure the file edit token timeout #40 * refactor code * moved token timeout value on the token, in order to cache it * update parameter description --- .../configuration/CollaboraConfiguration.java | 5 +++-- .../com/xwiki/collabora/internal/FileToken.java | 16 +++++++++++----- .../collabora/internal/FileTokenManager.java | 13 +++++-------- .../DefaultCollaboraConfiguration.java | 4 ++-- .../collabora/internal/rest/DefaultWopi.java | 3 ++- .../resources/Collabora/Code/Translations.xml | 2 +- 6 files changed, 24 insertions(+), 19 deletions(-) diff --git a/application-collabora-api/src/main/java/com/xwiki/collabora/configuration/CollaboraConfiguration.java b/application-collabora-api/src/main/java/com/xwiki/collabora/configuration/CollaboraConfiguration.java index 762593c..fe11bef 100644 --- a/application-collabora-api/src/main/java/com/xwiki/collabora/configuration/CollaboraConfiguration.java +++ b/application-collabora-api/src/main/java/com/xwiki/collabora/configuration/CollaboraConfiguration.java @@ -73,9 +73,10 @@ public interface CollaboraConfiguration boolean editUsingMainWiki(); /** - * To add. + * Get the number of hours (e.g. 1 for one hour, 2 for 2 hours) after which the file token will expire and edit will + * stop working. If not defined on the sub wiki or main wiki, a default value of 5 is used. * - * @return to add. + * @return the number of hours after which the file editing token expires. */ int getTokenTimeout(); } diff --git a/application-collabora-default/src/main/java/com/xwiki/collabora/internal/FileToken.java b/application-collabora-default/src/main/java/com/xwiki/collabora/internal/FileToken.java index 89a714d..3628970 100644 --- a/application-collabora-default/src/main/java/com/xwiki/collabora/internal/FileToken.java +++ b/application-collabora-default/src/main/java/com/xwiki/collabora/internal/FileToken.java @@ -43,28 +43,34 @@ public class FileToken private final int randomNumber; + /** + * Token timeout, in seconds. + */ + private final int tokenTimeout; + private int usage; - FileToken(String user, String fileId) + FileToken(String user, String fileId, int tokenTimeout) { this.user = user; this.fileId = fileId; this.timestamp = new Date().getTime(); this.randomNumber = Math.abs(SECURE_RANDOM.nextInt()); this.usage = 1; + // Transform from hours to seconds. + this.tokenTimeout = tokenTimeout * 1200; } /** - * Check if this token is expired. Tokens have a default valability of 4 hours, but this value can be configured. + * Check if this token is expired. Tokens have a default lifetime of 5 hours, but this value can be configured. * - * @param timeout timeout for the editing token, in hours * @return {@code true} if the token has expired, {@code false} otherwise. */ - public boolean isExpired(long timeout) + public boolean isExpired() { long currentTime = new Date().getTime(); long differenceInSec = (currentTime - timestamp) / 1000; - return differenceInSec > timeout * 1200; + return differenceInSec > this.tokenTimeout; } /** diff --git a/application-collabora-default/src/main/java/com/xwiki/collabora/internal/FileTokenManager.java b/application-collabora-default/src/main/java/com/xwiki/collabora/internal/FileTokenManager.java index 80696c8..fc3c562 100644 --- a/application-collabora-default/src/main/java/com/xwiki/collabora/internal/FileTokenManager.java +++ b/application-collabora-default/src/main/java/com/xwiki/collabora/internal/FileTokenManager.java @@ -76,7 +76,7 @@ public FileToken getToken(DocumentReference userReference, String fileId) String user = userReference != null ? this.referenceSerializer.serialize(userReference) : XWIKI_GUEST; FileToken token = getExistingToken(user, fileId); if (token != null) { - if (token.isExpired(configurationProvider.get().getTokenTimeout())) { + if (token.isExpired()) { tokens.remove(token.toString()); } else { token.setUsage(token.getUsage() + 1); @@ -84,7 +84,7 @@ public FileToken getToken(DocumentReference userReference, String fileId) } } - return createNewToken(user, fileId); + return createNewToken(user, fileId, configurationProvider.get().getTokenTimeout()); } /** @@ -96,11 +96,8 @@ public FileToken getToken(DocumentReference userReference, String fileId) public boolean isInvalid(String token) { FileToken foundToken = tokens.get(token); - logger.debug("Is token expired [{}]. timeout: [{}]", - foundToken.isExpired(configurationProvider.get().getTokenTimeout()), - configurationProvider.get().getTokenTimeout()); - return foundToken == null || foundToken.isExpired(configurationProvider.get().getTokenTimeout()); + return foundToken == null || foundToken.isExpired(); } /** @@ -156,9 +153,9 @@ private FileToken getExistingToken(String user, String fileId) return tokenEntry.map(Map.Entry::getValue).orElse(null); } - private FileToken createNewToken(String user, String fileId) + private FileToken createNewToken(String user, String fileId, int tokenTimeout) { - FileToken token = new FileToken(user, fileId); + FileToken token = new FileToken(user, fileId, tokenTimeout); tokens.put(token.toString(), token); logger.debug("New token created for file [{}] and user [{}],", fileId, user); diff --git a/application-collabora-default/src/main/java/com/xwiki/collabora/internal/configuration/DefaultCollaboraConfiguration.java b/application-collabora-default/src/main/java/com/xwiki/collabora/internal/configuration/DefaultCollaboraConfiguration.java index 4c28e0f..1b4b40d 100644 --- a/application-collabora-default/src/main/java/com/xwiki/collabora/internal/configuration/DefaultCollaboraConfiguration.java +++ b/application-collabora-default/src/main/java/com/xwiki/collabora/internal/configuration/DefaultCollaboraConfiguration.java @@ -90,8 +90,8 @@ public boolean editUsingMainWiki() @Override public int getTokenTimeout() { - int currentWikiTokenTimeout = this.currentConfiguration.getProperty(TOKEN_TIMEOUT); - return currentWikiTokenTimeout == 0 ? this.mainConfiguration.getProperty(TOKEN_TIMEOUT, 4) + int currentWikiTokenTimeout = this.currentConfiguration.getProperty(TOKEN_TIMEOUT, 0); + return currentWikiTokenTimeout == 0 ? this.mainConfiguration.getProperty(TOKEN_TIMEOUT, 5) : currentWikiTokenTimeout; } } diff --git a/application-collabora-default/src/main/java/com/xwiki/collabora/internal/rest/DefaultWopi.java b/application-collabora-default/src/main/java/com/xwiki/collabora/internal/rest/DefaultWopi.java index 670acf1..7c605f4 100644 --- a/application-collabora-default/src/main/java/com/xwiki/collabora/internal/rest/DefaultWopi.java +++ b/application-collabora-default/src/main/java/com/xwiki/collabora/internal/rest/DefaultWopi.java @@ -181,10 +181,11 @@ public Token getToken(String fileId) throws XWikiRestException try { String urlSrc = discoveryManager.getURLSrc(fileId); + String fileTokenValue = fileTokenManager.getToken(xcontext.getUserReference(), fileId).toString(); Token token = (new ObjectFactory()).createToken(); token.setUrlSrc(urlSrc); - token.setValue(fileTokenManager.getToken(xcontext.getUserReference(), fileId).toString()); + token.setValue(fileTokenValue); return token; } catch (IOException e) { diff --git a/application-collabora-ui/src/main/resources/Collabora/Code/Translations.xml b/application-collabora-ui/src/main/resources/Collabora/Code/Translations.xml index 88a05d5..f72c84d 100644 --- a/application-collabora-ui/src/main/resources/Collabora/Code/Translations.xml +++ b/application-collabora-ui/src/main/resources/Collabora/Code/Translations.xml @@ -46,7 +46,7 @@ Collabora.Code.ConfigurationClass_server.hint=Collabora Online server address. T Collabora.Code.ConfigurationClass_editUsingMainWiki=Edit using main wiki Collabora.Code.ConfigurationClass_editUsingMainWiki.hint=Use the main wiki domain when editing files, in order to not add each subwiki domain to the docker hosts configuration of Collabora server. The main wiki configuration will be used if not set at subwiki level. Collabora.Code.ConfigurationClass_tokenTimeout=Editing token timeout -Collabora.Code.ConfigurationClass_tokenTimeout.hint=The time after an editing session token expires. Note that after expiration, edition will stop working and a refresh is needed. The number of hours should be added (e.g. 1 for 1 hour, 2 for 2 hours). Default value is 4. +Collabora.Code.ConfigurationClass_tokenTimeout.hint=The number of hours after which an editing token expires. Note that after expiration, edition will stop working and a refresh is needed. The number of hours should be added (e.g. 1 for 1 hour, 2 for 2 hours). If not set at the subwiki or main wiki level, a default value of 5 is used. ## Attachments tab collabora.attachment.edit.title=Edit using Collabora