Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to configure the file edit token timeout #40 #41

Merged
merged 2 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,12 @@ public interface CollaboraConfiguration
* used
*/
boolean editUsingMainWiki();

/**
* 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 the number of hours after which the file editing token expires.
*/
int getTokenTimeout();
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +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 valability of 1 hour.
* Check if this token is expired. Tokens have a default lifetime of 5 hours, but this value can be configured.
*
* @return {@code true} if the token has expired, {@code false} otherwise.
*/
public boolean isExpired()
{
long currentTime = new Date().getTime();
long differenceInSec = (currentTime - timestamp) / 1000;
return differenceInSec > 3600;
return differenceInSec > this.tokenTimeout;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;

import org.slf4j.Logger;
Expand All @@ -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.
*
Expand All @@ -55,6 +58,9 @@ public class FileTokenManager
@Named("current")
private DocumentReferenceResolver<String> documentReferenceResolver;

@Inject
private Provider<CollaboraConfiguration> configurationProvider;

private Map<String, FileToken> tokens = new HashMap<>();

/**
Expand All @@ -78,7 +84,7 @@ public FileToken getToken(DocumentReference userReference, String fileId)
}
}

return createNewToken(user, fileId);
return createNewToken(user, fileId, configurationProvider.get().getTokenTimeout());
}

/**
Expand Down Expand Up @@ -147,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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, 0);
return currentWikiTokenTimeout == 0 ? this.mainConfiguration.getProperty(TOKEN_TIMEOUT, 5)
: currentWikiTokenTimeout;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@
<unmodifiable>0</unmodifiable>
<classType>com.xpn.xwiki.objects.classes.StringClass</classType>
</server>
<tokenTimeout>
<customDisplay/>
<disabled>0</disabled>
<hint/>
<name>tokenTimeout</name>
<number>4</number>
<numberType>integer</numberType>
<prettyName>tokenTimeout</prettyName>
<size>30</size>
<unmodifiable>0</unmodifiable>
<validationMessage/>
<validationRegExp/>
<classType>com.xpn.xwiki.objects.classes.NumberClass</classType>
</tokenTimeout>
</class>
<property>
<editUsingMainWiki/>
Expand All @@ -88,6 +102,9 @@
<property>
<server/>
</property>
<property>
<tokenTimeout/>
</property>
</object>
<object>
<name>Collabora.Code.Configuration</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@
<unmodifiable>0</unmodifiable>
<classType>com.xpn.xwiki.objects.classes.StringClass</classType>
</server>
<tokenTimeout>
<customDisplay/>
<disabled>0</disabled>
<hint/>
<name>tokenTimeout</name>
<number>4</number>
<numberType>integer</numberType>
<prettyName>tokenTimeout</prettyName>
<size>30</size>
<unmodifiable>0</unmodifiable>
<validationMessage/>
<validationRegExp/>
<classType>com.xpn.xwiki.objects.classes.NumberClass</classType>
</tokenTimeout>
</class>
<object>
<name>Collabora.Code.ConfigurationClass</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 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
collabora.attachment.modal.title=Create new file using Collabora
Expand Down