2016-08-16 07:50:09 +00:00
|
|
|
<?php
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2022-08-19 00:56:39 +00:00
|
|
|
namespace auth_userkey;
|
2016-08-16 07:50:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for core_userkey_manager class.
|
|
|
|
*
|
2016-08-27 06:17:21 +00:00
|
|
|
* Key validation is fully covered in auth_plugin_test.php file.
|
|
|
|
* TODO: write tests for validate_key() function.
|
|
|
|
*
|
2022-08-19 00:56:39 +00:00
|
|
|
* @package auth_userkey
|
2016-08-16 07:50:09 +00:00
|
|
|
* @copyright 2016 Dmitrii Metelkin (dmitriim@catalyst-au.net)
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2022-08-19 00:56:39 +00:00
|
|
|
class core_userkey_manager_test extends \advanced_testcase {
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test user object.
|
|
|
|
* @var
|
|
|
|
*/
|
2016-08-16 07:50:09 +00:00
|
|
|
protected $user;
|
2016-08-17 02:16:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test config object.
|
|
|
|
* @var
|
|
|
|
*/
|
2016-08-16 07:50:09 +00:00
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initial set up.
|
|
|
|
*/
|
2022-06-03 06:44:24 +00:00
|
|
|
public function setUp(): void {
|
2020-05-08 07:22:13 +00:00
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
parent::setUp();
|
|
|
|
|
2016-08-16 07:50:09 +00:00
|
|
|
$this->resetAfterTest();
|
2020-05-08 07:22:13 +00:00
|
|
|
$CFG->getremoteaddrconf = GETREMOTEADDR_SKIP_HTTP_X_FORWARDED_FOR;
|
2016-08-16 07:50:09 +00:00
|
|
|
$this->user = self::getDataGenerator()->create_user();
|
2022-08-19 00:56:39 +00:00
|
|
|
$this->config = new \stdClass();
|
2016-08-16 07:50:09 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test that core_userkey_manager implements userkey_manager_interface interface.
|
|
|
|
*/
|
2016-08-16 07:50:09 +00:00
|
|
|
public function test_implements_userkey_manager_interface() {
|
2016-08-27 06:12:01 +00:00
|
|
|
$manager = new core_userkey_manager($this->config);
|
2016-08-19 01:46:22 +00:00
|
|
|
|
|
|
|
$expected = 'auth_userkey\userkey_manager_interface';
|
|
|
|
$this->assertInstanceOf($expected, $manager);
|
2016-08-16 07:50:09 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test that key gets created correctly if config option iprestriction is not set.
|
|
|
|
*/
|
|
|
|
public function test_create_correct_key_if_iprestriction_is_not_set() {
|
2016-08-16 07:50:09 +00:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$_SERVER['HTTP_CLIENT_IP'] = '192.168.1.1';
|
2016-08-27 06:12:01 +00:00
|
|
|
$manager = new core_userkey_manager($this->config);
|
|
|
|
$value = $manager->create_key($this->user->id);
|
2016-08-16 07:50:09 +00:00
|
|
|
|
|
|
|
$actualkey = $DB->get_record('user_private_key', array('userid' => $this->user->id));
|
|
|
|
|
2016-08-19 04:51:41 +00:00
|
|
|
$this->assertEquals($value, $actualkey->value);
|
|
|
|
$this->assertEquals($this->user->id, $actualkey->userid);
|
|
|
|
$this->assertEquals('auth/userkey', $actualkey->script);
|
|
|
|
$this->assertEquals($this->user->id, $actualkey->instance);
|
|
|
|
$this->assertEquals(null, $actualkey->iprestriction);
|
|
|
|
$this->assertEquals(time() + 60, $actualkey->validuntil);
|
2016-08-16 07:50:09 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test that key gets created correctly if config option iprestriction is set to true.
|
|
|
|
*/
|
2016-08-16 07:50:09 +00:00
|
|
|
public function test_create_correct_key_if_iprestriction_is_true() {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->config->iprestriction = true;
|
|
|
|
$_SERVER['HTTP_CLIENT_IP'] = '192.168.1.1';
|
2016-08-27 06:12:01 +00:00
|
|
|
$manager = new core_userkey_manager($this->config);
|
|
|
|
$value = $manager->create_key($this->user->id);
|
2016-08-16 07:50:09 +00:00
|
|
|
|
|
|
|
$actualkey = $DB->get_record('user_private_key', array('userid' => $this->user->id));
|
|
|
|
|
2016-08-19 04:51:41 +00:00
|
|
|
$this->assertEquals($value, $actualkey->value);
|
|
|
|
$this->assertEquals($this->user->id, $actualkey->userid);
|
|
|
|
$this->assertEquals('auth/userkey', $actualkey->script);
|
|
|
|
$this->assertEquals($this->user->id, $actualkey->instance);
|
|
|
|
$this->assertEquals('192.168.1.1', $actualkey->iprestriction);
|
|
|
|
$this->assertEquals(time() + 60, $actualkey->validuntil);
|
2016-08-16 07:50:09 +00:00
|
|
|
}
|
|
|
|
|
2016-08-19 04:47:40 +00:00
|
|
|
/**
|
|
|
|
* Test that key gets created correctly if config option iprestriction is set to true and we set allowedips.
|
|
|
|
*/
|
|
|
|
public function test_create_correct_key_if_iprestriction_is_true_and_we_set_allowedips() {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->config->iprestriction = true;
|
2016-08-27 06:12:01 +00:00
|
|
|
$manager = new core_userkey_manager($this->config);
|
|
|
|
$value = $manager->create_key($this->user->id, '192.168.1.3');
|
2016-08-19 04:47:40 +00:00
|
|
|
|
|
|
|
$actualkey = $DB->get_record('user_private_key', array('userid' => $this->user->id));
|
|
|
|
|
2016-08-19 04:51:41 +00:00
|
|
|
$this->assertEquals($value, $actualkey->value);
|
|
|
|
$this->assertEquals($this->user->id, $actualkey->userid);
|
|
|
|
$this->assertEquals('auth/userkey', $actualkey->script);
|
|
|
|
$this->assertEquals($this->user->id, $actualkey->instance);
|
|
|
|
$this->assertEquals('192.168.1.3', $actualkey->iprestriction);
|
|
|
|
$this->assertEquals(time() + 60, $actualkey->validuntil);
|
2016-08-19 04:47:40 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test that key gets created correctly if config option iprestriction is set to false.
|
|
|
|
*/
|
2016-08-16 07:50:09 +00:00
|
|
|
public function test_create_correct_key_if_iprestriction_is_false() {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->config->iprestriction = false;
|
|
|
|
$_SERVER['HTTP_CLIENT_IP'] = '192.168.1.1';
|
2016-08-27 06:12:01 +00:00
|
|
|
$manager = new core_userkey_manager($this->config);
|
|
|
|
$value = $manager->create_key($this->user->id);
|
2016-08-16 07:50:09 +00:00
|
|
|
|
|
|
|
$actualkey = $DB->get_record('user_private_key', array('userid' => $this->user->id));
|
|
|
|
|
2016-08-19 04:51:41 +00:00
|
|
|
$this->assertEquals($value, $actualkey->value);
|
|
|
|
$this->assertEquals($this->user->id, $actualkey->userid);
|
|
|
|
$this->assertEquals('auth/userkey', $actualkey->script);
|
|
|
|
$this->assertEquals($this->user->id, $actualkey->instance);
|
|
|
|
$this->assertEquals(null, $actualkey->iprestriction);
|
|
|
|
$this->assertEquals(time() + 60, $actualkey->validuntil);
|
2016-08-19 04:47:40 +00:00
|
|
|
}
|
|
|
|
|
2017-10-20 17:04:56 +00:00
|
|
|
/**
|
|
|
|
* Test that IP address mismatch exception gets thrown if incorrect IP and outside whitelist.
|
|
|
|
*/
|
|
|
|
public function test_exception_if_ip_is_outside_whitelist() {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->config->iprestriction = true;
|
|
|
|
$this->config->ipwhitelist = '10.0.0.0/8;172.16.0.0/12;192.168.0.0/16';
|
|
|
|
|
|
|
|
$manager = new core_userkey_manager($this->config);
|
|
|
|
$value = $manager->create_key($this->user->id, '193.168.1.1');
|
|
|
|
|
|
|
|
$_SERVER['HTTP_CLIENT_IP'] = '193.168.1.2';
|
|
|
|
|
2022-08-19 00:56:39 +00:00
|
|
|
$this->expectException(\moodle_exception::class);
|
2022-08-19 00:08:02 +00:00
|
|
|
$this->expectExceptionMessage('Client IP address mismatch');
|
|
|
|
|
2017-10-20 17:04:56 +00:00
|
|
|
$manager->validate_key($value);
|
|
|
|
}
|
|
|
|
|
2017-11-18 14:10:45 +00:00
|
|
|
/**
|
|
|
|
* Test that IP address mismatch exception gets thrown if incorrect IP and outside whitelist.
|
|
|
|
*/
|
|
|
|
public function test_create_correct_key_if_ip_correct_not_whitelisted_and_whitelist_set() {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->config->iprestriction = true;
|
|
|
|
|
|
|
|
$this->config->ipwhitelist = '10.0.0.0/8;172.16.0.0/12;192.168.0.0/16';
|
|
|
|
|
|
|
|
$manager = new core_userkey_manager($this->config);
|
|
|
|
$value = $manager->create_key($this->user->id, '193.168.1.1');
|
|
|
|
|
|
|
|
$_SERVER['HTTP_CLIENT_IP'] = '193.168.1.1';
|
|
|
|
|
|
|
|
$key = $manager->validate_key($value);
|
|
|
|
$this->assertEquals($this->user->id, $key->userid);
|
|
|
|
}
|
|
|
|
|
2017-10-20 17:04:56 +00:00
|
|
|
/**
|
|
|
|
* Test that key is accepted if incorrect IP and within whitelist.
|
|
|
|
*/
|
|
|
|
public function test_create_correct_key_if_ip_is_whitelisted() {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->config->iprestriction = true;
|
|
|
|
|
|
|
|
$this->config->ipwhitelist = '10.0.0.0/8;172.16.0.0/12;192.168.0.0/16';
|
|
|
|
|
|
|
|
$manager = new core_userkey_manager($this->config);
|
|
|
|
$value = $manager->create_key($this->user->id, '192.168.1.1');
|
|
|
|
|
|
|
|
$_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2';
|
|
|
|
|
|
|
|
$key = $manager->validate_key($value);
|
|
|
|
$this->assertEquals($this->user->id, $key->userid);
|
|
|
|
}
|
|
|
|
|
2016-08-19 04:47:40 +00:00
|
|
|
/**
|
|
|
|
* Test that key gets created correctly if config option iprestriction is set to false and we set allowedips.
|
|
|
|
*/
|
|
|
|
public function test_create_correct_key_if_iprestriction_is_falseand_we_set_allowedips() {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->config->iprestriction = false;
|
|
|
|
$_SERVER['HTTP_CLIENT_IP'] = '192.168.1.1';
|
2016-08-27 06:12:01 +00:00
|
|
|
$manager = new core_userkey_manager($this->config);
|
|
|
|
$value = $manager->create_key($this->user->id, '192.168.1.1');
|
2016-08-19 04:47:40 +00:00
|
|
|
|
|
|
|
$actualkey = $DB->get_record('user_private_key', array('userid' => $this->user->id));
|
|
|
|
|
2016-08-19 04:51:41 +00:00
|
|
|
$this->assertEquals($value, $actualkey->value);
|
|
|
|
$this->assertEquals($this->user->id, $actualkey->userid);
|
|
|
|
$this->assertEquals('auth/userkey', $actualkey->script);
|
|
|
|
$this->assertEquals($this->user->id, $actualkey->instance);
|
|
|
|
$this->assertEquals(null, $actualkey->iprestriction);
|
|
|
|
$this->assertEquals(time() + 60, $actualkey->validuntil);
|
2016-08-16 07:50:09 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test that key gets created correctly if config option iprestriction is set to a string.
|
|
|
|
*/
|
2016-08-16 07:50:09 +00:00
|
|
|
public function test_create_correct_key_if_iprestriction_is_string() {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->config->iprestriction = 'string';
|
|
|
|
$_SERVER['HTTP_CLIENT_IP'] = '192.168.1.1';
|
2016-08-27 06:12:01 +00:00
|
|
|
$manager = new core_userkey_manager($this->config);
|
|
|
|
$value = $manager->create_key($this->user->id);
|
2016-08-16 07:50:09 +00:00
|
|
|
|
|
|
|
$actualkey = $DB->get_record('user_private_key', array('userid' => $this->user->id));
|
|
|
|
|
2016-08-19 04:51:41 +00:00
|
|
|
$this->assertEquals($value, $actualkey->value);
|
|
|
|
$this->assertEquals($this->user->id, $actualkey->userid);
|
|
|
|
$this->assertEquals('auth/userkey', $actualkey->script);
|
|
|
|
$this->assertEquals($this->user->id, $actualkey->instance);
|
|
|
|
$this->assertEquals('192.168.1.1', $actualkey->iprestriction);
|
|
|
|
$this->assertEquals(time() + 60, $actualkey->validuntil);
|
2016-08-16 07:50:09 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test that key gets created correctly if config option keylifetime is not set.
|
|
|
|
*/
|
|
|
|
public function test_create_correct_key_if_keylifetime_is_not_set() {
|
2016-08-16 07:50:09 +00:00
|
|
|
global $DB;
|
|
|
|
|
2016-08-27 06:12:01 +00:00
|
|
|
$manager = new core_userkey_manager($this->config);
|
|
|
|
$value = $manager->create_key($this->user->id);
|
2016-08-16 07:50:09 +00:00
|
|
|
|
|
|
|
$actualkey = $DB->get_record('user_private_key', array('userid' => $this->user->id));
|
|
|
|
|
2016-08-19 04:51:41 +00:00
|
|
|
$this->assertEquals($value, $actualkey->value);
|
|
|
|
$this->assertEquals($this->user->id, $actualkey->userid);
|
|
|
|
$this->assertEquals('auth/userkey', $actualkey->script);
|
|
|
|
$this->assertEquals($this->user->id, $actualkey->instance);
|
|
|
|
$this->assertEquals(null, $actualkey->iprestriction);
|
|
|
|
$this->assertEquals(time() + 60, $actualkey->validuntil);
|
2016-08-16 07:50:09 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test that key gets created correctly if config option keylifetime is set to integer.
|
|
|
|
*/
|
|
|
|
public function test_create_correct_key_if_keylifetime_is_set_to_integer() {
|
2016-08-16 07:50:09 +00:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->config->keylifetime = 3000;
|
|
|
|
|
2016-08-27 06:12:01 +00:00
|
|
|
$manager = new core_userkey_manager($this->config);
|
|
|
|
$value = $manager->create_key($this->user->id);
|
2016-08-16 07:50:09 +00:00
|
|
|
|
|
|
|
$actualkey = $DB->get_record('user_private_key', array('userid' => $this->user->id));
|
|
|
|
|
2016-08-19 04:51:41 +00:00
|
|
|
$this->assertEquals($value, $actualkey->value);
|
|
|
|
$this->assertEquals($this->user->id, $actualkey->userid);
|
|
|
|
$this->assertEquals('auth/userkey', $actualkey->script);
|
|
|
|
$this->assertEquals($this->user->id, $actualkey->instance);
|
|
|
|
$this->assertEquals(null, $actualkey->iprestriction);
|
|
|
|
$this->assertEquals(time() + 3000, $actualkey->validuntil);
|
2016-08-16 07:50:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test that key gets created correctly if config option keylifetime is set to a string.
|
|
|
|
*/
|
|
|
|
public function test_create_correct_key_if_keylifetime_is_set_to_string() {
|
2016-08-16 07:50:09 +00:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->config->keylifetime = '3000';
|
|
|
|
|
2016-08-27 06:12:01 +00:00
|
|
|
$manager = new core_userkey_manager($this->config);
|
|
|
|
$value = $manager->create_key($this->user->id);
|
2016-08-16 07:50:09 +00:00
|
|
|
|
|
|
|
$actualkey = $DB->get_record('user_private_key', array('userid' => $this->user->id));
|
|
|
|
|
2016-08-19 04:51:41 +00:00
|
|
|
$this->assertEquals($value, $actualkey->value);
|
|
|
|
$this->assertEquals($this->user->id, $actualkey->userid);
|
|
|
|
$this->assertEquals('auth/userkey', $actualkey->script);
|
|
|
|
$this->assertEquals($this->user->id, $actualkey->instance);
|
|
|
|
$this->assertEquals(null, $actualkey->iprestriction);
|
|
|
|
$this->assertEquals(time() + 3000, $actualkey->validuntil);
|
|
|
|
|
2016-08-16 07:50:09 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test that we can delete created key.
|
|
|
|
*/
|
2016-08-16 07:50:09 +00:00
|
|
|
public function test_can_delete_created_key() {
|
|
|
|
global $DB;
|
|
|
|
|
2016-08-27 06:12:01 +00:00
|
|
|
$manager = new core_userkey_manager($this->config);
|
|
|
|
$value = $manager->create_key($this->user->id);
|
2016-08-16 07:50:09 +00:00
|
|
|
|
|
|
|
$keys = $DB->get_records('user_private_key', array('userid' => $this->user->id));
|
|
|
|
$this->assertEquals(1, count($keys));
|
|
|
|
|
2016-08-27 06:12:01 +00:00
|
|
|
$manager->delete_keys($this->user->id);
|
2016-08-16 07:50:09 +00:00
|
|
|
|
|
|
|
$keys = $DB->get_records('user_private_key', array('userid' => $this->user->id));
|
|
|
|
$this->assertEquals(0, count($keys));
|
|
|
|
}
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test that we can delete all existing keys.
|
|
|
|
*/
|
2016-08-16 07:50:09 +00:00
|
|
|
public function test_can_delete_all_existing_keys() {
|
|
|
|
global $DB;
|
|
|
|
|
2016-08-27 06:12:01 +00:00
|
|
|
$manager = new core_userkey_manager($this->config);
|
2016-08-16 07:50:09 +00:00
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
create_user_key('auth/userkey', $this->user->id);
|
|
|
|
create_user_key('auth/userkey', $this->user->id);
|
|
|
|
create_user_key('auth/userkey', $this->user->id);
|
2016-08-16 07:50:09 +00:00
|
|
|
|
|
|
|
$keys = $DB->get_records('user_private_key', array('userid' => $this->user->id));
|
|
|
|
$this->assertEquals(3, count($keys));
|
|
|
|
|
2016-08-27 06:12:01 +00:00
|
|
|
$manager->delete_keys($this->user->id);
|
2016-08-16 07:50:09 +00:00
|
|
|
|
|
|
|
$keys = $DB->get_records('user_private_key', array('userid' => $this->user->id));
|
|
|
|
$this->assertEquals(0, count($keys));
|
|
|
|
}
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test that we create only one key.
|
|
|
|
*/
|
2016-08-16 07:50:09 +00:00
|
|
|
public function test_create_only_one_key() {
|
|
|
|
global $DB;
|
|
|
|
|
2016-08-27 06:12:01 +00:00
|
|
|
$manager = new core_userkey_manager($this->config);
|
2016-08-16 07:50:09 +00:00
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
create_user_key('auth/userkey', $this->user->id);
|
|
|
|
create_user_key('auth/userkey', $this->user->id);
|
|
|
|
create_user_key('auth/userkey', $this->user->id);
|
2016-08-16 07:50:09 +00:00
|
|
|
|
|
|
|
$keys = $DB->get_records('user_private_key', array('userid' => $this->user->id));
|
|
|
|
$this->assertEquals(3, count($keys));
|
|
|
|
|
2016-08-27 06:12:01 +00:00
|
|
|
$manager->create_key($this->user->id);
|
2016-08-16 07:50:09 +00:00
|
|
|
$keys = $DB->get_records('user_private_key', array('userid' => $this->user->id));
|
|
|
|
$this->assertEquals(1, count($keys));
|
|
|
|
}
|
|
|
|
}
|