timestamp = $timestamp; $this->tolerance = $tolerance; $this->hash = $hash; } /** * Gets the timestamp value of this token. * * @return int Timestamp for this token. */ public function getTimestamp(): int { return $this->timestamp; } /** * Gets the tolerance value of this token. * * @return int Tolerance for this token. */ public function getTolerance(): int { return $this->tolerance; } /** * Gets the hash of this token. * * @return string Hash for this token. */ public function getHash(): string { return $this->hash; } /** * Encodes the CSRFPToken instance as a token string. * * @return string CSRF prevention token string. */ public function encode(): string { return Serialiser::uriBase64()->serialise(pack('Vv', $this->timestamp, $this->tolerance) . $this->hash); } /** * Decodes a token string to a CSRFPToken instance. * * If an invalid token is provided, no exception will be thrown. * * @param string $token Input token string. * @return CSRFPToken Instance representing the provided token. */ public static function decode(string $token): CSRFPToken { $token = Serialiser::uriBase64()->deserialise($token); try { $decode = unpack('Vtimestamp/vtolerance', $token); } catch(ErrorException $ex) { $decode = [ 'timestamp' => -1, 'tolerance' => 0, ]; } // arbitrary length $hash = substr($token, 6, 128); return new CSRFPToken($decode['timestamp'], $decode['tolerance'], $hash); } public function __toString(): string { return $this->encode(); } }