Actualización

This commit is contained in:
Xes
2025-04-10 12:36:07 +02:00
parent 1da7c3f3b9
commit 4aff98e77b
3147 changed files with 320647 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
<?php
/* For license terms, see /license.txt */
use Packback\Lti1p3\Interfaces\Cache as Lti1p3Cache;
class Lti13Cache implements Lti1p3Cache
{
public const NONCE_PREFIX = 'nonce_';
private $cache;
public function getLaunchData($key)
{
$this->loadCache();
return $this->cache[$key];
}
public function cacheLaunchData($key, $jwtBody): Lti13Cache
{
$this->cache[$key] = $jwtBody;
$this->saveCache();
return $this;
}
public function cacheNonce($nonce): Lti13Cache
{
$this->cache['nonce'][$nonce] = true;
$this->saveCache();
return $this;
}
public function checkNonce($nonce): bool
{
$this->loadCache();
if (!isset($this->cache['nonce'][$nonce])) {
return false;
}
return true;
}
private function loadCache()
{
$cache = file_get_contents(api_get_path(SYS_ARCHIVE_PATH).'lti_cache.txt');
if (empty($cache)) {
file_put_contents(api_get_path(SYS_ARCHIVE_PATH).'lti_cache.txt', '{}');
$this->cache = [];
}
$this->cache = json_decode($cache, true);
}
private function saveCache()
{
file_put_contents(api_get_path(SYS_ARCHIVE_PATH).'lti_cache.txt', json_encode($this->cache));
}
}

View File

@@ -0,0 +1,45 @@
<?php
/* For license terms, see /license.txt */
use Packback\Lti1p3\Interfaces\Cookie as Lti1p3Cookie;
class Lti13Cookie implements Lti1p3Cookie
{
public function getCookie($name)
{
if (isset($_REQUEST['state']) && $name === 'lti1p3_'.$_REQUEST['state']) {
return $_REQUEST['state'];
}
if (isset($_COOKIE[$name])) {
return $_COOKIE[$name];
}
// Look for backup cookie if same site is not supported by the user's browser.
if (isset($_COOKIE["LEGACY_".$name])) {
return $_COOKIE["LEGACY_".$name];
}
return false;
}
public function setCookie($name, $value, $exp = 3600, $options = []): self
{
$cookieOptions = [
'expires' => time() + $exp,
];
// SameSite none and secure will be required for tools to work inside iframes
$sameSiteOptions = [
'samesite' => 'None',
'secure' => false,
'httponly' => true,
];
setcookie($name, $value, array_merge($cookieOptions, $sameSiteOptions, $options));
// Set a second fallback cookie in the event that "SameSite" is not supported
setcookie("LEGACY_".$name, $value, array_merge($cookieOptions, $options));
return $this;
}
}

View File

@@ -0,0 +1,91 @@
<?php
/* For license terms, see /license.txt */
use ChamiloSession as Session;
use Packback\Lti1p3\Interfaces;
use Packback\Lti1p3\LtiDeployment;
use Packback\Lti1p3\LtiRegistration;
class Lti13Database implements Interfaces\Database
{
public function findRegistrationByIssuer($iss, $clientId = null)
{
if (!isset($clientId)) {
$clientId = $this->getClientIdByIssuer($iss);
}
$ltiCustomers = $this->getLtiConnection();
if (empty($ltiCustomers[$clientId])) {
return false;
}
return LtiRegistration::new()
->setAuthLoginUrl($ltiCustomers[$clientId]['auth_login_url'])
->setAuthTokenUrl($ltiCustomers[$clientId]['auth_token_url'])
->setClientId($clientId)
->setKeySetUrl($ltiCustomers[$clientId]['key_set_url'])
->setKid($ltiCustomers[$clientId]['kid'])
->setIssuer($iss)
->setToolPrivateKey($this->getPrivateKey());
}
public function findDeployment($iss, $deploymentId, $clientId = null)
{
$issSession = Session::read('iss');
if (!in_array($deploymentId, $issSession[$clientId]['deployment'])) {
return false;
}
return LtiDeployment::new()->setDeploymentId($deploymentId);
}
private function getLtiConnection(): array
{
$em = Database::getManager();
$platforms = $em->getRepository('ChamiloPluginBundle:LtiProvider\Platform')->findAll();
$ltiCustomers = [];
foreach ($platforms as $platform) {
$clientId = $platform->getClientId();
$ltiCustomers[$clientId] = [
'client_id' => $clientId,
'issuer' => $platform->getIssuer(),
'auth_login_url' => $platform->getAuthLoginUrl(),
'auth_token_url' => $platform->getAuthTokenUrl(),
'key_set_url' => $platform->getKeySetUrl(),
'kid' => $platform->getKid(),
'deployment' => [$platform->getDeploymentId()],
];
}
Session::write('iss', $ltiCustomers);
return $ltiCustomers;
}
private function getClientIdByIssuer($issuer)
{
$clientId = '';
$platform = Database::getManager()
->getRepository('ChamiloPluginBundle:LtiProvider\Platform')
->findOneBy(['issuer' => $issuer]);
if ($platform) {
$clientId = $platform->getClientId();
}
return $clientId;
}
private function getPrivateKey()
{
$privateKey = '';
$platformKey = Database::getManager()
->getRepository('ChamiloPluginBundle:LtiProvider\PlatformKey')
->findOneBy([]);
if ($platformKey) {
$privateKey = $platformKey->getPrivateKey();
}
return $privateKey;
}
}