ApiRemoveAuthenticationData.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. * Remove authentication data from AuthManager
  25. *
  26. * @ingroup API
  27. */
  28. class ApiRemoveAuthenticationData extends ApiBase {
  29. private $authAction;
  30. private $operation;
  31. public function __construct( ApiMain $main, $action ) {
  32. parent::__construct( $main, $action );
  33. $this->authAction = $action === 'unlinkaccount'
  34. ? AuthManager::ACTION_UNLINK
  35. : AuthManager::ACTION_REMOVE;
  36. $this->operation = $action === 'unlinkaccount'
  37. ? 'UnlinkAccount'
  38. : 'RemoveCredentials';
  39. }
  40. public function execute() {
  41. if ( !$this->getUser()->isLoggedIn() ) {
  42. $this->dieWithError( 'apierror-mustbeloggedin-removeauth', 'notloggedin' );
  43. }
  44. $params = $this->extractRequestParams();
  45. $manager = AuthManager::singleton();
  46. // Check security-sensitive operation status
  47. ApiAuthManagerHelper::newForModule( $this )->securitySensitiveOperation( $this->operation );
  48. // Fetch the request. No need to load from the request, so don't use
  49. // ApiAuthManagerHelper's method.
  50. $blacklist = $this->authAction === AuthManager::ACTION_REMOVE
  51. ? array_flip( $this->getConfig()->get( 'RemoveCredentialsBlacklist' ) )
  52. : [];
  53. $reqs = array_filter(
  54. $manager->getAuthenticationRequests( $this->authAction, $this->getUser() ),
  55. function ( $req ) use ( $params, $blacklist ) {
  56. return $req->getUniqueId() === $params['request'] &&
  57. !isset( $blacklist[get_class( $req )] );
  58. }
  59. );
  60. if ( count( $reqs ) !== 1 ) {
  61. $this->dieWithError( 'apierror-changeauth-norequest', 'badrequest' );
  62. }
  63. $req = reset( $reqs );
  64. // Perform the removal
  65. $status = $manager->allowsAuthenticationDataChange( $req, true );
  66. Hooks::run( 'ChangeAuthenticationDataAudit', [ $req, $status ] );
  67. if ( !$status->isGood() ) {
  68. $this->dieStatus( $status );
  69. }
  70. $manager->changeAuthenticationData( $req );
  71. $this->getResult()->addValue( null, $this->getModuleName(), [ 'status' => 'success' ] );
  72. }
  73. public function isWriteMode() {
  74. return true;
  75. }
  76. public function needsToken() {
  77. return 'csrf';
  78. }
  79. public function getAllowedParams() {
  80. return ApiAuthManagerHelper::getStandardParams( $this->authAction,
  81. 'request'
  82. );
  83. }
  84. protected function getExamplesMessages() {
  85. $path = $this->getModulePath();
  86. $action = $this->getModuleName();
  87. return [
  88. "action={$action}&request=FooAuthenticationRequest&token=123ABC"
  89. => "apihelp-{$path}-example-simple",
  90. ];
  91. }
  92. public function getHelpUrls() {
  93. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Manage_authentication_data';
  94. }
  95. }