Paillasse/portail/app/Services/Lti/CacheLti.php

70 lines
1.8 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Services\Lti;
use Illuminate\Support\Facades\Cache;
use Packback\Lti1p3\Interfaces\ICache;
/**
* Pont vers le cache Laravel (Redis en production) pour la bibliothèque
* LTI 1.3 : nonce OIDC, données d'un lancement en cours, jetons d'accès
* aux services LTI (jamais utilisés aujourd'hui, voir ADR).
*/
class CacheLti implements ICache
{
private const EXPIRATION_NONCE = 3600;
private const EXPIRATION_DONNEES_LANCEMENT = 86400;
private const EXPIRATION_JETON_ACCES = 3600;
/**
* @return array<string, mixed>|null
*/
public function getLaunchData(string $key): ?array
{
return Cache::get("lti1p3-lancement-{$key}");
}
/**
* @param array<string, mixed> $jwtBody
*/
public function cacheLaunchData(string $key, array $jwtBody): void
{
Cache::put("lti1p3-lancement-{$key}", $jwtBody, self::EXPIRATION_DONNEES_LANCEMENT);
}
public function cacheNonce(string $nonce, string $state): void
{
Cache::put("lti1p3-nonce-{$nonce}", $state, self::EXPIRATION_NONCE);
}
public function checkNonceIsValid(string $nonce, string $state): bool
{
$etatEnCache = Cache::get("lti1p3-nonce-{$nonce}");
if ($etatEnCache === null) {
return false;
}
Cache::forget("lti1p3-nonce-{$nonce}");
return $etatEnCache === $state;
}
public function cacheAccessToken(string $key, string $accessToken): void
{
Cache::put("lti1p3-jeton-{$key}", $accessToken, self::EXPIRATION_JETON_ACCES);
}
public function getAccessToken(string $key): ?string
{
return Cache::get("lti1p3-jeton-{$key}");
}
public function clearAccessToken(string $key): void
{
Cache::forget("lti1p3-jeton-{$key}");
}
}