Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
59.65% |
34 / 57 |
|
42.86% |
3 / 7 |
CRAP | |
0.00% |
0 / 1 |
| ApiQueryTokens | |
60.71% |
34 / 56 |
|
42.86% |
3 / 7 |
23.25 | |
0.00% |
0 / 1 |
| execute | |
88.89% |
16 / 18 |
|
0.00% |
0 / 1 |
4.02 | |||
| getTokenTypeSalts | |
18.75% |
3 / 16 |
|
0.00% |
0 / 1 |
4.15 | |||
| getToken | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| getAllowedParams | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| getExamplesMessages | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| isReadMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Module to fetch tokens via action=query&meta=tokens |
| 4 | * |
| 5 | * Copyright © 2014 Wikimedia Foundation and contributors |
| 6 | * |
| 7 | * @license GPL-2.0-or-later |
| 8 | * @file |
| 9 | * @since 1.24 |
| 10 | */ |
| 11 | |
| 12 | namespace MediaWiki\Api; |
| 13 | |
| 14 | use MediaWiki\MediaWikiServices; |
| 15 | use MediaWiki\User\User; |
| 16 | use Wikimedia\ParamValidator\ParamValidator; |
| 17 | |
| 18 | /** |
| 19 | * Module to fetch tokens via action=query&meta=tokens |
| 20 | * |
| 21 | * @ingroup API |
| 22 | * @since 1.24 |
| 23 | */ |
| 24 | class ApiQueryTokens extends ApiQueryBase { |
| 25 | |
| 26 | public function execute() { |
| 27 | $params = $this->extractRequestParams(); |
| 28 | |
| 29 | if ( $this->lacksSameOriginSecurity() ) { |
| 30 | $this->addWarning( [ 'apiwarn-tokens-origin' ] ); |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | $user = $this->getUser(); |
| 35 | $session = $this->getRequest()->getSession(); |
| 36 | $salts = self::getTokenTypeSalts(); |
| 37 | |
| 38 | $done = []; |
| 39 | $path = [ 'query', $this->getModuleName() ]; |
| 40 | $this->getResult()->addArrayType( $path, 'assoc' ); |
| 41 | |
| 42 | foreach ( $params['type'] as $type ) { |
| 43 | $token = self::getToken( $user, $session, $salts[$type] )->toString(); |
| 44 | $fit = $this->getResult()->addValue( $path, $type . 'token', $token ); |
| 45 | |
| 46 | if ( !$fit ) { |
| 47 | // Abuse type as a query-continue parameter and set it to all unprocessed types |
| 48 | $this->setContinueEnumParameter( 'type', |
| 49 | array_diff( $params['type'], $done ) ); |
| 50 | break; |
| 51 | } |
| 52 | $done[] = $type; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Get the salts for known token types |
| 58 | * @return (string|array)[] Returning a string will use that as the salt |
| 59 | * for User::getEditTokenObject() to fetch the token, which will give a |
| 60 | * LoggedOutEditToken (always "+\\") for anonymous users. Returning an |
| 61 | * array will use it as parameters to MediaWiki\Session\Session::getToken(), |
| 62 | * which will always return a full token even for anonymous users. |
| 63 | */ |
| 64 | public static function getTokenTypeSalts() { |
| 65 | static $salts = null; |
| 66 | if ( !$salts ) { |
| 67 | $salts = [ |
| 68 | 'csrf' => '', |
| 69 | 'watch' => 'watch', |
| 70 | 'patrol' => 'patrol', |
| 71 | 'rollback' => 'rollback', |
| 72 | 'userrights' => 'userrights', |
| 73 | 'login' => [ '', 'login' ], |
| 74 | 'createaccount' => [ '', 'createaccount' ], |
| 75 | ]; |
| 76 | $hookContainer = MediaWikiServices::getInstance()->getHookContainer(); |
| 77 | $hookRunner = new ApiHookRunner( $hookContainer ); |
| 78 | $hookRunner->onApiQueryTokensRegisterTypes( $salts ); |
| 79 | ksort( $salts ); |
| 80 | } |
| 81 | |
| 82 | return $salts; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Get a token from a salt |
| 87 | * @param User $user |
| 88 | * @param \MediaWiki\Session\Session $session |
| 89 | * @param string|array $salt A string will be used as the salt for |
| 90 | * User::getEditTokenObject() to fetch the token, which will give a |
| 91 | * LoggedOutEditToken (always "+\\") for anonymous users. An array will |
| 92 | * be used as parameters to MediaWiki\Session\Session::getToken(), which |
| 93 | * will always return a full token even for anonymous users. An array will |
| 94 | * also persist the session. |
| 95 | * @return \MediaWiki\Session\Token |
| 96 | */ |
| 97 | public static function getToken( User $user, \MediaWiki\Session\Session $session, $salt ) { |
| 98 | if ( is_array( $salt ) ) { |
| 99 | $token = $session->getToken( ...$salt ); |
| 100 | } else { |
| 101 | $token = $user->getEditTokenObject( $salt, $session->getRequest() ); |
| 102 | } |
| 103 | if ( $token->wasNew() ) { |
| 104 | $session->persist(); |
| 105 | } |
| 106 | return $token; |
| 107 | } |
| 108 | |
| 109 | /** @inheritDoc */ |
| 110 | public function getAllowedParams() { |
| 111 | return [ |
| 112 | 'type' => [ |
| 113 | ParamValidator::PARAM_DEFAULT => 'csrf', |
| 114 | ParamValidator::PARAM_ISMULTI => true, |
| 115 | ParamValidator::PARAM_TYPE => array_keys( self::getTokenTypeSalts() ), |
| 116 | ParamValidator::PARAM_ALL => true, |
| 117 | ], |
| 118 | ]; |
| 119 | } |
| 120 | |
| 121 | /** @inheritDoc */ |
| 122 | protected function getExamplesMessages() { |
| 123 | return [ |
| 124 | 'action=query&meta=tokens' |
| 125 | => 'apihelp-query+tokens-example-simple', |
| 126 | 'action=query&meta=tokens&type=watch|patrol' |
| 127 | => 'apihelp-query+tokens-example-types', |
| 128 | ]; |
| 129 | } |
| 130 | |
| 131 | /** @inheritDoc */ |
| 132 | public function isReadMode() { |
| 133 | // So login tokens can be fetched on private wikis |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | /** @inheritDoc */ |
| 138 | public function getHelpUrls() { |
| 139 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tokens'; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** @deprecated class alias since 1.43 */ |
| 144 | class_alias( ApiQueryTokens::class, 'ApiQueryTokens' ); |