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/>.
|
|
|
|
|
2024-06-25 10:00:04 +00:00
|
|
|
namespace auth_jwt;
|
|
|
|
|
2024-06-25 16:04:16 +00:00
|
|
|
require_once("../vendor/autoload.php");
|
|
|
|
|
2024-06-25 10:00:04 +00:00
|
|
|
use \Firebase\JWT\JWT;
|
2022-08-19 00:25:45 +00:00
|
|
|
|
2016-08-16 07:49:43 +00:00
|
|
|
/**
|
|
|
|
* Key manager class.
|
|
|
|
*
|
2024-06-25 10:00:04 +00:00
|
|
|
* @package auth_jwt
|
|
|
|
* @copyright 2016 Dmitrii Metelkin (dmitriim@catalyst-au.net), 2024 Kumi Systems e.U.
|
2016-08-16 07:49:43 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2024-06-25 10:00:04 +00:00
|
|
|
class core_jwt_manager
|
|
|
|
{
|
2016-08-16 07:49:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2024-06-25 10:00:04 +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.
|
|
|
|
*/
|
2024-06-25 10:00:04 +00:00
|
|
|
public function create_key($userid, $allowedips = null)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2024-06-25 10:00:04 +00:00
|
|
|
$payload = [
|
|
|
|
'userid' => $userid,
|
|
|
|
'exp' => $validuntil
|
|
|
|
];
|
2016-08-27 06:06:35 +00:00
|
|
|
|
2024-06-25 10:00:04 +00:00
|
|
|
if ($allowedips) {
|
|
|
|
$payload['allowedips'] = $allowedips;
|
2016-08-27 05:58:39 +00:00
|
|
|
}
|
|
|
|
|
2024-06-25 10:00:04 +00:00
|
|
|
$secret = $this->config->jwtsecret;
|
2016-08-16 07:49:43 +00:00
|
|
|
|
2024-06-25 10:00:04 +00:00
|
|
|
return JWT::encode($payload, $secret);
|
2016-08-27 05:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2024-06-25 10:00:04 +00:00
|
|
|
public function validate_key($keyvalue)
|
|
|
|
{
|
|
|
|
$secret = $this->config->jwtsecret;
|
2016-08-27 05:58:39 +00:00
|
|
|
|
2024-06-25 10:00:04 +00:00
|
|
|
try {
|
|
|
|
$decoded = JWT::decode($keyvalue, $secret, ['HS256']);
|
|
|
|
} catch (\Exception $e) {
|
2022-08-18 23:42:23 +00:00
|
|
|
throw new \moodle_exception('invalidkey');
|
2016-08-27 05:58:39 +00:00
|
|
|
}
|
|
|
|
|
2024-06-25 10:00:04 +00:00
|
|
|
if (!empty($decoded->exp) && $decoded->exp < time()) {
|
2022-08-18 23:42:23 +00:00
|
|
|
throw new \moodle_exception('expiredkey');
|
2016-08-27 05:58:39 +00:00
|
|
|
}
|
|
|
|
|
2024-06-25 10:00:04 +00:00
|
|
|
$this->validate_ip_address($decoded);
|
2017-12-12 11:53:21 +00:00
|
|
|
|
2024-06-25 10:00:04 +00:00
|
|
|
return $decoded;
|
2016-08-16 07:49:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 14:10:45 +00:00
|
|
|
/**
|
|
|
|
* Validates key IP address and returns true if valid.
|
|
|
|
*
|
|
|
|
* @param object $key Key object including userid property.
|
|
|
|
*
|
|
|
|
* @throws \moodle_exception If provided key is not valid.
|
|
|
|
*/
|
2024-06-25 10:00:04 +00:00
|
|
|
protected function validate_ip_address($key)
|
|
|
|
{
|
|
|
|
if (empty($key->allowedips)) {
|
2017-11-18 14:10:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$remoteaddr = getremoteaddr(null);
|
|
|
|
|
|
|
|
if (empty($remoteaddr)) {
|
2024-06-25 10:00:04 +00:00
|
|
|
throw new \moodle_exception('noip', 'auth_jwt');
|
2017-11-18 14:10:45 +00:00
|
|
|
}
|
|
|
|
|
2024-06-25 10:00:04 +00:00
|
|
|
foreach ($key->allowedips as $allowedip) {
|
|
|
|
if (address_in_subnet($remoteaddr, $allowedip)) {
|
|
|
|
return true;
|
2017-11-18 14:10:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-25 10:00:04 +00:00
|
|
|
throw new \moodle_exception('ipmismatch', 'error', '', null, "Remote address: $remoteaddr\nKey IP: " . implode(', ', $key->allowedips));
|
2017-11-18 14:10:45 +00:00
|
|
|
}
|
2016-08-16 07:49:43 +00:00
|
|
|
}
|