MediaWiki  1.28.0
ApiQueryTokens.php
Go to the documentation of this file.
1 <?php
35 
36  public function execute() {
37  $params = $this->extractRequestParams();
38  $res = [
39  ApiResult::META_TYPE => 'assoc',
40  ];
41 
42  if ( $this->lacksSameOriginSecurity() ) {
43  $this->setWarning( 'Tokens may not be obtained when the same-origin policy is not applied' );
44  return;
45  }
46 
47  $user = $this->getUser();
48  $session = $this->getRequest()->getSession();
49  $salts = self::getTokenTypeSalts();
50  foreach ( $params['type'] as $type ) {
51  $res[$type . 'token'] = self::getToken( $user, $session, $salts[$type] )->toString();
52  }
53 
54  $this->getResult()->addValue( 'query', $this->getModuleName(), $res );
55  }
56 
65  public static function getTokenTypeSalts() {
66  static $salts = null;
67  if ( !$salts ) {
68  $salts = [
69  'csrf' => '',
70  'watch' => 'watch',
71  'patrol' => 'patrol',
72  'rollback' => 'rollback',
73  'userrights' => 'userrights',
74  'login' => [ '', 'login' ],
75  'createaccount' => [ '', 'createaccount' ],
76  ];
77  Hooks::run( 'ApiQueryTokensRegisterTypes', [ &$salts ] );
78  ksort( $salts );
79  }
80 
81  return $salts;
82  }
83 
96  public static function getToken( User $user, MediaWiki\Session\Session $session, $salt ) {
97  if ( is_array( $salt ) ) {
98  $session->persist();
99  return call_user_func_array( [ $session, 'getToken' ], $salt );
100  } else {
101  return $user->getEditTokenObject( $salt, $session->getRequest() );
102  }
103  }
104 
105  public function getAllowedParams() {
106  return [
107  'type' => [
108  ApiBase::PARAM_DFLT => 'csrf',
109  ApiBase::PARAM_ISMULTI => true,
110  ApiBase::PARAM_TYPE => array_keys( self::getTokenTypeSalts() ),
111  ],
112  ];
113  }
114 
115  protected function getExamplesMessages() {
116  return [
117  'action=query&meta=tokens'
118  => 'apihelp-query+tokens-example-simple',
119  'action=query&meta=tokens&type=watch|patrol'
120  => 'apihelp-query+tokens-example-types',
121  ];
122  }
123 
124  public function isReadMode() {
125  // So login tokens can be fetched on private wikis
126  return false;
127  }
128 
129  public function getCacheMode( $params ) {
130  return 'private';
131  }
132 
133  public function getHelpUrls() {
134  return 'https://www.mediawiki.org/wiki/API:Tokens';
135  }
136 }
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below...
Definition: ApiBase.php:88
getResult()
Get the result object.
Definition: ApiBase.php:577
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:50
This is a base class for all Query modules.
lacksSameOriginSecurity()
Returns true if the current request breaks the same-origin policy.
Definition: ApiBase.php:505
extractRequestParams($parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user...
Definition: ApiBase.php:678
const META_TYPE
Key for the 'type' metadata item.
Definition: ApiResult.php:108
A helper class for throttling authentication attempts.
getRequest()
Get the WebRequest object.
$res
Definition: database.txt:21
getCacheMode($params)
$params
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:457
static run($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
setWarning($warning)
Set warning section for this module.
Definition: ApiBase.php:1543
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition: hooks.txt:242
static getTokenTypeSalts()
Get the salts for known token types.
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
static getToken(User $user, MediaWiki\Session\Session $session, $salt)
Get a token from a salt.
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:53
Module to fetch tokens via action=query&meta=tokens.
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:4229
getUser()
Get the User object.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2491