diff --git a/auth.php b/auth.php
index ae009f3..04502ef 100644
--- a/auth.php
+++ b/auth.php
@@ -57,6 +57,7 @@ class auth_plugin_userkey extends auth_plugin_base {
'mappingfield' => self::DEFAULT_MAPPING_FIELD,
'keylifetime' => 60,
'iprestriction' => 0,
+ 'ipwhitelist' => '',
'redirecturl' => '',
'ssourl' => '',
'createuser' => false,
diff --git a/classes/core_userkey_manager.php b/classes/core_userkey_manager.php
index 8d897b8..c302cb9 100644
--- a/classes/core_userkey_manager.php
+++ b/classes/core_userkey_manager.php
@@ -124,8 +124,28 @@ class core_userkey_manager implements userkey_manager_interface {
if ($key->iprestriction) {
$remoteaddr = getremoteaddr(null);
- if (empty($remoteaddr) or !address_in_subnet($remoteaddr, $key->iprestriction)) {
- print_error('ipmismatch');
+
+ if (isset($this->config->ipwhitelist)) {
+ $whitelist = $this->config->ipwhitelist;
+ } else {
+ $whitelist = false;
+ }
+
+ if (empty($remoteaddr) ) {
+ print_error('noip', 'auth_userkey');
+ } else if (!empty($whitelist)) {
+ $ips = explode(';', $whitelist);
+ $whitelisted = false;
+ foreach ($ips as $ip) {
+ if (address_in_subnet($remoteaddr, $ip)) {
+ $whitelisted = true;
+ }
+ }
+ if (!$whitelisted) {
+ print_error('ipmismatch', 'error', '', null, "Remote address: $remoteaddr\nKey IP: $key->iprestriction");
+ }
+ } else if (!address_in_subnet($remoteaddr, $key->iprestriction)) {
+ print_error('ipmismatch', 'error', '', null, "Remote address: $remoteaddr\nKey IP: $key->iprestriction");
}
}
diff --git a/lang/en/auth_userkey.php b/lang/en/auth_userkey.php
index cf3e55b..ab5db6f 100644
--- a/lang/en/auth_userkey.php
+++ b/lang/en/auth_userkey.php
@@ -29,6 +29,10 @@ $string['mappingfield_desc'] = 'This user field will be used to find relevant us
$string['iprestriction'] = 'IP restriction';
$string['iprestriction_desc'] = 'If enabled, a web call has to contain "ip" parameter when requesting login URL.
A user has to have provided IP to be able to use a key to login to LMS.';
+$string['ipwhitelist'] = 'Whitelist IP ranges';
+$string['ipwhitelist_desc'] = "Ignore IP restrictions if the IP address the token was issued for or the login attempt comes from falls within any of these ranges.
+\nThis can happen when some users reach Moodle or the system issuing login tokens via a private network or DMZ.
+\nIf the route to either the system issuing tokens or this Moodle is via a private address range then set this value to 10.0.0.0/8;172.16.0.0/12;192.168.0.0/16";
$string['keylifetime'] = 'User key life time';
$string['keylifetime_desc'] = 'Life time in seconds of the each user login key.';
$string['incorrectkeylifetime'] = 'User key life time should be a number';
diff --git a/settings.html b/settings.html
index 8e95618..d82c526 100644
--- a/settings.html
+++ b/settings.html
@@ -49,6 +49,13 @@ $fields = get_auth_plugin('userkey')->get_allowed_mapping_fields();
notification($err[$field], 'notifyfailure'); } ?>
+