ApiQueryTokens.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Module to fetch tokens via action=query&meta=tokens
  4. *
  5. * Copyright © 2014 Wikimedia Foundation and contributors
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. * http://www.gnu.org/copyleft/gpl.html
  21. *
  22. * @file
  23. * @since 1.24
  24. */
  25. /**
  26. * Module to fetch tokens via action=query&meta=tokens
  27. *
  28. * @ingroup API
  29. * @since 1.24
  30. */
  31. class ApiQueryTokens extends ApiQueryBase {
  32. public function execute() {
  33. $params = $this->extractRequestParams();
  34. $res = [
  35. ApiResult::META_TYPE => 'assoc',
  36. ];
  37. if ( $this->lacksSameOriginSecurity() ) {
  38. $this->addWarning( [ 'apiwarn-tokens-origin' ] );
  39. return;
  40. }
  41. $user = $this->getUser();
  42. $session = $this->getRequest()->getSession();
  43. $salts = self::getTokenTypeSalts();
  44. foreach ( $params['type'] as $type ) {
  45. $res[$type . 'token'] = self::getToken( $user, $session, $salts[$type] )->toString();
  46. }
  47. $this->getResult()->addValue( 'query', $this->getModuleName(), $res );
  48. }
  49. /**
  50. * Get the salts for known token types
  51. * @return (string|array)[] Returning a string will use that as the salt
  52. * for User::getEditTokenObject() to fetch the token, which will give a
  53. * LoggedOutEditToken (always "+\\") for anonymous users. Returning an
  54. * array will use it as parameters to MediaWiki\Session\Session::getToken(),
  55. * which will always return a full token even for anonymous users.
  56. */
  57. public static function getTokenTypeSalts() {
  58. static $salts = null;
  59. if ( !$salts ) {
  60. $salts = [
  61. 'csrf' => '',
  62. 'watch' => 'watch',
  63. 'patrol' => 'patrol',
  64. 'rollback' => 'rollback',
  65. 'userrights' => 'userrights',
  66. 'login' => [ '', 'login' ],
  67. 'createaccount' => [ '', 'createaccount' ],
  68. ];
  69. Hooks::run( 'ApiQueryTokensRegisterTypes', [ &$salts ] );
  70. ksort( $salts );
  71. }
  72. return $salts;
  73. }
  74. /**
  75. * Get a token from a salt
  76. * @param User $user
  77. * @param MediaWiki\Session\Session $session
  78. * @param string|array $salt A string will be used as the salt for
  79. * User::getEditTokenObject() to fetch the token, which will give a
  80. * LoggedOutEditToken (always "+\\") for anonymous users. An array will
  81. * be used as parameters to MediaWiki\Session\Session::getToken(), which
  82. * will always return a full token even for anonymous users. An array will
  83. * also persist the session.
  84. * @return MediaWiki\Session\Token
  85. */
  86. public static function getToken( User $user, MediaWiki\Session\Session $session, $salt ) {
  87. if ( is_array( $salt ) ) {
  88. $session->persist();
  89. return $session->getToken( ...$salt );
  90. } else {
  91. return $user->getEditTokenObject( $salt, $session->getRequest() );
  92. }
  93. }
  94. public function getAllowedParams() {
  95. return [
  96. 'type' => [
  97. ApiBase::PARAM_DFLT => 'csrf',
  98. ApiBase::PARAM_ISMULTI => true,
  99. ApiBase::PARAM_TYPE => array_keys( self::getTokenTypeSalts() ),
  100. ],
  101. ];
  102. }
  103. protected function getExamplesMessages() {
  104. return [
  105. 'action=query&meta=tokens'
  106. => 'apihelp-query+tokens-example-simple',
  107. 'action=query&meta=tokens&type=watch|patrol'
  108. => 'apihelp-query+tokens-example-types',
  109. ];
  110. }
  111. public function isReadMode() {
  112. // So login tokens can be fetched on private wikis
  113. return false;
  114. }
  115. public function getCacheMode( $params ) {
  116. return 'private';
  117. }
  118. public function getHelpUrls() {
  119. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tokens';
  120. }
  121. }