ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
SimpleSAML\Auth\TimeLimitedToken Class Reference

A class that generates and verifies time-limited tokens. More...

+ Collaboration diagram for SimpleSAML\Auth\TimeLimitedToken:

Public Member Functions

 __construct ($lifetime=900, $secretSalt=null, $skew=1, $algo='sha1')
 Create a new time-limited token. More...
 
 addVerificationData ($data)
 Add some given data to the current token. More...
 
 generate ()
 Generates a token that contains an offset and a token value, using the current offset. More...
 
 generate_token ()
 
 validate ($token)
 Validates a token by calculating the token value for the provided offset and comparing it. More...
 
 validate_token ($token)
 

Protected Attributes

 $secretSalt
 
 $lifetime
 
 $skew
 
 $algo
 

Private Member Functions

 calculateTokenValue ($offset, $time=null)
 Calculates a token value for a given offset. More...
 

Detailed Description

A class that generates and verifies time-limited tokens.

Definition at line 8 of file TimeLimitedToken.php.

Constructor & Destructor Documentation

◆ __construct()

SimpleSAML\Auth\TimeLimitedToken::__construct (   $lifetime = 900,
  $secretSalt = null,
  $skew = 1,
  $algo = 'sha1' 
)

Create a new time-limited token.

Please note that the default algorithm will change in SSP 1.15.0 to SHA-256 instead of SHA-1.

Parameters
int$lifetimeToken lifetime in seconds. Defaults to 900 (15 min).
string$secretSaltA random and unique salt per installation. Defaults to the salt in the configuration.
int$skewThe allowed time skew (in seconds) to correct clock deviations. Defaults to 1 second.
string$algoThe hash algorithm to use to generate the tokens. Defaults to SHA-1.
Exceptions

Definition at line 44 of file TimeLimitedToken.php.

References SimpleSAML\Auth\TimeLimitedToken\$algo, SimpleSAML\Auth\TimeLimitedToken\$lifetime, SimpleSAML\Auth\TimeLimitedToken\$secretSalt, SimpleSAML\Auth\TimeLimitedToken\$skew, and SimpleSAML\Utils\Config\getSecretSalt().

45  {
46  if ($secretSalt === null) {
48  }
49 
50  if (!in_array($algo, hash_algos(), true)) {
51  throw new \InvalidArgumentException('Invalid hash algorithm "'.$algo.'"');
52  }
53 
54  $this->secretSalt = $secretSalt;
55  $this->lifetime = $lifetime;
56  $this->skew = $skew;
57  $this->algo = $algo;
58  }
static getSecretSalt()
Retrieve the secret salt.
Definition: Config.php:49
+ Here is the call graph for this function:

Member Function Documentation

◆ addVerificationData()

SimpleSAML\Auth\TimeLimitedToken::addVerificationData (   $data)

Add some given data to the current token.

This data will be needed later too for token validation.

This mechanism can be used to provide context for a token, such as a user identifier of the only subject authorised to use it. Note also that multiple data can be added to the token. This means that upon validation, not only the same data must be added, but also in the same order.

Parameters
string$dataThe data to incorporate into the current token.

Definition at line 70 of file TimeLimitedToken.php.

71  {
72  $this->secretSalt .= '|'.$data;
73  }

◆ calculateTokenValue()

SimpleSAML\Auth\TimeLimitedToken::calculateTokenValue (   $offset,
  $time = null 
)
private

Calculates a token value for a given offset.

Parameters
int$offsetThe offset to use.
int | null$timeThe time stamp to which the offset is relative to. Defaults to the current time.
Returns
string The token for the given time and offset.

Definition at line 84 of file TimeLimitedToken.php.

References $time, GuzzleHttp\Psr7\hash(), and time.

Referenced by SimpleSAML\Auth\TimeLimitedToken\generate(), and SimpleSAML\Auth\TimeLimitedToken\validate().

85  {
86  if ($time === null) {
87  $time = time();
88  }
89  // a secret salt that should be randomly generated for each installation
90  return hash(
91  $this->algo,
92  $offset.':'.floor(($time - $offset) / ($this->lifetime + $this->skew)).':'.$this->secretSalt
93  );
94  }
$time
Definition: cron.php:21
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
hash(StreamInterface $stream, $algo, $rawOutput=false)
Calculate a hash of a Stream.
Definition: functions.php:406
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ generate()

SimpleSAML\Auth\TimeLimitedToken::generate ( )

Generates a token that contains an offset and a token value, using the current offset.

Returns
string A time-limited token with the offset respect to the beginning of its time slot prepended.

Definition at line 102 of file TimeLimitedToken.php.

References SimpleSAML\Auth\TimeLimitedToken\$skew, $time, SimpleSAML\Auth\TimeLimitedToken\calculateTokenValue(), and time.

Referenced by SimpleSAML\Auth\TimeLimitedToken\generate_token().

103  {
104  $time = time();
105  $current_offset = ($time - $this->skew) % ($this->lifetime + $this->skew);
106  return dechex($current_offset).'-'.$this->calculateTokenValue($current_offset, $time);
107  }
calculateTokenValue($offset, $time=null)
Calculates a token value for a given offset.
$time
Definition: cron.php:21
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ generate_token()

SimpleSAML\Auth\TimeLimitedToken::generate_token ( )
See also
generate
Deprecated:
This method will be removed in SSP 2.0. Use generate() instead.

Definition at line 114 of file TimeLimitedToken.php.

References SimpleSAML\Auth\TimeLimitedToken\generate().

115  {
116  return $this->generate();
117  }
generate()
Generates a token that contains an offset and a token value, using the current offset.
+ Here is the call graph for this function:

◆ validate()

SimpleSAML\Auth\TimeLimitedToken::validate (   $token)

Validates a token by calculating the token value for the provided offset and comparing it.

Parameters
string$tokenThe token to validate.
Returns
boolean True if the given token is currently valid, false otherwise.

Definition at line 127 of file TimeLimitedToken.php.

References SimpleSAML\Auth\TimeLimitedToken\calculateTokenValue().

Referenced by SimpleSAML\Auth\TimeLimitedToken\validate_token().

128  {
129  $splittoken = explode('-', $token);
130  if (count($splittoken) !== 2) {
131  return false;
132  }
133  $offset = intval(hexdec($splittoken[0]));
134  $value = $splittoken[1];
135  return ($this->calculateTokenValue($offset) === $value);
136  }
calculateTokenValue($offset, $time=null)
Calculates a token value for a given offset.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ validate_token()

SimpleSAML\Auth\TimeLimitedToken::validate_token (   $token)
See also
validate
Deprecated:
This method will be removed in SSP 2.0. Use validate() instead.

Definition at line 143 of file TimeLimitedToken.php.

References SimpleSAML\Auth\TimeLimitedToken\validate().

144  {
145  return $this->validate($token);
146  }
validate($token)
Validates a token by calculating the token value for the provided offset and comparing it...
+ Here is the call graph for this function:

Field Documentation

◆ $algo

SimpleSAML\Auth\TimeLimitedToken::$algo
protected

Definition at line 29 of file TimeLimitedToken.php.

Referenced by SimpleSAML\Auth\TimeLimitedToken\__construct().

◆ $lifetime

SimpleSAML\Auth\TimeLimitedToken::$lifetime
protected

Definition at line 19 of file TimeLimitedToken.php.

Referenced by SimpleSAML\Auth\TimeLimitedToken\__construct().

◆ $secretSalt

SimpleSAML\Auth\TimeLimitedToken::$secretSalt
protected

Definition at line 14 of file TimeLimitedToken.php.

Referenced by SimpleSAML\Auth\TimeLimitedToken\__construct().

◆ $skew

SimpleSAML\Auth\TimeLimitedToken::$skew
protected

The documentation for this class was generated from the following file: