MediaWiki  1.30.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 ) .
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 }
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2040
MediaWiki\Session\Token\$salt
$salt
Definition: Token.php:38
MediaWiki\Session\Token\__toString
__toString()
Definition: Token.php:93
MediaWiki\Session\Token\__construct
__construct( $secret, $salt, $new=false)
Definition: Token.php:46
MediaWiki\Session\Token\getTimestamp
static getTimestamp( $token)
Decode the timestamp from a token string.
Definition: Token.php:61
php
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
MediaWiki\Session\Token\$new
$new
Definition: Token.php:39
MediaWiki\Session\Token\$secret
$secret
Definition: Token.php:37
MediaWiki\Session
Definition: BotPasswordSessionProvider.php:24
MediaWiki\Session\Token\toStringAtTimestamp
toStringAtTimestamp( $timestamp)
Get the string representation of the token at a timestamp.
Definition: Token.php:79
MediaWiki\Session\Token\SUFFIX
const SUFFIX
CSRF token suffix.
Definition: Token.php:35
MediaWiki\Session\Token\toString
toString()
Get the string representation of the token.
Definition: Token.php:89
MediaWiki\Session\Token\match
match( $userToken, $maxAge=null)
Test if the token-string matches this token.
Definition: Token.php:103
MediaWiki\Session\Token
Value object representing a CSRF token.
Definition: Token.php:32
MediaWiki\Session\Token\wasNew
wasNew()
Indicate whether this token was just created.
Definition: Token.php:121