Skip to content
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
9 changes: 6 additions & 3 deletions classes/core_userkey_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

namespace auth_userkey;

use core\di;
use core\clock;

/**
* Key manager class.
*
Expand Down Expand Up @@ -62,9 +65,9 @@ public function create_key($userid, $allowedips = null) {
$this->delete_keys($userid);

if (isset($this->config->keylifetime) && (int)$this->config->keylifetime > 0) {
$validuntil = time() + $this->config->keylifetime;
$validuntil = di::get(clock::class)->time() + $this->config->keylifetime;
} else {
$validuntil = time() + self::DEFAULT_KEY_LIFE_TIME_IN_SECONDS;
$validuntil = di::get(clock::class)->time() + self::DEFAULT_KEY_LIFE_TIME_IN_SECONDS;
}

$iprestriction = null;
Expand Down Expand Up @@ -116,7 +119,7 @@ public function validate_key($keyvalue) {
throw new \moodle_exception('invalidkey');
}

if (!empty($key->validuntil) && $key->validuntil < time()) {
if (!empty($key->validuntil) && $key->validuntil < di::get(clock::class)->time()) {
throw new \moodle_exception('expiredkey');
}

Expand Down
3 changes: 2 additions & 1 deletion tests/core_userkey_manager_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ private function validate_iprestriction(
int $keylifetime = 60
): void {
global $DB;
$clock = $this->mock_clock_with_frozen();
$manager = new core_userkey_manager($this->config);
if ($allowips) {
$value = $manager->create_key($this->user->id, $allowips);
Expand All @@ -83,7 +84,7 @@ private function validate_iprestriction(
$this->assertEquals($script, $actualkey->script);
$this->assertEquals($this->user->id, $actualkey->instance);
$this->assertEquals($iprestriction, $actualkey->iprestriction);
$this->assertEquals(time() + $keylifetime, $actualkey->validuntil);
$this->assertEquals($clock->time() + $keylifetime, $actualkey->validuntil);
}
/**
* Test that core_userkey_manager implements userkey_manager_interface interface.
Expand Down
Loading