MediaWiki  1.33.1
Token.php
Go to the documentation of this file.
1 <?php
24 namespace MediaWiki\Session;
25 
32 class Token {
35  const SUFFIX = '+\\';
36 
38  private $secret = '';
39 
41  private $salt = '';
42 
44  private $new = false;
45 
51  public function __construct( $secret, $salt, $new = false ) {
52  $this->secret = $secret;
53  $this->salt = $salt;
54  $this->new = $new;
55  }
56 
66  public static function getTimestamp( $token ) {
67  $suffixLen = strlen( self::SUFFIX );
68  $len = strlen( $token );
69  if ( $len <= 32 + $suffixLen ||
70  substr( $token, -$suffixLen ) !== self::SUFFIX ||
71  strspn( $token, '0123456789abcdef' ) + $suffixLen !== $len
72  ) {
73  return null;
74  }
75 
76  return hexdec( substr( $token, 32, -$suffixLen ) );
77  }
78 
84  protected function toStringAtTimestamp( $timestamp ) {
85  return hash_hmac( 'md5', $timestamp . $this->salt, $this->secret, false ) .
86  dechex( $timestamp ) .
88  }
89 
94  public function toString() {
95  return $this->toStringAtTimestamp( wfTimestamp() );
96  }
97 
98  public function __toString() {
99  return $this->toString();
100  }
101 
108  public function match( $userToken, $maxAge = null ) {
109  $timestamp = self::getTimestamp( $userToken );
110  if ( $timestamp === null ) {
111  return false;
112  }
113  if ( $maxAge !== null && $timestamp < wfTimestamp() - $maxAge ) {
114  // Expired token
115  return false;
116  }
117 
118  $sessionToken = $this->toStringAtTimestamp( $timestamp );
119  return hash_equals( $sessionToken, $userToken );
120  }
121 
126  public function wasNew() {
127  return $this->new;
128  }
129 
130 }
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1912
MediaWiki\Session\Token\__toString
__toString()
Definition: Token.php:98
MediaWiki\Session\Token\__construct
__construct( $secret, $salt, $new=false)
Definition: Token.php:51
MediaWiki\Session\Token\getTimestamp
static getTimestamp( $token)
Decode the timestamp from a token string.
Definition: Token.php:66
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\$salt
string $salt
Definition: Token.php:41
MediaWiki\Session\Token\$secret
string $secret
Definition: Token.php:38
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:84
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:94
MediaWiki\Session\Token\match
match( $userToken, $maxAge=null)
Test if the token-string matches this token.
Definition: Token.php:108
MediaWiki\Session\Token\$new
bool $new
Definition: Token.php:44
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:126