MediaWiki master
ApiCheckToken.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Api;
24
28
33class ApiCheckToken extends ApiBase {
34
35 public function execute() {
36 $params = $this->extractRequestParams();
37 $token = $params['token'];
38 $maxage = $params['maxtokenage'];
40
41 $res = [];
42
43 $tokenObj = ApiQueryTokens::getToken(
44 $this->getUser(), $this->getRequest()->getSession(), $salts[$params['type']]
45 );
46
47 if ( str_ends_with( $token, urldecode( Token::SUFFIX ) ) ) {
48 $this->addWarning( 'apiwarn-checktoken-percentencoding' );
49 }
50
51 if ( $tokenObj->match( $token, $maxage ) ) {
52 $res['result'] = 'valid';
53 } elseif ( $maxage !== null && $tokenObj->match( $token ) ) {
54 $res['result'] = 'expired';
55 } else {
56 $res['result'] = 'invalid';
57 }
58
59 $ts = Token::getTimestamp( $token );
60 if ( $ts !== null ) {
61 $mwts = new MWTimestamp();
62 $mwts->timestamp->setTimestamp( $ts );
63 $res['generated'] = $mwts->getTimestamp( TS_ISO_8601 );
64 }
65
66 $this->getResult()->addValue( null, $this->getModuleName(), $res );
67 }
68
69 public function getAllowedParams() {
70 return [
71 'type' => [
72 ParamValidator::PARAM_TYPE => array_keys( ApiQueryTokens::getTokenTypeSalts() ),
73 ParamValidator::PARAM_REQUIRED => true,
74 ],
75 'token' => [
76 ParamValidator::PARAM_TYPE => 'string',
77 ParamValidator::PARAM_REQUIRED => true,
78 ParamValidator::PARAM_SENSITIVE => true,
79 ],
80 'maxtokenage' => [
81 ParamValidator::PARAM_TYPE => 'integer',
82 ],
83 ];
84 }
85
86 protected function getExamplesMessages() {
87 return [
88 'action=checktoken&type=csrf&token=123ABC'
89 => 'apihelp-checktoken-example-simple',
90 ];
91 }
92
93 public function getHelpUrls() {
94 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Checktoken';
95 }
96}
97
99class_alias( ApiCheckToken::class, 'ApiCheckToken' );
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:75
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:557
getResult()
Get the result object.
Definition ApiBase.php:696
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition ApiBase.php:1440
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getHelpUrls()
Return links to more detailed help pages about the module.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getExamplesMessages()
Returns usage examples for this module.
static getToken(User $user, \MediaWiki\Session\Session $session, $salt)
Get a token from a salt.
static getTokenTypeSalts()
Get the salts for known token types.
Value object representing a CSRF token.
Definition Token.php:34
const SUFFIX
CSRF token suffix.
Definition Token.php:38
static getTimestamp( $token)
Decode the timestamp from a token string.
Definition Token.php:69
Library for creating and parsing MW-style timestamps.
Service for formatting and validating API parameters.