2016-08-16 07:49:43 +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/>.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Key manager class.
|
|
|
|
*
|
|
|
|
* @package auth_userkey
|
|
|
|
* @copyright 2016 Dmitrii Metelkin (dmitriim@catalyst-au.net)
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace auth_userkey;
|
|
|
|
|
|
|
|
class core_userkey_manager implements userkey_manager_interface {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This script script required by core create_user_key().
|
|
|
|
*/
|
|
|
|
const CORE_USER_KEY_MANAGER_SCRIPT = 'auth/userkey';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default life time of the user key in seconds.
|
|
|
|
*/
|
|
|
|
const DEFAULT_KEY_LIFE_TIME_IN_SECONDS = 60;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Config object.
|
|
|
|
*
|
|
|
|
* @var \stdClass
|
|
|
|
*/
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param \stdClass $config
|
|
|
|
*/
|
2016-08-27 05:58:39 +00:00
|
|
|
public function __construct(\stdClass $config) {
|
2016-08-16 07:49:43 +00:00
|
|
|
$this->config = $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a user key.
|
|
|
|
*
|
2016-08-27 05:58:39 +00:00
|
|
|
* @param int $userid User ID.
|
|
|
|
* @param null|array $allowedips A list of allowed ips for this key.
|
|
|
|
*
|
2016-08-16 07:49:43 +00:00
|
|
|
* @return string Generated key.
|
|
|
|
*/
|
2016-08-27 05:58:39 +00:00
|
|
|
public function create_key($userid, $allowedips = null) {
|
|
|
|
$this->delete_keys($userid);
|
|
|
|
|
2016-08-27 06:06:35 +00:00
|
|
|
if (isset($this->config->keylifetime) && (int)$this->config->keylifetime > 0) {
|
|
|
|
$validuntil = time() + $this->config->keylifetime;
|
|
|
|
} else {
|
|
|
|
$validuntil = time() + self::DEFAULT_KEY_LIFE_TIME_IN_SECONDS;
|
|
|
|
}
|
|
|
|
|
|
|
|
$iprestriction = null;
|
|
|
|
|
2016-08-27 05:58:39 +00:00
|
|
|
if (isset($this->config->iprestriction) && !empty($this->config->iprestriction)) {
|
|
|
|
if ($allowedips) {
|
2016-08-27 06:06:35 +00:00
|
|
|
$iprestriction = $allowedips;
|
2016-08-27 05:58:39 +00:00
|
|
|
} else {
|
2016-08-27 06:06:35 +00:00
|
|
|
$iprestriction = getremoteaddr(null);
|
2016-08-27 05:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-27 06:06:35 +00:00
|
|
|
return create_user_key(
|
2016-08-16 07:49:43 +00:00
|
|
|
self::CORE_USER_KEY_MANAGER_SCRIPT,
|
2016-08-27 05:58:39 +00:00
|
|
|
$userid,
|
|
|
|
$userid,
|
2016-08-27 06:06:35 +00:00
|
|
|
$iprestriction,
|
|
|
|
$validuntil
|
2016-08-16 07:49:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-27 05:58:39 +00:00
|
|
|
* Delete all keys for a specific user.
|
|
|
|
*
|
|
|
|
* @param int $userid User ID.
|
2016-08-16 07:49:43 +00:00
|
|
|
*/
|
2016-08-27 05:58:39 +00:00
|
|
|
public function delete_keys($userid) {
|
|
|
|
delete_user_key(self::CORE_USER_KEY_MANAGER_SCRIPT, $userid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates key and returns key data object if valid.
|
|
|
|
*
|
|
|
|
* @param string $keyvalue User key value.
|
|
|
|
*
|
|
|
|
* @return object Key object including userid property.
|
|
|
|
*
|
|
|
|
* @throws \moodle_exception If provided key is not valid.
|
|
|
|
*/
|
|
|
|
public function validate_key($keyvalue) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$options = array(
|
|
|
|
'script' => self::CORE_USER_KEY_MANAGER_SCRIPT,
|
|
|
|
'value' => $keyvalue
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$key = $DB->get_record('user_private_key', $options)) {
|
|
|
|
print_error('invalidkey');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($key->validuntil) and $key->validuntil < time()) {
|
|
|
|
print_error('expiredkey');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($key->iprestriction) {
|
|
|
|
$remoteaddr = getremoteaddr(null);
|
2017-10-20 17:04:56 +00:00
|
|
|
|
|
|
|
if (isset($this->config->ipwhitelist)) {
|
|
|
|
$whitelist = $this->config->ipwhitelist;
|
|
|
|
} else {
|
|
|
|
$whitelist = false;
|
|
|
|
}
|
|
|
|
|
2017-10-03 17:03:02 +00:00
|
|
|
if (empty($remoteaddr) ) {
|
|
|
|
print_error('noip', 'auth_userkey');
|
|
|
|
} else if (!empty($whitelist)) {
|
2017-10-03 10:05:40 +00:00
|
|
|
$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");
|
2016-08-27 05:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$user = $DB->get_record('user', array('id' => $key->userid))) {
|
|
|
|
print_error('invaliduserid');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $key;
|
2016-08-16 07:49:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|