Merge "resourceloader: Add coverage for DerivativeRLContext::getDirection inheritance"
[lhc/web/wiklou.git] / includes / session / Token.php
1 <?php
2 /**
3 * MediaWiki session token
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Session
22 */
23
24 namespace MediaWiki\Session;
25
26 /**
27 * Value object representing a CSRF token
28 *
29 * @ingroup Session
30 * @since 1.27
31 */
32 class Token {
33 /** CSRF token suffix. Plus and terminal backslash are included to stop
34 * editing from certain broken proxies. */
35 const SUFFIX = '+\\';
36
37 /** @var string */
38 private $secret = '';
39
40 /** @var string */
41 private $salt = '';
42
43 /** @var bool */
44 private $new = false;
45
46 /**
47 * @param string $secret Token secret
48 * @param string $salt Token salt
49 * @param bool $new Whether the secret was newly-created
50 */
51 public function __construct( $secret, $salt, $new = false ) {
52 $this->secret = $secret;
53 $this->salt = $salt;
54 $this->new = $new;
55 }
56
57 /**
58 * Decode the timestamp from a token string
59 *
60 * Does not validate the token beyond the syntactic checks necessary to
61 * be able to extract the timestamp.
62 *
63 * @param string $token
64 * @return int|null
65 */
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
79 /**
80 * Get the string representation of the token at a timestamp
81 * @param int $timestamp
82 * @return string
83 */
84 protected function toStringAtTimestamp( $timestamp ) {
85 return hash_hmac( 'md5', $timestamp . $this->salt, $this->secret, false ) .
86 dechex( $timestamp ) .
87 self::SUFFIX;
88 }
89
90 /**
91 * Get the string representation of the token
92 * @return string
93 */
94 public function toString() {
95 return $this->toStringAtTimestamp( wfTimestamp() );
96 }
97
98 public function __toString() {
99 return $this->toString();
100 }
101
102 /**
103 * Test if the token-string matches this token
104 * @param string $userToken
105 * @param int|null $maxAge Return false if $userToken is older than this many seconds
106 * @return bool
107 */
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
122 /**
123 * Indicate whether this token was just created
124 * @return bool
125 */
126 public function wasNew() {
127 return $this->new;
128 }
129
130 }