ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilCtrlTokenRepository.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /* Copyright (c) 2021 Thibeau Fuhrer <thf@studer-raimann.ch> Extended GPL, see docs/LICENSE */
6 
13 {
17  public function getToken(): ilCtrlTokenInterface
18  {
19  $token = $this->fetchToken() ?? $this->generateToken();
20 
21  $this->storeToken($token);
22 
23  return $token;
24  }
25 
31  protected function fetchToken(): ?ilCtrlTokenInterface
32  {
34  return unserialize(ilSession::get(ilCtrlInterface::PARAM_CSRF_TOKEN), [ilCtrlTokenInterface::class]);
35  }
36 
37  return null;
38  }
39 
45  protected function storeToken(ilCtrlTokenInterface $token): void
46  {
48  }
49 
55  protected function generateToken(): ilCtrlTokenInterface
56  {
57  // random_bytes() is cryptographically secure but
58  // depends on the system it's running on. If the
59  // generation fails, we use a less secure option
60  // that is available for sure.
61 
62  try {
63  $token = bin2hex(random_bytes(32));
64  } catch (Throwable $t) {
65  $token = md5(uniqid((string) time(), true));
66  }
67 
68  return new ilCtrlToken($token);
69  }
70 }
static get(string $a_var)
fetchToken()
Returns the currently stored token from the session.
const PARAM_CSRF_TOKEN
$_GET request parameter names, used throughout ilCtrl.
storeToken(ilCtrlTokenInterface $token)
Stores the given token in the curren session.
Interface ilCtrlTokenRepositoryInterface describes an ilCtrl token.
Class ilCtrlToken is responsible for generating and storing unique CSRF tokens.
$token
Definition: xapitoken.php:70
static has($a_var)
Class ilCtrlTokenRepository.
Interface ilCtrlTokenInterface describes an ilCtrl token.
static set(string $a_var, $a_val)
Set a value.
generateToken()
Returns a cryptographically secure token.