ApiQueryAuthManagerInfo.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Copyright © 2016 Wikimedia Foundation and contributors
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @since 1.27
  22. */
  23. use MediaWiki\Auth\AuthManager;
  24. /**
  25. * A query action to return meta information about AuthManager state.
  26. *
  27. * @ingroup API
  28. */
  29. class ApiQueryAuthManagerInfo extends ApiQueryBase {
  30. public function __construct( ApiQuery $query, $moduleName ) {
  31. parent::__construct( $query, $moduleName, 'ami' );
  32. }
  33. public function execute() {
  34. $params = $this->extractRequestParams();
  35. $helper = new ApiAuthManagerHelper( $this );
  36. $manager = AuthManager::singleton();
  37. $ret = [
  38. 'canauthenticatenow' => $manager->canAuthenticateNow(),
  39. 'cancreateaccounts' => $manager->canCreateAccounts(),
  40. 'canlinkaccounts' => $manager->canLinkAccounts(),
  41. ];
  42. if ( $params['securitysensitiveoperation'] !== null ) {
  43. $ret['securitysensitiveoperationstatus'] = $manager->securitySensitiveOperationStatus(
  44. $params['securitysensitiveoperation']
  45. );
  46. }
  47. if ( $params['requestsfor'] ) {
  48. $action = $params['requestsfor'];
  49. $preservedReq = $helper->getPreservedRequest();
  50. if ( $preservedReq ) {
  51. $ret += [
  52. 'haspreservedstate' => $preservedReq->hasStateForAction( $action ),
  53. 'hasprimarypreservedstate' => $preservedReq->hasPrimaryStateForAction( $action ),
  54. 'preservedusername' => (string)$preservedReq->username,
  55. ];
  56. } else {
  57. $ret += [
  58. 'haspreservedstate' => false,
  59. 'hasprimarypreservedstate' => false,
  60. 'preservedusername' => '',
  61. ];
  62. }
  63. $reqs = $manager->getAuthenticationRequests( $action, $this->getUser() );
  64. // Filter out blacklisted requests, depending on the action
  65. switch ( $action ) {
  66. case AuthManager::ACTION_CHANGE:
  67. $reqs = ApiAuthManagerHelper::blacklistAuthenticationRequests(
  68. $reqs, $this->getConfig()->get( 'ChangeCredentialsBlacklist' )
  69. );
  70. break;
  71. case AuthManager::ACTION_REMOVE:
  72. $reqs = ApiAuthManagerHelper::blacklistAuthenticationRequests(
  73. $reqs, $this->getConfig()->get( 'RemoveCredentialsBlacklist' )
  74. );
  75. break;
  76. }
  77. $ret += $helper->formatRequests( $reqs );
  78. }
  79. $this->getResult()->addValue( [ 'query' ], $this->getModuleName(), $ret );
  80. }
  81. public function isReadMode() {
  82. return false;
  83. }
  84. public function getAllowedParams() {
  85. return [
  86. 'securitysensitiveoperation' => null,
  87. 'requestsfor' => [
  88. ApiBase::PARAM_TYPE => [
  89. AuthManager::ACTION_LOGIN,
  90. AuthManager::ACTION_LOGIN_CONTINUE,
  91. AuthManager::ACTION_CREATE,
  92. AuthManager::ACTION_CREATE_CONTINUE,
  93. AuthManager::ACTION_LINK,
  94. AuthManager::ACTION_LINK_CONTINUE,
  95. AuthManager::ACTION_CHANGE,
  96. AuthManager::ACTION_REMOVE,
  97. AuthManager::ACTION_UNLINK,
  98. ],
  99. ],
  100. ] + ApiAuthManagerHelper::getStandardParams( '', 'mergerequestfields', 'messageformat' );
  101. }
  102. protected function getExamplesMessages() {
  103. return [
  104. 'action=query&meta=authmanagerinfo&amirequestsfor=' . urlencode( AuthManager::ACTION_LOGIN )
  105. => 'apihelp-query+authmanagerinfo-example-login',
  106. 'action=query&meta=authmanagerinfo&amirequestsfor=' . urlencode( AuthManager::ACTION_LOGIN ) .
  107. '&amimergerequestfields=1'
  108. => 'apihelp-query+authmanagerinfo-example-login-merged',
  109. 'action=query&meta=authmanagerinfo&amisecuritysensitiveoperation=foo'
  110. => 'apihelp-query+authmanagerinfo-example-securitysensitiveoperation',
  111. ];
  112. }
  113. public function getHelpUrls() {
  114. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Authmanagerinfo';
  115. }
  116. }