ILIAS  release_8 Revision v8.24
class.ilCtrlTokenRepository.php
Go to the documentation of this file.
1<?php
2
3declare(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}
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.
Interface ilCtrlTokenInterface describes an ilCtrl token.
Interface ilCtrlTokenRepositoryInterface describes an ilCtrl token.
$token
Definition: xapitoken.php:70