MediaWiki  master
ApiQueryTokens.php
Go to the documentation of this file.
1 <?php
33 
34  public function execute() {
35  $params = $this->extractRequestParams();
36  $res = [
37  ApiResult::META_TYPE => 'assoc',
38  ];
39 
40  if ( $this->lacksSameOriginSecurity() ) {
41  $this->addWarning( [ 'apiwarn-tokens-origin' ] );
42  return;
43  }
44 
45  $user = $this->getUser();
46  $session = $this->getRequest()->getSession();
47  $salts = self::getTokenTypeSalts();
48  foreach ( $params['type'] as $type ) {
49  $res[$type . 'token'] = self::getToken( $user, $session, $salts[$type] )->toString();
50  }
51 
52  $this->getResult()->addValue( 'query', $this->getModuleName(), $res );
53  }
54 
63  public static function getTokenTypeSalts() {
64  static $salts = null;
65  if ( !$salts ) {
66  $salts = [
67  'csrf' => '',
68  'watch' => 'watch',
69  'patrol' => 'patrol',
70  'rollback' => 'rollback',
71  'userrights' => 'userrights',
72  'login' => [ '', 'login' ],
73  'createaccount' => [ '', 'createaccount' ],
74  ];
75  Hooks::run( 'ApiQueryTokensRegisterTypes', [ &$salts ] );
76  ksort( $salts );
77  }
78 
79  return $salts;
80  }
81 
94  public static function getToken( User $user, MediaWiki\Session\Session $session, $salt ) {
95  if ( is_array( $salt ) ) {
96  $session->persist();
97  return $session->getToken( ...$salt );
98  } else {
99  return $user->getEditTokenObject( $salt, $session->getRequest() );
100  }
101  }
102 
103  public function getAllowedParams() {
104  return [
105  'type' => [
106  ApiBase::PARAM_DFLT => 'csrf',
107  ApiBase::PARAM_ISMULTI => true,
108  ApiBase::PARAM_TYPE => array_keys( self::getTokenTypeSalts() ),
109  ],
110  ];
111  }
112 
113  protected function getExamplesMessages() {
114  return [
115  'action=query&meta=tokens'
116  => 'apihelp-query+tokens-example-simple',
117  'action=query&meta=tokens&type=watch|patrol'
118  => 'apihelp-query+tokens-example-types',
119  ];
120  }
121 
122  public function isReadMode() {
123  // So login tokens can be fetched on private wikis
124  return false;
125  }
126 
127  public function getCacheMode( $params ) {
128  return 'private';
129  }
130 
131  public function getHelpUrls() {
132  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tokens';
133  }
134 }
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below...
Definition: ApiBase.php:94
getResult()
Get the result object.
Definition: ApiBase.php:639
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:55
This is a base class for all Query modules.
lacksSameOriginSecurity()
Returns true if the current request breaks the same-origin policy.
Definition: ApiBase.php:567
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user...
Definition: ApiBase.php:760
const META_TYPE
Key for the &#39;type&#39; metadata item.
Definition: ApiResult.php:110
A helper class for throttling authentication attempts.
The User object encapsulates all of the user-specific settings (user_id, name, rights, email address, options, last login time).
Definition: User.php:51
getCacheMode( $params)
$res
Definition: database.txt:21
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:4497
$params
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:519
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
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.
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition: ApiBase.php:1927
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:58
Module to fetch tokens via action=query&meta=tokens.
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1473
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200