ApiChangeAuthenticationData.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. */
  22. use MediaWiki\Auth\AuthManager;
  23. /**
  24. * Change authentication data with AuthManager
  25. *
  26. * @ingroup API
  27. */
  28. class ApiChangeAuthenticationData extends ApiBase {
  29. public function __construct( ApiMain $main, $action ) {
  30. parent::__construct( $main, $action, 'changeauth' );
  31. }
  32. public function execute() {
  33. if ( !$this->getUser()->isLoggedIn() ) {
  34. $this->dieWithError( 'apierror-mustbeloggedin-changeauthenticationdata', 'notloggedin' );
  35. }
  36. $helper = new ApiAuthManagerHelper( $this );
  37. $manager = AuthManager::singleton();
  38. // Check security-sensitive operation status
  39. $helper->securitySensitiveOperation( 'ChangeCredentials' );
  40. // Fetch the request
  41. $reqs = ApiAuthManagerHelper::blacklistAuthenticationRequests(
  42. $helper->loadAuthenticationRequests( AuthManager::ACTION_CHANGE ),
  43. $this->getConfig()->get( 'ChangeCredentialsBlacklist' )
  44. );
  45. if ( count( $reqs ) !== 1 ) {
  46. $this->dieWithError( 'apierror-changeauth-norequest', 'badrequest' );
  47. }
  48. $req = reset( $reqs );
  49. // Make the change
  50. $status = $manager->allowsAuthenticationDataChange( $req, true );
  51. Hooks::run( 'ChangeAuthenticationDataAudit', [ $req, $status ] );
  52. if ( !$status->isGood() ) {
  53. $this->dieStatus( $status );
  54. }
  55. $manager->changeAuthenticationData( $req );
  56. $this->getResult()->addValue( null, 'changeauthenticationdata', [ 'status' => 'success' ] );
  57. }
  58. public function isWriteMode() {
  59. return true;
  60. }
  61. public function needsToken() {
  62. return 'csrf';
  63. }
  64. public function getAllowedParams() {
  65. return ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_CHANGE,
  66. 'request'
  67. );
  68. }
  69. public function dynamicParameterDocumentation() {
  70. return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_CHANGE ];
  71. }
  72. protected function getExamplesMessages() {
  73. return [
  74. 'action=changeauthenticationdata' .
  75. '&changeauthrequest=MediaWiki%5CAuth%5CPasswordAuthenticationRequest' .
  76. '&password=ExamplePassword&retype=ExamplePassword&changeauthtoken=123ABC'
  77. => 'apihelp-changeauthenticationdata-example-password',
  78. ];
  79. }
  80. public function getHelpUrls() {
  81. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Manage_authentication_data';
  82. }
  83. }