MediaWiki  1.28.0
Token.php
Go to the documentation of this file.
1 <?php
24 namespace MediaWiki\Session;
25 
32 class Token {
35  const SUFFIX = '+\\';
36 
37  private $secret = '';
38  private $salt = '';
39  private $new = false;
40 
46  public function __construct( $secret, $salt, $new = false ) {
47  $this->secret = $secret;
48  $this->salt = $salt;
49  $this->new = $new;
50  }
51 
61  public static function getTimestamp( $token ) {
62  $suffixLen = strlen( self::SUFFIX );
63  $len = strlen( $token );
64  if ( $len <= 32 + $suffixLen ||
65  substr( $token, -$suffixLen ) !== self::SUFFIX ||
66  strspn( $token, '0123456789abcdef' ) + $suffixLen !== $len
67  ) {
68  return null;
69  }
70 
71  return hexdec( substr( $token, 32, -$suffixLen ) );
72  }
73 
79  protected function toStringAtTimestamp( $timestamp ) {
80  return hash_hmac( 'md5', $timestamp . $this->salt, $this->secret, false ) .
81  dechex( $timestamp ) .
82  self::SUFFIX;
83  }
84 
89  public function toString() {
90  return $this->toStringAtTimestamp( wfTimestamp() );
91  }
92 
93  public function __toString() {
94  return $this->toString();
95  }
96 
103  public function match( $userToken, $maxAge = null ) {
104  $timestamp = self::getTimestamp( $userToken );
105  if ( $timestamp === null ) {
106  return false;
107  }
108  if ( $maxAge !== null && $timestamp < wfTimestamp() - $maxAge ) {
109  // Expired token
110  return false;
111  }
112 
113  $sessionToken = $this->toStringAtTimestamp( $timestamp );
114  return hash_equals( $sessionToken, $userToken );
115  }
116 
121  public function wasNew() {
122  return $this->new;
123  }
124 
125 }
const SUFFIX
CSRF token suffix.
Definition: Token.php:35
static getTimestamp($token)
Decode the timestamp from a token string.
Definition: Token.php:61
match($userToken, $maxAge=null)
Test if the token-string matches this token.
Definition: Token.php:103
wfTimestamp($outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Value object representing a CSRF token.
Definition: Token.php:32
if($limit) $timestamp
toStringAtTimestamp($timestamp)
Get the string representation of the token at a timestamp.
Definition: Token.php:79
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
toString()
Get the string representation of the token.
Definition: Token.php:89
__construct($secret, $salt, $new=false)
Definition: Token.php:46
wasNew()
Indicate whether this token was just created.
Definition: Token.php:121