MediaWiki
master
Toggle main menu visibility
ApiCheckToken.php
Go to the documentation of this file.
1
<?php
8
9
namespace
MediaWiki\Api
;
10
11
use
MediaWiki\Session\Token
;
12
use
MediaWiki\Utils\MWTimestamp
;
13
use Wikimedia\ParamValidator\ParamValidator;
14
use Wikimedia\Timestamp\TimestampFormat as TS;
15
20
class
ApiCheckToken
extends
ApiBase
{
21
22
public
function
execute
() {
23
$params = $this->
extractRequestParams
();
24
$token = $params[
'token'
];
25
$maxage = $params[
'maxtokenage'
];
26
$salts =
ApiQueryTokens::getTokenTypeSalts
();
27
28
$res = [];
29
30
$tokenObj =
ApiQueryTokens::getToken
(
31
$this->
getUser
(), $this->
getRequest
()->getSession(), $salts[$params[
'type'
]]
32
);
33
34
if
( str_ends_with( $token, urldecode(
Token::SUFFIX
) ) ) {
35
$this->
addWarning
(
'apiwarn-checktoken-percentencoding'
);
36
}
37
38
if
( $tokenObj->match( $token, $maxage ) ) {
39
$res[
'result'
] =
'valid'
;
40
} elseif ( $maxage !==
null
&& $tokenObj->match( $token ) ) {
41
$res[
'result'
] =
'expired'
;
42
}
else
{
43
$res[
'result'
] =
'invalid'
;
44
}
45
46
$ts =
Token::getTimestamp
( $token );
47
if
( $ts !==
null
) {
48
$mwts =
new
MWTimestamp
();
49
$mwts->timestamp->setTimestamp( $ts );
50
$res[
'generated'
] = $mwts->getTimestamp( TS::ISO_8601 );
51
}
52
53
$this->
getResult
()->addValue(
null
, $this->
getModuleName
(), $res );
54
}
55
57
public
function
getAllowedParams
() {
58
return
[
59
'type'
=> [
60
ParamValidator::PARAM_TYPE => array_keys(
ApiQueryTokens::getTokenTypeSalts
() ),
61
ParamValidator::PARAM_REQUIRED =>
true
,
62
],
63
'token'
=> [
64
ParamValidator::PARAM_TYPE =>
'string'
,
65
ParamValidator::PARAM_REQUIRED =>
true
,
66
ParamValidator::PARAM_SENSITIVE =>
true
,
67
],
68
'maxtokenage'
=> [
69
ParamValidator::PARAM_TYPE =>
'integer'
,
70
],
71
];
72
}
73
75
protected
function
getExamplesMessages
() {
76
return
[
77
'action=checktoken&type=csrf&token=123ABC'
78
=>
'apihelp-checktoken-example-simple'
,
79
];
80
}
81
83
public
function
getHelpUrls
() {
84
return
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Checktoken'
;
85
}
86
}
87
89
class_alias( ApiCheckToken::class,
'ApiCheckToken'
);
MediaWiki\Api\ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition
ApiBase.php:59
MediaWiki\Api\ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition
ApiBase.php:556
MediaWiki\Api\ApiBase\getResult
getResult()
Get the result object.
Definition
ApiBase.php:695
MediaWiki\Api\ApiBase\addWarning
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition
ApiBase.php:1399
MediaWiki\Api\ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition
ApiBase.php:830
MediaWiki\Api\ApiCheckToken
Definition
ApiCheckToken.php:20
MediaWiki\Api\ApiCheckToken\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition
ApiCheckToken.php:57
MediaWiki\Api\ApiCheckToken\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
Definition
ApiCheckToken.php:83
MediaWiki\Api\ApiCheckToken\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition
ApiCheckToken.php:22
MediaWiki\Api\ApiCheckToken\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
Definition
ApiCheckToken.php:75
MediaWiki\Api\ApiQueryTokens\getToken
static getToken(User $user, \MediaWiki\Session\Session $session, $salt)
Get a token from a salt.
Definition
ApiQueryTokens.php:97
MediaWiki\Api\ApiQueryTokens\getTokenTypeSalts
static getTokenTypeSalts()
Get the salts for known token types.
Definition
ApiQueryTokens.php:64
MediaWiki\Session\Token
Value object representing a CSRF token.
Definition
Token.php:19
MediaWiki\Session\Token\SUFFIX
const SUFFIX
CSRF token suffix.
Definition
Token.php:23
MediaWiki\Session\Token\getTimestamp
static getTimestamp( $token)
Decode the timestamp from a token string.
Definition
Token.php:54
MediaWiki\Utils\MWTimestamp
Library for creating and parsing MW-style timestamps.
Definition
MWTimestamp.php:36
MediaWiki\Api
Definition
ApiAcquireTempUserName.php:8
MediaWiki\Api\getUser
getUser()
MediaWiki\Api\getRequest
getRequest()
includes
Api
ApiCheckToken.php
Generated on
for MediaWiki by
1.18.0