MediaWiki  master
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 ) .
87  self::SUFFIX;
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 }
const SUFFIX
CSRF token suffix.
Definition: Token.php:35
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
static getTimestamp( $token)
Decode the timestamp from a token string.
Definition: Token.php:66
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
__construct( $secret, $salt, $new=false)
Definition: Token.php:51
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:94
wasNew()
Indicate whether this token was just created.
Definition: Token.php:126
match( $userToken, $maxAge=null)
Test if the token-string matches this token.
Definition: Token.php:108
toStringAtTimestamp( $timestamp)
Get the string representation of the token at a timestamp.
Definition: Token.php:84