MediaWiki master
ApiQueryTokens.php
Go to the documentation of this file.
1<?php
26namespace MediaWiki\Api;
27
31
39
40 public function execute() {
42
43 if ( $this->lacksSameOriginSecurity() ) {
44 $this->addWarning( [ 'apiwarn-tokens-origin' ] );
45 return;
46 }
47
48 $user = $this->getUser();
49 $session = $this->getRequest()->getSession();
50 $salts = self::getTokenTypeSalts();
51
52 $done = [];
53 $path = [ 'query', $this->getModuleName() ];
54 $this->getResult()->addArrayType( $path, 'assoc' );
55
56 foreach ( $params['type'] as $type ) {
57 $token = self::getToken( $user, $session, $salts[$type] )->toString();
58 $fit = $this->getResult()->addValue( $path, $type . 'token', $token );
59
60 if ( !$fit ) {
61 // Abuse type as a query-continue parameter and set it to all unprocessed types
62 $this->setContinueEnumParameter( 'type',
63 array_diff( $params['type'], $done ) );
64 break;
65 }
66 $done[] = $type;
67 }
68 }
69
78 public static function getTokenTypeSalts() {
79 static $salts = null;
80 if ( !$salts ) {
81 $salts = [
82 'csrf' => '',
83 'watch' => 'watch',
84 'patrol' => 'patrol',
85 'rollback' => 'rollback',
86 'userrights' => 'userrights',
87 'login' => [ '', 'login' ],
88 'createaccount' => [ '', 'createaccount' ],
89 ];
90 $hookContainer = MediaWikiServices::getInstance()->getHookContainer();
91 $hookRunner = new ApiHookRunner( $hookContainer );
92 $hookRunner->onApiQueryTokensRegisterTypes( $salts );
93 ksort( $salts );
94 }
95
96 return $salts;
97 }
98
111 public static function getToken( User $user, \MediaWiki\Session\Session $session, $salt ) {
112 if ( is_array( $salt ) ) {
113 $session->persist();
114 return $session->getToken( ...$salt );
115 } else {
116 return $user->getEditTokenObject( $salt, $session->getRequest() );
117 }
118 }
119
120 public function getAllowedParams() {
121 return [
122 'type' => [
123 ParamValidator::PARAM_DEFAULT => 'csrf',
124 ParamValidator::PARAM_ISMULTI => true,
125 ParamValidator::PARAM_TYPE => array_keys( self::getTokenTypeSalts() ),
126 ParamValidator::PARAM_ALL => true,
127 ],
128 ];
129 }
130
131 protected function getExamplesMessages() {
132 return [
133 'action=query&meta=tokens'
134 => 'apihelp-query+tokens-example-simple',
135 'action=query&meta=tokens&type=watch|patrol'
136 => 'apihelp-query+tokens-example-types',
137 ];
138 }
139
140 public function isReadMode() {
141 // So login tokens can be fetched on private wikis
142 return false;
143 }
144
145 public function getHelpUrls() {
146 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tokens';
147 }
148}
149
151class_alias( ApiQueryTokens::class, 'ApiQueryTokens' );
array $params
The job parameters.
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:571
getResult()
Get the result object.
Definition ApiBase.php:710
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition ApiBase.php:1483
lacksSameOriginSecurity()
Returns true if the current request breaks the same-origin policy.
Definition ApiBase.php:637
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:851
This class provides an implementation of the hook interfaces used by the core Action API,...
This is a base class for all Query modules.
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
Module to fetch tokens via action=query&meta=tokens.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
isReadMode()
Indicates whether this module requires read rights.
getHelpUrls()
Return links to more detailed help pages about the module.
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.
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Manages data for an authenticated session.
Definition Session.php:54
internal since 1.36
Definition User.php:93
getEditTokenObject( $salt='', $request=null)
Initialize (if necessary) and return a session token value which can be used in edit forms to show th...
Definition User.php:2755
Service for formatting and validating API parameters.
A helper class for throttling authentication attempts.