ApiQueryTokens.php 4.0 KB

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