ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCtrlTokenRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27{
31 public function getToken(): ilCtrlTokenInterface
32 {
33 $token = $this->fetchToken() ?? $this->generateToken();
34
35 $this->storeToken($token);
36
37 return $token;
38 }
39
45 protected function fetchToken(): ?ilCtrlTokenInterface
46 {
48 return unserialize(ilSession::get(ilCtrlInterface::PARAM_CSRF_TOKEN), [ilCtrlTokenInterface::class]);
49 }
50
51 return null;
52 }
53
59 protected function storeToken(ilCtrlTokenInterface $token): void
60 {
62 }
63
69 protected function generateToken(): ilCtrlTokenInterface
70 {
71 // random_bytes() is cryptographically secure but
72 // depends on the system it's running on. If the
73 // generation fails, we use a less secure option
74 // that is available for sure.
75
76 try {
77 $token = bin2hex(random_bytes(32));
78 } catch (Throwable $t) {
79 $token = md5(uniqid((string) time(), true));
80 }
81
82 return new ilCtrlToken($token);
83 }
84}
Class ilCtrlTokenRepository.
fetchToken()
Returns the currently stored token from the session.
storeToken(ilCtrlTokenInterface $token)
Stores the given token in the curren session.
generateToken()
Returns a cryptographically secure token.
Class ilCtrlToken is responsible for generating and storing unique CSRF tokens.
static get(string $a_var)
static set(string $a_var, $a_val)
Set a value.
static has($a_var)
const PARAM_CSRF_TOKEN
$_GET request parameter names, used throughout ilCtrl.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$token
Definition: xapitoken.php:70