Allow configuring the activity update interval of token

On some systems with a lot of users this creates a lot of extra DB
writes.
Being able to increase this interval helps there.

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2020-09-18 12:34:43 +02:00
parent eba83d22bb
commit 76a7600e2e
No known key found for this signature in database
GPG Key ID: F941078878347C0C
3 changed files with 23 additions and 1 deletions

View File

@ -269,6 +269,18 @@ $CONFIG = [
*/
'token_auth_enforced' => false,
/**
* The interval at which token activity should be updated.
* Increasing this value means that the last activty on the security page gets
* more outdated.
*
* Tokens are still checked every 5 minutes for validity
* max value: 300
*
* Defaults to ``300``
*/
'token_auth_activity_update' => 60,
/**
* Whether the bruteforce protection shipped with Nextcloud should be enabled or not.
*

View File

@ -215,9 +215,13 @@ class PublicKeyTokenProvider implements IProvider {
if (!($token instanceof PublicKeyToken)) {
throw new InvalidTokenException("Invalid token type");
}
$activityInterval = $this->config->getSystemValueInt('token_auth_activity_update', 60);
$activityInterval = min(max($activityInterval, 0), 300);
/** @var DefaultToken $token */
$now = $this->time->getTime();
if ($token->getLastActivity() < ($now - 60)) {
if ($token->getLastActivity() < ($now - $activityInterval)) {
// Update token only once per minute
$token->setLastActivity($now);
$this->mapper->update($token);

View File

@ -112,6 +112,12 @@ class PublicKeyTokenProviderTest extends TestCase {
public function testUpdateTokenDebounce() {
$tk = new PublicKeyToken();
$this->config->method('getSystemValueInt')
->willReturnCallback(function ($value, $default) {
return $default;
});
$tk->setLastActivity($this->time - 30);
$this->mapper->expects($this->never())
->method('update')