From ec059a219160d77e22f705aa8ebbae5b119695b0 Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Fri, 19 Aug 2016 14:47:40 +1000 Subject: [PATCH] Add allowedips parameter to the core key manager --- classes/core_userkey_manager.php | 9 +++-- tests/core_userkey_manager_test.php | 55 +++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/classes/core_userkey_manager.php b/classes/core_userkey_manager.php index 0d0bd15..66f8190 100644 --- a/classes/core_userkey_manager.php +++ b/classes/core_userkey_manager.php @@ -76,15 +76,20 @@ class core_userkey_manager implements userkey_manager_interface { * * @param $userid * @param \stdClass $config + * @param null|string $allowedips * * @throws \Exception If there is no user with provided userid. */ - public function __construct($userid, \stdClass $config) { + public function __construct($userid, \stdClass $config, $allowedips = null) { $this->userid = $userid; $this->config = $config; if (isset($config->iprestriction) && !empty($config->iprestriction)) { - $this->iprestriction = getremoteaddr(null); + if ($allowedips) { + $this->iprestriction = $allowedips; + } else { + $this->iprestriction = getremoteaddr($this->iprestriction); + } } if (isset($config->keylifetime) && (int)$config->keylifetime > 0) { diff --git a/tests/core_userkey_manager_test.php b/tests/core_userkey_manager_test.php index 00de1af..019ffa8 100644 --- a/tests/core_userkey_manager_test.php +++ b/tests/core_userkey_manager_test.php @@ -130,6 +130,33 @@ class core_userkey_manager_testcase extends advanced_testcase { $this->assertEquals($expectedvaliduntil, $actualkey->validuntil); } + /** + * 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; + $manager = new core_userkey_manager($this->user->id, $this->config, '192.168.1.3'); + $value = $manager->create_key(); + + $actualkey = $DB->get_record('user_private_key', array('userid' => $this->user->id)); + + $expectedvalue = $value; + $expecteduserid = $this->user->id; + $expectedscript = 'auth/userkey'; + $expectedinstance = $this->user->id; + $expectediprestriction = '192.168.1.3'; + $expectedvaliduntil = time() + 60; + + $this->assertEquals($expectedvalue, $actualkey->value); + $this->assertEquals($expecteduserid, $actualkey->userid); + $this->assertEquals($expectedscript, $actualkey->script); + $this->assertEquals($expectedinstance, $actualkey->instance); + $this->assertEquals($expectediprestriction, $actualkey->iprestriction); + $this->assertEquals($expectedvaliduntil, $actualkey->validuntil); + } + /** * Test that key gets created correctly if config option iprestriction is set to false. */ @@ -158,6 +185,34 @@ class core_userkey_manager_testcase extends advanced_testcase { $this->assertEquals($expectedvaliduntil, $actualkey->validuntil); } + /** + * 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'; + $manager = new core_userkey_manager($this->user->id, $this->config, '192.168.1.1'); + $value = $manager->create_key(); + + $actualkey = $DB->get_record('user_private_key', array('userid' => $this->user->id)); + + $expectedvalue = $value; + $expecteduserid = $this->user->id; + $expectedscript = 'auth/userkey'; + $expectedinstance = $this->user->id; + $expectediprestriction = null; + $expectedvaliduntil = time() + 60; + + $this->assertEquals($expectedvalue, $actualkey->value); + $this->assertEquals($expecteduserid, $actualkey->userid); + $this->assertEquals($expectedscript, $actualkey->script); + $this->assertEquals($expectedinstance, $actualkey->instance); + $this->assertEquals($expectediprestriction, $actualkey->iprestriction); + $this->assertEquals($expectedvaliduntil, $actualkey->validuntil); + } + /** * Test that key gets created correctly if config option iprestriction is set to a string. */